Correctly use ManualResetEventSlim's kernel-based counterpart to deep sleep when...
[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 = 10;
36                 const int deepSleepTime = 20;
37
38                 int state;
39                 readonly int spinCount;
40
41                 ManualResetEvent handle;
42
43                 public ManualResetEventSlim () : this (false, defaultSpinCount)
44                 {
45                 }
46
47                 public ManualResetEventSlim (bool initState) : this (initState, defaultSpinCount)
48                 {
49                 }
50
51                 public ManualResetEventSlim (bool initState, int spinCount)
52                 {
53                         if (spinCount < 0)
54                                 throw new ArgumentOutOfRangeException ("spinCount is less than 0", "spinCount");
55
56                         this.state = initState ? isSet : isNotSet;
57                         this.spinCount = spinCount;
58                         this.handle = new ManualResetEvent (initState);
59                 }
60
61                 public bool IsSet {
62                         get {
63                                 return state == isSet;
64                         }
65                 }
66
67                 public int SpinCount {
68                         get {
69                                 return spinCount;
70                         }
71                 }
72
73                 public void Reset ()
74                 {
75                         Interlocked.Exchange (ref state, isNotSet);
76                         handle.Reset ();
77                 }
78
79                 public void Set ()
80                 {
81                         Interlocked.Exchange (ref state, isSet);
82                         handle.Set ();
83                 }
84
85                 public void Wait ()
86                 {
87                         Wait (CancellationToken.None);
88                 }
89
90                 public bool Wait (int millisecondsTimeout)
91                 {
92                         return Wait (millisecondsTimeout, CancellationToken.None);
93                 }
94
95                 public bool Wait (TimeSpan ts)
96                 {
97                         return Wait ((int)ts.TotalMilliseconds, CancellationToken.None);
98                 }
99
100                 public void Wait (CancellationToken token)
101                 {
102                         Wait (-1, token);
103                 }
104
105                 public bool Wait (int millisecondsTimeout, CancellationToken token)
106                 {
107                         if (millisecondsTimeout < -1)
108                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout",
109                                                                        "millisecondsTimeout is a negative number other than -1");
110
111                         Watch s = Watch.StartNew ();
112                         SpinWait sw = new SpinWait ();
113
114                         while (state == isNotSet) {
115                                 token.ThrowIfCancellationRequested ();
116
117                                 if (millisecondsTimeout > -1 && s.ElapsedMilliseconds > millisecondsTimeout)
118                                         return false;
119
120                                 if (sw.Count < spinCount)
121                                         sw.SpinOnce ();
122                                 else
123                                         if (handle.WaitOne (Math.Min (Math.Max (millisecondsTimeout - (int)s.ElapsedMilliseconds, 1), deepSleepTime)))
124                                                 return true;
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