Monitor.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // System.Threading.Monitor.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Runtime.CompilerServices;
  32. using System.Runtime.Remoting.Contexts;
  33. using System.Runtime.ConstrainedExecution;
  34. using System.Runtime.InteropServices;
  35. namespace System.Threading
  36. {
  37. [ComVisible (true)]
  38. public static class Monitor
  39. {
  40. // Grabs the mutex on object 'obj', with a maximum
  41. // wait time 'ms' but doesn't block - if it can't get
  42. // the lock it returns false, true if it can
  43. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  44. private extern static bool Monitor_try_enter(object obj, int ms);
  45. // Enter/Exit are implemented directly as icalls for performance reasons
  46. // Acquires the mutex on object 'obj'
  47. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  48. public extern static void Enter(object obj);
  49. // Releases the mutex on object 'obj'
  50. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
  51. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  52. public extern static void Exit(object obj);
  53. // Signals one of potentially many objects waiting on
  54. // object 'obj'
  55. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  56. private extern static void Monitor_pulse(object obj);
  57. // Checks whether object 'obj' is currently synchronised
  58. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  59. private extern static bool Monitor_test_synchronised(object obj);
  60. public static void Pulse(object obj) {
  61. if(obj==null) {
  62. throw new ArgumentNullException("obj");
  63. }
  64. if(Monitor_test_synchronised(obj)==false) {
  65. throw new SynchronizationLockException("Object is not synchronized");
  66. }
  67. Monitor_pulse(obj);
  68. }
  69. // Signals all of potentially many objects waiting on
  70. // object 'obj'
  71. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  72. private extern static void Monitor_pulse_all(object obj);
  73. public static void PulseAll(object obj) {
  74. if(obj==null) {
  75. throw new ArgumentNullException("obj");
  76. }
  77. if(Monitor_test_synchronised(obj)==false) {
  78. throw new SynchronizationLockException("Object is not synchronized");
  79. }
  80. Monitor_pulse_all(obj);
  81. }
  82. public static bool TryEnter (object obj)
  83. {
  84. return TryEnter (obj, 0);
  85. }
  86. public static bool TryEnter (object obj, int millisecondsTimeout)
  87. {
  88. if (obj == null)
  89. throw new ArgumentNullException ("obj");
  90. if (millisecondsTimeout == Timeout.Infinite) {
  91. Enter (obj);
  92. return true;
  93. }
  94. if (millisecondsTimeout < 0)
  95. throw new ArgumentException ("negative value for millisecondsTimeout", "millisecondsTimeout");
  96. return Monitor_try_enter (obj, millisecondsTimeout);
  97. }
  98. public static bool TryEnter (object obj, TimeSpan timeout)
  99. {
  100. long ms = (long) timeout.TotalMilliseconds;
  101. if (ms < Timeout.Infinite || ms > Int32.MaxValue)
  102. throw new ArgumentOutOfRangeException ("timeout", "timeout out of range");
  103. return TryEnter (obj, (int) ms);
  104. }
  105. // Waits for a signal on object 'obj' with maximum
  106. // wait time 'ms'. Returns true if the object was
  107. // signalled, false if it timed out
  108. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  109. private extern static bool Monitor_wait(object obj, int ms);
  110. public static bool Wait (object obj)
  111. {
  112. return Wait (obj, Timeout.Infinite);
  113. }
  114. public static bool Wait (object obj, int millisecondsTimeout)
  115. {
  116. if (obj == null)
  117. throw new ArgumentNullException ("obj");
  118. if (millisecondsTimeout < Timeout.Infinite)
  119. throw new ArgumentOutOfRangeException ("millisecondsTimeout", "timeout out of range");
  120. if (!Monitor_test_synchronised (obj))
  121. throw new SynchronizationLockException ("Object is not synchronized");
  122. return Monitor_wait (obj, millisecondsTimeout);
  123. }
  124. public static bool Wait (object obj, TimeSpan timeout)
  125. {
  126. long ms = (long) timeout.TotalMilliseconds;
  127. if (ms < Timeout.Infinite || ms > Int32.MaxValue)
  128. throw new ArgumentOutOfRangeException ("timeout", "timeout out of range");
  129. return Wait (obj, (int) ms);
  130. }
  131. public static bool Wait(object obj, int millisecondsTimeout, bool exitContext) {
  132. try {
  133. if (exitContext) {
  134. #if MONOTOUCH
  135. throw new NotSupportedException ("exitContext == true is not supported");
  136. #else
  137. SynchronizationAttribute.ExitContext ();
  138. #endif
  139. }
  140. return Wait (obj, millisecondsTimeout);
  141. }
  142. finally {
  143. if (exitContext) SynchronizationAttribute.EnterContext ();
  144. }
  145. }
  146. public static bool Wait(object obj, TimeSpan timeout, bool exitContext) {
  147. try {
  148. if (exitContext) {
  149. #if MONOTOUCH
  150. throw new NotSupportedException ("exitContext == true is not supported");
  151. #else
  152. SynchronizationAttribute.ExitContext ();
  153. #endif
  154. }
  155. return Wait (obj, timeout);
  156. }
  157. finally {
  158. if (exitContext) SynchronizationAttribute.EnterContext ();
  159. }
  160. }
  161. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  162. extern static void try_enter_with_atomic_var (object obj, int millisecondsTimeout, ref bool lockTaken);
  163. public static void Enter (object obj, ref bool lockTaken)
  164. {
  165. TryEnter (obj, Timeout.Infinite, ref lockTaken);
  166. }
  167. public static void TryEnter (object obj, ref bool lockTaken)
  168. {
  169. TryEnter (obj, 0, ref lockTaken);
  170. }
  171. public static void TryEnter (object obj, TimeSpan timeout, ref bool lockTaken)
  172. {
  173. long ms = (long) timeout.TotalMilliseconds;
  174. if (ms < Timeout.Infinite || ms > Int32.MaxValue)
  175. throw new ArgumentOutOfRangeException ("timeout", "timeout out of range");
  176. TryEnter (obj, (int)ms, ref lockTaken);
  177. }
  178. public static void TryEnter (object obj, int millisecondsTimeout, ref bool lockTaken)
  179. {
  180. if (obj == null)
  181. throw new ArgumentNullException ("obj");
  182. if (lockTaken)
  183. throw new ArgumentException ("lockTaken");
  184. if (millisecondsTimeout < 0 && millisecondsTimeout != Timeout.Infinite)
  185. throw new ArgumentException ("negative value for millisecondsTimeout", "millisecondsTimeout");
  186. try_enter_with_atomic_var (obj, millisecondsTimeout, ref lockTaken);
  187. }
  188. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  189. extern static bool Monitor_test_owner (object obj);
  190. public
  191. static bool IsEntered (object obj)
  192. {
  193. return Monitor_test_owner(obj);
  194. }
  195. }
  196. }