Monitor.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. using System.Runtime.CompilerServices;
  10. namespace System.Threading
  11. {
  12. public sealed class Monitor
  13. {
  14. private Monitor () {}
  15. // Grabs the mutex on object 'obj', with a maximum
  16. // wait time 'ms' but doesn't block - if it can't get
  17. // the lock it returns false, true if it can
  18. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  19. private extern static bool Monitor_try_enter(object obj, int ms);
  20. public static void Enter(object obj) {
  21. if(obj==null) {
  22. throw new ArgumentNullException("Object is null");
  23. }
  24. //if(obj.GetType().IsValueType==true) {
  25. // throw new ArgumentException("Value type");
  26. //}
  27. Monitor_try_enter(obj, Timeout.Infinite);
  28. }
  29. // Releases the mutex on object 'obj'
  30. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  31. private extern static void Monitor_exit(object obj);
  32. // Checks whether the current thread currently owns
  33. // the lock on object 'obj'
  34. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  35. private extern static bool Monitor_test_owner(object obj);
  36. public static void Exit(object obj) {
  37. if(obj==null) {
  38. throw new ArgumentNullException("Object is null");
  39. }
  40. //if(obj.GetType().IsValueType==true) {
  41. // throw new ArgumentException("Value type");
  42. //}
  43. if(Monitor_test_owner(obj)==false) {
  44. throw new SynchronizationLockException("The current thread does not own the lock");
  45. }
  46. Monitor_exit(obj);
  47. }
  48. // Signals one of potentially many objects waiting on
  49. // object 'obj'
  50. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  51. private extern static void Monitor_pulse(object obj);
  52. // Checks whether object 'obj' is currently synchronised
  53. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  54. private extern static bool Monitor_test_synchronised(object obj);
  55. public static void Pulse(object obj) {
  56. if(obj==null) {
  57. throw new ArgumentNullException("Object is null");
  58. }
  59. if(Monitor_test_synchronised(obj)==false) {
  60. throw new SynchronizationLockException("Object is not synchronised");
  61. }
  62. Monitor_pulse(obj);
  63. }
  64. // Signals all of potentially many objects waiting on
  65. // object 'obj'
  66. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  67. private extern static void Monitor_pulse_all(object obj);
  68. public static void PulseAll(object obj) {
  69. if(obj==null) {
  70. throw new ArgumentNullException("Object is null");
  71. }
  72. if(Monitor_test_synchronised(obj)==false) {
  73. throw new SynchronizationLockException("Object is not synchronised");
  74. }
  75. Monitor_pulse_all(obj);
  76. }
  77. public static bool TryEnter(object obj) {
  78. if(obj==null) {
  79. throw new ArgumentNullException("Object is null");
  80. }
  81. //if(obj.GetType().IsValueType==true) {
  82. // throw new ArgumentException("Value type");
  83. //}
  84. return(Monitor_try_enter(obj, 0));
  85. }
  86. public static bool TryEnter(object obj, int millisecondsTimeout) {
  87. if(obj==null) {
  88. throw new ArgumentNullException("Object is null");
  89. }
  90. //if(obj.GetType().IsValueType==true) {
  91. // throw new ArgumentException("Value type");
  92. //}
  93. // LAMESPEC: should throw an exception when ms<0, but
  94. // Timeout.Infinite is -1
  95. if(millisecondsTimeout == Timeout.Infinite) {
  96. Enter(obj);
  97. return(true);
  98. }
  99. if(millisecondsTimeout<0) {
  100. throw new ArgumentException("millisecondsTimeout negative");
  101. }
  102. return(Monitor_try_enter(obj, millisecondsTimeout));
  103. }
  104. public static bool TryEnter(object obj, TimeSpan timeout) {
  105. if(obj==null) {
  106. throw new ArgumentNullException("Object is null");
  107. }
  108. //if(obj.GetType().IsValueType==true) {
  109. // throw new ArgumentException("Value type");
  110. //}
  111. // LAMESPEC: should throw an exception when ms<0, but
  112. // Timeout.Infinite is -1
  113. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  114. if(ms == Timeout.Infinite) {
  115. Enter(obj);
  116. return(true);
  117. }
  118. if(ms < 0 || ms > Int32.MaxValue) {
  119. throw new ArgumentOutOfRangeException("timeout out of range");
  120. }
  121. return(Monitor_try_enter(obj, ms));
  122. }
  123. // Waits for a signal on object 'obj' with maximum
  124. // wait time 'ms'. Returns true if the object was
  125. // signalled, false if it timed out
  126. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  127. private extern static bool Monitor_wait(object obj, int ms);
  128. public static bool Wait(object obj) {
  129. if(obj==null) {
  130. throw new ArgumentNullException("Object is null");
  131. }
  132. if(Monitor_test_synchronised(obj)==false) {
  133. throw new SynchronizationLockException("Object is not synchronised");
  134. }
  135. return(Monitor_wait(obj, Timeout.Infinite));
  136. }
  137. public static bool Wait(object obj, int millisecondsTimeout) {
  138. if(obj==null) {
  139. throw new ArgumentNullException("Object is null");
  140. }
  141. if(Monitor_test_synchronised(obj)==false) {
  142. throw new SynchronizationLockException("Object is not synchronised");
  143. }
  144. // LAMESPEC: no mention of timeout sanity checking
  145. return(Monitor_wait(obj, millisecondsTimeout));
  146. }
  147. public static bool Wait(object obj, TimeSpan timeout) {
  148. if(obj==null) {
  149. throw new ArgumentNullException("Object is null");
  150. }
  151. // LAMESPEC: says to throw ArgumentException too
  152. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  153. if(ms < 0 || ms > Int32.MaxValue) {
  154. throw new ArgumentOutOfRangeException("timeout out of range");
  155. }
  156. if(Monitor_test_synchronised(obj)==false) {
  157. throw new SynchronizationLockException("Object is not synchronised");
  158. }
  159. return(Monitor_wait(obj, ms));
  160. }
  161. [MonoTODO]
  162. public static bool Wait(object obj, int millisecondsTimeout, bool exitContext) {
  163. if(obj==null) {
  164. throw new ArgumentNullException("Object is null");
  165. }
  166. // FIXME when I understand what a
  167. // "synchronisation domain" is and does
  168. return(false);
  169. }
  170. [MonoTODO]
  171. public static bool Wait(object obj, TimeSpan timeout, bool exitContext) {
  172. if(obj==null) {
  173. throw new ArgumentNullException("Object is null");
  174. }
  175. // LAMESPEC: says to throw ArgumentException too
  176. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  177. if(ms < 0 || ms > Int32.MaxValue) {
  178. throw new ArgumentOutOfRangeException("timeout out of range");
  179. }
  180. // FIXME when I understand what a
  181. // "synchronisation domain" is and does
  182. return(false);
  183. }
  184. }
  185. }