WaitHandle.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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;
  31. using System.Reflection;
  32. using System.Runtime.CompilerServices;
  33. using System.Runtime.Remoting.Contexts;
  34. using System.Security.Permissions;
  35. namespace System.Threading
  36. {
  37. public abstract class WaitHandle : MarshalByRefObject, IDisposable
  38. {
  39. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  40. private static extern bool WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
  41. static void CheckArray (WaitHandle [] handles, bool waitAll)
  42. {
  43. if (handles == null)
  44. throw new ArgumentNullException ("waitHandles");
  45. int length = handles.Length;
  46. if (length > 64)
  47. throw new NotSupportedException ("Too many handles");
  48. if (waitAll && length > 1 &&
  49. (Thread.CurrentThread.ApartmentState == ApartmentState.STA ||
  50. Assembly.GetEntryAssembly ().EntryPoint.GetCustomAttributes (typeof (STAThreadAttribute), false).Length == 1))
  51. throw new NotSupportedException ("WaitAll for multiple handles is not allowed on an STA thread.");
  52. foreach (WaitHandle w in handles) {
  53. if (w == null)
  54. throw new ArgumentNullException ("waitHandles", "null handle");
  55. if (w.os_handle == InvalidHandle)
  56. throw new ArgumentException ("null element found", "waitHandle");
  57. }
  58. }
  59. public static bool WaitAll(WaitHandle[] waitHandles)
  60. {
  61. CheckArray (waitHandles, true);
  62. return(WaitAll_internal(waitHandles, Timeout.Infinite, false));
  63. }
  64. public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
  65. {
  66. CheckArray (waitHandles, true);
  67. try {
  68. if (exitContext) SynchronizationAttribute.ExitContext ();
  69. return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
  70. }
  71. finally {
  72. if (exitContext) SynchronizationAttribute.EnterContext ();
  73. }
  74. }
  75. public static bool WaitAll(WaitHandle[] waitHandles,
  76. TimeSpan timeout,
  77. bool exitContext)
  78. {
  79. CheckArray (waitHandles, true);
  80. long ms = (long) timeout.TotalMilliseconds;
  81. if (ms < -1 || ms > Int32.MaxValue)
  82. throw new ArgumentOutOfRangeException ("timeout");
  83. try {
  84. if (exitContext) SynchronizationAttribute.ExitContext ();
  85. return (WaitAll_internal (waitHandles, (int) ms, exitContext));
  86. }
  87. finally {
  88. if (exitContext) SynchronizationAttribute.EnterContext ();
  89. }
  90. }
  91. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  92. private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
  93. // LAMESPEC: Doesn't specify how to signal failures
  94. public static int WaitAny(WaitHandle[] waitHandles)
  95. {
  96. CheckArray (waitHandles, false);
  97. return(WaitAny_internal(waitHandles, Timeout.Infinite, false));
  98. }
  99. public static int WaitAny(WaitHandle[] waitHandles,
  100. int millisecondsTimeout,
  101. bool exitContext)
  102. {
  103. CheckArray (waitHandles, false);
  104. try {
  105. if (exitContext) SynchronizationAttribute.ExitContext ();
  106. return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
  107. }
  108. finally {
  109. if (exitContext) SynchronizationAttribute.EnterContext ();
  110. }
  111. }
  112. public static int WaitAny(WaitHandle[] waitHandles,
  113. TimeSpan timeout, bool exitContext)
  114. {
  115. CheckArray (waitHandles, false);
  116. long ms = (long) timeout.TotalMilliseconds;
  117. if (ms < -1 || ms > Int32.MaxValue)
  118. throw new ArgumentOutOfRangeException ("timeout");
  119. try {
  120. if (exitContext) SynchronizationAttribute.ExitContext ();
  121. return (WaitAny_internal(waitHandles, (int) ms, exitContext));
  122. }
  123. finally {
  124. if (exitContext) SynchronizationAttribute.EnterContext ();
  125. }
  126. }
  127. [MonoTODO]
  128. public WaitHandle() {
  129. // FIXME
  130. }
  131. public const int WaitTimeout = 258;
  132. private IntPtr os_handle = InvalidHandle;
  133. public virtual IntPtr Handle {
  134. get {
  135. return(os_handle);
  136. }
  137. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  138. [SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
  139. set {
  140. os_handle=value;
  141. }
  142. }
  143. public virtual void Close() {
  144. Dispose(true);
  145. GC.SuppressFinalize (this);
  146. }
  147. internal void CheckDisposed ()
  148. {
  149. if (disposed || os_handle == InvalidHandle)
  150. throw new ObjectDisposedException (GetType ().FullName);
  151. }
  152. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  153. private extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);
  154. public virtual bool WaitOne()
  155. {
  156. CheckDisposed ();
  157. return(WaitOne_internal(os_handle, Timeout.Infinite, false));
  158. }
  159. public virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
  160. {
  161. CheckDisposed ();
  162. try {
  163. if (exitContext) SynchronizationAttribute.ExitContext ();
  164. return(WaitOne_internal(os_handle, millisecondsTimeout, exitContext));
  165. }
  166. finally {
  167. if (exitContext) SynchronizationAttribute.EnterContext ();
  168. }
  169. }
  170. public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
  171. {
  172. CheckDisposed ();
  173. long ms = (long) timeout.TotalMilliseconds;
  174. if (ms < -1 || ms > Int32.MaxValue)
  175. throw new ArgumentOutOfRangeException ("timeout");
  176. try {
  177. if (exitContext) SynchronizationAttribute.ExitContext ();
  178. return (WaitOne_internal(os_handle, (int) ms, exitContext));
  179. }
  180. finally {
  181. if (exitContext) SynchronizationAttribute.EnterContext ();
  182. }
  183. }
  184. protected static readonly IntPtr InvalidHandle = IntPtr.Zero;
  185. bool disposed = false;
  186. void IDisposable.Dispose() {
  187. Dispose(true);
  188. // Take yourself off the Finalization queue
  189. GC.SuppressFinalize(this);
  190. }
  191. protected virtual void Dispose(bool explicitDisposing) {
  192. // Check to see if Dispose has already been called.
  193. if (!disposed) {
  194. disposed=true;
  195. if (os_handle == InvalidHandle)
  196. return;
  197. lock (this) {
  198. if (os_handle != InvalidHandle) {
  199. NativeEventCalls.CloseEvent_internal (os_handle);
  200. os_handle = InvalidHandle;
  201. }
  202. }
  203. }
  204. }
  205. ~WaitHandle() {
  206. Dispose(false);
  207. }
  208. }
  209. }