1 // ManuelResetEventSlim.cs
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 namespace System.Threading
31 [System.Diagnostics.DebuggerDisplayAttribute ("Set = {IsSet}")]
32 public class ManualResetEventSlim : IDisposable
35 const int isNotSet = 0;
36 const int defaultSpinCount = 100;
39 readonly int spinCount;
41 ManualResetEvent handle;
42 AtomicBooleanValue disposed;
45 readonly static Watch sw = Watch.StartNew ();
47 public ManualResetEventSlim () : this (false, defaultSpinCount)
51 public ManualResetEventSlim (bool initialState) : this (initialState, defaultSpinCount)
55 public ManualResetEventSlim (bool initialState, int spinCount)
58 throw new ArgumentOutOfRangeException ("spinCount is less than 0", "spinCount");
60 this.state = initialState ? isSet : isNotSet;
61 this.spinCount = spinCount;
66 return state == isSet;
70 public int SpinCount {
81 Thread.MemoryBarrier ();
82 var tmpHandle = handle;
83 if (tmpHandle != null)
85 Thread.MemoryBarrier ();
95 Thread.MemoryBarrier ();
96 var tmpHandle = handle;
97 if (tmpHandle != null)
99 Thread.MemoryBarrier ();
106 Wait (CancellationToken.None);
109 public bool Wait (int millisecondsTimeout)
111 return Wait (millisecondsTimeout, CancellationToken.None);
114 public bool Wait (TimeSpan timeout)
116 return Wait (CheckTimeout (timeout), CancellationToken.None);
119 public void Wait (CancellationToken cancellationToken)
121 Wait (Timeout.Infinite, cancellationToken);
124 public bool Wait (int millisecondsTimeout, CancellationToken cancellationToken)
126 if (millisecondsTimeout < -1)
127 throw new ArgumentOutOfRangeException ("millisecondsTimeout",
128 "millisecondsTimeout is a negative number other than -1");
131 long start = millisecondsTimeout == -1 ? 0 : sw.ElapsedMilliseconds;
132 SpinWait wait = new SpinWait ();
134 while (state == isNotSet) {
135 cancellationToken.ThrowIfCancellationRequested ();
137 if (millisecondsTimeout > -1 && (sw.ElapsedMilliseconds - start) > millisecondsTimeout)
140 if (wait.Count < spinCount) {
143 int waitTime = millisecondsTimeout == -1 ? -1 : Math.Max (millisecondsTimeout - (int)(sw.ElapsedMilliseconds - start) , 1);
145 WaitHandle handle = WaitHandle;
150 if (cancellationToken.CanBeCanceled)
151 if (WaitHandle.WaitAny (new[] { handle, cancellationToken.WaitHandle }, waitTime, false) == 0)
154 if (handle.WaitOne (waitTime, false))
162 public bool Wait (TimeSpan timeout, CancellationToken cancellationToken)
164 return Wait (CheckTimeout (timeout), cancellationToken);
167 public WaitHandle WaitHandle {
169 if (handle != null) {
176 var result = LazyInitializer.EnsureInitialized (ref handle,
177 () => new ManualResetEvent (state == isSet ? true : false));
185 public void Dispose ()
190 protected virtual void Dispose (bool disposing)
192 if (!disposed.TryRelaxedSet ())
194 if (handle != null) {
195 var tmpHandle = Interlocked.Exchange (ref handle, null);
197 // A tiny wait (just a few cycles normally) before releasing
198 SpinWait wait = new SpinWait ();
202 tmpHandle.Dispose ();
206 void ThrowIfDisposed ()
209 throw new ObjectDisposedException ("ManualResetEventSlim");
212 static int CheckTimeout (TimeSpan timeout)
215 return checked ((int)timeout.TotalMilliseconds);
216 } catch (System.OverflowException) {
217 throw new ArgumentOutOfRangeException ("timeout");