3 // Copyright (c) 2008 Jérémie "Garuma" Laval
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:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
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
26 using System.Diagnostics;
29 namespace System.Threading
31 public class SemaphoreSlim : IDisposable
37 ManualResetEvent handle;
39 public SemaphoreSlim (int initial) : this (initial, int.MaxValue)
43 public SemaphoreSlim (int initial, int max)
45 if (initial < 0 || initial > max || max < 0)
46 throw new ArgumentOutOfRangeException ("The initial argument is negative, initial is greater than max, or max is not positive.");
49 this.currCount = initial;
50 this.handle = new ManualResetEvent (initial == 0);
58 public void Dispose ()
63 protected virtual void Dispose (bool managedRes)
71 throw new ObjectDisposedException ("The SemaphoreSlim has been disposed.");
74 public int CurrentCount {
85 public int Release (int releaseCount)
89 throw new ArgumentOutOfRangeException ("releaseCount", "releaseCount is less than 1");
91 // As we have to take care of the max limit we resort to CAS
92 int oldValue, newValue;
95 newValue = (currCount + releaseCount);
96 newValue = newValue > max ? max : newValue;
97 } while (Interlocked.CompareExchange (ref currCount, newValue, oldValue) != oldValue);
106 Wait (CancellationToken.None);
109 public bool Wait (TimeSpan ts)
111 return Wait ((int)ts.TotalMilliseconds, CancellationToken.None);
114 public bool Wait (int millisecondsTimeout)
116 return Wait (millisecondsTimeout, CancellationToken.None);
119 public void Wait (CancellationToken token)
124 public bool Wait (TimeSpan ts, CancellationToken token)
127 return Wait ((int)ts.TotalMilliseconds, token);
130 public bool Wait (int millisecondsTimeout, CancellationToken token)
133 if (millisecondsTimeout < -1)
134 throw new ArgumentOutOfRangeException ("millisecondsTimeout",
135 "millisecondsTimeout is a negative number other than -1");
137 Watch sw = Watch.StartNew ();
139 Func<bool> stopCondition =
140 () => token.IsCancellationRequested || (millisecondsTimeout >= 0 && sw.ElapsedMilliseconds > millisecondsTimeout);
147 if (stopCondition ())
157 } while (Interlocked.CompareExchange (ref currCount, result - 1, result) != result);
165 SpinWait wait = new SpinWait ();
167 while (Thread.VolatileRead (ref currCount) <= 0) {
168 if (stopCondition ())
178 public WaitHandle AvailableWaitHandle {