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;
43 readonly static Watch sw = Watch.StartNew ();
45 public ManualResetEventSlim () : this (false, defaultSpinCount)
49 public ManualResetEventSlim (bool initialState) : this (initialState, defaultSpinCount)
53 public ManualResetEventSlim (bool initialState, int spinCount)
56 throw new ArgumentOutOfRangeException ("spinCount is less than 0", "spinCount");
58 this.state = initialState ? isSet : isNotSet;
59 this.spinCount = spinCount;
64 return state == isSet;
68 public int SpinCount {
90 Wait (CancellationToken.None);
93 public bool Wait (int millisecondsTimeout)
95 return Wait (millisecondsTimeout, CancellationToken.None);
98 public bool Wait (TimeSpan timeout)
100 return Wait ((int)timeout.TotalMilliseconds, CancellationToken.None);
103 public void Wait (CancellationToken cancellationToken)
105 Wait (-1, cancellationToken);
108 public bool Wait (int millisecondsTimeout, CancellationToken cancellationToken)
110 if (millisecondsTimeout < -1)
111 throw new ArgumentOutOfRangeException ("millisecondsTimeout",
112 "millisecondsTimeout is a negative number other than -1");
114 long start = millisecondsTimeout == -1 ? 0 : sw.ElapsedMilliseconds;
115 SpinWait wait = new SpinWait ();
117 while (state == isNotSet) {
118 cancellationToken.ThrowIfCancellationRequested ();
120 if (millisecondsTimeout > -1 && (sw.ElapsedMilliseconds - start) > millisecondsTimeout)
123 if (wait.Count < spinCount) {
126 int waitTime = millisecondsTimeout == -1 ? -1 : Math.Max (millisecondsTimeout - (int)(sw.ElapsedMilliseconds - start) , 1);
127 WaitHandle handle = WaitHandle;
130 if (WaitHandle.WaitAny (new[] { handle, cancellationToken.WaitHandle }, waitTime, false) == 0)
138 public bool Wait (TimeSpan timeout, CancellationToken cancellationToken)
140 return Wait ((int)timeout.TotalMilliseconds, cancellationToken);
143 public WaitHandle WaitHandle {
147 return LazyInitializer.EnsureInitialized (ref handle,
148 () => new ManualResetEvent (state == isSet ? true : false));
152 #region IDisposable implementation
153 public void Dispose ()
158 protected virtual void Dispose (bool disposing)