WaitHandle.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Diagnostics.CodeAnalysis;
  6. using Microsoft.Win32.SafeHandles;
  7. namespace System.Threading
  8. {
  9. public abstract partial class WaitHandle : MarshalByRefObject, IDisposable
  10. {
  11. internal const int MaxWaitHandles = 64;
  12. protected static readonly IntPtr InvalidHandle = new IntPtr(-1);
  13. // IMPORTANT:
  14. // - Do not add or rearrange fields as the EE depends on this layout.
  15. private SafeWaitHandle? _waitHandle;
  16. [ThreadStatic]
  17. private static SafeWaitHandle?[]? t_safeWaitHandlesForRent;
  18. private protected enum OpenExistingResult
  19. {
  20. Success,
  21. NameNotFound,
  22. PathNotFound,
  23. NameInvalid
  24. }
  25. // The wait result values below match Win32 wait result codes (WAIT_OBJECT_0,
  26. // WAIT_ABANDONED, WAIT_TIMEOUT).
  27. // Successful wait on first object. When waiting for multiple objects the
  28. // return value is (WaitSuccess + waitIndex).
  29. internal const int WaitSuccess = 0;
  30. // The specified object is a mutex object that was not released by the
  31. // thread that owned the mutex object before the owning thread terminated.
  32. // When waiting for multiple objects the return value is (WaitAbandoned +
  33. // waitIndex).
  34. internal const int WaitAbandoned = 0x80;
  35. public const int WaitTimeout = 0x102;
  36. protected WaitHandle()
  37. {
  38. }
  39. [Obsolete("Use the SafeWaitHandle property instead.")]
  40. public virtual IntPtr Handle
  41. {
  42. get => _waitHandle == null ? InvalidHandle : _waitHandle.DangerousGetHandle();
  43. set
  44. {
  45. if (value == InvalidHandle)
  46. {
  47. // This line leaks a handle. However, it's currently
  48. // not perfectly clear what the right behavior is here
  49. // anyways. This preserves Everett behavior. We should
  50. // ideally do these things:
  51. // *) Expose a settable SafeHandle property on WaitHandle.
  52. // *) Expose a settable OwnsHandle property on SafeHandle.
  53. if (_waitHandle != null)
  54. {
  55. _waitHandle.SetHandleAsInvalid();
  56. _waitHandle = null;
  57. }
  58. }
  59. else
  60. {
  61. _waitHandle = new SafeWaitHandle(value, true);
  62. }
  63. }
  64. }
  65. [AllowNull]
  66. public SafeWaitHandle SafeWaitHandle
  67. {
  68. get => _waitHandle ??= new SafeWaitHandle(InvalidHandle, false);
  69. set => _waitHandle = value;
  70. }
  71. internal static int ToTimeoutMilliseconds(TimeSpan timeout)
  72. {
  73. long timeoutMilliseconds = (long)timeout.TotalMilliseconds;
  74. if (timeoutMilliseconds < -1)
  75. {
  76. throw new ArgumentOutOfRangeException(nameof(timeout), SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
  77. }
  78. if (timeoutMilliseconds > int.MaxValue)
  79. {
  80. throw new ArgumentOutOfRangeException(nameof(timeout), SR.ArgumentOutOfRange_LessEqualToIntegerMaxVal);
  81. }
  82. return (int)timeoutMilliseconds;
  83. }
  84. public virtual void Close() => Dispose();
  85. protected virtual void Dispose(bool explicitDisposing)
  86. {
  87. _waitHandle?.Close();
  88. }
  89. public void Dispose()
  90. {
  91. Dispose(true);
  92. GC.SuppressFinalize(this);
  93. }
  94. public virtual bool WaitOne(int millisecondsTimeout)
  95. {
  96. if (millisecondsTimeout < -1)
  97. {
  98. throw new ArgumentOutOfRangeException(nameof(millisecondsTimeout), SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
  99. }
  100. return WaitOneNoCheck(millisecondsTimeout);
  101. }
  102. private bool WaitOneNoCheck(int millisecondsTimeout)
  103. {
  104. Debug.Assert(millisecondsTimeout >= -1);
  105. // The field value is modifiable via the public <see cref="WaitHandle.SafeWaitHandle"/> property, save it locally
  106. // to ensure that one instance is used in all places in this method
  107. SafeWaitHandle waitHandle = _waitHandle ?? throw new ObjectDisposedException(null, SR.ObjectDisposed_Generic);
  108. bool success = false;
  109. try
  110. {
  111. int waitResult;
  112. waitHandle.DangerousAddRef(ref success);
  113. SynchronizationContext? context = SynchronizationContext.Current;
  114. if (context != null && context.IsWaitNotificationRequired())
  115. {
  116. waitResult = context.Wait(new[] { waitHandle.DangerousGetHandle() }, false, millisecondsTimeout);
  117. }
  118. else
  119. {
  120. waitResult = WaitOneCore(waitHandle.DangerousGetHandle(), millisecondsTimeout);
  121. }
  122. if (waitResult == WaitAbandoned)
  123. {
  124. throw new AbandonedMutexException();
  125. }
  126. return waitResult != WaitTimeout;
  127. }
  128. finally
  129. {
  130. if (success)
  131. waitHandle.DangerousRelease();
  132. }
  133. }
  134. // Returns an array for storing SafeWaitHandles in WaitMultiple calls. The array
  135. // is reused for subsequent calls to reduce GC pressure.
  136. private static SafeWaitHandle?[] RentSafeWaitHandleArray(int capacity)
  137. {
  138. SafeWaitHandle?[]? safeWaitHandles = t_safeWaitHandlesForRent;
  139. t_safeWaitHandlesForRent = null;
  140. // t_safeWaitHandlesForRent can be null when it was not initialized yet or
  141. // if a re-entrant wait is performed and the array is already rented. In
  142. // that case we just allocate a new one and reuse it as necessary.
  143. int currentLength = (safeWaitHandles != null) ? safeWaitHandles.Length : 0;
  144. if (currentLength < capacity)
  145. {
  146. safeWaitHandles = new SafeWaitHandle[Math.Max(capacity,
  147. Math.Min(MaxWaitHandles, 2 * currentLength))];
  148. }
  149. return safeWaitHandles;
  150. }
  151. private static void ReturnSafeWaitHandleArray(SafeWaitHandle?[]? safeWaitHandles)
  152. => t_safeWaitHandlesForRent = safeWaitHandles;
  153. /// <summary>
  154. /// Obtains all of the corresponding safe wait handles and adds a ref to each. Since the <see cref="SafeWaitHandle"/>
  155. /// property is publically modifiable, this makes sure that we add and release refs one the same set of safe wait
  156. /// handles to keep them alive during a multi-wait operation.
  157. /// </summary>
  158. private static void ObtainSafeWaitHandles(
  159. ReadOnlySpan<WaitHandle> waitHandles,
  160. Span<SafeWaitHandle?> safeWaitHandles,
  161. Span<IntPtr> unsafeWaitHandles)
  162. {
  163. Debug.Assert(waitHandles.Length > 0);
  164. Debug.Assert(waitHandles.Length <= MaxWaitHandles);
  165. bool lastSuccess = true;
  166. SafeWaitHandle? lastSafeWaitHandle = null;
  167. try
  168. {
  169. for (int i = 0; i < waitHandles.Length; ++i)
  170. {
  171. WaitHandle waitHandle = waitHandles[i];
  172. if (waitHandle == null)
  173. {
  174. throw new ArgumentNullException("waitHandles[" + i + ']', SR.ArgumentNull_ArrayElement);
  175. }
  176. SafeWaitHandle safeWaitHandle = waitHandle._waitHandle ??
  177. // Throw ObjectDisposedException for backward compatibility even though it is not representative of the issue
  178. throw new ObjectDisposedException(null, SR.ObjectDisposed_Generic);
  179. lastSafeWaitHandle = safeWaitHandle;
  180. lastSuccess = false;
  181. safeWaitHandle.DangerousAddRef(ref lastSuccess);
  182. safeWaitHandles[i] = safeWaitHandle;
  183. unsafeWaitHandles[i] = safeWaitHandle.DangerousGetHandle();
  184. }
  185. }
  186. catch
  187. {
  188. for (int i = 0; i < waitHandles.Length; ++i)
  189. {
  190. SafeWaitHandle? safeWaitHandle = safeWaitHandles[i];
  191. if (safeWaitHandle == null)
  192. {
  193. break;
  194. }
  195. safeWaitHandle.DangerousRelease();
  196. safeWaitHandles[i] = null;
  197. if (safeWaitHandle == lastSafeWaitHandle)
  198. {
  199. lastSafeWaitHandle = null;
  200. lastSuccess = true;
  201. }
  202. }
  203. if (!lastSuccess)
  204. {
  205. Debug.Assert(lastSafeWaitHandle != null);
  206. lastSafeWaitHandle.DangerousRelease();
  207. }
  208. throw;
  209. }
  210. }
  211. private static int WaitMultiple(WaitHandle[] waitHandles, bool waitAll, int millisecondsTimeout)
  212. {
  213. if (waitHandles == null)
  214. {
  215. throw new ArgumentNullException(nameof(waitHandles), SR.ArgumentNull_Waithandles);
  216. }
  217. return WaitMultiple(new ReadOnlySpan<WaitHandle>(waitHandles), waitAll, millisecondsTimeout);
  218. }
  219. private static int WaitMultiple(ReadOnlySpan<WaitHandle> waitHandles, bool waitAll, int millisecondsTimeout)
  220. {
  221. if (waitHandles.Length == 0)
  222. {
  223. //
  224. // Some history: in CLR 1.0 and 1.1, we threw ArgumentException in this case, which was correct.
  225. // Somehow, in 2.0, this became ArgumentNullException. This was not fixed until Silverlight 2,
  226. // which went back to ArgumentException.
  227. //
  228. // Now we're in a bit of a bind. Backward-compatibility requires us to keep throwing ArgumentException
  229. // in CoreCLR, and ArgumentNullException in the desktop CLR. This is ugly, but so is breaking
  230. // user code.
  231. //
  232. throw new ArgumentException(SR.Argument_EmptyWaithandleArray, nameof(waitHandles));
  233. }
  234. if (waitHandles.Length > MaxWaitHandles)
  235. {
  236. throw new NotSupportedException(SR.NotSupported_MaxWaitHandles);
  237. }
  238. if (millisecondsTimeout < -1)
  239. {
  240. throw new ArgumentOutOfRangeException(nameof(millisecondsTimeout), SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
  241. }
  242. SynchronizationContext? context = SynchronizationContext.Current;
  243. bool useWaitContext = context != null && context.IsWaitNotificationRequired();
  244. SafeWaitHandle?[]? safeWaitHandles = RentSafeWaitHandleArray(waitHandles.Length);
  245. try
  246. {
  247. int waitResult;
  248. if (useWaitContext)
  249. {
  250. IntPtr[] unsafeWaitHandles = new IntPtr[waitHandles.Length];
  251. ObtainSafeWaitHandles(waitHandles, safeWaitHandles, unsafeWaitHandles);
  252. waitResult = context!.Wait(unsafeWaitHandles, waitAll, millisecondsTimeout);
  253. }
  254. else
  255. {
  256. Span<IntPtr> unsafeWaitHandles = stackalloc IntPtr[waitHandles.Length];
  257. ObtainSafeWaitHandles(waitHandles, safeWaitHandles, unsafeWaitHandles);
  258. waitResult = WaitMultipleIgnoringSyncContext(unsafeWaitHandles, waitAll, millisecondsTimeout);
  259. }
  260. if (waitResult >= WaitAbandoned && waitResult < WaitAbandoned + waitHandles.Length)
  261. {
  262. if (waitAll)
  263. {
  264. // In the case of WaitAll the OS will only provide the information that mutex was abandoned.
  265. // It won't tell us which one. So we can't set the Index or provide access to the Mutex
  266. throw new AbandonedMutexException();
  267. }
  268. waitResult -= WaitAbandoned;
  269. throw new AbandonedMutexException(waitResult, waitHandles[waitResult]);
  270. }
  271. return waitResult;
  272. }
  273. finally
  274. {
  275. for (int i = 0; i < waitHandles.Length; ++i)
  276. {
  277. if (safeWaitHandles[i] != null)
  278. {
  279. safeWaitHandles[i]!.DangerousRelease(); // TODO-NULLABLE: Indexer nullability tracked (https://github.com/dotnet/roslyn/issues/34644)
  280. safeWaitHandles[i] = null;
  281. }
  282. }
  283. ReturnSafeWaitHandleArray(safeWaitHandles);
  284. }
  285. }
  286. private static bool SignalAndWait(WaitHandle toSignal, WaitHandle toWaitOn, int millisecondsTimeout)
  287. {
  288. if (toSignal == null)
  289. {
  290. throw new ArgumentNullException(nameof(toSignal));
  291. }
  292. if (toWaitOn == null)
  293. {
  294. throw new ArgumentNullException(nameof(toWaitOn));
  295. }
  296. if (millisecondsTimeout < -1)
  297. {
  298. throw new ArgumentOutOfRangeException(nameof(millisecondsTimeout), SR.ArgumentOutOfRange_NeedNonNegOrNegative1);
  299. }
  300. // The field value is modifiable via the public <see cref="WaitHandle.SafeWaitHandle"/> property, save it locally
  301. // to ensure that one instance is used in all places in this method
  302. SafeWaitHandle? safeWaitHandleToSignal = toSignal._waitHandle;
  303. SafeWaitHandle? safeWaitHandleToWaitOn = toWaitOn._waitHandle;
  304. if (safeWaitHandleToSignal == null || safeWaitHandleToWaitOn == null)
  305. {
  306. // Throw ObjectDisposedException for backward compatibility even though it is not be representative of the issue
  307. throw new ObjectDisposedException(null, SR.ObjectDisposed_Generic);
  308. }
  309. bool successSignal = false, successWait = false;
  310. try
  311. {
  312. safeWaitHandleToSignal.DangerousAddRef(ref successSignal);
  313. safeWaitHandleToWaitOn.DangerousAddRef(ref successWait);
  314. int ret = SignalAndWaitCore(
  315. safeWaitHandleToSignal.DangerousGetHandle(),
  316. safeWaitHandleToWaitOn.DangerousGetHandle(),
  317. millisecondsTimeout);
  318. if (ret == WaitAbandoned)
  319. {
  320. throw new AbandonedMutexException();
  321. }
  322. return ret != WaitTimeout;
  323. }
  324. finally
  325. {
  326. if (successWait)
  327. {
  328. safeWaitHandleToWaitOn.DangerousRelease();
  329. }
  330. if (successSignal)
  331. {
  332. safeWaitHandleToSignal.DangerousRelease();
  333. }
  334. }
  335. }
  336. public virtual bool WaitOne(TimeSpan timeout) => WaitOneNoCheck(ToTimeoutMilliseconds(timeout));
  337. public virtual bool WaitOne() => WaitOneNoCheck(-1);
  338. public virtual bool WaitOne(int millisecondsTimeout, bool exitContext) => WaitOne(millisecondsTimeout);
  339. public virtual bool WaitOne(TimeSpan timeout, bool exitContext) => WaitOneNoCheck(ToTimeoutMilliseconds(timeout));
  340. public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout) =>
  341. WaitMultiple(waitHandles, true, millisecondsTimeout) != WaitTimeout;
  342. public static bool WaitAll(WaitHandle[] waitHandles, TimeSpan timeout) =>
  343. WaitMultiple(waitHandles, true, ToTimeoutMilliseconds(timeout)) != WaitTimeout;
  344. public static bool WaitAll(WaitHandle[] waitHandles) =>
  345. WaitMultiple(waitHandles, true, -1) != WaitTimeout;
  346. public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext) =>
  347. WaitMultiple(waitHandles, true, millisecondsTimeout) != WaitTimeout;
  348. public static bool WaitAll(WaitHandle[] waitHandles, TimeSpan timeout, bool exitContext) =>
  349. WaitMultiple(waitHandles, true, ToTimeoutMilliseconds(timeout)) != WaitTimeout;
  350. public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout) =>
  351. WaitMultiple(waitHandles, false, millisecondsTimeout);
  352. public static int WaitAny(WaitHandle[] waitHandles, TimeSpan timeout) =>
  353. WaitMultiple(waitHandles, false, ToTimeoutMilliseconds(timeout));
  354. public static int WaitAny(WaitHandle[] waitHandles) =>
  355. WaitMultiple(waitHandles, false, -1);
  356. public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext) =>
  357. WaitMultiple(waitHandles, false, millisecondsTimeout);
  358. public static int WaitAny(WaitHandle[] waitHandles, TimeSpan timeout, bool exitContext) =>
  359. WaitMultiple(waitHandles, false, ToTimeoutMilliseconds(timeout));
  360. public static bool SignalAndWait(WaitHandle toSignal, WaitHandle toWaitOn) =>
  361. SignalAndWait(toSignal, toWaitOn, -1);
  362. public static bool SignalAndWait(WaitHandle toSignal, WaitHandle toWaitOn, TimeSpan timeout, bool exitContext) =>
  363. SignalAndWait(toSignal, toWaitOn, ToTimeoutMilliseconds(timeout));
  364. public static bool SignalAndWait(WaitHandle toSignal, WaitHandle toWaitOn, int millisecondsTimeout, bool exitContext) =>
  365. SignalAndWait(toSignal, toWaitOn, millisecondsTimeout);
  366. }
  367. }