/// Sends a signal to a single task waiting on this condition variable. The associated lock MUST be held when calling this method, and it will still be held when this method returns.
/// </summary>
publicvoidNotify()
{
IDisposablefinish=null;
lock(_mutex)
{
if(!_queue.IsEmpty)
finish=_queue.Dequeue();
}
if(finish!=null)
finish.Dispose();
}
/// <summary>
/// Sends a signal to all tasks waiting on this condition variable. The associated lock MUST be held when calling this method, and it will still be held when this method returns.
/// </summary>
publicvoidNotifyAll()
{
IDisposablefinish;
lock(_mutex)
{
finish=_queue.DequeueAll();
}
finish.Dispose();
}
/// <summary>
/// Asynchronously waits for a signal on this condition variable. The associated lock MUST be held when calling this method, and it will still be held when this method returns, even if the method is cancelled.
/// </summary>
/// <param name="cancellationToken">The cancellation signal used to cancel this wait.</param>
/// Synchronously waits for a signal on this condition variable. This method may block the calling thread. The associated lock MUST be held when calling this method, and it will still be held when this method returns, even if the method is cancelled.
/// </summary>
/// <param name="cancellationToken">The cancellation signal used to cancel this wait.</param>
// Begin waiting for either a signal or cancellation.
enqueuedTask=_queue.Enqueue(cancellationToken);
}
// Release the lock while we are waiting.
_asyncLock.ReleaseLock();
// Wait for the signal or cancellation.
enqueuedTask.WaitWithoutException();
// Re-take the lock.
_asyncLock.Lock();
// Propagate the cancellation exception if necessary.
enqueuedTask.WaitAndUnwrapException();
}
/// <summary>
/// Asynchronously waits for a signal on this condition variable. The associated lock MUST be held when calling this method, and it will still be held when this method returns.
/// </summary>
publicTaskWaitAsync()
{
returnWaitAsync(CancellationToken.None);
}
/// <summary>
/// Synchronously waits for a signal on this condition variable. This method may block the calling thread. The associated lock MUST be held when calling this method, and it will still be held when this method returns.