Monitor.cs 7.1 KB

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