2 // ManuelResetEventSlim.cs
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 using System.Diagnostics;
29 namespace System.Threading
31 public class ManualResetEventSlim : IDisposable
34 const int isNotSet = 0;
35 const int defaultSpinCount = 10;
36 const int deepSleepTime = 20;
39 readonly int spinCount;
41 ManualResetEvent handle;
43 public ManualResetEventSlim () : this (false, defaultSpinCount)
47 public ManualResetEventSlim (bool initState) : this (initState, defaultSpinCount)
51 public ManualResetEventSlim (bool initState, int spinCount)
54 throw new ArgumentOutOfRangeException ("spinCount is less than 0", "spinCount");
56 this.state = initState ? isSet : isNotSet;
57 this.spinCount = spinCount;
58 this.handle = new ManualResetEvent (initState);
63 return state == isSet;
67 public int SpinCount {
75 Interlocked.Exchange (ref state, isNotSet);
81 Interlocked.Exchange (ref state, isSet);
87 Wait (CancellationToken.None);
90 public bool Wait (int millisecondsTimeout)
92 return Wait (millisecondsTimeout, CancellationToken.None);
95 public bool Wait (TimeSpan ts)
97 return Wait ((int)ts.TotalMilliseconds, CancellationToken.None);
100 public void Wait (CancellationToken token)
105 public bool Wait (int millisecondsTimeout, CancellationToken token)
107 if (millisecondsTimeout < -1)
108 throw new ArgumentOutOfRangeException ("millisecondsTimeout",
109 "millisecondsTimeout is a negative number other than -1");
111 Watch s = Watch.StartNew ();
112 SpinWait sw = new SpinWait ();
114 while (state == isNotSet) {
115 token.ThrowIfCancellationRequested ();
117 if (millisecondsTimeout > -1 && s.ElapsedMilliseconds > millisecondsTimeout)
120 if (sw.Count < spinCount)
123 if (handle.WaitOne (Math.Min (Math.Max (millisecondsTimeout - (int)s.ElapsedMilliseconds, 1), deepSleepTime)))
130 public bool Wait (TimeSpan ts, CancellationToken token)
132 return Wait ((int)ts.TotalMilliseconds, token);
135 public WaitHandle WaitHandle {
141 #region IDisposable implementation
142 public void Dispose ()
147 protected virtual void Dispose(bool managedRes)