diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListener.Windows.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListener.Windows.cs index 5bb30850fce..8485da03cef 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListener.Windows.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListener.Windows.cs @@ -116,24 +116,8 @@ namespace System.Net } } - private Dictionary DisconnectResults - { - get - { - if (_disconnectResults == null) - { - lock (_internalLock) - { - if (_disconnectResults == null) - { - _disconnectResults = new Dictionary(); - } - } - } - return _disconnectResults; - } - } - + private Dictionary DisconnectResults => + LazyInitializer.EnsureInitialized(ref _disconnectResults, () => new Dictionary()); private void SetUrlGroupProperty(Interop.HttpApi.HTTP_SERVER_PROPERTY property, IntPtr info, uint infosize) { diff --git a/src/libraries/System.Net.Requests/src/System/Net/FtpWebRequest.cs b/src/libraries/System.Net.Requests/src/System/Net/FtpWebRequest.cs index 2b3e6fe9d36..4b0472cda34 100644 --- a/src/libraries/System.Net.Requests/src/System/Net/FtpWebRequest.cs +++ b/src/libraries/System.Net.Requests/src/System/Net/FtpWebRequest.cs @@ -10,6 +10,7 @@ using System.Net.Sockets; using System.Security; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; +using System.Threading; using System.Threading.Tasks; namespace System.Net @@ -1643,17 +1644,7 @@ namespace System.Net { get { - if (_clientCertificates == null) - { - lock (_syncObject) - { - if (_clientCertificates == null) - { - _clientCertificates = new X509CertificateCollection(); - } - } - } - return _clientCertificates; + return LazyInitializer.EnsureInitialized(ref _clientCertificates, ref _syncObject, () => new X509CertificateCollection()); } set { diff --git a/src/libraries/System.Net.Requests/src/System/Net/SystemWebProxy.cs b/src/libraries/System.Net.Requests/src/System/Net/SystemWebProxy.cs index c50ee529e15..181a3fa82da 100644 --- a/src/libraries/System.Net.Requests/src/System/Net/SystemWebProxy.cs +++ b/src/libraries/System.Net.Requests/src/System/Net/SystemWebProxy.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Threading; namespace System.Net { @@ -29,22 +30,7 @@ namespace System.Net { } - public static IWebProxy Get() - { - if (!s_systemWebProxyInitialized) - { - lock (s_lockObject) - { - if (!s_systemWebProxyInitialized) - { - s_systemWebProxy = new SystemWebProxy(); - s_systemWebProxyInitialized = true; - } - } - } - - return s_systemWebProxy; - } + public static IWebProxy Get() => LazyInitializer.EnsureInitialized(ref s_systemWebProxy, ref s_systemWebProxyInitialized, ref s_lockObject, () => new SystemWebProxy()); public ICredentials Credentials { diff --git a/src/libraries/System.Net.Requests/src/System/Net/WebRequest.cs b/src/libraries/System.Net.Requests/src/System/Net/WebRequest.cs index b4f7e004ccc..85f8bbafe23 100644 --- a/src/libraries/System.Net.Requests/src/System/Net/WebRequest.cs +++ b/src/libraries/System.Net.Requests/src/System/Net/WebRequest.cs @@ -9,6 +9,7 @@ using System.Net.Cache; using System.Net.Security; using System.Runtime.Serialization; using System.Security.Principal; +using System.Threading; using System.Threading.Tasks; namespace System.Net { @@ -27,8 +28,8 @@ namespace System.Net } } - private static volatile List s_prefixList; - private static readonly object s_internalSyncObject = new object(); + private static List s_prefixList; + private static object s_internalSyncObject = new object(); internal const int DefaultTimeoutMilliseconds = 100 * 1000; @@ -353,36 +354,28 @@ namespace System.Net // GetConfig() might use us, so we have a circular dependency issue // that causes us to nest here. We grab the lock only if we haven't // initialized. - if (s_prefixList == null) + return LazyInitializer.EnsureInitialized(ref s_prefixList, ref s_internalSyncObject, () => { - lock (s_internalSyncObject) + var httpRequestCreator = new HttpRequestCreator(); + var ftpRequestCreator = new FtpWebRequestCreator(); + var fileRequestCreator = new FileWebRequestCreator(); + + const int Count = 4; + var prefixList = new List(Count) { - if (s_prefixList == null) - { - var httpRequestCreator = new HttpRequestCreator(); - var ftpRequestCreator = new FtpWebRequestCreator(); - var fileRequestCreator = new FileWebRequestCreator(); + new WebRequestPrefixElement("http:", httpRequestCreator), + new WebRequestPrefixElement("https:", httpRequestCreator), + new WebRequestPrefixElement("ftp:", ftpRequestCreator), + new WebRequestPrefixElement("file:", fileRequestCreator), + }; + Debug.Assert(prefixList.Count == Count, $"Expected {Count}, got {prefixList.Count}"); - const int Count = 4; - var prefixList = new List(Count) - { - new WebRequestPrefixElement("http:", httpRequestCreator), - new WebRequestPrefixElement("https:", httpRequestCreator), - new WebRequestPrefixElement("ftp:", ftpRequestCreator), - new WebRequestPrefixElement("file:", fileRequestCreator), - }; - Debug.Assert(prefixList.Count == Count, $"Expected {Count}, got {prefixList.Count}"); - - s_prefixList = prefixList; - } - } - } - - return s_prefixList; + return prefixList; + }); } set { - s_prefixList = value; + Volatile.Write(ref s_prefixList, value); } } @@ -568,24 +561,13 @@ namespace System.Net { get { - lock (s_internalSyncObject) - { - if (!s_DefaultWebProxyInitialized) - { - s_DefaultWebProxy = SystemWebProxy.Get(); - s_DefaultWebProxyInitialized = true; - } - - return s_DefaultWebProxy; - } + return LazyInitializer.EnsureInitialized(ref s_DefaultWebProxy, ref s_DefaultWebProxyInitialized, ref s_internalSyncObject, () => SystemWebProxy.Get()); } - set { lock (s_internalSyncObject) { s_DefaultWebProxy = value; - s_DefaultWebProxyInitialized = true; } } } diff --git a/src/libraries/System.Net.Security/src/System/Net/CertificateValidationPal.Unix.cs b/src/libraries/System.Net.Security/src/System/Net/CertificateValidationPal.Unix.cs index 0c4fa705f1c..64b20442c2e 100644 --- a/src/libraries/System.Net.Security/src/System/Net/CertificateValidationPal.Unix.cs +++ b/src/libraries/System.Net.Security/src/System/Net/CertificateValidationPal.Unix.cs @@ -14,7 +14,7 @@ namespace System.Net { internal static partial class CertificateValidationPal { - private static readonly object s_lockObject = new object(); + private static object s_lockObject = new object(); private static X509Store s_userCertStore; internal static SslPolicyErrors VerifyCertificateProperties( @@ -148,35 +148,22 @@ namespace System.Net private static X509Store EnsureStoreOpened(ref X509Store storeField, StoreLocation storeLocation) { - X509Store store = Volatile.Read(ref storeField); - - if (store == null) + return LazyInitializer.EnsureInitialized(ref storeField, ref s_lockObject, () => { - lock (s_lockObject) + try { - store = Volatile.Read(ref storeField); + X509Store store = new X509Store(StoreName.My, storeLocation); + store.Open(OpenFlags.ReadOnly); - if (store == null) - { - try - { - store = new X509Store(StoreName.My, storeLocation); - store.Open(OpenFlags.ReadOnly); - - Volatile.Write(ref storeField, store); - - if (NetEventSource.IsEnabled) NetEventSource.Info(null, $"storeLocation: {storeLocation} returned store {store}"); - } - catch (CryptographicException e) - { - NetEventSource.Fail(null, $"Failed to open cert store, location: {storeLocation} exception {e}"); - throw; - } - } + if (NetEventSource.IsEnabled) NetEventSource.Info(null, $"storeLocation: {storeLocation} returned store {store}"); + return store; } - } - - return store; + catch (CryptographicException e) + { + NetEventSource.Fail(null, $"Failed to open cert store, location: {storeLocation} exception {e}"); + throw; + } + }); } private static int QueryContextRemoteCertificate(SafeDeleteContext securityContext, out SafeFreeCertContext remoteCertContext) diff --git a/src/libraries/System.Transactions.Local/src/System/Transactions/DurableEnlistmentState.cs b/src/libraries/System.Transactions.Local/src/System/Transactions/DurableEnlistmentState.cs index b68f134dc66..67a42614b1b 100644 --- a/src/libraries/System.Transactions.Local/src/System/Transactions/DurableEnlistmentState.cs +++ b/src/libraries/System.Transactions.Local/src/System/Transactions/DurableEnlistmentState.cs @@ -10,129 +10,29 @@ namespace System.Transactions // Base class for all durable enlistment states internal abstract class DurableEnlistmentState : EnlistmentState { - // Double-checked locking pattern requires volatile for read/write synchronization - private static volatile DurableEnlistmentActive s_durableEnlistmentActive; - private static volatile DurableEnlistmentAborting s_durableEnlistmentAborting; - private static volatile DurableEnlistmentCommitting s_durableEnlistmentCommitting; - private static volatile DurableEnlistmentDelegated s_durableEnlistmentDelegated; - private static volatile DurableEnlistmentEnded s_durableEnlistmentEnded; + private static DurableEnlistmentActive s_durableEnlistmentActive; + private static DurableEnlistmentAborting s_durableEnlistmentAborting; + private static DurableEnlistmentCommitting s_durableEnlistmentCommitting; + private static DurableEnlistmentDelegated s_durableEnlistmentDelegated; + private static DurableEnlistmentEnded s_durableEnlistmentEnded; // Object for synchronizing access to the entire class( avoiding lock( typeof( ... )) ) private static object s_classSyncObject; - internal static DurableEnlistmentActive DurableEnlistmentActive - { - get - { - if (s_durableEnlistmentActive == null) - { - lock (ClassSyncObject) - { - if (s_durableEnlistmentActive == null) - { - DurableEnlistmentActive temp = new DurableEnlistmentActive(); - s_durableEnlistmentActive = temp; - } - } - } + internal static DurableEnlistmentActive DurableEnlistmentActive => + LazyInitializer.EnsureInitialized(ref s_durableEnlistmentActive, ref s_classSyncObject, () => new DurableEnlistmentActive()); - return s_durableEnlistmentActive; - } - } + protected static DurableEnlistmentAborting DurableEnlistmentAborting => + LazyInitializer.EnsureInitialized(ref s_durableEnlistmentAborting, ref s_classSyncObject, () => new DurableEnlistmentAborting()); - protected static DurableEnlistmentAborting DurableEnlistmentAborting - { - get - { - if (s_durableEnlistmentAborting == null) - { - lock (ClassSyncObject) - { - if (s_durableEnlistmentAborting == null) - { - DurableEnlistmentAborting temp = new DurableEnlistmentAborting(); - s_durableEnlistmentAborting = temp; - } - } - } + protected static DurableEnlistmentCommitting DurableEnlistmentCommitting => + LazyInitializer.EnsureInitialized(ref s_durableEnlistmentCommitting, ref s_classSyncObject, () => new DurableEnlistmentCommitting()); - return s_durableEnlistmentAborting; - } - } + protected static DurableEnlistmentDelegated DurableEnlistmentDelegated => + LazyInitializer.EnsureInitialized(ref s_durableEnlistmentDelegated, ref s_classSyncObject, () => new DurableEnlistmentDelegated()); - protected static DurableEnlistmentCommitting DurableEnlistmentCommitting - { - get - { - if (s_durableEnlistmentCommitting == null) - { - lock (ClassSyncObject) - { - if (s_durableEnlistmentCommitting == null) - { - DurableEnlistmentCommitting temp = new DurableEnlistmentCommitting(); - s_durableEnlistmentCommitting = temp; - } - } - } - - return s_durableEnlistmentCommitting; - } - } - - protected static DurableEnlistmentDelegated DurableEnlistmentDelegated - { - get - { - if (s_durableEnlistmentDelegated == null) - { - lock (ClassSyncObject) - { - if (s_durableEnlistmentDelegated == null) - { - DurableEnlistmentDelegated temp = new DurableEnlistmentDelegated(); - s_durableEnlistmentDelegated = temp; - } - } - } - - return s_durableEnlistmentDelegated; - } - } - - protected static DurableEnlistmentEnded DurableEnlistmentEnded - { - get - { - if (s_durableEnlistmentEnded == null) - { - lock (ClassSyncObject) - { - if (s_durableEnlistmentEnded == null) - { - DurableEnlistmentEnded temp = new DurableEnlistmentEnded(); - s_durableEnlistmentEnded = temp; - } - } - } - - return s_durableEnlistmentEnded; - } - } - - // Helper object for static synchronization - private static object ClassSyncObject - { - get - { - if (s_classSyncObject == null) - { - object o = new object(); - Interlocked.CompareExchange(ref s_classSyncObject, o, null); - } - return s_classSyncObject; - } - } + protected static DurableEnlistmentEnded DurableEnlistmentEnded => + LazyInitializer.EnsureInitialized(ref s_durableEnlistmentEnded, ref s_classSyncObject, () => new DurableEnlistmentEnded()); } // Active state for a durable enlistment. In this state the transaction can be aborted diff --git a/src/libraries/System.Transactions.Local/src/System/Transactions/EnlistmentState.cs b/src/libraries/System.Transactions.Local/src/System/Transactions/EnlistmentState.cs index ab2a6e1f2b4..7acf2a62ab8 100644 --- a/src/libraries/System.Transactions.Local/src/System/Transactions/EnlistmentState.cs +++ b/src/libraries/System.Transactions.Local/src/System/Transactions/EnlistmentState.cs @@ -12,45 +12,13 @@ namespace System.Transactions { internal abstract void EnterState(InternalEnlistment enlistment); - // Double-checked locking pattern requires volatile for read/write synchronization - internal static volatile EnlistmentStatePromoted _enlistmentStatePromoted; + internal static EnlistmentStatePromoted _enlistmentStatePromoted; // Object for synchronizing access to the entire class( avoiding lock( typeof( ... )) ) private static object s_classSyncObject; - // Helper object for static synchronization - private static object ClassSyncObject - { - get - { - if (s_classSyncObject == null) - { - object o = new object(); - Interlocked.CompareExchange(ref s_classSyncObject, o, null); - } - return s_classSyncObject; - } - } - - internal static EnlistmentStatePromoted EnlistmentStatePromoted - { - get - { - if (_enlistmentStatePromoted == null) - { - lock (ClassSyncObject) - { - if (_enlistmentStatePromoted == null) - { - EnlistmentStatePromoted temp = new EnlistmentStatePromoted(); - _enlistmentStatePromoted = temp; - } - } - } - - return _enlistmentStatePromoted; - } - } + internal static EnlistmentStatePromoted EnlistmentStatePromoted => + LazyInitializer.EnsureInitialized(ref _enlistmentStatePromoted, ref s_classSyncObject, () => new EnlistmentStatePromoted()); internal virtual void EnlistmentDone(InternalEnlistment enlistment) { diff --git a/src/libraries/System.Transactions.Local/src/System/Transactions/InternalTransaction.cs b/src/libraries/System.Transactions.Local/src/System/Transactions/InternalTransaction.cs index 2e91ade0468..3d04edd26bf 100644 --- a/src/libraries/System.Transactions.Local/src/System/Transactions/InternalTransaction.cs +++ b/src/libraries/System.Transactions.Local/src/System/Transactions/InternalTransaction.cs @@ -164,41 +164,12 @@ namespace System.Transactions // Object for synchronizing access to the entire class( avoiding lock( typeof( ... )) ) private static object s_classSyncObject; - internal static object ClassSyncObject - { - get - { - if (s_classSyncObject == null) - { - object o = new object(); - Interlocked.CompareExchange(ref s_classSyncObject, o, null); - } - return s_classSyncObject; - } - } internal Guid DistributedTxId => State.get_Identifier(this); - // Double-checked locking pattern requires volatile for read/write synchronization - private static volatile string s_instanceIdentifier; - internal static string InstanceIdentifier - { - get - { - if (s_instanceIdentifier == null) - { - lock (ClassSyncObject) - { - if (s_instanceIdentifier == null) - { - string temp = Guid.NewGuid().ToString() + ":"; - s_instanceIdentifier = temp; - } - } - } - return s_instanceIdentifier; - } - } + private static string s_instanceIdentifier; + internal static string InstanceIdentifier => + LazyInitializer.EnsureInitialized(ref s_instanceIdentifier, ref s_classSyncObject, () => Guid.NewGuid().ToString() + ":"); // Double-checked locking pattern requires volatile for read/write synchronization private volatile bool _traceIdentifierInited = false; diff --git a/src/libraries/System.Transactions.Local/src/System/Transactions/TransactionManager.cs b/src/libraries/System.Transactions.Local/src/System/Transactions/TransactionManager.cs index 626e4b818ef..5c02a8646e7 100644 --- a/src/libraries/System.Transactions.Local/src/System/Transactions/TransactionManager.cs +++ b/src/libraries/System.Transactions.Local/src/System/Transactions/TransactionManager.cs @@ -26,12 +26,10 @@ namespace System.Transactions // Hashtable of promoted transactions, keyed by identifier guid. This is used by // FindPromotedTransaction to support transaction equivalence when a transaction is // serialized and then deserialized back in this app-domain. - // Double-checked locking pattern requires volatile for read/write synchronization - private static volatile Hashtable s_promotedTransactionTable; + private static Hashtable s_promotedTransactionTable; // Sorted Table of transaction timeouts - // Double-checked locking pattern requires volatile for read/write synchronization - private static volatile TransactionTable s_transactionTable; + private static TransactionTable s_transactionTable; private static TransactionStartedEventHandler s_distributedTransactionStartedDelegate; public static event TransactionStartedEventHandler DistributedTransactionStarted @@ -269,18 +267,7 @@ namespace System.Transactions private static object s_classSyncObject; // Helper object for static synchronization - private static object ClassSyncObject - { - get - { - if (s_classSyncObject == null) - { - object o = new object(); - Interlocked.CompareExchange(ref s_classSyncObject, o, null); - } - return s_classSyncObject; - } - } + private static object ClassSyncObject => LazyInitializer.EnsureInitialized(ref s_classSyncObject); internal static IsolationLevel DefaultIsolationLevel { @@ -362,8 +349,7 @@ namespace System.Transactions } - // Double-checked locking pattern requires volatile for read/write synchronization - private static volatile bool s_cachedMaxTimeout; + private static bool s_cachedMaxTimeout; private static TimeSpan s_maximumTimeout; public static TimeSpan MaximumTimeout { @@ -375,18 +361,7 @@ namespace System.Transactions etwLog.MethodEnter(TraceSourceType.TraceSourceBase, "TransactionManager.get_DefaultMaximumTimeout"); } - if (!s_cachedMaxTimeout) - { - lock (ClassSyncObject) - { - if (!s_cachedMaxTimeout) - { - TimeSpan temp = MachineSettings.MaxTimeout; - s_maximumTimeout = temp; - s_cachedMaxTimeout = true; - } - } - } + LazyInitializer.EnsureInitialized(ref s_maximumTimeout, ref s_cachedMaxTimeout, ref s_classSyncObject, () => MachineSettings.MaxTimeout); if (etwLog.IsEnabled()) { @@ -583,69 +558,17 @@ namespace System.Transactions } // Table for promoted transactions - internal static Hashtable PromotedTransactionTable - { - get - { - if (s_promotedTransactionTable == null) - { - lock (ClassSyncObject) - { - if (s_promotedTransactionTable == null) - { - Hashtable temp = new Hashtable(100); - s_promotedTransactionTable = temp; - } - } - } - - return s_promotedTransactionTable; - } - } + internal static Hashtable PromotedTransactionTable => + LazyInitializer.EnsureInitialized(ref s_promotedTransactionTable, ref s_classSyncObject, () => new Hashtable(100)); // Table for transaction timeouts - internal static TransactionTable TransactionTable - { - get - { - if (s_transactionTable == null) - { - lock (ClassSyncObject) - { - if (s_transactionTable == null) - { - TransactionTable temp = new TransactionTable(); - s_transactionTable = temp; - } - } - } - - return s_transactionTable; - } - } - + internal static TransactionTable TransactionTable => + LazyInitializer.EnsureInitialized(ref s_transactionTable, ref s_classSyncObject, () => new TransactionTable()); // Fault in a DistributedTransactionManager if one has not already been created. - // Double-checked locking pattern requires volatile for read/write synchronization - internal static volatile DistributedTransactionManager distributedTransactionManager; - internal static DistributedTransactionManager DistributedTransactionManager - { - get - { - // If the distributed transaction manager is not configured, throw an exception - if (distributedTransactionManager == null) - { - lock (ClassSyncObject) - { - if (distributedTransactionManager == null) - { - distributedTransactionManager = new DistributedTransactionManager(); - } - } - } - - return distributedTransactionManager; - } - } + internal static DistributedTransactionManager distributedTransactionManager; + internal static DistributedTransactionManager DistributedTransactionManager => + // If the distributed transaction manager is not configured, throw an exception + LazyInitializer.EnsureInitialized(ref distributedTransactionManager, ref s_classSyncObject, () => new DistributedTransactionManager()); } } diff --git a/src/libraries/System.Transactions.Local/src/System/Transactions/TransactionState.cs b/src/libraries/System.Transactions.Local/src/System/Transactions/TransactionState.cs index 7c1743dd760..4660c929b4f 100644 --- a/src/libraries/System.Transactions.Local/src/System/Transactions/TransactionState.cs +++ b/src/libraries/System.Transactions.Local/src/System/Transactions/TransactionState.cs @@ -20,720 +20,144 @@ namespace System.Transactions // The state machines themselves are designed to be internally consistent. So the only externally visable // state transition is to active. All other state transitions must happen within the state machines // themselves. - // Double-checked locking pattern requires volatile for read/write synchronization - private static volatile TransactionStateActive s_transactionStateActive; - private static volatile TransactionStateSubordinateActive s_transactionStateSubordinateActive; - private static volatile TransactionStatePhase0 s_transactionStatePhase0; - private static volatile TransactionStateVolatilePhase1 s_transactionStateVolatilePhase1; - private static volatile TransactionStateVolatileSPC s_transactionStateVolatileSPC; - private static volatile TransactionStateSPC s_transactionStateSPC; - private static volatile TransactionStateAborted s_transactionStateAborted; - private static volatile TransactionStateCommitted s_transactionStateCommitted; - private static volatile TransactionStateInDoubt s_transactionStateInDoubt; + private static TransactionStateActive s_transactionStateActive; + private static TransactionStateSubordinateActive s_transactionStateSubordinateActive; + private static TransactionStatePhase0 s_transactionStatePhase0; + private static TransactionStateVolatilePhase1 s_transactionStateVolatilePhase1; + private static TransactionStateVolatileSPC s_transactionStateVolatileSPC; + private static TransactionStateSPC s_transactionStateSPC; + private static TransactionStateAborted s_transactionStateAborted; + private static TransactionStateCommitted s_transactionStateCommitted; + private static TransactionStateInDoubt s_transactionStateInDoubt; - private static volatile TransactionStatePromoted s_transactionStatePromoted; - private static volatile TransactionStateNonCommittablePromoted s_transactionStateNonCommittablePromoted; - private static volatile TransactionStatePromotedP0Wave s_transactionStatePromotedP0Wave; - private static volatile TransactionStatePromotedCommitting s_transactionStatePromotedCommitting; - private static volatile TransactionStatePromotedPhase0 s_transactionStatePromotedPhase0; - private static volatile TransactionStatePromotedPhase1 s_transactionStatePromotedPhase1; - private static volatile TransactionStatePromotedP0Aborting s_transactionStatePromotedP0Aborting; - private static volatile TransactionStatePromotedP1Aborting s_transactionStatePromotedP1Aborting; - private static volatile TransactionStatePromotedAborted s_transactionStatePromotedAborted; - private static volatile TransactionStatePromotedCommitted s_transactionStatePromotedCommitted; - private static volatile TransactionStatePromotedIndoubt s_transactionStatePromotedIndoubt; + private static TransactionStatePromoted s_transactionStatePromoted; + private static TransactionStateNonCommittablePromoted s_transactionStateNonCommittablePromoted; + private static TransactionStatePromotedP0Wave s_transactionStatePromotedP0Wave; + private static TransactionStatePromotedCommitting s_transactionStatePromotedCommitting; + private static TransactionStatePromotedPhase0 s_transactionStatePromotedPhase0; + private static TransactionStatePromotedPhase1 s_transactionStatePromotedPhase1; + private static TransactionStatePromotedP0Aborting s_transactionStatePromotedP0Aborting; + private static TransactionStatePromotedP1Aborting s_transactionStatePromotedP1Aborting; + private static TransactionStatePromotedAborted s_transactionStatePromotedAborted; + private static TransactionStatePromotedCommitted s_transactionStatePromotedCommitted; + private static TransactionStatePromotedIndoubt s_transactionStatePromotedIndoubt; - private static volatile TransactionStateDelegated s_transactionStateDelegated; - private static volatile TransactionStateDelegatedSubordinate s_transactionStateDelegatedSubordinate; - private static volatile TransactionStateDelegatedP0Wave s_transactionStateDelegatedP0Wave; - private static volatile TransactionStateDelegatedCommitting s_transactionStateDelegatedCommitting; - private static volatile TransactionStateDelegatedAborting s_transactionStateDelegatedAborting; - private static volatile TransactionStatePSPEOperation s_transactionStatePSPEOperation; + private static TransactionStateDelegated s_transactionStateDelegated; + private static TransactionStateDelegatedSubordinate s_transactionStateDelegatedSubordinate; + private static TransactionStateDelegatedP0Wave s_transactionStateDelegatedP0Wave; + private static TransactionStateDelegatedCommitting s_transactionStateDelegatedCommitting; + private static TransactionStateDelegatedAborting s_transactionStateDelegatedAborting; + private static TransactionStatePSPEOperation s_transactionStatePSPEOperation; - private static volatile TransactionStateDelegatedNonMSDTC s_transactionStateDelegatedNonMSDTC; - private static volatile TransactionStatePromotedNonMSDTCPhase0 s_transactionStatePromotedNonMSDTCPhase0; - private static volatile TransactionStatePromotedNonMSDTCVolatilePhase1 s_transactionStatePromotedNonMSDTCVolatilePhase1; - private static volatile TransactionStatePromotedNonMSDTCSinglePhaseCommit s_transactionStatePromotedNonMSDTCSinglePhaseCommit; - private static volatile TransactionStatePromotedNonMSDTCAborted s_transactionStatePromotedNonMSDTCAborted; - private static volatile TransactionStatePromotedNonMSDTCCommitted s_transactionStatePromotedNonMSDTCCommitted; - private static volatile TransactionStatePromotedNonMSDTCIndoubt s_transactionStatePromotedNonMSDTCIndoubt; + private static TransactionStateDelegatedNonMSDTC s_transactionStateDelegatedNonMSDTC; + private static TransactionStatePromotedNonMSDTCPhase0 s_transactionStatePromotedNonMSDTCPhase0; + private static TransactionStatePromotedNonMSDTCVolatilePhase1 s_transactionStatePromotedNonMSDTCVolatilePhase1; + private static TransactionStatePromotedNonMSDTCSinglePhaseCommit s_transactionStatePromotedNonMSDTCSinglePhaseCommit; + private static TransactionStatePromotedNonMSDTCAborted s_transactionStatePromotedNonMSDTCAborted; + private static TransactionStatePromotedNonMSDTCCommitted s_transactionStatePromotedNonMSDTCCommitted; + private static TransactionStatePromotedNonMSDTCIndoubt s_transactionStatePromotedNonMSDTCIndoubt; // Object for synchronizing access to the entire class( avoiding lock( typeof( ... )) ) - private static object s_classSyncObject; - - internal static TransactionStateActive TransactionStateActive - { - get - { - if (s_transactionStateActive == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateActive == null) - { - TransactionStateActive temp = new TransactionStateActive(); - s_transactionStateActive = temp; - } - } - } - - return s_transactionStateActive; - } - } - - internal static TransactionStateSubordinateActive TransactionStateSubordinateActive - { - get - { - if (s_transactionStateSubordinateActive == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateSubordinateActive == null) - { - TransactionStateSubordinateActive temp = new TransactionStateSubordinateActive(); - s_transactionStateSubordinateActive = temp; - } - } - } - - return s_transactionStateSubordinateActive; - } - } - - internal static TransactionStatePSPEOperation TransactionStatePSPEOperation - { - get - { - if (s_transactionStatePSPEOperation == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePSPEOperation == null) - { - TransactionStatePSPEOperation temp = new TransactionStatePSPEOperation(); - s_transactionStatePSPEOperation = temp; - } - } - } - - return s_transactionStatePSPEOperation; - } - } - - protected static TransactionStatePhase0 TransactionStatePhase0 - { - get - { - if (s_transactionStatePhase0 == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePhase0 == null) - { - TransactionStatePhase0 temp = new TransactionStatePhase0(); - s_transactionStatePhase0 = temp; - } - } - } - - return s_transactionStatePhase0; - } - } - - protected static TransactionStateVolatilePhase1 TransactionStateVolatilePhase1 - { - get - { - if (s_transactionStateVolatilePhase1 == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateVolatilePhase1 == null) - { - TransactionStateVolatilePhase1 temp = new TransactionStateVolatilePhase1(); - s_transactionStateVolatilePhase1 = temp; - } - } - } - - return s_transactionStateVolatilePhase1; - } - } - - protected static TransactionStateVolatileSPC TransactionStateVolatileSPC - { - get - { - if (s_transactionStateVolatileSPC == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateVolatileSPC == null) - { - TransactionStateVolatileSPC temp = new TransactionStateVolatileSPC(); - s_transactionStateVolatileSPC = temp; - } - } - } - - return s_transactionStateVolatileSPC; - } - } - - protected static TransactionStateSPC TransactionStateSPC - { - get - { - if (s_transactionStateSPC == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateSPC == null) - { - TransactionStateSPC temp = new TransactionStateSPC(); - s_transactionStateSPC = temp; - } - } - } - - return s_transactionStateSPC; - } - } - - protected static TransactionStateAborted TransactionStateAborted - { - get - { - if (s_transactionStateAborted == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateAborted == null) - { - TransactionStateAborted temp = new TransactionStateAborted(); - s_transactionStateAborted = temp; - } - } - } - - return s_transactionStateAborted; - } - } - - protected static TransactionStateCommitted TransactionStateCommitted - { - get - { - if (s_transactionStateCommitted == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateCommitted == null) - { - TransactionStateCommitted temp = new TransactionStateCommitted(); - s_transactionStateCommitted = temp; - } - } - } - - return s_transactionStateCommitted; - } - } - - protected static TransactionStateInDoubt TransactionStateInDoubt - { - get - { - if (s_transactionStateInDoubt == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateInDoubt == null) - { - TransactionStateInDoubt temp = new TransactionStateInDoubt(); - s_transactionStateInDoubt = temp; - } - } - } - - return s_transactionStateInDoubt; - } - } - - internal static TransactionStatePromoted TransactionStatePromoted - { - get - { - if (s_transactionStatePromoted == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromoted == null) - { - TransactionStatePromoted temp = new TransactionStatePromoted(); - s_transactionStatePromoted = temp; - } - } - } - - return s_transactionStatePromoted; - } - } - - internal static TransactionStateNonCommittablePromoted TransactionStateNonCommittablePromoted - { - get - { - if (s_transactionStateNonCommittablePromoted == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateNonCommittablePromoted == null) - { - TransactionStateNonCommittablePromoted temp = new TransactionStateNonCommittablePromoted(); - s_transactionStateNonCommittablePromoted = temp; - } - } - } - - return s_transactionStateNonCommittablePromoted; - } - } - - protected static TransactionStatePromotedP0Wave TransactionStatePromotedP0Wave - { - get - { - if (s_transactionStatePromotedP0Wave == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedP0Wave == null) - { - TransactionStatePromotedP0Wave temp = new TransactionStatePromotedP0Wave(); - s_transactionStatePromotedP0Wave = temp; - } - } - } - - return s_transactionStatePromotedP0Wave; - } - } - - protected static TransactionStatePromotedCommitting TransactionStatePromotedCommitting - { - get - { - if (s_transactionStatePromotedCommitting == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedCommitting == null) - { - TransactionStatePromotedCommitting temp = new TransactionStatePromotedCommitting(); - s_transactionStatePromotedCommitting = temp; - } - } - } - - return s_transactionStatePromotedCommitting; - } - } - - protected static TransactionStatePromotedPhase0 TransactionStatePromotedPhase0 - { - get - { - if (s_transactionStatePromotedPhase0 == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedPhase0 == null) - { - TransactionStatePromotedPhase0 temp = new TransactionStatePromotedPhase0(); - s_transactionStatePromotedPhase0 = temp; - } - } - } - - return s_transactionStatePromotedPhase0; - } - } - - protected static TransactionStatePromotedPhase1 TransactionStatePromotedPhase1 - { - get - { - if (s_transactionStatePromotedPhase1 == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedPhase1 == null) - { - TransactionStatePromotedPhase1 temp = new TransactionStatePromotedPhase1(); - s_transactionStatePromotedPhase1 = temp; - } - } - } - - return s_transactionStatePromotedPhase1; - } - } - - protected static TransactionStatePromotedP0Aborting TransactionStatePromotedP0Aborting - { - get - { - if (s_transactionStatePromotedP0Aborting == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedP0Aborting == null) - { - TransactionStatePromotedP0Aborting temp = new TransactionStatePromotedP0Aborting(); - s_transactionStatePromotedP0Aborting = temp; - } - } - } - - return s_transactionStatePromotedP0Aborting; - } - } - - protected static TransactionStatePromotedP1Aborting TransactionStatePromotedP1Aborting - { - get - { - if (s_transactionStatePromotedP1Aborting == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedP1Aborting == null) - { - TransactionStatePromotedP1Aborting temp = new TransactionStatePromotedP1Aborting(); - s_transactionStatePromotedP1Aborting = temp; - } - } - } - - return s_transactionStatePromotedP1Aborting; - } - } - - protected static TransactionStatePromotedAborted TransactionStatePromotedAborted - { - get - { - if (s_transactionStatePromotedAborted == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedAborted == null) - { - TransactionStatePromotedAborted temp = new TransactionStatePromotedAborted(); - s_transactionStatePromotedAborted = temp; - } - } - } - - return s_transactionStatePromotedAborted; - } - } - - protected static TransactionStatePromotedCommitted TransactionStatePromotedCommitted - { - get - { - if (s_transactionStatePromotedCommitted == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedCommitted == null) - { - TransactionStatePromotedCommitted temp = new TransactionStatePromotedCommitted(); - s_transactionStatePromotedCommitted = temp; - } - } - } - - return s_transactionStatePromotedCommitted; - } - } - - protected static TransactionStatePromotedIndoubt TransactionStatePromotedIndoubt - { - get - { - if (s_transactionStatePromotedIndoubt == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedIndoubt == null) - { - TransactionStatePromotedIndoubt temp = new TransactionStatePromotedIndoubt(); - s_transactionStatePromotedIndoubt = temp; - } - } - } - - return s_transactionStatePromotedIndoubt; - } - } - - protected static TransactionStateDelegated TransactionStateDelegated - { - get - { - if (s_transactionStateDelegated == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateDelegated == null) - { - TransactionStateDelegated temp = new TransactionStateDelegated(); - s_transactionStateDelegated = temp; - } - } - } - - return s_transactionStateDelegated; - } - } - - internal static TransactionStateDelegatedSubordinate TransactionStateDelegatedSubordinate - { - get - { - if (s_transactionStateDelegatedSubordinate == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateDelegatedSubordinate == null) - { - TransactionStateDelegatedSubordinate temp = new TransactionStateDelegatedSubordinate(); - s_transactionStateDelegatedSubordinate = temp; - } - } - } - - return s_transactionStateDelegatedSubordinate; - } - } - - protected static TransactionStateDelegatedP0Wave TransactionStateDelegatedP0Wave - { - get - { - if (s_transactionStateDelegatedP0Wave == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateDelegatedP0Wave == null) - { - TransactionStateDelegatedP0Wave temp = new TransactionStateDelegatedP0Wave(); - s_transactionStateDelegatedP0Wave = temp; - } - } - } - - return s_transactionStateDelegatedP0Wave; - } - } - - protected static TransactionStateDelegatedCommitting TransactionStateDelegatedCommitting - { - get - { - if (s_transactionStateDelegatedCommitting == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateDelegatedCommitting == null) - { - TransactionStateDelegatedCommitting temp = new TransactionStateDelegatedCommitting(); - s_transactionStateDelegatedCommitting = temp; - } - } - } - - return s_transactionStateDelegatedCommitting; - } - } - - protected static TransactionStateDelegatedAborting TransactionStateDelegatedAborting - { - get - { - if (s_transactionStateDelegatedAborting == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateDelegatedAborting == null) - { - TransactionStateDelegatedAborting temp = new TransactionStateDelegatedAborting(); - s_transactionStateDelegatedAborting = temp; - } - } - } - - return s_transactionStateDelegatedAborting; - } - } - - protected static TransactionStateDelegatedNonMSDTC TransactionStateDelegatedNonMSDTC - { - get - { - if (s_transactionStateDelegatedNonMSDTC == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateDelegatedNonMSDTC == null) - { - TransactionStateDelegatedNonMSDTC temp = new TransactionStateDelegatedNonMSDTC(); - s_transactionStateDelegatedNonMSDTC = temp; - } - } - } - - return s_transactionStateDelegatedNonMSDTC; - } - } - - protected static TransactionStatePromotedNonMSDTCPhase0 TransactionStatePromotedNonMSDTCPhase0 - { - get - { - if (s_transactionStatePromotedNonMSDTCPhase0 == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedNonMSDTCPhase0 == null) - { - TransactionStatePromotedNonMSDTCPhase0 temp = new TransactionStatePromotedNonMSDTCPhase0(); - s_transactionStatePromotedNonMSDTCPhase0 = temp; - } - } - } - - return s_transactionStatePromotedNonMSDTCPhase0; - } - } - - protected static TransactionStatePromotedNonMSDTCVolatilePhase1 TransactionStatePromotedNonMSDTCVolatilePhase1 - { - get - { - if (s_transactionStatePromotedNonMSDTCVolatilePhase1 == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedNonMSDTCVolatilePhase1 == null) - { - TransactionStatePromotedNonMSDTCVolatilePhase1 temp = new TransactionStatePromotedNonMSDTCVolatilePhase1(); - s_transactionStatePromotedNonMSDTCVolatilePhase1 = temp; - } - } - } - - return s_transactionStatePromotedNonMSDTCVolatilePhase1; - } - } - - protected static TransactionStatePromotedNonMSDTCSinglePhaseCommit TransactionStatePromotedNonMSDTCSinglePhaseCommit - { - get - { - if (s_transactionStatePromotedNonMSDTCSinglePhaseCommit == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedNonMSDTCSinglePhaseCommit == null) - { - TransactionStatePromotedNonMSDTCSinglePhaseCommit temp = new TransactionStatePromotedNonMSDTCSinglePhaseCommit(); - s_transactionStatePromotedNonMSDTCSinglePhaseCommit = temp; - } - } - } - - return s_transactionStatePromotedNonMSDTCSinglePhaseCommit; - } - } - - protected static TransactionStatePromotedNonMSDTCAborted TransactionStatePromotedNonMSDTCAborted - { - get - { - if (s_transactionStatePromotedNonMSDTCAborted == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedNonMSDTCAborted == null) - { - TransactionStatePromotedNonMSDTCAborted temp = new TransactionStatePromotedNonMSDTCAborted(); - s_transactionStatePromotedNonMSDTCAborted = temp; - } - } - } - - return s_transactionStatePromotedNonMSDTCAborted; - } - } - - protected static TransactionStatePromotedNonMSDTCCommitted TransactionStatePromotedNonMSDTCCommitted - { - get - { - if (s_transactionStatePromotedNonMSDTCCommitted == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedNonMSDTCCommitted == null) - { - TransactionStatePromotedNonMSDTCCommitted temp = new TransactionStatePromotedNonMSDTCCommitted(); - s_transactionStatePromotedNonMSDTCCommitted = temp; - } - } - } - - return s_transactionStatePromotedNonMSDTCCommitted; - } - } - - protected static TransactionStatePromotedNonMSDTCIndoubt TransactionStatePromotedNonMSDTCIndoubt - { - get - { - if (s_transactionStatePromotedNonMSDTCIndoubt == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedNonMSDTCIndoubt == null) - { - TransactionStatePromotedNonMSDTCIndoubt temp = new TransactionStatePromotedNonMSDTCIndoubt(); - s_transactionStatePromotedNonMSDTCIndoubt = temp; - } - } - } - - return s_transactionStatePromotedNonMSDTCIndoubt; - } - } - - // Helper object for static synchronization - internal static object ClassSyncObject - { - get - { - if (s_classSyncObject == null) - { - object o = new object(); - Interlocked.CompareExchange(ref s_classSyncObject, o, null); - } - return s_classSyncObject; - } - } + internal static object s_classSyncObject; + + internal static TransactionStateActive TransactionStateActive => + LazyInitializer.EnsureInitialized(ref s_transactionStateActive, ref s_classSyncObject, () => new TransactionStateActive()); + + internal static TransactionStateSubordinateActive TransactionStateSubordinateActive => + LazyInitializer.EnsureInitialized(ref s_transactionStateSubordinateActive, ref s_classSyncObject, () => new TransactionStateSubordinateActive()); + + internal static TransactionStatePSPEOperation TransactionStatePSPEOperation => + LazyInitializer.EnsureInitialized(ref s_transactionStatePSPEOperation, ref s_classSyncObject, () => new TransactionStatePSPEOperation()); + + protected static TransactionStatePhase0 TransactionStatePhase0 => + LazyInitializer.EnsureInitialized(ref s_transactionStatePhase0, ref s_classSyncObject, () => new TransactionStatePhase0()); + + protected static TransactionStateVolatilePhase1 TransactionStateVolatilePhase1 => + LazyInitializer.EnsureInitialized(ref s_transactionStateVolatilePhase1, ref s_classSyncObject, () => new TransactionStateVolatilePhase1()); + + protected static TransactionStateVolatileSPC TransactionStateVolatileSPC => + LazyInitializer.EnsureInitialized(ref s_transactionStateVolatileSPC, ref s_classSyncObject, () => new TransactionStateVolatileSPC()); + + protected static TransactionStateSPC TransactionStateSPC => + LazyInitializer.EnsureInitialized(ref s_transactionStateSPC, ref s_classSyncObject, () => new TransactionStateSPC()); + + protected static TransactionStateAborted TransactionStateAborted => + LazyInitializer.EnsureInitialized(ref s_transactionStateAborted, ref s_classSyncObject, () => new TransactionStateAborted()); + + protected static TransactionStateCommitted TransactionStateCommitted => + LazyInitializer.EnsureInitialized(ref s_transactionStateCommitted, ref s_classSyncObject, () => new TransactionStateCommitted()); + + protected static TransactionStateInDoubt TransactionStateInDoubt => + LazyInitializer.EnsureInitialized(ref s_transactionStateInDoubt, ref s_classSyncObject, () => new TransactionStateInDoubt()); + + internal static TransactionStatePromoted TransactionStatePromoted => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromoted, ref s_classSyncObject, () => new TransactionStatePromoted()); + + internal static TransactionStateNonCommittablePromoted TransactionStateNonCommittablePromoted => + LazyInitializer.EnsureInitialized(ref s_transactionStateNonCommittablePromoted, ref s_classSyncObject, () => new TransactionStateNonCommittablePromoted()); + + protected static TransactionStatePromotedP0Wave TransactionStatePromotedP0Wave => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedP0Wave, ref s_classSyncObject, () => new TransactionStatePromotedP0Wave()); + + protected static TransactionStatePromotedCommitting TransactionStatePromotedCommitting => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedCommitting, ref s_classSyncObject, () => new TransactionStatePromotedCommitting()); + + protected static TransactionStatePromotedPhase0 TransactionStatePromotedPhase0 => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedPhase0, ref s_classSyncObject, () => new TransactionStatePromotedPhase0()); + + protected static TransactionStatePromotedPhase1 TransactionStatePromotedPhase1 => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedPhase1, ref s_classSyncObject, () => new TransactionStatePromotedPhase1()); + + protected static TransactionStatePromotedP0Aborting TransactionStatePromotedP0Aborting => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedP0Aborting, ref s_classSyncObject, () => new TransactionStatePromotedP0Aborting()); + + protected static TransactionStatePromotedP1Aborting TransactionStatePromotedP1Aborting => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedP1Aborting, ref s_classSyncObject, () => new TransactionStatePromotedP1Aborting()); + + protected static TransactionStatePromotedAborted TransactionStatePromotedAborted => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedAborted, ref s_classSyncObject, () => new TransactionStatePromotedAborted()); + + protected static TransactionStatePromotedCommitted TransactionStatePromotedCommitted => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedCommitted, ref s_classSyncObject, () => new TransactionStatePromotedCommitted()); + + protected static TransactionStatePromotedIndoubt TransactionStatePromotedIndoubt => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedIndoubt, ref s_classSyncObject, () => new TransactionStatePromotedIndoubt()); + + protected static TransactionStateDelegated TransactionStateDelegated => + LazyInitializer.EnsureInitialized(ref s_transactionStateDelegated, ref s_classSyncObject, () => new TransactionStateDelegated()); + + internal static TransactionStateDelegatedSubordinate TransactionStateDelegatedSubordinate => + LazyInitializer.EnsureInitialized(ref s_transactionStateDelegatedSubordinate, ref s_classSyncObject, () => new TransactionStateDelegatedSubordinate()); + + protected static TransactionStateDelegatedP0Wave TransactionStateDelegatedP0Wave => + LazyInitializer.EnsureInitialized(ref s_transactionStateDelegatedP0Wave, ref s_classSyncObject, () => new TransactionStateDelegatedP0Wave()); + + protected static TransactionStateDelegatedCommitting TransactionStateDelegatedCommitting => + LazyInitializer.EnsureInitialized(ref s_transactionStateDelegatedCommitting, ref s_classSyncObject, () => new TransactionStateDelegatedCommitting()); + + protected static TransactionStateDelegatedAborting TransactionStateDelegatedAborting => + LazyInitializer.EnsureInitialized(ref s_transactionStateDelegatedAborting, ref s_classSyncObject, () => new TransactionStateDelegatedAborting()); + + protected static TransactionStateDelegatedNonMSDTC TransactionStateDelegatedNonMSDTC => + LazyInitializer.EnsureInitialized(ref s_transactionStateDelegatedNonMSDTC, ref s_classSyncObject, () => new TransactionStateDelegatedNonMSDTC()); + + protected static TransactionStatePromotedNonMSDTCPhase0 TransactionStatePromotedNonMSDTCPhase0 => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedNonMSDTCPhase0, ref s_classSyncObject, () => new TransactionStatePromotedNonMSDTCPhase0()); + + protected static TransactionStatePromotedNonMSDTCVolatilePhase1 TransactionStatePromotedNonMSDTCVolatilePhase1 => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedNonMSDTCVolatilePhase1, ref s_classSyncObject, () => new TransactionStatePromotedNonMSDTCVolatilePhase1()); + + protected static TransactionStatePromotedNonMSDTCSinglePhaseCommit TransactionStatePromotedNonMSDTCSinglePhaseCommit => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedNonMSDTCSinglePhaseCommit, ref s_classSyncObject, () => new TransactionStatePromotedNonMSDTCSinglePhaseCommit()); + + protected static TransactionStatePromotedNonMSDTCAborted TransactionStatePromotedNonMSDTCAborted => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedNonMSDTCAborted, ref s_classSyncObject, () => new TransactionStatePromotedNonMSDTCAborted()); + + protected static TransactionStatePromotedNonMSDTCCommitted TransactionStatePromotedNonMSDTCCommitted => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedNonMSDTCCommitted, ref s_classSyncObject, () => new TransactionStatePromotedNonMSDTCCommitted()); + + protected static TransactionStatePromotedNonMSDTCIndoubt TransactionStatePromotedNonMSDTCIndoubt => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedNonMSDTCIndoubt, ref s_classSyncObject, () => new TransactionStatePromotedNonMSDTCIndoubt()); internal void CommonEnterState(InternalTransaction tx) { @@ -3534,26 +2958,8 @@ namespace System.Transactions protected abstract void PromotedTransactionOutcome(InternalTransaction tx); - // Double-checked locking pattern requires volatile for read/write synchronization - private static volatile WaitCallback s_signalMethod; - private static WaitCallback SignalMethod - { - get - { - if (s_signalMethod == null) - { - lock (ClassSyncObject) - { - if (s_signalMethod == null) - { - s_signalMethod = new WaitCallback(SignalCallback); - } - } - } - - return s_signalMethod; - } - } + private static WaitCallback s_signalMethod; + private static WaitCallback SignalMethod => LazyInitializer.EnsureInitialized(ref s_signalMethod, ref s_classSyncObject, () => new WaitCallback(SignalCallback)); private static void SignalCallback(object state) @@ -4665,27 +4071,8 @@ namespace System.Transactions protected abstract void PromotedTransactionOutcome(InternalTransaction tx); - // Double-checked locking pattern requires volatile for read/write synchronization - private static volatile WaitCallback s_signalMethod; - private static WaitCallback SignalMethod - { - get - { - if (s_signalMethod == null) - { - lock (ClassSyncObject) - { - if (s_signalMethod == null) - { - s_signalMethod = new WaitCallback(SignalCallback); - } - } - } - - return s_signalMethod; - } - } - + private static WaitCallback s_signalMethod; + private static WaitCallback SignalMethod => LazyInitializer.EnsureInitialized(ref s_signalMethod, ref s_classSyncObject, () => new WaitCallback(SignalCallback)); private static void SignalCallback(object state) { diff --git a/src/libraries/System.Transactions.Local/src/System/Transactions/VolatileEnlistmentMultiplexing.cs b/src/libraries/System.Transactions.Local/src/System/Transactions/VolatileEnlistmentMultiplexing.cs index 61f2cc39190..e955f484eaf 100644 --- a/src/libraries/System.Transactions.Local/src/System/Transactions/VolatileEnlistmentMultiplexing.cs +++ b/src/libraries/System.Transactions.Local/src/System/Transactions/VolatileEnlistmentMultiplexing.cs @@ -73,27 +73,9 @@ namespace System.Transactions } } - // Double-checked locking pattern requires volatile for read/write synchronization - private static volatile WaitCallback s_prepareCallback; - private static WaitCallback PrepareCallback - { - get - { - if (s_prepareCallback == null) - { - lock (ClassSyncObject) - { - if (s_prepareCallback == null) - { - WaitCallback temp = new WaitCallback(PoolablePrepare); - s_prepareCallback = temp; - } - } - } + private static WaitCallback s_prepareCallback; + private static WaitCallback PrepareCallback => LazyInitializer.EnsureInitialized(ref s_prepareCallback, ref s_classSyncObject, () => new WaitCallback(PoolablePrepare)); - return s_prepareCallback; - } - } protected static void PoolablePrepare(object state) { VolatileDemultiplexer demux = (VolatileDemultiplexer)state; @@ -130,27 +112,8 @@ namespace System.Transactions } } - // Double-checked locking pattern requires volatile for read/write synchronization - private static volatile WaitCallback s_commitCallback; - private static WaitCallback CommitCallback - { - get - { - if (s_commitCallback == null) - { - lock (ClassSyncObject) - { - if (s_commitCallback == null) - { - WaitCallback temp = new WaitCallback(PoolableCommit); - s_commitCallback = temp; - } - } - } - - return s_commitCallback; - } - } + private static WaitCallback s_commitCallback; + private static WaitCallback CommitCallback => LazyInitializer.EnsureInitialized(ref s_commitCallback, ref s_classSyncObject, () => new WaitCallback(PoolableCommit)); protected static void PoolableCommit(object state) { @@ -188,27 +151,8 @@ namespace System.Transactions } } - // Double-checked locking pattern requires volatile for read/write synchronization - private static volatile WaitCallback s_rollbackCallback; - private static WaitCallback RollbackCallback - { - get - { - if (s_rollbackCallback == null) - { - lock (ClassSyncObject) - { - if (s_rollbackCallback == null) - { - WaitCallback temp = new WaitCallback(PoolableRollback); - s_rollbackCallback = temp; - } - } - } - - return s_rollbackCallback; - } - } + private static WaitCallback s_rollbackCallback; + private static WaitCallback RollbackCallback => LazyInitializer.EnsureInitialized(ref s_rollbackCallback, ref s_classSyncObject, () => new WaitCallback(PoolableRollback)); protected static void PoolableRollback(object state) { @@ -246,27 +190,8 @@ namespace System.Transactions } } - // Double-checked locking pattern requires volatile for read/write synchronization - private static volatile WaitCallback s_inDoubtCallback; - private static WaitCallback InDoubtCallback - { - get - { - if (s_inDoubtCallback == null) - { - lock (ClassSyncObject) - { - if (s_inDoubtCallback == null) - { - WaitCallback temp = new WaitCallback(PoolableInDoubt); - s_inDoubtCallback = temp; - } - } - } - - return s_inDoubtCallback; - } - } + private static WaitCallback s_inDoubtCallback; + private static WaitCallback InDoubtCallback => LazyInitializer.EnsureInitialized(ref s_inDoubtCallback, ref s_classSyncObject, () => new WaitCallback(PoolableInDoubt)); protected static void PoolableInDoubt(object state) { diff --git a/src/libraries/System.Transactions.Local/src/System/Transactions/VolatileEnlistmentState.cs b/src/libraries/System.Transactions.Local/src/System/Transactions/VolatileEnlistmentState.cs index 1ac383d7077..aa804b4e457 100644 --- a/src/libraries/System.Transactions.Local/src/System/Transactions/VolatileEnlistmentState.cs +++ b/src/libraries/System.Transactions.Local/src/System/Transactions/VolatileEnlistmentState.cs @@ -11,242 +11,59 @@ namespace System.Transactions // Base class for all volatile enlistment states internal abstract class VolatileEnlistmentState : EnlistmentState { - // Double-checked locking pattern requires volatile for read/write synchronization - private static volatile VolatileEnlistmentActive s_volatileEnlistmentActive; - private static volatile VolatileEnlistmentPreparing s_volatileEnlistmentPreparing; - private static volatile VolatileEnlistmentPrepared s_volatileEnlistmentPrepared; - private static volatile VolatileEnlistmentSPC s_volatileEnlistmentSPC; - private static volatile VolatileEnlistmentPreparingAborting s_volatileEnlistmentPreparingAborting; - private static volatile VolatileEnlistmentAborting s_volatileEnlistmentAborting; - private static volatile VolatileEnlistmentCommitting s_volatileEnlistmentCommitting; - private static volatile VolatileEnlistmentInDoubt s_volatileEnlistmentInDoubt; - private static volatile VolatileEnlistmentEnded s_volatileEnlistmentEnded; - private static volatile VolatileEnlistmentDone s_volatileEnlistmentDone; + private static VolatileEnlistmentActive s_volatileEnlistmentActive; + private static VolatileEnlistmentPreparing s_volatileEnlistmentPreparing; + private static VolatileEnlistmentPrepared s_volatileEnlistmentPrepared; + private static VolatileEnlistmentSPC s_volatileEnlistmentSPC; + private static VolatileEnlistmentPreparingAborting s_volatileEnlistmentPreparingAborting; + private static VolatileEnlistmentAborting s_volatileEnlistmentAborting; + private static VolatileEnlistmentCommitting s_volatileEnlistmentCommitting; + private static VolatileEnlistmentInDoubt s_volatileEnlistmentInDoubt; + private static VolatileEnlistmentEnded s_volatileEnlistmentEnded; + private static VolatileEnlistmentDone s_volatileEnlistmentDone; // Object for synchronizing access to the entire class( avoiding lock( typeof( ... )) ) private static object s_classSyncObject; - internal static VolatileEnlistmentActive VolatileEnlistmentActive - { - get - { - if (s_volatileEnlistmentActive == null) - { - lock (ClassSyncObject) - { - if (s_volatileEnlistmentActive == null) - { - VolatileEnlistmentActive temp = new VolatileEnlistmentActive(); - s_volatileEnlistmentActive = temp; - } - } - } - - return s_volatileEnlistmentActive; - } - } + internal static VolatileEnlistmentActive VolatileEnlistmentActive => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentActive, ref s_classSyncObject, () => new VolatileEnlistmentActive()); - protected static VolatileEnlistmentPreparing VolatileEnlistmentPreparing - { - get - { - if (s_volatileEnlistmentPreparing == null) - { - lock (ClassSyncObject) - { - if (s_volatileEnlistmentPreparing == null) - { - VolatileEnlistmentPreparing temp = new VolatileEnlistmentPreparing(); - s_volatileEnlistmentPreparing = temp; - } - } - } - - return s_volatileEnlistmentPreparing; - } - } + protected static VolatileEnlistmentPreparing VolatileEnlistmentPreparing => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentPreparing, ref s_classSyncObject, () => new VolatileEnlistmentPreparing()); - protected static VolatileEnlistmentPrepared VolatileEnlistmentPrepared - { - get - { - if (s_volatileEnlistmentPrepared == null) - { - lock (ClassSyncObject) - { - if (s_volatileEnlistmentPrepared == null) - { - VolatileEnlistmentPrepared temp = new VolatileEnlistmentPrepared(); - s_volatileEnlistmentPrepared = temp; - } - } - } - - return s_volatileEnlistmentPrepared; - } - } + protected static VolatileEnlistmentPrepared VolatileEnlistmentPrepared => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentPrepared, ref s_classSyncObject, () => new VolatileEnlistmentPrepared()); - protected static VolatileEnlistmentSPC VolatileEnlistmentSPC - { - get - { - if (s_volatileEnlistmentSPC == null) - { - lock (ClassSyncObject) - { - if (s_volatileEnlistmentSPC == null) - { - VolatileEnlistmentSPC temp = new VolatileEnlistmentSPC(); - s_volatileEnlistmentSPC = temp; - } - } - } - - return s_volatileEnlistmentSPC; - } - } + protected static VolatileEnlistmentSPC VolatileEnlistmentSPC => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentSPC, ref s_classSyncObject, () => new VolatileEnlistmentSPC()); - protected static VolatileEnlistmentPreparingAborting VolatileEnlistmentPreparingAborting - { - get - { - if (s_volatileEnlistmentPreparingAborting == null) - { - lock (ClassSyncObject) - { - if (s_volatileEnlistmentPreparingAborting == null) - { - VolatileEnlistmentPreparingAborting temp = new VolatileEnlistmentPreparingAborting(); - s_volatileEnlistmentPreparingAborting = temp; - } - } - } - - return s_volatileEnlistmentPreparingAborting; - } - } + protected static VolatileEnlistmentPreparingAborting VolatileEnlistmentPreparingAborting => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentPreparingAborting, ref s_classSyncObject, () => new VolatileEnlistmentPreparingAborting()); - protected static VolatileEnlistmentAborting VolatileEnlistmentAborting - { - get - { - if (s_volatileEnlistmentAborting == null) - { - lock (ClassSyncObject) - { - if (s_volatileEnlistmentAborting == null) - { - VolatileEnlistmentAborting temp = new VolatileEnlistmentAborting(); - s_volatileEnlistmentAborting = temp; - } - } - } - - return s_volatileEnlistmentAborting; - } - } + protected static VolatileEnlistmentAborting VolatileEnlistmentAborting => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentAborting, ref s_classSyncObject, () => new VolatileEnlistmentAborting()); - protected static VolatileEnlistmentCommitting VolatileEnlistmentCommitting - { - get - { - if (s_volatileEnlistmentCommitting == null) - { - lock (ClassSyncObject) - { - if (s_volatileEnlistmentCommitting == null) - { - VolatileEnlistmentCommitting temp = new VolatileEnlistmentCommitting(); - s_volatileEnlistmentCommitting = temp; - } - } - } - - return s_volatileEnlistmentCommitting; - } - } - - protected static VolatileEnlistmentInDoubt VolatileEnlistmentInDoubt - { - get - { - if (s_volatileEnlistmentInDoubt == null) - { - lock (ClassSyncObject) - { - if (s_volatileEnlistmentInDoubt == null) - { - VolatileEnlistmentInDoubt temp = new VolatileEnlistmentInDoubt(); - s_volatileEnlistmentInDoubt = temp; - } - } - } - - return s_volatileEnlistmentInDoubt; - } - } + protected static VolatileEnlistmentCommitting VolatileEnlistmentCommitting => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentCommitting, ref s_classSyncObject, () => new VolatileEnlistmentCommitting()); - protected static VolatileEnlistmentEnded VolatileEnlistmentEnded - { - get - { - if (s_volatileEnlistmentEnded == null) - { - lock (ClassSyncObject) - { - if (s_volatileEnlistmentEnded == null) - { - VolatileEnlistmentEnded temp = new VolatileEnlistmentEnded(); - s_volatileEnlistmentEnded = temp; - } - } - } - - return s_volatileEnlistmentEnded; - } - } + protected static VolatileEnlistmentInDoubt VolatileEnlistmentInDoubt => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentInDoubt, ref s_classSyncObject, () => new VolatileEnlistmentInDoubt()); - protected static VolatileEnlistmentDone VolatileEnlistmentDone - { - get - { - if (s_volatileEnlistmentDone == null) - { - lock (ClassSyncObject) - { - if (s_volatileEnlistmentDone == null) - { - VolatileEnlistmentDone temp = new VolatileEnlistmentDone(); - s_volatileEnlistmentDone = temp; - } - } - } + protected static VolatileEnlistmentEnded VolatileEnlistmentEnded => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentEnded, ref s_classSyncObject, () => new VolatileEnlistmentEnded()); - return s_volatileEnlistmentDone; - } - } - // Helper object for static synchronization - private static object ClassSyncObject - { - get - { - if (s_classSyncObject == null) - { - object o = new object(); - Interlocked.CompareExchange(ref s_classSyncObject, o, null); - } - return s_classSyncObject; - } - } + protected static VolatileEnlistmentDone VolatileEnlistmentDone => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentDone, ref s_classSyncObject, () => new VolatileEnlistmentDone()); + // Override of get_RecoveryInformation to be more specific with the exception string. internal override byte[] RecoveryInformation(InternalEnlistment enlistment)