From: Anirudh Agnihotry Date: Sat, 11 Aug 2018 06:19:28 +0000 (-0700) Subject: Moved SpinLock.cs to shared (dotnet/coreclr#19391) X-Git-Tag: submit/tizen/20210909.063632~11030^2~4163 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6eb4c639efd839cd5933511a0273d688436e67a2;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Moved SpinLock.cs to shared (dotnet/coreclr#19391) * m removed from names, spaces after \\ and braces added * Moved to shared Commit migrated from https://github.com/dotnet/coreclr/commit/b2c4c8c4713a029b8c59f481c967dec3c98f005a --- diff --git a/src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj index 74148cd..73819e1 100644 --- a/src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -393,7 +393,6 @@ - diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 1977d68..1133ce5 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -607,6 +607,7 @@ + diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Threading/SpinLock.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/SpinLock.cs similarity index 88% rename from src/coreclr/src/System.Private.CoreLib/src/System/Threading/SpinLock.cs rename to src/libraries/System.Private.CoreLib/src/System/Threading/SpinLock.cs index 64dfd0c..2b06105 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Threading/SpinLock.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/SpinLock.cs @@ -12,7 +12,6 @@ // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- using System.Diagnostics; -using System.Runtime.InteropServices; using System.Runtime.CompilerServices; namespace System.Threading @@ -63,7 +62,7 @@ namespace System.Threading // // There are several masks and constants below for convenience. - private volatile int m_owner; + private volatile int _owner; // After how many yields, call Sleep(1) private const int SLEEP_ONE_FREQUENCY = 40; @@ -72,16 +71,16 @@ namespace System.Threading private const int TIMEOUT_CHECK_FREQUENCY = 10; // Thr thread tracking disabled mask - private const int LOCK_ID_DISABLE_MASK = unchecked((int)0x80000000); //1000 0000 0000 0000 0000 0000 0000 0000 + private const int LOCK_ID_DISABLE_MASK = unchecked((int)0x80000000); // 1000 0000 0000 0000 0000 0000 0000 0000 //the lock is held by some thread, but we don't know which - private const int LOCK_ANONYMOUS_OWNED = 0x1; //0000 0000 0000 0000 0000 0000 0000 0001 + private const int LOCK_ANONYMOUS_OWNED = 0x1; // 0000 0000 0000 0000 0000 0000 0000 0001 // Waiters mask if the thread tracking is disabled - private const int WAITERS_MASK = ~(LOCK_ID_DISABLE_MASK | 1); //0111 1111 1111 1111 1111 1111 1111 1110 + private const int WAITERS_MASK = ~(LOCK_ID_DISABLE_MASK | 1); // 0111 1111 1111 1111 1111 1111 1111 1110 // The Thread tacking is disabled and the lock bit is set, used in Enter fast path to make sure the id is disabled and lock is available - private const int ID_DISABLED_AND_ANONYMOUS_OWNED = unchecked((int)0x80000001); //1000 0000 0000 0000 0000 0000 0000 0001 + private const int ID_DISABLED_AND_ANONYMOUS_OWNED = unchecked((int)0x80000001); // 1000 0000 0000 0000 0000 0000 0000 0001 // If the thread is unowned if: // m_owner zero and the thread tracking is enabled @@ -112,10 +111,10 @@ namespace System.Threading /// purposes. public SpinLock(bool enableThreadOwnerTracking) { - m_owner = LOCK_UNOWNED; + _owner = LOCK_UNOWNED; if (!enableThreadOwnerTracking) { - m_owner |= LOCK_ID_DISABLE_MASK; + _owner |= LOCK_ID_DISABLE_MASK; Debug.Assert(!IsThreadOwnerTrackingEnabled, "property should be false by now"); } } @@ -149,11 +148,11 @@ namespace System.Threading /// public void Enter(ref bool lockTaken) { - //Try to keep the code and branching in this method as small as possible in order to inline the method - int observedOwner = m_owner; - if (lockTaken || //invalid parameter - (observedOwner & ID_DISABLED_AND_ANONYMOUS_OWNED) != LOCK_ID_DISABLE_MASK || //thread tracking is enabled or the lock is already acquired - CompareExchange(ref m_owner, observedOwner | LOCK_ANONYMOUS_OWNED, observedOwner, ref lockTaken) != observedOwner) //acquiring the lock failed + // Try to keep the code and branching in this method as small as possible in order to inline the method + int observedOwner = _owner; + if (lockTaken || // invalid parameter + (observedOwner & ID_DISABLED_AND_ANONYMOUS_OWNED) != LOCK_ID_DISABLE_MASK || // thread tracking is enabled or the lock is already acquired + CompareExchange(ref _owner, observedOwner | LOCK_ANONYMOUS_OWNED, observedOwner, ref lockTaken) != observedOwner) //acquiring the lock failed ContinueTryEnter(Timeout.Infinite, ref lockTaken); // Then try the slow path if any of the above conditions is met } @@ -177,7 +176,7 @@ namespace System.Threading /// public void TryEnter(ref bool lockTaken) { - int observedOwner = m_owner; + int observedOwner = _owner; if (((observedOwner & LOCK_ID_DISABLE_MASK) == 0) | lockTaken) { // Thread tracking enabled or invalid arg. Take slow path. @@ -191,7 +190,7 @@ namespace System.Threading else { // Lock wasn't held; try to acquire it. - CompareExchange(ref m_owner, observedOwner | LOCK_ANONYMOUS_OWNED, observedOwner, ref lockTaken); + CompareExchange(ref _owner, observedOwner | LOCK_ANONYMOUS_OWNED, observedOwner, ref lockTaken); } } @@ -259,11 +258,11 @@ namespace System.Threading /// a negative number other than -1, which represents an infinite time-out. public void TryEnter(int millisecondsTimeout, ref bool lockTaken) { - int observedOwner = m_owner; + int observedOwner = _owner; if (millisecondsTimeout < -1 || //invalid parameter lockTaken || //invalid parameter (observedOwner & ID_DISABLED_AND_ANONYMOUS_OWNED) != LOCK_ID_DISABLE_MASK || //thread tracking is enabled or the lock is already acquired - CompareExchange(ref m_owner, observedOwner | LOCK_ANONYMOUS_OWNED, observedOwner, ref lockTaken) != observedOwner) // acquiring the lock failed + CompareExchange(ref _owner, observedOwner | LOCK_ANONYMOUS_OWNED, observedOwner, ref lockTaken) != observedOwner) // acquiring the lock failed ContinueTryEnter(millisecondsTimeout, ref lockTaken); // The call the slow pth } @@ -280,7 +279,7 @@ namespace System.Threading if (lockTaken) { lockTaken = false; - throw new System.ArgumentException(SR.SpinLock_TryReliableEnter_ArgumentException); + throw new ArgumentException(SR.SpinLock_TryReliableEnter_ArgumentException); } if (millisecondsTimeout < -1) @@ -317,10 +316,10 @@ namespace System.Threading //***Step 1, take the lock or update the waiters // try to acquire the lock directly if possible or update the waiters count - observedOwner = m_owner; + observedOwner = _owner; if ((observedOwner & LOCK_ANONYMOUS_OWNED) == LOCK_UNOWNED) { - if (CompareExchange(ref m_owner, observedOwner | 1, observedOwner, ref lockTaken) == observedOwner) + if (CompareExchange(ref _owner, observedOwner | 1, observedOwner, ref lockTaken) == observedOwner) { // Acquired lock return; @@ -342,11 +341,11 @@ namespace System.Threading if ((observedOwner & WAITERS_MASK) != MAXIMUM_WAITERS) { // This can still overflow, but maybe there will never be that many waiters - turn = (Interlocked.Add(ref m_owner, 2) & WAITERS_MASK) >> 1; + turn = (Interlocked.Add(ref _owner, 2) & WAITERS_MASK) >> 1; } } - //lock acquired failed and waiters updated + // lock acquired failed and waiters updated //*** Step 2, Spinning and Yielding var spinner = new SpinWait(); @@ -358,7 +357,7 @@ namespace System.Threading { spinner.SpinOnce(SLEEP_ONE_FREQUENCY); - observedOwner = m_owner; + observedOwner = _owner; if ((observedOwner & LOCK_ANONYMOUS_OWNED) == LOCK_UNOWNED) { int newOwner = (observedOwner & WAITERS_MASK) == 0 ? // Gets the number of waiters, if zero @@ -366,7 +365,7 @@ namespace System.Threading : (observedOwner - 2) | 1; // otherwise decrement the waiters and set the lock bit Debug.Assert((newOwner & WAITERS_MASK) >= 0); - if (CompareExchange(ref m_owner, newOwner, observedOwner, ref lockTaken) == observedOwner) + if (CompareExchange(ref _owner, newOwner, observedOwner, ref lockTaken) == observedOwner) { return; } @@ -374,7 +373,7 @@ namespace System.Threading if (spinner.Count % TIMEOUT_CHECK_FREQUENCY == 0) { - //Check the timeout. + // Check the timeout. if (millisecondsTimeout != Timeout.Infinite && TimeoutHelper.UpdateTimeOut(startTime, millisecondsTimeout) <= 0) { DecrementWaiters(); @@ -392,9 +391,9 @@ namespace System.Threading SpinWait spinner = new SpinWait(); while (true) { - int observedOwner = m_owner; + int observedOwner = _owner; if ((observedOwner & WAITERS_MASK) == 0) return; // don't decrement the waiters if it's corrupted by previous call of Exit(false) - if (Interlocked.CompareExchange(ref m_owner, observedOwner - 2, observedOwner) == observedOwner) + if (Interlocked.CompareExchange(ref _owner, observedOwner - 2, observedOwner) == observedOwner) { Debug.Assert(!IsThreadOwnerTrackingEnabled); // Make sure the waiters never be negative which will cause the thread tracking bit to be flipped break; @@ -413,8 +412,8 @@ namespace System.Threading int lockUnowned = 0; // We are using thread IDs to mark ownership. Snap the thread ID and check for recursion. // We also must or the ID enablement bit, to ensure we propagate when we CAS it in. - int m_newOwner = Environment.CurrentManagedThreadId; - if (m_owner == m_newOwner) + int newOwner = Environment.CurrentManagedThreadId; + if (_owner == newOwner) { // We don't allow lock recursion. throw new LockRecursionException(SR.SpinLock_TryEnter_LockRecursionException); @@ -432,9 +431,9 @@ namespace System.Threading // Test before trying to CAS, to avoid acquiring the line exclusively unnecessarily. - if (m_owner == lockUnowned) + if (_owner == lockUnowned) { - if (CompareExchange(ref m_owner, m_newOwner, lockUnowned, ref lockTaken) == lockUnowned) + if (CompareExchange(ref _owner, newOwner, lockUnowned, ref lockTaken) == lockUnowned) { return; } @@ -462,10 +461,10 @@ namespace System.Threading public void Exit() { //This is the fast path for the thread tracking is disabled, otherwise go to the slow path - if ((m_owner & LOCK_ID_DISABLE_MASK) == 0) + if ((_owner & LOCK_ID_DISABLE_MASK) == 0) ExitSlowPath(true); else - Interlocked.Decrement(ref m_owner); + Interlocked.Decrement(ref _owner); } /// @@ -489,13 +488,15 @@ namespace System.Threading // This is the fast path for the thread tracking is disabled and not to use memory barrier, otherwise go to the slow path // The reason not to add else statement if the usememorybarrier is that it will add more branching in the code and will prevent // method inlining, so this is optimized for useMemoryBarrier=false and Exit() overload optimized for useMemoryBarrier=true. - int tmpOwner = m_owner; + int tmpOwner = _owner; if ((tmpOwner & LOCK_ID_DISABLE_MASK) != 0 & !useMemoryBarrier) { - m_owner = tmpOwner & (~LOCK_ANONYMOUS_OWNED); + _owner = tmpOwner & (~LOCK_ANONYMOUS_OWNED); } else + { ExitSlowPath(useMemoryBarrier); + } } /// @@ -507,28 +508,33 @@ namespace System.Threading /// private void ExitSlowPath(bool useMemoryBarrier) { - bool threadTrackingEnabled = (m_owner & LOCK_ID_DISABLE_MASK) == 0; + bool threadTrackingEnabled = (_owner & LOCK_ID_DISABLE_MASK) == 0; if (threadTrackingEnabled && !IsHeldByCurrentThread) { - throw new System.Threading.SynchronizationLockException( - SR.SpinLock_Exit_SynchronizationLockException); + throw new SynchronizationLockException(SR.SpinLock_Exit_SynchronizationLockException); } if (useMemoryBarrier) { if (threadTrackingEnabled) - Interlocked.Exchange(ref m_owner, LOCK_UNOWNED); + { + Interlocked.Exchange(ref _owner, LOCK_UNOWNED); + } else - Interlocked.Decrement(ref m_owner); + { + Interlocked.Decrement(ref _owner); + } } else { if (threadTrackingEnabled) - m_owner = LOCK_UNOWNED; + { + _owner = LOCK_UNOWNED; + } else { - int tmpOwner = m_owner; - m_owner = tmpOwner & (~LOCK_ANONYMOUS_OWNED); + int tmpOwner = _owner; + _owner = tmpOwner & (~LOCK_ANONYMOUS_OWNED); } } } @@ -541,9 +547,9 @@ namespace System.Threading get { if (IsThreadOwnerTrackingEnabled) - return m_owner != LOCK_UNOWNED; + return _owner != LOCK_UNOWNED; - return (m_owner & LOCK_ANONYMOUS_OWNED) != LOCK_UNOWNED; + return (_owner & LOCK_ANONYMOUS_OWNED) != LOCK_UNOWNED; } } @@ -569,15 +575,12 @@ namespace System.Threading { throw new InvalidOperationException(SR.SpinLock_IsHeldByCurrentThread); } - return ((m_owner & (~LOCK_ID_DISABLE_MASK)) == Environment.CurrentManagedThreadId); + return ((_owner & (~LOCK_ID_DISABLE_MASK)) == Environment.CurrentManagedThreadId); } } /// Gets whether thread ownership tracking is enabled for this instance. - public bool IsThreadOwnerTrackingEnabled - { - get { return (m_owner & LOCK_ID_DISABLE_MASK) == 0; } - } + public bool IsThreadOwnerTrackingEnabled => (_owner & LOCK_ID_DISABLE_MASK) == 0; #region Debugger proxy class /// @@ -586,7 +589,7 @@ namespace System.Threading internal class SystemThreading_SpinLockDebugView { // SpinLock object - private SpinLock m_spinLock; + private SpinLock _spinLock; /// /// SystemThreading_SpinLockDebugView constructor @@ -595,7 +598,7 @@ namespace System.Threading public SystemThreading_SpinLockDebugView(SpinLock spinLock) { // Note that this makes a copy of the SpinLock (struct). It doesn't hold a reference to it. - m_spinLock = spinLock; + _spinLock = spinLock; } /// @@ -607,7 +610,7 @@ namespace System.Threading { try { - return m_spinLock.IsHeldByCurrentThread; + return _spinLock.IsHeldByCurrentThread; } catch (InvalidOperationException) { @@ -623,9 +626,9 @@ namespace System.Threading { get { - if (m_spinLock.IsThreadOwnerTrackingEnabled) + if (_spinLock.IsThreadOwnerTrackingEnabled) { - return m_spinLock.m_owner; + return _spinLock._owner; } else { @@ -638,10 +641,7 @@ namespace System.Threading /// /// Gets whether the lock is currently held by any thread or not. /// - public bool IsHeld - { - get { return m_spinLock.IsHeld; } - } + public bool IsHeld => _spinLock.IsHeld; } #endregion