diff options
author | Stephen Toub <stoub@microsoft.com> | 2017-03-13 19:50:14 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-13 19:50:14 +0300 |
commit | 6abde68de9611895fa8697fb7fc17105e906081e (patch) | |
tree | 911fe36c55f9cb351172f4b65919c468355c5541 /src/System.Transactions.Local | |
parent | 4ebab0433410e88f64783ab6978f61c0b78172a6 (diff) | |
parent | 84967d03d5f6f74d6be3fab39204e78d61f11374 (diff) |
Merge pull request #16909 from WinCPP/issue-3862
Fix #3862 Replace double-check lock with lazy initialization.
Diffstat (limited to 'src/System.Transactions.Local')
7 files changed, 179 insertions, 1288 deletions
diff --git a/src/System.Transactions.Local/src/System/Transactions/DurableEnlistmentState.cs b/src/System.Transactions.Local/src/System/Transactions/DurableEnlistmentState.cs index b68f134dc6..67a42614b1 100644 --- a/src/System.Transactions.Local/src/System/Transactions/DurableEnlistmentState.cs +++ b/src/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; - } - } - } - - return s_durableEnlistmentActive; - } - } - - protected static DurableEnlistmentAborting DurableEnlistmentAborting - { - get - { - if (s_durableEnlistmentAborting == null) - { - lock (ClassSyncObject) - { - if (s_durableEnlistmentAborting == null) - { - DurableEnlistmentAborting temp = new DurableEnlistmentAborting(); - s_durableEnlistmentAborting = temp; - } - } - } - - return s_durableEnlistmentAborting; - } - } - - 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; - } - } + internal static DurableEnlistmentActive DurableEnlistmentActive => + LazyInitializer.EnsureInitialized(ref s_durableEnlistmentActive, ref s_classSyncObject, () => new DurableEnlistmentActive()); - protected static DurableEnlistmentDelegated DurableEnlistmentDelegated - { - get - { - if (s_durableEnlistmentDelegated == null) - { - lock (ClassSyncObject) - { - if (s_durableEnlistmentDelegated == null) - { - DurableEnlistmentDelegated temp = new DurableEnlistmentDelegated(); - s_durableEnlistmentDelegated = temp; - } - } - } + protected static DurableEnlistmentAborting DurableEnlistmentAborting => + LazyInitializer.EnsureInitialized(ref s_durableEnlistmentAborting, ref s_classSyncObject, () => new DurableEnlistmentAborting()); - return s_durableEnlistmentDelegated; - } - } + protected static DurableEnlistmentCommitting DurableEnlistmentCommitting => + LazyInitializer.EnsureInitialized(ref s_durableEnlistmentCommitting, ref s_classSyncObject, () => new DurableEnlistmentCommitting()); - protected static DurableEnlistmentEnded DurableEnlistmentEnded - { - get - { - if (s_durableEnlistmentEnded == null) - { - lock (ClassSyncObject) - { - if (s_durableEnlistmentEnded == null) - { - DurableEnlistmentEnded temp = new DurableEnlistmentEnded(); - s_durableEnlistmentEnded = temp; - } - } - } + protected static DurableEnlistmentDelegated DurableEnlistmentDelegated => + LazyInitializer.EnsureInitialized(ref s_durableEnlistmentDelegated, ref s_classSyncObject, () => new DurableEnlistmentDelegated()); - 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/System.Transactions.Local/src/System/Transactions/EnlistmentState.cs b/src/System.Transactions.Local/src/System/Transactions/EnlistmentState.cs index ab2a6e1f2b..7acf2a62ab 100644 --- a/src/System.Transactions.Local/src/System/Transactions/EnlistmentState.cs +++ b/src/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/System.Transactions.Local/src/System/Transactions/InternalTransaction.cs b/src/System.Transactions.Local/src/System/Transactions/InternalTransaction.cs index 2e91ade046..3d04edd26b 100644 --- a/src/System.Transactions.Local/src/System/Transactions/InternalTransaction.cs +++ b/src/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/System.Transactions.Local/src/System/Transactions/TransactionManager.cs b/src/System.Transactions.Local/src/System/Transactions/TransactionManager.cs index 626e4b818e..5c02a8646e 100644 --- a/src/System.Transactions.Local/src/System/Transactions/TransactionManager.cs +++ b/src/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/System.Transactions.Local/src/System/Transactions/TransactionState.cs b/src/System.Transactions.Local/src/System/Transactions/TransactionState.cs index 7c1743dd76..4660c929b4 100644 --- a/src/System.Transactions.Local/src/System/Transactions/TransactionState.cs +++ b/src/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 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 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 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 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 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 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 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 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; - } - } - } + internal static TransactionStateActive TransactionStateActive => + LazyInitializer.EnsureInitialized(ref s_transactionStateActive, ref s_classSyncObject, () => new TransactionStateActive()); - return s_transactionStateActive; - } - } + internal static TransactionStateSubordinateActive TransactionStateSubordinateActive => + LazyInitializer.EnsureInitialized(ref s_transactionStateSubordinateActive, ref s_classSyncObject, () => new TransactionStateSubordinateActive()); - internal static TransactionStateSubordinateActive TransactionStateSubordinateActive - { - get - { - if (s_transactionStateSubordinateActive == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateSubordinateActive == null) - { - TransactionStateSubordinateActive temp = new TransactionStateSubordinateActive(); - s_transactionStateSubordinateActive = temp; - } - } - } + internal static TransactionStatePSPEOperation TransactionStatePSPEOperation => + LazyInitializer.EnsureInitialized(ref s_transactionStatePSPEOperation, ref s_classSyncObject, () => new TransactionStatePSPEOperation()); - return s_transactionStateSubordinateActive; - } - } + protected static TransactionStatePhase0 TransactionStatePhase0 => + LazyInitializer.EnsureInitialized(ref s_transactionStatePhase0, ref s_classSyncObject, () => new TransactionStatePhase0()); - internal static TransactionStatePSPEOperation TransactionStatePSPEOperation - { - get - { - if (s_transactionStatePSPEOperation == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePSPEOperation == null) - { - TransactionStatePSPEOperation temp = new TransactionStatePSPEOperation(); - s_transactionStatePSPEOperation = temp; - } - } - } + protected static TransactionStateVolatilePhase1 TransactionStateVolatilePhase1 => + LazyInitializer.EnsureInitialized(ref s_transactionStateVolatilePhase1, ref s_classSyncObject, () => new TransactionStateVolatilePhase1()); - return s_transactionStatePSPEOperation; - } - } + protected static TransactionStateVolatileSPC TransactionStateVolatileSPC => + LazyInitializer.EnsureInitialized(ref s_transactionStateVolatileSPC, ref s_classSyncObject, () => new TransactionStateVolatileSPC()); - protected static TransactionStatePhase0 TransactionStatePhase0 - { - get - { - if (s_transactionStatePhase0 == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePhase0 == null) - { - TransactionStatePhase0 temp = new TransactionStatePhase0(); - s_transactionStatePhase0 = temp; - } - } - } + protected static TransactionStateSPC TransactionStateSPC => + LazyInitializer.EnsureInitialized(ref s_transactionStateSPC, ref s_classSyncObject, () => new TransactionStateSPC()); - return s_transactionStatePhase0; - } - } + protected static TransactionStateAborted TransactionStateAborted => + LazyInitializer.EnsureInitialized(ref s_transactionStateAborted, ref s_classSyncObject, () => new TransactionStateAborted()); - protected static TransactionStateVolatilePhase1 TransactionStateVolatilePhase1 - { - get - { - if (s_transactionStateVolatilePhase1 == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateVolatilePhase1 == null) - { - TransactionStateVolatilePhase1 temp = new TransactionStateVolatilePhase1(); - s_transactionStateVolatilePhase1 = temp; - } - } - } + protected static TransactionStateCommitted TransactionStateCommitted => + LazyInitializer.EnsureInitialized(ref s_transactionStateCommitted, ref s_classSyncObject, () => new TransactionStateCommitted()); - return s_transactionStateVolatilePhase1; - } - } + protected static TransactionStateInDoubt TransactionStateInDoubt => + LazyInitializer.EnsureInitialized(ref s_transactionStateInDoubt, ref s_classSyncObject, () => new TransactionStateInDoubt()); - protected static TransactionStateVolatileSPC TransactionStateVolatileSPC - { - get - { - if (s_transactionStateVolatileSPC == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateVolatileSPC == null) - { - TransactionStateVolatileSPC temp = new TransactionStateVolatileSPC(); - s_transactionStateVolatileSPC = temp; - } - } - } + internal static TransactionStatePromoted TransactionStatePromoted => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromoted, ref s_classSyncObject, () => new TransactionStatePromoted()); - return s_transactionStateVolatileSPC; - } - } + internal static TransactionStateNonCommittablePromoted TransactionStateNonCommittablePromoted => + LazyInitializer.EnsureInitialized(ref s_transactionStateNonCommittablePromoted, ref s_classSyncObject, () => new TransactionStateNonCommittablePromoted()); - protected static TransactionStateSPC TransactionStateSPC - { - get - { - if (s_transactionStateSPC == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateSPC == null) - { - TransactionStateSPC temp = new TransactionStateSPC(); - s_transactionStateSPC = temp; - } - } - } + protected static TransactionStatePromotedP0Wave TransactionStatePromotedP0Wave => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedP0Wave, ref s_classSyncObject, () => new TransactionStatePromotedP0Wave()); - return s_transactionStateSPC; - } - } + protected static TransactionStatePromotedCommitting TransactionStatePromotedCommitting => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedCommitting, ref s_classSyncObject, () => new TransactionStatePromotedCommitting()); - protected static TransactionStateAborted TransactionStateAborted - { - get - { - if (s_transactionStateAborted == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateAborted == null) - { - TransactionStateAborted temp = new TransactionStateAborted(); - s_transactionStateAborted = temp; - } - } - } + protected static TransactionStatePromotedPhase0 TransactionStatePromotedPhase0 => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedPhase0, ref s_classSyncObject, () => new TransactionStatePromotedPhase0()); - return s_transactionStateAborted; - } - } + protected static TransactionStatePromotedPhase1 TransactionStatePromotedPhase1 => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedPhase1, ref s_classSyncObject, () => new TransactionStatePromotedPhase1()); - protected static TransactionStateCommitted TransactionStateCommitted - { - get - { - if (s_transactionStateCommitted == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateCommitted == null) - { - TransactionStateCommitted temp = new TransactionStateCommitted(); - s_transactionStateCommitted = temp; - } - } - } + protected static TransactionStatePromotedP0Aborting TransactionStatePromotedP0Aborting => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedP0Aborting, ref s_classSyncObject, () => new TransactionStatePromotedP0Aborting()); - return s_transactionStateCommitted; - } - } + protected static TransactionStatePromotedP1Aborting TransactionStatePromotedP1Aborting => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedP1Aborting, ref s_classSyncObject, () => new TransactionStatePromotedP1Aborting()); - protected static TransactionStateInDoubt TransactionStateInDoubt - { - get - { - if (s_transactionStateInDoubt == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateInDoubt == null) - { - TransactionStateInDoubt temp = new TransactionStateInDoubt(); - s_transactionStateInDoubt = temp; - } - } - } + protected static TransactionStatePromotedAborted TransactionStatePromotedAborted => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedAborted, ref s_classSyncObject, () => new TransactionStatePromotedAborted()); - return s_transactionStateInDoubt; - } - } + protected static TransactionStatePromotedCommitted TransactionStatePromotedCommitted => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedCommitted, ref s_classSyncObject, () => new TransactionStatePromotedCommitted()); - internal static TransactionStatePromoted TransactionStatePromoted - { - get - { - if (s_transactionStatePromoted == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromoted == null) - { - TransactionStatePromoted temp = new TransactionStatePromoted(); - s_transactionStatePromoted = temp; - } - } - } + protected static TransactionStatePromotedIndoubt TransactionStatePromotedIndoubt => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedIndoubt, ref s_classSyncObject, () => new TransactionStatePromotedIndoubt()); - return s_transactionStatePromoted; - } - } + protected static TransactionStateDelegated TransactionStateDelegated => + LazyInitializer.EnsureInitialized(ref s_transactionStateDelegated, ref s_classSyncObject, () => new TransactionStateDelegated()); - internal static TransactionStateNonCommittablePromoted TransactionStateNonCommittablePromoted - { - get - { - if (s_transactionStateNonCommittablePromoted == null) - { - lock (ClassSyncObject) - { - if (s_transactionStateNonCommittablePromoted == null) - { - TransactionStateNonCommittablePromoted temp = new TransactionStateNonCommittablePromoted(); - s_transactionStateNonCommittablePromoted = temp; - } - } - } + internal static TransactionStateDelegatedSubordinate TransactionStateDelegatedSubordinate => + LazyInitializer.EnsureInitialized(ref s_transactionStateDelegatedSubordinate, ref s_classSyncObject, () => new TransactionStateDelegatedSubordinate()); - 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; - } - } - } + protected static TransactionStateDelegatedP0Wave TransactionStateDelegatedP0Wave => + LazyInitializer.EnsureInitialized(ref s_transactionStateDelegatedP0Wave, ref s_classSyncObject, () => new TransactionStateDelegatedP0Wave()); - return s_transactionStatePromotedPhase0; - } - } + protected static TransactionStateDelegatedCommitting TransactionStateDelegatedCommitting => + LazyInitializer.EnsureInitialized(ref s_transactionStateDelegatedCommitting, ref s_classSyncObject, () => new TransactionStateDelegatedCommitting()); - protected static TransactionStatePromotedPhase1 TransactionStatePromotedPhase1 - { - get - { - if (s_transactionStatePromotedPhase1 == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedPhase1 == null) - { - TransactionStatePromotedPhase1 temp = new TransactionStatePromotedPhase1(); - s_transactionStatePromotedPhase1 = temp; - } - } - } + protected static TransactionStateDelegatedAborting TransactionStateDelegatedAborting => + LazyInitializer.EnsureInitialized(ref s_transactionStateDelegatedAborting, ref s_classSyncObject, () => new TransactionStateDelegatedAborting()); - return s_transactionStatePromotedPhase1; - } - } + protected static TransactionStateDelegatedNonMSDTC TransactionStateDelegatedNonMSDTC => + LazyInitializer.EnsureInitialized(ref s_transactionStateDelegatedNonMSDTC, ref s_classSyncObject, () => new TransactionStateDelegatedNonMSDTC()); - protected static TransactionStatePromotedP0Aborting TransactionStatePromotedP0Aborting - { - get - { - if (s_transactionStatePromotedP0Aborting == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedP0Aborting == null) - { - TransactionStatePromotedP0Aborting temp = new TransactionStatePromotedP0Aborting(); - s_transactionStatePromotedP0Aborting = temp; - } - } - } + protected static TransactionStatePromotedNonMSDTCPhase0 TransactionStatePromotedNonMSDTCPhase0 => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedNonMSDTCPhase0, ref s_classSyncObject, () => new TransactionStatePromotedNonMSDTCPhase0()); - return s_transactionStatePromotedP0Aborting; - } - } + protected static TransactionStatePromotedNonMSDTCVolatilePhase1 TransactionStatePromotedNonMSDTCVolatilePhase1 => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedNonMSDTCVolatilePhase1, ref s_classSyncObject, () => new TransactionStatePromotedNonMSDTCVolatilePhase1()); - protected static TransactionStatePromotedP1Aborting TransactionStatePromotedP1Aborting - { - get - { - if (s_transactionStatePromotedP1Aborting == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedP1Aborting == null) - { - TransactionStatePromotedP1Aborting temp = new TransactionStatePromotedP1Aborting(); - s_transactionStatePromotedP1Aborting = temp; - } - } - } + protected static TransactionStatePromotedNonMSDTCSinglePhaseCommit TransactionStatePromotedNonMSDTCSinglePhaseCommit => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedNonMSDTCSinglePhaseCommit, ref s_classSyncObject, () => new TransactionStatePromotedNonMSDTCSinglePhaseCommit()); - return s_transactionStatePromotedP1Aborting; - } - } + protected static TransactionStatePromotedNonMSDTCAborted TransactionStatePromotedNonMSDTCAborted => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedNonMSDTCAborted, ref s_classSyncObject, () => new TransactionStatePromotedNonMSDTCAborted()); - protected static TransactionStatePromotedAborted TransactionStatePromotedAborted - { - get - { - if (s_transactionStatePromotedAborted == null) - { - lock (ClassSyncObject) - { - if (s_transactionStatePromotedAborted == null) - { - TransactionStatePromotedAborted temp = new TransactionStatePromotedAborted(); - s_transactionStatePromotedAborted = temp; - } - } - } + protected static TransactionStatePromotedNonMSDTCCommitted TransactionStatePromotedNonMSDTCCommitted => + LazyInitializer.EnsureInitialized(ref s_transactionStatePromotedNonMSDTCCommitted, ref s_classSyncObject, () => new TransactionStatePromotedNonMSDTCCommitted()); - 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; - } - } + 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/System.Transactions.Local/src/System/Transactions/VolatileEnlistmentMultiplexing.cs b/src/System.Transactions.Local/src/System/Transactions/VolatileEnlistmentMultiplexing.cs index 61f2cc3919..e955f484ea 100644 --- a/src/System.Transactions.Local/src/System/Transactions/VolatileEnlistmentMultiplexing.cs +++ b/src/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/System.Transactions.Local/src/System/Transactions/VolatileEnlistmentState.cs b/src/System.Transactions.Local/src/System/Transactions/VolatileEnlistmentState.cs index 1ac383d707..aa804b4e45 100644 --- a/src/System.Transactions.Local/src/System/Transactions/VolatileEnlistmentState.cs +++ b/src/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; - } - } - } + internal static VolatileEnlistmentActive VolatileEnlistmentActive => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentActive, ref s_classSyncObject, () => new VolatileEnlistmentActive()); - return s_volatileEnlistmentActive; - } - } + protected static VolatileEnlistmentPreparing VolatileEnlistmentPreparing => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentPreparing, ref s_classSyncObject, () => new VolatileEnlistmentPreparing()); - 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 VolatileEnlistmentPrepared VolatileEnlistmentPrepared => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentPrepared, ref s_classSyncObject, () => new VolatileEnlistmentPrepared()); - protected static VolatileEnlistmentPrepared VolatileEnlistmentPrepared - { - get - { - if (s_volatileEnlistmentPrepared == null) - { - lock (ClassSyncObject) - { - if (s_volatileEnlistmentPrepared == null) - { - VolatileEnlistmentPrepared temp = new VolatileEnlistmentPrepared(); - s_volatileEnlistmentPrepared = temp; - } - } - } + protected static VolatileEnlistmentSPC VolatileEnlistmentSPC => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentSPC, ref s_classSyncObject, () => new VolatileEnlistmentSPC()); - return s_volatileEnlistmentPrepared; - } - } + protected static VolatileEnlistmentPreparingAborting VolatileEnlistmentPreparingAborting => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentPreparingAborting, ref s_classSyncObject, () => new VolatileEnlistmentPreparingAborting()); - 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 VolatileEnlistmentAborting VolatileEnlistmentAborting => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentAborting, ref s_classSyncObject, () => new VolatileEnlistmentAborting()); - protected static VolatileEnlistmentPreparingAborting VolatileEnlistmentPreparingAborting - { - get - { - if (s_volatileEnlistmentPreparingAborting == null) - { - lock (ClassSyncObject) - { - if (s_volatileEnlistmentPreparingAborting == null) - { - VolatileEnlistmentPreparingAborting temp = new VolatileEnlistmentPreparingAborting(); - s_volatileEnlistmentPreparingAborting = temp; - } - } - } + protected static VolatileEnlistmentCommitting VolatileEnlistmentCommitting => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentCommitting, ref s_classSyncObject, () => new VolatileEnlistmentCommitting()); - return s_volatileEnlistmentPreparingAborting; - } - } + protected static VolatileEnlistmentInDoubt VolatileEnlistmentInDoubt => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentInDoubt, ref s_classSyncObject, () => new VolatileEnlistmentInDoubt()); - 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 VolatileEnlistmentEnded VolatileEnlistmentEnded => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentEnded, ref s_classSyncObject, () => new VolatileEnlistmentEnded()); - 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 VolatileEnlistmentEnded VolatileEnlistmentEnded - { - get - { - if (s_volatileEnlistmentEnded == null) - { - lock (ClassSyncObject) - { - if (s_volatileEnlistmentEnded == null) - { - VolatileEnlistmentEnded temp = new VolatileEnlistmentEnded(); - s_volatileEnlistmentEnded = temp; - } - } - } + protected static VolatileEnlistmentDone VolatileEnlistmentDone => + LazyInitializer.EnsureInitialized(ref s_volatileEnlistmentDone, ref s_classSyncObject, () => new VolatileEnlistmentDone()); - return s_volatileEnlistmentEnded; - } - } - - - protected static VolatileEnlistmentDone VolatileEnlistmentDone - { - get - { - if (s_volatileEnlistmentDone == null) - { - lock (ClassSyncObject) - { - if (s_volatileEnlistmentDone == null) - { - VolatileEnlistmentDone temp = new VolatileEnlistmentDone(); - s_volatileEnlistmentDone = temp; - } - } - } - - 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; - } - } // Override of get_RecoveryInformation to be more specific with the exception string. internal override byte[] RecoveryInformation(InternalEnlistment enlistment) |