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 ();
82 Thread.MemoryBarrier ();
83 var tmpHandle = handle;
84 if (tmpHandle != null)
86 Thread.MemoryBarrier ();
94 Thread.MemoryBarrier ();
97 Thread.MemoryBarrier ();
98 var tmpHandle = handle;
99 if (tmpHandle != null)
101 Thread.MemoryBarrier ();
108 Wait (CancellationToken.None);
111 public bool Wait (int millisecondsTimeout)
113 return Wait (millisecondsTimeout, CancellationToken.None);
116 public bool Wait (TimeSpan timeout)
118 return Wait (CheckTimeout (timeout), CancellationToken.None);
121 public void Wait (CancellationToken cancellationToken)
123 Wait (Timeout.Infinite, cancellationToken);
126 public bool Wait (int millisecondsTimeout, CancellationToken cancellationToken)
128 if (millisecondsTimeout < -1)
129 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
134 SpinWait wait = new SpinWait ();
137 cancellationToken.ThrowIfCancellationRequested ();
139 if (wait.Count < spinCount) {
150 WaitHandle handle = WaitHandle;
152 if (cancellationToken.CanBeCanceled) {
153 if (WaitHandle.WaitAny (new[] { handle, cancellationToken.WaitHandle }, millisecondsTimeout, false) == 0)
156 cancellationToken.ThrowIfCancellationRequested ();
158 if (!handle.WaitOne (millisecondsTimeout, false))
166 public bool Wait (TimeSpan timeout, CancellationToken cancellationToken)
168 return Wait (CheckTimeout (timeout), cancellationToken);
171 public WaitHandle WaitHandle {
179 var mre = new ManualResetEvent (isSet);
180 if (Interlocked.CompareExchange (ref handle, mre, null) == null) {
182 // Ensure the Set has not ran meantime
193 // Release the event when other thread was faster
202 public void Dispose ()
207 protected virtual void Dispose (bool disposing)
209 if (!disposed.TryRelaxedSet ())
212 if (handle != null) {
213 var tmpHandle = Interlocked.Exchange (ref handle, null);
215 // A tiny wait (just a few cycles normally) before releasing
216 SpinWait wait = new SpinWait ();
220 tmpHandle.Dispose ();
224 void ThrowIfDisposed ()
227 throw new ObjectDisposedException ("ManualResetEventSlim");
230 static int CheckTimeout (TimeSpan timeout)
233 return checked ((int)timeout.TotalMilliseconds);
234 } catch (System.OverflowException) {
235 throw new ArgumentOutOfRangeException ("timeout");