Monitor.cs 5.9 KB

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