Monitor.cs 5.7 KB

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