Add pfx to the mobile profile
[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
43                 readonly static Watch sw = Watch.StartNew ();
44
45                 public ManualResetEventSlim () : this (false, defaultSpinCount)
46                 {
47                 }
48
49                 public ManualResetEventSlim (bool initialState) : this (initialState, defaultSpinCount)
50                 {
51                 }
52
53                 public ManualResetEventSlim (bool initialState, int spinCount)
54                 {
55                         if (spinCount < 0)
56                                 throw new ArgumentOutOfRangeException ("spinCount is less than 0", "spinCount");
57
58                         this.state = initialState ? isSet : isNotSet;
59                         this.spinCount = spinCount;
60                 }
61
62                 public bool IsSet {
63                         get {
64                                 return state == isSet;
65                         }
66                 }
67
68                 public int SpinCount {
69                         get {
70                                 return spinCount;
71                         }
72                 }
73
74                 public void Reset ()
75                 {
76                         state = isNotSet;
77                         if (handle != null)
78                                 handle.Reset ();
79                 }
80
81                 public void Set ()
82                 {
83                         state = isSet;
84                         if (handle != null)
85                                 handle.Set ();
86                 }
87
88                 public void Wait ()
89                 {
90                         Wait (CancellationToken.None);
91                 }
92
93                 public bool Wait (int millisecondsTimeout)
94                 {
95                         return Wait (millisecondsTimeout, CancellationToken.None);
96                 }
97
98                 public bool Wait (TimeSpan timeout)
99                 {
100                         return Wait ((int)timeout.TotalMilliseconds, CancellationToken.None);
101                 }
102
103                 public void Wait (CancellationToken cancellationToken)
104                 {
105                         Wait (-1, cancellationToken);
106                 }
107
108                 public bool Wait (int millisecondsTimeout, CancellationToken cancellationToken)
109                 {
110                         if (millisecondsTimeout < -1)
111                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout",
112                                                                        "millisecondsTimeout is a negative number other than -1");
113
114                         long start = millisecondsTimeout == -1 ? 0 : sw.ElapsedMilliseconds;
115                         SpinWait wait = new SpinWait ();
116
117                         while (state == isNotSet) {
118                                 cancellationToken.ThrowIfCancellationRequested ();
119
120                                 if (millisecondsTimeout > -1 && (sw.ElapsedMilliseconds - start) > millisecondsTimeout)
121                                         return false;
122
123                                 if (wait.Count < spinCount) {
124                                         wait.SpinOnce ();
125                                 } else {
126                                         int waitTime = millisecondsTimeout == -1 ? -1 : Math.Max (millisecondsTimeout - (int)(sw.ElapsedMilliseconds - start) , 1);
127                                         WaitHandle handle = WaitHandle;
128                                         if (state == isSet)
129                                                 return true;
130                                         if (WaitHandle.WaitAny (new[] { handle, cancellationToken.WaitHandle }, waitTime, false) == 0)
131                                                 return true;
132                                 }
133                         }
134
135                         return true;
136                 }
137
138                 public bool Wait (TimeSpan timeout, CancellationToken cancellationToken)
139                 {
140                         return Wait ((int)timeout.TotalMilliseconds, cancellationToken);
141                 }
142
143                 public WaitHandle WaitHandle {
144                         get {
145                                 if (handle != null)
146                                         return handle;
147                                 return LazyInitializer.EnsureInitialized (ref handle,
148                                                                           () => new ManualResetEvent (state == isSet ? true : false));
149                         }
150                 }
151
152                 #region IDisposable implementation
153                 public void Dispose ()
154                 {
155                         Dispose(true);
156                 }
157
158                 protected virtual void Dispose (bool disposing)
159                 {
160
161                 }
162                 #endregion
163
164         }
165 }
166 #endif