2010-06-07 Jonathan Chambers <joncham@gmail.com>
[mono.git] / mcs / class / corlib / System.Threading / ManualResetEventSlim.cs
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:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
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.
23 //
24 //
25
26 using System;
27 using System.Diagnostics;
28
29 namespace System.Threading
30 {
31         public class ManualResetEventSlim : IDisposable
32         {
33                 const int isSet    = 1;
34                 const int isNotSet = 0;
35                 const int defaultSpinCount = 20;
36
37                 int state;
38                 readonly int spinCount;
39
40                 ManualResetEvent handle;
41
42                 public ManualResetEventSlim () : this(false, defaultSpinCount)
43                 {
44                 }
45
46                 public ManualResetEventSlim (bool initState) : this (initState, defaultSpinCount)
47                 {
48                 }
49
50                 public ManualResetEventSlim (bool initState, int spinCount)
51                 {
52                         if (spinCount < 0)
53                                 throw new ArgumentOutOfRangeException ("spinCount is less than 0", "spinCount");
54
55                         this.state = initState ? isSet : isNotSet;
56                         this.spinCount = spinCount;
57                         this.handle = new ManualResetEvent (initState);
58                 }
59
60                 public bool IsSet {
61                         get {
62                                 return state == isSet;
63                         }
64                 }
65
66                 public int SpinCount {
67                         get {
68                                 return spinCount;
69                         }
70                 }
71
72                 public void Reset ()
73                 {
74                         Interlocked.Exchange (ref state, isNotSet);
75                 }
76
77                 public void Set ()
78                 {
79                         Interlocked.Exchange (ref state, isSet);
80                 }
81
82                 public void Wait ()
83                 {
84                         Wait (CancellationToken.None);
85                 }
86
87                 public bool Wait (int millisecondsTimeout)
88                 {
89                         return Wait (millisecondsTimeout, CancellationToken.None);
90                 }
91
92                 public bool Wait (TimeSpan ts)
93                 {
94                         return Wait ((int)ts.TotalMilliseconds, CancellationToken.None);
95                 }
96
97                 public void Wait (CancellationToken token)
98                 {
99                         Wait (-1, token);
100                 }
101
102                 public bool Wait (int millisecondsTimeout, CancellationToken token)
103                 {
104                         if (millisecondsTimeout < -1)
105                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout",
106                                                                        "millisecondsTimeout is a negative number other than -1");
107
108                         Watch s = Watch.StartNew ();
109                         SpinWait sw = new SpinWait ();
110                         int count = 0;
111
112                         while (state == isNotSet) {
113                                 if (token.IsCancellationRequested)
114                                         return false;
115
116                                 if (millisecondsTimeout > -1 && s.ElapsedMilliseconds > millisecondsTimeout)
117                                         return false;
118
119                                 if (count < spinCount) {
120                                     sw.SpinOnce ();
121                                         count++;
122                                 } else {
123                                         Thread.Sleep (0);
124                                 }
125                         }
126
127                         return true;
128                 }
129
130                 public bool Wait (TimeSpan ts, CancellationToken token)
131                 {
132                         return Wait ((int)ts.TotalMilliseconds, token);
133                 }
134
135                 public WaitHandle WaitHandle {
136                         get {
137                                 return handle;
138                         }
139                 }
140
141                 #region IDisposable implementation
142                 public void Dispose ()
143                 {
144                         Dispose(true);
145                 }
146
147                 protected virtual void Dispose(bool managedRes)
148                 {
149
150                 }
151                 #endregion
152
153         }
154 }
155 #endif