PageRenderTime 76ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/corlib/System.Threading/WaitHandle.cs

https://bitbucket.org/luobailiang/mono
C# | 411 lines | 303 code | 56 blank | 52 comment | 58 complexity | a16341c844301e373dd0696633548d9a MD5 | raw file
  1. //
  2. // System.Threading.WaitHandle.cs
  3. //
  4. // Author:
  5. // Dick Porter (dick@ximian.com)
  6. // Gonzalo Paniagua Javier (gonzalo@ximian.com
  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. using System.Runtime.InteropServices;
  36. using Microsoft.Win32.SafeHandles;
  37. using System.Runtime.ConstrainedExecution;
  38. namespace System.Threading
  39. {
  40. [ComVisible (true)]
  41. public abstract class WaitHandle : MarshalByRefObject, IDisposable
  42. {
  43. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  44. private static extern bool WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
  45. static void CheckArray (WaitHandle [] handles, bool waitAll)
  46. {
  47. if (handles == null)
  48. throw new ArgumentNullException ("waitHandles");
  49. int length = handles.Length;
  50. if (length > 64)
  51. throw new NotSupportedException ("Too many handles");
  52. if (handles.Length == 0) {
  53. // MS throws different exceptions from the different methods.
  54. if (waitAll)
  55. throw new ArgumentNullException ("waitHandles");
  56. else
  57. throw new ArgumentException ();
  58. }
  59. #if false
  60. //
  61. // Although we should thrown an exception if this is an STA thread,
  62. // Mono does not know anything about STA threads, and just makes
  63. // things like Paint.NET not even possible to work.
  64. //
  65. // See bug #78455 for the bug this is supposed to fix.
  66. //
  67. if (waitAll && length > 1 && IsSTAThread)
  68. throw new NotSupportedException ("WaitAll for multiple handles is not allowed on an STA thread.");
  69. #endif
  70. foreach (WaitHandle w in handles) {
  71. if (w == null)
  72. throw new ArgumentNullException ("waitHandles", "null handle");
  73. if (w.safe_wait_handle == null)
  74. throw new ArgumentException ("null element found", "waitHandle");
  75. }
  76. }
  77. #if false
  78. // usage of property is commented - see above
  79. static bool IsSTAThread {
  80. get {
  81. bool isSTA = Thread.CurrentThread.ApartmentState ==
  82. ApartmentState.STA;
  83. // FIXME: remove this check after Thread.ApartmentState
  84. // has been properly implemented.
  85. if (!isSTA) {
  86. Assembly asm = Assembly.GetEntryAssembly ();
  87. if (asm != null)
  88. isSTA = asm.EntryPoint.GetCustomAttributes (typeof (STAThreadAttribute), false).Length > 0;
  89. }
  90. return isSTA;
  91. }
  92. }
  93. #endif
  94. public static bool WaitAll(WaitHandle[] waitHandles)
  95. {
  96. CheckArray (waitHandles, true);
  97. return(WaitAll_internal(waitHandles, Timeout.Infinite, false));
  98. }
  99. public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext)
  100. {
  101. CheckArray (waitHandles, true);
  102. // check negative - except for -1 (which is Timeout.Infinite)
  103. if (millisecondsTimeout < Timeout.Infinite)
  104. throw new ArgumentOutOfRangeException ("millisecondsTimeout");
  105. try {
  106. if (exitContext) SynchronizationAttribute.ExitContext ();
  107. return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
  108. }
  109. finally {
  110. if (exitContext) SynchronizationAttribute.EnterContext ();
  111. }
  112. }
  113. public static bool WaitAll(WaitHandle[] waitHandles,
  114. TimeSpan timeout,
  115. bool exitContext)
  116. {
  117. CheckArray (waitHandles, true);
  118. long ms = (long) timeout.TotalMilliseconds;
  119. if (ms < -1 || ms > Int32.MaxValue)
  120. throw new ArgumentOutOfRangeException ("timeout");
  121. try {
  122. if (exitContext) SynchronizationAttribute.ExitContext ();
  123. return (WaitAll_internal (waitHandles, (int) ms, exitContext));
  124. }
  125. finally {
  126. if (exitContext) SynchronizationAttribute.EnterContext ();
  127. }
  128. }
  129. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  130. private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);
  131. // LAMESPEC: Doesn't specify how to signal failures
  132. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
  133. public static int WaitAny(WaitHandle[] waitHandles)
  134. {
  135. CheckArray (waitHandles, false);
  136. return(WaitAny_internal(waitHandles, Timeout.Infinite, false));
  137. }
  138. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
  139. public static int WaitAny(WaitHandle[] waitHandles,
  140. int millisecondsTimeout,
  141. bool exitContext)
  142. {
  143. CheckArray (waitHandles, false);
  144. // check negative - except for -1 (which is Timeout.Infinite)
  145. if (millisecondsTimeout < Timeout.Infinite)
  146. throw new ArgumentOutOfRangeException ("millisecondsTimeout");
  147. try {
  148. if (exitContext) SynchronizationAttribute.ExitContext ();
  149. return(WaitAny_internal(waitHandles, millisecondsTimeout, exitContext));
  150. }
  151. finally {
  152. if (exitContext) SynchronizationAttribute.EnterContext ();
  153. }
  154. }
  155. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
  156. public static int WaitAny(WaitHandle[] waitHandles, TimeSpan timeout)
  157. {
  158. return WaitAny (waitHandles, timeout, false);
  159. }
  160. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
  161. public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout)
  162. {
  163. return WaitAny (waitHandles, millisecondsTimeout, false);
  164. }
  165. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
  166. public static int WaitAny(WaitHandle[] waitHandles,
  167. TimeSpan timeout, bool exitContext)
  168. {
  169. CheckArray (waitHandles, false);
  170. long ms = (long) timeout.TotalMilliseconds;
  171. if (ms < -1 || ms > Int32.MaxValue)
  172. throw new ArgumentOutOfRangeException ("timeout");
  173. try {
  174. if (exitContext) SynchronizationAttribute.ExitContext ();
  175. return (WaitAny_internal(waitHandles, (int) ms, exitContext));
  176. }
  177. finally {
  178. if (exitContext) SynchronizationAttribute.EnterContext ();
  179. }
  180. }
  181. protected WaitHandle()
  182. {
  183. // FIXME
  184. }
  185. public virtual void Close() {
  186. Dispose(true);
  187. GC.SuppressFinalize (this);
  188. }
  189. #if NET_4_0
  190. public void Dispose ()
  191. #else
  192. void IDisposable.Dispose ()
  193. #endif
  194. {
  195. Close ();
  196. }
  197. public const int WaitTimeout = 258;
  198. //
  199. // In 2.0 we use SafeWaitHandles instead of IntPtrs
  200. //
  201. SafeWaitHandle safe_wait_handle;
  202. [Obsolete ("In the profiles > 2.x, use SafeHandle instead of Handle")]
  203. public virtual IntPtr Handle {
  204. get {
  205. return safe_wait_handle.DangerousGetHandle ();
  206. }
  207. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  208. [SecurityPermission (SecurityAction.InheritanceDemand, UnmanagedCode = true)]
  209. set {
  210. if (value == InvalidHandle)
  211. safe_wait_handle = new SafeWaitHandle (InvalidHandle, false);
  212. else
  213. safe_wait_handle = new SafeWaitHandle (value, true);
  214. }
  215. }
  216. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  217. private extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);
  218. protected virtual void Dispose (bool explicitDisposing)
  219. {
  220. if (!disposed){
  221. disposed = true;
  222. //
  223. // This is only the case if the handle was never properly initialized
  224. // most likely a bug in the derived class
  225. //
  226. if (safe_wait_handle == null)
  227. return;
  228. lock (this){
  229. if (safe_wait_handle != null)
  230. safe_wait_handle.Dispose ();
  231. }
  232. }
  233. }
  234. public SafeWaitHandle SafeWaitHandle {
  235. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
  236. get {
  237. return safe_wait_handle;
  238. }
  239. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
  240. set {
  241. if (value == null)
  242. safe_wait_handle = new SafeWaitHandle (InvalidHandle, false);
  243. else
  244. safe_wait_handle = value;
  245. }
  246. }
  247. public static bool SignalAndWait (WaitHandle toSignal,
  248. WaitHandle toWaitOn)
  249. {
  250. return SignalAndWait (toSignal, toWaitOn, -1, false);
  251. }
  252. public static bool SignalAndWait (WaitHandle toSignal,
  253. WaitHandle toWaitOn,
  254. int millisecondsTimeout,
  255. bool exitContext)
  256. {
  257. if (toSignal == null)
  258. throw new ArgumentNullException ("toSignal");
  259. if (toWaitOn == null)
  260. throw new ArgumentNullException ("toWaitOn");
  261. if (millisecondsTimeout < -1)
  262. throw new ArgumentOutOfRangeException ("millisecondsTimeout");
  263. return SignalAndWait_Internal (toSignal.Handle, toWaitOn.Handle, millisecondsTimeout, exitContext);
  264. }
  265. public static bool SignalAndWait (WaitHandle toSignal,
  266. WaitHandle toWaitOn,
  267. TimeSpan timeout,
  268. bool exitContext)
  269. {
  270. double ms = timeout.TotalMilliseconds;
  271. if (ms > Int32.MaxValue)
  272. throw new ArgumentOutOfRangeException ("timeout");
  273. return SignalAndWait (toSignal, toWaitOn, Convert.ToInt32 (ms), false);
  274. }
  275. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  276. static extern bool SignalAndWait_Internal (IntPtr toSignal, IntPtr toWaitOn, int ms, bool exitContext);
  277. public virtual bool WaitOne()
  278. {
  279. CheckDisposed ();
  280. bool release = false;
  281. try {
  282. safe_wait_handle.DangerousAddRef (ref release);
  283. return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), Timeout.Infinite, false));
  284. } finally {
  285. if (release)
  286. safe_wait_handle.DangerousRelease ();
  287. }
  288. }
  289. public virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
  290. {
  291. CheckDisposed ();
  292. // check negative - except for -1 (which is Timeout.Infinite)
  293. if (millisecondsTimeout < Timeout.Infinite)
  294. throw new ArgumentOutOfRangeException ("millisecondsTimeout");
  295. bool release = false;
  296. try {
  297. if (exitContext)
  298. SynchronizationAttribute.ExitContext ();
  299. safe_wait_handle.DangerousAddRef (ref release);
  300. return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), millisecondsTimeout, exitContext));
  301. } finally {
  302. if (exitContext)
  303. SynchronizationAttribute.EnterContext ();
  304. if (release)
  305. safe_wait_handle.DangerousRelease ();
  306. }
  307. }
  308. public virtual bool WaitOne (int millisecondsTimeout)
  309. {
  310. return WaitOne (millisecondsTimeout, false);
  311. }
  312. public virtual bool WaitOne (TimeSpan timeout)
  313. {
  314. return WaitOne (timeout, false);
  315. }
  316. public virtual bool WaitOne(TimeSpan timeout, bool exitContext)
  317. {
  318. CheckDisposed ();
  319. long ms = (long) timeout.TotalMilliseconds;
  320. if (ms < -1 || ms > Int32.MaxValue)
  321. throw new ArgumentOutOfRangeException ("timeout");
  322. bool release = false;
  323. try {
  324. if (exitContext)
  325. SynchronizationAttribute.ExitContext ();
  326. safe_wait_handle.DangerousAddRef (ref release);
  327. return (WaitOne_internal(safe_wait_handle.DangerousGetHandle (), (int) ms, exitContext));
  328. }
  329. finally {
  330. if (exitContext)
  331. SynchronizationAttribute.EnterContext ();
  332. if (release)
  333. safe_wait_handle.DangerousRelease ();
  334. }
  335. }
  336. internal void CheckDisposed ()
  337. {
  338. if (disposed || safe_wait_handle == null)
  339. throw new ObjectDisposedException (GetType ().FullName);
  340. }
  341. public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout)
  342. {
  343. return WaitAll (waitHandles, millisecondsTimeout, false);
  344. }
  345. public static bool WaitAll(WaitHandle[] waitHandles, TimeSpan timeout)
  346. {
  347. return WaitAll (waitHandles, timeout, false);
  348. }
  349. protected static readonly IntPtr InvalidHandle = (IntPtr) (-1);
  350. bool disposed = false;
  351. ~WaitHandle() {
  352. Dispose(false);
  353. }
  354. }
  355. }