1 // ManualResetEventSlim.cs
4 // Marek Safar <marek.safar@gmail.com>
6 // Copyright (c) 2008 Jérémie "Garuma" Laval
7 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 namespace System.Threading
33 [System.Diagnostics.DebuggerDisplayAttribute ("Set = {IsSet}")]
34 public class ManualResetEventSlim : IDisposable
36 readonly int spinCount;
38 ManualResetEvent handle;
39 internal AtomicBooleanValue disposed;
43 public ManualResetEventSlim ()
48 public ManualResetEventSlim (bool initialState)
49 : this (initialState, 10)
53 public ManualResetEventSlim (bool initialState, int spinCount)
55 if (spinCount < 0 || spinCount > 2047)
56 throw new ArgumentOutOfRangeException ("spinCount");
58 this.set = initialState;
59 this.spinCount = spinCount;
68 public int SpinCount {
79 Thread.MemoryBarrier ();
81 Interlocked.Increment (ref used);
82 var tmpHandle = handle;
83 if (tmpHandle != null)
85 Interlocked.Decrement (ref used);
92 Thread.MemoryBarrier ();
94 Interlocked.Increment (ref used);
95 var tmpHandle = handle;
96 if (tmpHandle != null)
98 Interlocked.Decrement (ref used);
104 Wait (CancellationToken.None);
107 public bool Wait (int millisecondsTimeout)
109 return Wait (millisecondsTimeout, CancellationToken.None);
112 public bool Wait (TimeSpan timeout)
114 return Wait (CheckTimeout (timeout), CancellationToken.None);
117 public void Wait (CancellationToken cancellationToken)
119 Wait (Timeout.Infinite, cancellationToken);
122 public bool Wait (int millisecondsTimeout, CancellationToken cancellationToken)
124 if (millisecondsTimeout < -1)
125 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
130 SpinWait wait = new SpinWait ();
133 cancellationToken.ThrowIfCancellationRequested ();
135 if (wait.Count < spinCount) {
146 WaitHandle handle = WaitHandle;
148 if (cancellationToken.CanBeCanceled) {
149 if (WaitHandle.WaitAny (new[] { handle, cancellationToken.WaitHandle }, millisecondsTimeout, false) == 0)
152 cancellationToken.ThrowIfCancellationRequested ();
154 if (!handle.WaitOne (millisecondsTimeout, false))
162 public bool Wait (TimeSpan timeout, CancellationToken cancellationToken)
164 return Wait (CheckTimeout (timeout), cancellationToken);
167 public WaitHandle WaitHandle {
175 var mre = new ManualResetEvent (isSet);
176 if (Interlocked.CompareExchange (ref handle, mre, null) == null) {
178 // Ensure the Set has not ran meantime
189 // Release the event when other thread was faster
198 public void Dispose ()
203 protected virtual void Dispose (bool disposing)
205 if (!disposed.TryRelaxedSet ())
208 if (handle != null) {
209 var tmpHandle = Interlocked.Exchange (ref handle, null);
211 // A tiny wait (just a few cycles normally) before releasing
212 SpinWait wait = new SpinWait ();
216 tmpHandle.Dispose ();
220 void ThrowIfDisposed ()
223 throw new ObjectDisposedException ("ManualResetEventSlim");
226 static int CheckTimeout (TimeSpan timeout)
229 return checked ((int)timeout.TotalMilliseconds);
230 } catch (System.OverflowException) {
231 throw new ArgumentOutOfRangeException ("timeout");