Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / corlib / System.Threading / ManualResetEventSlim.cs
1 // ManuelResetEventSlim.cs
2 //
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
4 //
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:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
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
21 // THE SOFTWARE.
22 //
23 //
24
25 #if NET_4_0 || BOOTSTRAP_NET_4_0
26
27 using System;
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 = 100;
36
37                 int state;
38                 readonly int spinCount;
39
40                 ManualResetEvent handle;
41
42                 readonly static Watch sw = Watch.StartNew ();
43
44                 public ManualResetEventSlim () : this (false, defaultSpinCount)
45                 {
46                 }
47
48                 public ManualResetEventSlim (bool initState) : this (initState, defaultSpinCount)
49                 {
50                 }
51
52                 public ManualResetEventSlim (bool initState, int spinCount)
53                 {
54                         if (spinCount < 0)
55                                 throw new ArgumentOutOfRangeException ("spinCount is less than 0", "spinCount");
56
57                         this.state = initState ? isSet : isNotSet;
58                         this.spinCount = spinCount;
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                         state = isNotSet;
76                         if (handle != null)
77                                 handle.Reset ();
78                 }
79
80                 public void Set ()
81                 {
82                         state = isSet;
83                         if (handle != null)
84                                 handle.Set ();
85                 }
86
87                 public void Wait ()
88                 {
89                         Wait (CancellationToken.None);
90                 }
91
92                 public bool Wait (int millisecondsTimeout)
93                 {
94                         return Wait (millisecondsTimeout, CancellationToken.None);
95                 }
96
97                 public bool Wait (TimeSpan ts)
98                 {
99                         return Wait ((int)ts.TotalMilliseconds, CancellationToken.None);
100                 }
101
102                 public void Wait (CancellationToken token)
103                 {
104                         Wait (-1, token);
105                 }
106
107                 public bool Wait (int ms, CancellationToken token)
108                 {
109                         if (ms < -1)
110                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout",
111                                                                        "millisecondsTimeout is a negative number other than -1");
112
113                         long start = ms == -1 ? 0 : sw.ElapsedMilliseconds;
114                         SpinWait wait = new SpinWait ();
115
116                         while (state == isNotSet) {
117                                 token.ThrowIfCancellationRequested ();
118
119                                 if (ms > -1 && (sw.ElapsedMilliseconds - start) > ms)
120                                         return false;
121
122                                 if (wait.Count < spinCount) {
123                                         wait.SpinOnce ();
124                                 } else {
125                                         int waitTime = ms == -1 ? -1 : Math.Max (ms - (int)(sw.ElapsedMilliseconds - start) , 1);
126                                         WaitHandle handle = WaitHandle;
127                                         if (state == isSet)
128                                                 return true;
129                                         if (WaitHandle.WaitAny (new[] { handle, token.WaitHandle }, waitTime, false) == 0)
130                                                 return true;
131                                 }
132                         }
133
134                         return true;
135                 }
136
137                 public bool Wait (TimeSpan ts, CancellationToken token)
138                 {
139                         return Wait ((int)ts.TotalMilliseconds, token);
140                 }
141
142                 public WaitHandle WaitHandle {
143                         get {
144                                 if (handle != null)
145                                         return handle;
146                                 return LazyInitializer.EnsureInitialized (ref handle,
147                                                                           () => new ManualResetEvent (state == isSet ? true : false));
148                         }
149                 }
150
151                 #region IDisposable implementation
152                 public void Dispose ()
153                 {
154                         Dispose(true);
155                 }
156
157                 protected virtual void Dispose(bool managedRes)
158                 {
159
160                 }
161                 #endregion
162
163         }
164 }
165 #endif