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