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