Monitor.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #if NET_2_0
  34. using System.Runtime.ConstrainedExecution;
  35. #endif
  36. namespace System.Threading
  37. {
  38. public sealed class Monitor
  39. {
  40. private Monitor () {}
  41. // Grabs the mutex on object 'obj', with a maximum
  42. // wait time 'ms' but doesn't block - if it can't get
  43. // the lock it returns false, true if it can
  44. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  45. private extern static bool Monitor_try_enter(object obj, int ms);
  46. public static void Enter(object obj) {
  47. if(obj==null) {
  48. throw new ArgumentNullException("obj");
  49. }
  50. //if(obj.GetType().IsValueType==true) {
  51. // throw new ArgumentException("Value type");
  52. //}
  53. Monitor_try_enter(obj, Timeout.Infinite);
  54. }
  55. // Releases the mutex on object 'obj'
  56. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  57. private extern static void Monitor_exit(object obj);
  58. #if NET_2_0
  59. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
  60. #endif
  61. public static void Exit(object obj) {
  62. if(obj==null) {
  63. throw new ArgumentNullException("obj");
  64. }
  65. //if(obj.GetType().IsValueType==true) {
  66. // throw new ArgumentException("Value type");
  67. //}
  68. Monitor_exit(obj);
  69. }
  70. // Signals one of potentially many objects waiting on
  71. // object 'obj'
  72. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  73. private extern static void Monitor_pulse(object obj);
  74. // Checks whether object 'obj' is currently synchronised
  75. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  76. private extern static bool Monitor_test_synchronised(object obj);
  77. public static void Pulse(object obj) {
  78. if(obj==null) {
  79. throw new ArgumentNullException("obj");
  80. }
  81. if(Monitor_test_synchronised(obj)==false) {
  82. throw new SynchronizationLockException("Object is not synchronized");
  83. }
  84. Monitor_pulse(obj);
  85. }
  86. // Signals all of potentially many objects waiting on
  87. // object 'obj'
  88. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  89. private extern static void Monitor_pulse_all(object obj);
  90. public static void PulseAll(object obj) {
  91. if(obj==null) {
  92. throw new ArgumentNullException("obj");
  93. }
  94. if(Monitor_test_synchronised(obj)==false) {
  95. throw new SynchronizationLockException("Object is not synchronized");
  96. }
  97. Monitor_pulse_all(obj);
  98. }
  99. public static bool TryEnter(object obj) {
  100. if(obj==null) {
  101. throw new ArgumentNullException("obj");
  102. }
  103. //if(obj.GetType().IsValueType==true) {
  104. // throw new ArgumentException("Value type");
  105. //}
  106. return(Monitor_try_enter(obj, 0));
  107. }
  108. public static bool TryEnter(object obj, int millisecondsTimeout) {
  109. if(obj==null) {
  110. throw new ArgumentNullException("obj");
  111. }
  112. //if(obj.GetType().IsValueType==true) {
  113. // throw new ArgumentException("Value type");
  114. //}
  115. // LAMESPEC: should throw an exception when ms<0, but
  116. // Timeout.Infinite is -1
  117. if(millisecondsTimeout == Timeout.Infinite) {
  118. Enter(obj);
  119. return(true);
  120. }
  121. if(millisecondsTimeout<0) {
  122. throw new ArgumentException("millisecondsTimeout", "negative value for millisecondsTimeout");
  123. }
  124. return(Monitor_try_enter(obj, millisecondsTimeout));
  125. }
  126. public static bool TryEnter(object obj, TimeSpan timeout) {
  127. if(obj==null) {
  128. throw new ArgumentNullException("obj");
  129. }
  130. //if(obj.GetType().IsValueType==true) {
  131. // throw new ArgumentException("Value type");
  132. //}
  133. // LAMESPEC: should throw an exception when ms<0, but
  134. // Timeout.Infinite is -1
  135. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  136. if(ms == Timeout.Infinite) {
  137. Enter(obj);
  138. return(true);
  139. }
  140. if(ms < 0 || ms > Int32.MaxValue) {
  141. throw new ArgumentOutOfRangeException("timeout", "timeout out of range");
  142. }
  143. return(Monitor_try_enter(obj, ms));
  144. }
  145. // Waits for a signal on object 'obj' with maximum
  146. // wait time 'ms'. Returns true if the object was
  147. // signalled, false if it timed out
  148. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  149. private extern static bool Monitor_wait(object obj, int ms);
  150. public static bool Wait(object obj) {
  151. if(obj==null) {
  152. throw new ArgumentNullException("obj");
  153. }
  154. if(Monitor_test_synchronised(obj)==false) {
  155. throw new SynchronizationLockException("Object is not synchronized");
  156. }
  157. return(Monitor_wait(obj, Timeout.Infinite));
  158. }
  159. public static bool Wait(object obj, int millisecondsTimeout) {
  160. if(obj==null) {
  161. throw new ArgumentNullException("obj");
  162. }
  163. if(Monitor_test_synchronised(obj)==false) {
  164. throw new SynchronizationLockException("Object is not synchronized");
  165. }
  166. if (millisecondsTimeout < 0 && millisecondsTimeout != Timeout.Infinite)
  167. throw new ArgumentOutOfRangeException ("millisecondsTimeout", "timeout out of range");
  168. return(Monitor_wait(obj, millisecondsTimeout));
  169. }
  170. public static bool Wait(object obj, TimeSpan timeout) {
  171. if(obj==null) {
  172. throw new ArgumentNullException("obj");
  173. }
  174. // LAMESPEC: says to throw ArgumentException too
  175. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  176. if((ms < 0 && ms != Timeout.Infinite) || ms > Int32.MaxValue) {
  177. throw new ArgumentOutOfRangeException("timeout", "timeout out of range");
  178. }
  179. if(Monitor_test_synchronised(obj)==false) {
  180. throw new SynchronizationLockException("Object is not synchronized");
  181. }
  182. return(Monitor_wait(obj, ms));
  183. }
  184. public static bool Wait(object obj, int millisecondsTimeout, bool exitContext) {
  185. try {
  186. if (exitContext) SynchronizationAttribute.ExitContext ();
  187. return Wait (obj, millisecondsTimeout);
  188. }
  189. finally {
  190. if (exitContext) SynchronizationAttribute.EnterContext ();
  191. }
  192. }
  193. public static bool Wait(object obj, TimeSpan timeout, bool exitContext) {
  194. try {
  195. if (exitContext) SynchronizationAttribute.ExitContext ();
  196. return Wait (obj, timeout);
  197. }
  198. finally {
  199. if (exitContext) SynchronizationAttribute.EnterContext ();
  200. }
  201. }
  202. }
  203. }