2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / corlib / System.Threading / ManualResetEventSlim.cs
blob5c8d0bd933c37fb99322d60cfd55676925681c04
1 #if NET_4_0
2 // ManuelResetEventSlim.cs
3 //
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 //
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
22 // THE SOFTWARE.
26 using System;
27 using System.Diagnostics;
29 namespace System.Threading
31 public class ManualResetEventSlim : IDisposable
33 const int isSet = 1;
34 const int isNotSet = 0;
35 const int defaultSpinCount = 20;
37 int state;
38 readonly int spinCount;
40 ManualResetEvent handle;
42 public ManualResetEventSlim () : this(false, defaultSpinCount)
46 public ManualResetEventSlim (bool initState) : this (initState, defaultSpinCount)
50 public ManualResetEventSlim (bool initState, int spinCount)
52 if (spinCount < 0)
53 throw new ArgumentOutOfRangeException ("spinCount is less than 0", "spinCount");
55 this.state = initState ? isSet : isNotSet;
56 this.spinCount = spinCount;
57 this.handle = new ManualResetEvent (initState);
60 public bool IsSet {
61 get {
62 return state == isSet;
66 public int SpinCount {
67 get {
68 return spinCount;
72 public void Reset ()
74 Interlocked.Exchange (ref state, isNotSet);
77 public void Set ()
79 Interlocked.Exchange (ref state, isSet);
82 public void Wait ()
84 Wait (CancellationToken.None);
87 public bool Wait (int millisecondsTimeout)
89 return Wait (millisecondsTimeout, CancellationToken.None);
92 public bool Wait (TimeSpan ts)
94 return Wait ((int)ts.TotalMilliseconds, CancellationToken.None);
97 public void Wait (CancellationToken token)
99 Wait (-1, token);
102 public bool Wait (int millisecondsTimeout, CancellationToken token)
104 if (millisecondsTimeout < -1)
105 throw new ArgumentOutOfRangeException ("millisecondsTimeout",
106 "millisecondsTimeout is a negative number other than -1");
108 Watch s = Watch.StartNew ();
109 SpinWait sw = new SpinWait ();
110 int count = 0;
112 while (state == isNotSet) {
113 if (token.IsCancellationRequested)
114 return false;
116 if (millisecondsTimeout > -1 && s.ElapsedMilliseconds > millisecondsTimeout)
117 return false;
119 if (count < spinCount) {
120 sw.SpinOnce ();
121 count++;
122 } else {
123 Thread.Sleep (0);
127 return true;
130 public bool Wait (TimeSpan ts, CancellationToken token)
132 return Wait ((int)ts.TotalMilliseconds, token);
135 public WaitHandle WaitHandle {
136 get {
137 return handle;
141 #region IDisposable implementation
142 public void Dispose ()
144 Dispose(true);
147 protected virtual void Dispose(bool managedRes)
151 #endregion
155 #endif