Revert stalling changes introduced in SpinWait. Add a guard for ntime overflow.
[mono.git] / mcs / class / corlib / System.Threading / CountdownEvent.cs
1 // CountdownEvent.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 using System;
26
27 #if NET_4_0 || BOOTSTRAP_NET_4_0
28
29 namespace System.Threading
30 {       
31         public class CountdownEvent : IDisposable
32         {
33                 int count;
34                 readonly int initial;
35                 ManualResetEvent evt = new ManualResetEvent (false);
36                 
37                 public CountdownEvent (int count)
38                 {
39                         if (count < 0)
40                                 throw new ArgumentOutOfRangeException ("count is negative");
41                         this.initial = this.count = count;
42                 }
43                 
44                 public bool Signal ()
45                 {
46                         return Signal (1);
47                 }
48                 
49                 public bool Signal (int num)
50                 {
51                         if (num <= 0)
52                                 throw new ArgumentOutOfRangeException ("num");
53                         
54                         Action<int> check = delegate (int value) {
55                                 if (value < 0)
56                                 throw new InvalidOperationException ("the specified count is larger that CurrentCount");
57                         };
58                         
59                         int newValue;
60                         if (!ApplyOperation (-num, check, out newValue))
61                                 throw new InvalidOperationException ("The event is already set");
62                         
63                         if (newValue == 0) {
64                                 evt.Set ();
65                                 return true;
66                         }
67                         
68                         return false;
69                 }
70                 
71                 public void AddCount ()
72                 {
73                         AddCount (1);
74                 }
75                 
76                 public void AddCount (int num)
77                 {
78                         if (num < 0)
79                                 throw new ArgumentOutOfRangeException ("num");
80                         
81                         if (!TryAddCount (num))
82                                 throw new InvalidOperationException ("The event is already set");
83                 }
84                 
85                 public bool TryAddCount ()
86                 {
87                         return TryAddCount (1);
88                 }
89                 
90                 public bool TryAddCount (int num)
91                 {       
92                         if (num < 0)
93                                 throw new ArgumentOutOfRangeException ("num");
94                         
95                         return ApplyOperation (num, null);
96                 }
97                 
98                 bool ApplyOperation (int num, Action<int> doCheck)
99                 {
100                         int temp;
101                         return ApplyOperation (num, doCheck, out temp);
102                 }
103                         
104                 bool ApplyOperation (int num, Action<int> doCheck, out int newValue)
105                 {
106                         int oldCount;
107                         newValue = 0;
108                         
109                         do {
110                                 oldCount = count;
111                                 if (oldCount == 0)
112                                         return false;
113                                 
114                                 newValue = oldCount + num;
115                                 
116                                 if (doCheck != null)
117                                         doCheck (newValue);
118                         } while (Interlocked.CompareExchange (ref count, newValue, oldCount) != oldCount);
119                         
120                         return true;
121                 }
122                 
123                 public void Wait ()
124                 {
125                         SpinWait wait = new SpinWait ();
126                         while (!IsSet) {
127                                 wait.SpinOnce ();
128                         }
129                 }
130                 
131                 public void Wait (CancellationToken token)
132                 {
133                         Wait (() => token.IsCancellationRequested);
134                 }
135                 
136                 public bool Wait (int timeoutMilli)
137                 {
138                         if (timeoutMilli == -1) {
139                                 Wait ();
140                                 return true;
141                         }
142                         
143                         Watch sw = Watch.StartNew ();
144                         long timeout = (long)timeoutMilli;
145                         
146                         bool result = Wait (() => sw.ElapsedMilliseconds > timeout);
147                         sw.Stop ();
148                         
149                         return result;
150                 }
151                 
152                 public bool Wait(TimeSpan span)
153                 {
154                         return Wait ((int)span.TotalMilliseconds);
155                 }
156                 
157                 public bool Wait (int timeoutMilli, CancellationToken token)
158                 {
159                         if (timeoutMilli == -1) {
160                                 Wait ();
161                                 return true;
162                         }
163                         
164                         Watch sw = Watch.StartNew ();
165                         long timeout = (long)timeoutMilli;
166                         
167                         bool result = Wait (() => sw.ElapsedMilliseconds > timeout || token.IsCancellationRequested);
168                         sw.Stop ();
169                         
170                         return result;
171                 }
172                 
173                 public bool Wait(TimeSpan span, CancellationToken token)
174                 {
175                         return Wait ((int)span.TotalMilliseconds, token);
176                 }
177                 
178                 bool Wait (Func<bool> waitPredicate)
179                 {
180                         SpinWait wait = new SpinWait ();
181                         
182                         while (!IsSet) {
183                                 if (waitPredicate ())
184                                         return false;
185                                 wait.SpinOnce ();
186                         }
187                         
188                         return true;
189                 }
190                 
191                 public void Reset ()
192                 {
193                         Reset (initial);
194                 }
195                 
196                 public void Reset (int value)
197                 {
198                         evt.Reset ();
199                         Interlocked.Exchange (ref count, value);
200                 }
201                 
202                 public int CurrentCount {
203                         get {
204                                 return count;
205                         }
206                 }
207                 
208                 public int InitialCount {
209                         get {
210                                 return initial;
211                         }
212                 }
213                         
214                 public bool IsSet {
215                         get {
216                                 return count == 0;
217                         }
218                 }
219                 
220                 public WaitHandle WaitHandle {
221                         get {
222                                 return evt;
223                         }
224                 }
225
226                 #region IDisposable implementation 
227                 
228                 public void Dispose ()
229                 {
230                         
231                 }
232                 
233                 protected virtual void Dispose (bool managedRes)
234                 {
235                         
236                 }
237                 #endregion      
238         }
239 }
240 #endif