WaitHandle.cs 7.1 KB

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