Fix semantic of CountdownEvent Reset methods. Fix #658244.
[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 #if NET_4_0 || BOOTSTRAP_NET_4_0
26
27 using System;
28
29 namespace System.Threading
30 {       
31         public class CountdownEvent : IDisposable
32         {
33                 int count;
34                 int initial;
35                 ManualResetEventSlim evt = new ManualResetEventSlim (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                         evt.Wait ();
126                 }
127                 
128                 public void Wait (CancellationToken token)
129                 {
130                         evt.Wait (token);
131                 }
132                 
133                 public bool Wait (int timeoutMilli)
134                 {
135                         return evt.Wait (timeoutMilli);
136                 }
137                 
138                 public bool Wait(TimeSpan span)
139                 {
140                         return evt.Wait (span);
141                 }
142                 
143                 public bool Wait (int timeoutMilli, CancellationToken token)
144                 {
145                         return evt.Wait (timeoutMilli, token);
146                 }
147                 
148                 public bool Wait(TimeSpan span, CancellationToken token)
149                 {
150                         return evt.Wait (span, token);
151                 }
152
153                 public void Reset ()
154                 {
155                         Reset (initial);
156                 }
157                 
158                 public void Reset (int value)
159                 {
160                         evt.Reset ();
161                         count = initial = value;
162                 }
163                 
164                 public int CurrentCount {
165                         get {
166                                 return count;
167                         }
168                 }
169                 
170                 public int InitialCount {
171                         get {
172                                 return initial;
173                         }
174                 }
175                         
176                 public bool IsSet {
177                         get {
178                                 return count == 0;
179                         }
180                 }
181                 
182                 public WaitHandle WaitHandle {
183                         get {
184                                 return evt.WaitHandle;
185                         }
186                 }
187
188                 #region IDisposable implementation 
189                 
190                 public void Dispose ()
191                 {
192                         
193                 }
194                 
195                 protected virtual void Dispose (bool managedRes)
196                 {
197                         
198                 }
199                 #endregion      
200         }
201 }
202 #endif