2
0

Monitor.cs 7.2 KB

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