WaitHandle.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // System.Threading.WaitHandle.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected]
  7. //
  8. // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Runtime.CompilerServices;
  33. using System.Runtime.Remoting.Contexts;
  34. namespace System.Threading
  35. {
  36. public abstract class WaitHandle : MarshalByRefObject, IDisposable
  37. {
  38. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  39. private static extern bool WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
  40. static void CheckArray (WaitHandle [] handles)
  41. {
  42. if (handles == null)
  43. throw new ArgumentNullException ("waitHandles");
  44. int length = handles.Length;
  45. if (length > 64)
  46. throw new NotSupportedException ("Too many handles");
  47. foreach (WaitHandle w in handles) {
  48. if (w == null)
  49. throw new ArgumentNullException ("waitHandles", "null handle");
  50. if (w.os_handle == InvalidHandle)
  51. throw new ArgumentException ("null element found", "waitHandle");
  52. }
  53. }
  54. public static bool WaitAll(WaitHandle[] waitHandles)
  55. {
  56. CheckArray (waitHandles);
  57. return(WaitAll_internal(waitHandles, Timeout.Infinite, false));
  58. }
  59. public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
  60. {
  61. CheckArray (waitHandles);
  62. try {
  63. if (exitContext) SynchronizationAttribute.ExitContext ();
  64. return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
  65. }
  66. finally {
  67. if (exitContext) SynchronizationAttribute.EnterContext ();
  68. }
  69. }
  70. public static bool WaitAll(WaitHandle[] waitHandles,
  71. TimeSpan timeout,
  72. bool exitContext)
  73. {
  74. CheckArray (waitHandles);
  75. long ms = (long) timeout.TotalMilliseconds;
  76. if (ms < -1 || ms > Int32.MaxValue)
  77. throw new ArgumentOutOfRangeException ("timeout");
  78. try {
  79. if (exitContext) SynchronizationAttribute.ExitContext ();
  80. return (WaitAll_internal (waitHandles, (int) ms, exitContext));
  81. }
  82. finally {
  83. if (exitContext) SynchronizationAttribute.EnterContext ();
  84. }
  85. }
  86. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  87. private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
  88. // LAMESPEC: Doesn't specify how to signal failures
  89. public static int WaitAny(WaitHandle[] waitHandles)
  90. {
  91. CheckArray (waitHandles);
  92. return(WaitAny_internal(waitHandles, Timeout.Infinite, false));
  93. }
  94. public static int WaitAny(WaitHandle[] waitHandles,
  95. int millisecondsTimeout,
  96. bool exitContext)
  97. {
  98. CheckArray (waitHandles);
  99. try {
  100. if (exitContext) SynchronizationAttribute.ExitContext ();
  101. return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
  102. }
  103. finally {
  104. if (exitContext) SynchronizationAttribute.EnterContext ();
  105. }
  106. }
  107. public static int WaitAny(WaitHandle[] waitHandles,
  108. TimeSpan timeout, bool exitContext)
  109. {
  110. CheckArray (waitHandles);
  111. long ms = (long) timeout.TotalMilliseconds;
  112. if (ms < -1 || ms > Int32.MaxValue)
  113. throw new ArgumentOutOfRangeException ("timeout");
  114. try {
  115. if (exitContext) SynchronizationAttribute.ExitContext ();
  116. return (WaitAny_internal(waitHandles, (int) ms, exitContext));
  117. }
  118. finally {
  119. if (exitContext) SynchronizationAttribute.EnterContext ();
  120. }
  121. }
  122. [MonoTODO]
  123. public WaitHandle() {
  124. // FIXME
  125. }
  126. public const int WaitTimeout = 258;
  127. private IntPtr os_handle = InvalidHandle;
  128. public virtual IntPtr Handle {
  129. get {
  130. return(os_handle);
  131. }
  132. set {
  133. os_handle=value;
  134. }
  135. }
  136. public virtual void Close() {
  137. Dispose(true);
  138. GC.SuppressFinalize (this);
  139. }
  140. internal void CheckDisposed ()
  141. {
  142. if (disposed || os_handle == InvalidHandle)
  143. throw new ObjectDisposedException (GetType ().FullName);
  144. }
  145. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  146. private extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);
  147. public virtual bool WaitOne()
  148. {
  149. CheckDisposed ();
  150. return(WaitOne_internal(os_handle, Timeout.Infinite, false));
  151. }
  152. public virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
  153. {
  154. CheckDisposed ();
  155. try {
  156. if (exitContext) SynchronizationAttribute.ExitContext ();
  157. return(WaitOne_internal(os_handle, millisecondsTimeout, exitContext));
  158. }
  159. finally {
  160. if (exitContext) SynchronizationAttribute.EnterContext ();
  161. }
  162. }
  163. public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
  164. {
  165. CheckDisposed ();
  166. long ms = (long) timeout.TotalMilliseconds;
  167. if (ms < -1 || ms > Int32.MaxValue)
  168. throw new ArgumentOutOfRangeException ("timeout");
  169. try {
  170. if (exitContext) SynchronizationAttribute.ExitContext ();
  171. return (WaitOne_internal(os_handle, (int) ms, exitContext));
  172. }
  173. finally {
  174. if (exitContext) SynchronizationAttribute.EnterContext ();
  175. }
  176. }
  177. protected static readonly IntPtr InvalidHandle = IntPtr.Zero;
  178. bool disposed = false;
  179. void IDisposable.Dispose() {
  180. Dispose(true);
  181. // Take yourself off the Finalization queue
  182. GC.SuppressFinalize(this);
  183. }
  184. protected virtual void Dispose(bool explicitDisposing) {
  185. // Check to see if Dispose has already been called.
  186. if (!disposed) {
  187. disposed=true;
  188. if (os_handle == InvalidHandle)
  189. return;
  190. lock (this) {
  191. if (os_handle != InvalidHandle) {
  192. NativeEventCalls.CloseEvent_internal (os_handle);
  193. os_handle = InvalidHandle;
  194. }
  195. }
  196. }
  197. }
  198. ~WaitHandle() {
  199. Dispose(false);
  200. }
  201. }
  202. }