Make ManualResetEventSlim Dispose/(Set,Reset) combo a bit safer. Add unit test.
[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 || MOBILE
26
27 using System;
28
29 namespace System.Threading
30 {
31         [System.Diagnostics.DebuggerDisplayAttribute ("Set = {IsSet}")]
32         public class ManualResetEventSlim : IDisposable
33         {
34                 const int isSet    = 1;
35                 const int isNotSet = 0;
36                 const int defaultSpinCount = 100;
37
38                 int state;
39                 readonly int spinCount;
40
41                 ManualResetEvent handle;
42                 AtomicBooleanValue disposed;
43                 bool used;
44
45                 readonly static Watch sw = Watch.StartNew ();
46
47                 public ManualResetEventSlim () : this (false, defaultSpinCount)
48                 {
49                 }
50
51                 public ManualResetEventSlim (bool initialState) : this (initialState, defaultSpinCount)
52                 {
53                 }
54
55                 public ManualResetEventSlim (bool initialState, int spinCount)
56                 {
57                         if (spinCount < 0)
58                                 throw new ArgumentOutOfRangeException ("spinCount is less than 0", "spinCount");
59
60                         this.state = initialState ? isSet : isNotSet;
61                         this.spinCount = spinCount;
62                 }
63
64                 public bool IsSet {
65                         get {
66                                 return state == isSet;
67                         }
68                 }
69
70                 public int SpinCount {
71                         get {
72                                 return spinCount;
73                         }
74                 }
75
76                 public void Reset ()
77                 {
78                         state = isNotSet;
79                         if (handle != null) {
80                                 used = true;
81                                 Thread.MemoryBarrier ();
82                                 var tmpHandle = handle;
83                                 if (tmpHandle != null)
84                                         tmpHandle.Reset ();
85                                 Thread.MemoryBarrier ();
86                                 used = false;
87                         }
88                 }
89
90                 public void Set ()
91                 {
92                         state = isSet;
93                         if (handle != null) {
94                                 used = true;
95                                 Thread.MemoryBarrier ();
96                                 var tmpHandle = handle;
97                                 if (tmpHandle != null)
98                                         tmpHandle.Set ();
99                                 Thread.MemoryBarrier ();
100                                 used = false;
101                         }
102                 }
103
104                 public void Wait ()
105                 {
106                         Wait (CancellationToken.None);
107                 }
108
109                 public bool Wait (int millisecondsTimeout)
110                 {
111                         return Wait (millisecondsTimeout, CancellationToken.None);
112                 }
113
114                 public bool Wait (TimeSpan timeout)
115                 {
116                         return Wait (CheckTimeout (timeout), CancellationToken.None);
117                 }
118
119                 public void Wait (CancellationToken cancellationToken)
120                 {
121                         Wait (Timeout.Infinite, cancellationToken);
122                 }
123
124                 public bool Wait (int millisecondsTimeout, CancellationToken cancellationToken)
125                 {
126                         if (millisecondsTimeout < -1)
127                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout",
128                                                                        "millisecondsTimeout is a negative number other than -1");
129                         ThrowIfDisposed ();
130
131                         long start = millisecondsTimeout == -1 ? 0 : sw.ElapsedMilliseconds;
132                         SpinWait wait = new SpinWait ();
133
134                         while (state == isNotSet) {
135                                 cancellationToken.ThrowIfCancellationRequested ();
136
137                                 if (millisecondsTimeout > -1 && (sw.ElapsedMilliseconds - start) > millisecondsTimeout)
138                                         return false;
139
140                                 if (wait.Count < spinCount) {
141                                         wait.SpinOnce ();
142                                 } else {
143                                         int waitTime = millisecondsTimeout == -1 ? -1 : Math.Max (millisecondsTimeout - (int)(sw.ElapsedMilliseconds - start) , 1);
144                                         ThrowIfDisposed ();
145                                         WaitHandle handle = WaitHandle;
146                                         if (state == isSet)
147                                                 return true;
148                                         ThrowIfDisposed ();
149
150                                         if (cancellationToken.CanBeCanceled)
151                                                 if (WaitHandle.WaitAny (new[] { handle, cancellationToken.WaitHandle }, waitTime, false) == 0)
152                                                         return true;
153                                         else
154                                                 if (handle.WaitOne (waitTime, false))
155                                                         return true;
156                                 }
157                         }
158
159                         return true;
160                 }
161
162                 public bool Wait (TimeSpan timeout, CancellationToken cancellationToken)
163                 {
164                         return Wait (CheckTimeout (timeout), cancellationToken);
165                 }
166
167                 public WaitHandle WaitHandle {
168                         get {
169                                 if (handle != null) {
170                                         if (state == isSet)
171                                                 handle.Set ();
172
173                                         return handle;
174                                 }
175
176                                 var result = LazyInitializer.EnsureInitialized (ref handle,
177                                                                                 () => new ManualResetEvent (state == isSet ? true : false));
178                                 if (state == isSet)
179                                         result.Set ();
180
181                                 return result;
182                         }
183                 }
184
185                 public void Dispose ()
186                 {
187                         Dispose (true);
188                 }
189
190                 protected virtual void Dispose (bool disposing)
191                 {
192                         if (!disposed.TryRelaxedSet ())
193                                 return;
194                         if (handle != null) {
195                                 var tmpHandle = Interlocked.Exchange (ref handle, null);
196                                 if (used) {
197                                         // A tiny wait (just a few cycles normally) before releasing
198                                         SpinWait wait = new SpinWait ();
199                                         while (used)
200                                                 wait.SpinOnce ();
201                                 }
202                                 tmpHandle.Dispose ();
203                         }
204                 }
205
206                 void ThrowIfDisposed ()
207                 {
208                         if (disposed.Value)
209                                 throw new ObjectDisposedException ("ManualResetEventSlim");
210                 }
211
212                 static int CheckTimeout (TimeSpan timeout)
213                 {
214                         try {
215                                 return checked ((int)timeout.TotalMilliseconds);
216                         } catch (System.OverflowException) {
217                                 throw new ArgumentOutOfRangeException ("timeout");
218                         }
219                 }
220         }
221 }
222 #endif