Monitor.cs 6.9 KB

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