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
25 #if NET_4_0 || BOOTSTRAP_NET_4_0
29 namespace System.Threading
31 public class ManualResetEventSlim : IDisposable
34 const int isNotSet = 0;
35 const int defaultSpinCount = 100;
38 readonly int spinCount;
40 ManualResetEvent handle;
42 readonly static Watch sw = Watch.StartNew ();
44 public ManualResetEventSlim () : this (false, defaultSpinCount)
48 public ManualResetEventSlim (bool initState) : this (initState, defaultSpinCount)
52 public ManualResetEventSlim (bool initState, int spinCount)
55 throw new ArgumentOutOfRangeException ("spinCount is less than 0", "spinCount");
57 this.state = initState ? isSet : isNotSet;
58 this.spinCount = spinCount;
63 return state == isSet;
67 public int SpinCount {
89 Wait (CancellationToken.None);
92 public bool Wait (int millisecondsTimeout)
94 return Wait (millisecondsTimeout, CancellationToken.None);
97 public bool Wait (TimeSpan ts)
99 return Wait ((int)ts.TotalMilliseconds, CancellationToken.None);
102 public void Wait (CancellationToken token)
107 public bool Wait (int ms, CancellationToken token)
110 throw new ArgumentOutOfRangeException ("millisecondsTimeout",
111 "millisecondsTimeout is a negative number other than -1");
113 long start = ms == -1 ? 0 : sw.ElapsedMilliseconds;
114 SpinWait wait = new SpinWait ();
116 while (state == isNotSet) {
117 token.ThrowIfCancellationRequested ();
119 if (ms > -1 && (sw.ElapsedMilliseconds - start) > ms)
122 if (wait.Count < spinCount) {
125 int waitTime = ms == -1 ? -1 : Math.Max (ms - (int)(sw.ElapsedMilliseconds - start) , 1);
126 WaitHandle handle = WaitHandle;
129 if (WaitHandle.WaitAny (new[] { handle, token.WaitHandle }, waitTime, false) == 0)
137 public bool Wait (TimeSpan ts, CancellationToken token)
139 return Wait ((int)ts.TotalMilliseconds, token);
142 public WaitHandle WaitHandle {
146 return LazyInitializer.EnsureInitialized (ref handle,
147 () => new ManualResetEvent (state == isSet ? true : false));
151 #region IDisposable implementation
152 public void Dispose ()
157 protected virtual void Dispose(bool managedRes)