const int spinCount = 10;
const int deepSleepTime = 20;
- readonly int max;
+ readonly int maxCount;
int currCount;
bool isDisposed;
EventWaitHandle handle;
- public SemaphoreSlim (int initial) : this (initial, int.MaxValue)
+ public SemaphoreSlim (int initialCount) : this (initialCount, int.MaxValue)
{
}
- public SemaphoreSlim (int initial, int max)
+ public SemaphoreSlim (int initialCount, int maxCount)
{
- if (initial < 0 || initial > max || max < 0)
- throw new ArgumentOutOfRangeException ("The initial argument is negative, initial is greater than max, or max is not positive.");
+ if (initialCount < 0 || initialCount > maxCount || maxCount < 0)
+ throw new ArgumentOutOfRangeException ("The initialCount argument is negative, initialCount is greater than maxCount, or maxCount is not positive.");
- this.max = max;
- this.currCount = initial;
- this.handle = new ManualResetEvent (initial == 0);
+ this.maxCount = maxCount;
+ this.currCount = initialCount;
+ this.handle = new ManualResetEvent (initialCount == 0);
}
~SemaphoreSlim ()
Dispose(true);
}
- protected virtual void Dispose (bool managedRes)
+ protected virtual void Dispose (bool disposing)
{
isDisposed = true;
}
do {
oldValue = currCount;
newValue = (currCount + releaseCount);
- newValue = newValue > max ? max : newValue;
+ newValue = newValue > maxCount ? maxCount : newValue;
} while (Interlocked.CompareExchange (ref currCount, newValue, oldValue) != oldValue);
handle.Set ();
Wait (CancellationToken.None);
}
- public bool Wait (TimeSpan ts)
+ public bool Wait (TimeSpan timeout)
{
- return Wait ((int)ts.TotalMilliseconds, CancellationToken.None);
+ return Wait ((int)timeout.TotalMilliseconds, CancellationToken.None);
}
public bool Wait (int millisecondsTimeout)
return Wait (millisecondsTimeout, CancellationToken.None);
}
- public void Wait (CancellationToken token)
+ public void Wait (CancellationToken cancellationToken)
{
- Wait (-1, token);
+ Wait (-1, cancellationToken);
}
- public bool Wait (TimeSpan ts, CancellationToken token)
+ public bool Wait (TimeSpan timeout, CancellationToken cancellationToken)
{
CheckState();
- return Wait ((int)ts.TotalMilliseconds, token);
+ return Wait ((int)timeout.TotalMilliseconds, cancellationToken);
}
- public bool Wait (int millisecondsTimeout, CancellationToken token)
+ public bool Wait (int millisecondsTimeout, CancellationToken cancellationToken)
{
CheckState ();
if (millisecondsTimeout < -1)
int result;
do {
- token.ThrowIfCancellationRequested ();
+ cancellationToken.ThrowIfCancellationRequested ();
if (stopCondition ())
return false;
SpinWait wait = new SpinWait ();
while (Thread.VolatileRead (ref currCount) <= 0) {
- token.ThrowIfCancellationRequested ();
+ cancellationToken.ThrowIfCancellationRequested ();
if (stopCondition ())
return false;