WaitHandle.cs 7.3 KB

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