4 // Copyright (c) 2008 Jérémie "Garuma" Laval
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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
27 using System.Diagnostics;
29 namespace System.Threading
31 public class SemaphoreSlim: IDisposable, ISupportsCancellation
40 SpinWait wait = new SpinWait();
42 public SemaphoreSlim(int initial): this (initial, int.MaxValue)
46 public SemaphoreSlim(int initial, int max)
48 if (initial < 0 || initial > max || max < 0)
49 throw new ArgumentOutOfRangeException("The initial argument is negative, initial is greater than max, or max is not positive.");
52 this.currCount = initial;
65 protected virtual void Dispose(bool managedRes)
73 throw new OperationCanceledException("The SemaphoreSlim is canceled.");
75 throw new ObjectDisposedException("The SemaphoreSlim has been disposed.");
78 public int CurrentCount {
84 public bool IsCanceled {
100 public int Release(int releaseCount)
103 if (releaseCount < 0)
104 throw new ArgumentOutOfRangeException("releaseCount", " The releaseCount must be positive.");
106 // As we have to take care of the max limit we resort to CAS
107 int oldValue, newValue;
109 oldValue = currCount;
110 newValue = (currCount + releaseCount);
111 newValue = newValue > max ? max : newValue;
112 } while (Interlocked.CompareExchange(ref currCount, newValue, oldValue) != oldValue);
121 int result = Interlocked.Decrement(ref currCount);
125 // We revert back the operation
126 Interlocked.Increment(ref currCount);
127 while (Thread.VolatileRead(ref currCount) <= 0) {
133 public bool Wait(TimeSpan ts)
136 return Wait((int)ts.TotalMilliseconds);
139 public bool Wait(int millisecondsTimeout)
142 if (millisecondsTimeout < -1)
143 throw new ArgumentOutOfRangeException("millisecondsTimeout",
144 "millisecondsTimeout is a negative number other than -1");
145 if (millisecondsTimeout == -1) {
151 int result = Interlocked.Decrement(ref currCount);
155 // We revert back the operation
156 result = Interlocked.Increment(ref currCount);
157 Stopwatch sw = Stopwatch.StartNew();
158 while (Thread.VolatileRead(ref currCount) <= 0) {
159 if (sw.ElapsedMilliseconds > millisecondsTimeout) {
170 public WaitHandle AvailableWaitHandle {