SpinLock.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #if NET_4_0
  2. // SpinLock.cs
  3. //
  4. // Copyright (c) 2008 Jérémie "Garuma" Laval
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. //
  25. using System;
  26. using System.Runtime.ConstrainedExecution;
  27. namespace System.Threading
  28. {
  29. public struct SpinLock
  30. {
  31. const int isFree = 0;
  32. const int isOwned = 1;
  33. int lockState;
  34. readonly SpinWait sw;
  35. int threadWhoTookLock;
  36. readonly bool isThreadOwnerTrackingEnabled;
  37. public bool IsThreadOwnerTrackingEnabled {
  38. get {
  39. return isThreadOwnerTrackingEnabled;
  40. }
  41. }
  42. public bool IsHeld {
  43. get {
  44. return lockState == isOwned;
  45. }
  46. }
  47. public bool IsHeldByCurrentThread {
  48. get {
  49. if (isThreadOwnerTrackingEnabled)
  50. return lockState == isOwned && Thread.CurrentThread.ManagedThreadId == threadWhoTookLock;
  51. else
  52. return lockState == isOwned;
  53. }
  54. }
  55. public SpinLock (bool trackId)
  56. {
  57. this.isThreadOwnerTrackingEnabled = trackId;
  58. this.threadWhoTookLock = 0;
  59. this.lockState = isFree;
  60. this.sw = new SpinWait();
  61. }
  62. void CheckAndSetThreadId ()
  63. {
  64. if (threadWhoTookLock == Thread.CurrentThread.ManagedThreadId)
  65. throw new LockRecursionException("The current thread has already acquired this lock.");
  66. threadWhoTookLock = Thread.CurrentThread.ManagedThreadId;
  67. }
  68. public void Enter (ref bool lockTaken)
  69. {
  70. try {
  71. Enter ();
  72. lockTaken = lockState == isOwned && Thread.CurrentThread.ManagedThreadId == threadWhoTookLock;
  73. } catch {
  74. lockTaken = false;
  75. }
  76. }
  77. internal void Enter ()
  78. {
  79. int result = Interlocked.Exchange (ref lockState, isOwned);
  80. //Thread.BeginCriticalRegion();
  81. while (result == isOwned) {
  82. // If resource available, set it to in-use and return
  83. if (result == isFree) {
  84. result = Interlocked.Exchange (ref lockState, isOwned);
  85. if (result == isFree)
  86. break;
  87. }
  88. // Efficiently spin, until the resource looks like it might
  89. // be free. NOTE: Just reading here (as compared to repeatedly
  90. // calling Exchange) improves performance because writing
  91. // forces all CPUs to update this value
  92. //while (Thread.VolatileRead (ref lockState) == isOwned) {
  93. sw.SpinOnce ();
  94. //}
  95. }
  96. CheckAndSetThreadId ();
  97. }
  98. bool TryEnter ()
  99. {
  100. //Thread.BeginCriticalRegion();
  101. // If resource available, set it to in-use and return
  102. if (Interlocked.Exchange (ref lockState, isOwned) == isFree) {
  103. CheckAndSetThreadId ();
  104. return true;
  105. }
  106. return false;
  107. }
  108. public void TryEnter (ref bool lockTaken)
  109. {
  110. try {
  111. lockTaken = TryEnter ();
  112. } catch {
  113. lockTaken = false;
  114. }
  115. }
  116. public void TryEnter (TimeSpan timeout, ref bool lockTaken)
  117. {
  118. TryEnter ((int)timeout.TotalMilliseconds, ref lockTaken);
  119. }
  120. public void TryEnter (int milliSeconds, ref bool lockTaken)
  121. {
  122. //Thread.BeginCriticalRegion();
  123. Watch sw = Watch.StartNew ();
  124. while (sw.ElapsedMilliseconds < milliSeconds) {
  125. TryEnter (ref lockTaken);
  126. }
  127. sw.Stop ();
  128. }
  129. //[ReliabilityContractAttribute]
  130. public void Exit ()
  131. {
  132. Exit (false);
  133. }
  134. public void Exit (bool flushReleaseWrites)
  135. {
  136. threadWhoTookLock = int.MinValue;
  137. // Mark the resource as available
  138. if (!flushReleaseWrites) {
  139. lockState = isFree;
  140. } else {
  141. Interlocked.Exchange (ref lockState, isFree);
  142. }
  143. //Thread.EndCriticalRegion();
  144. }
  145. }
  146. // Wraps a SpinLock in a reference when we need to pass
  147. // around the lock
  148. internal class SpinLockWrapper
  149. {
  150. public readonly SpinLock Lock = new SpinLock (false);
  151. }
  152. }
  153. #endif