/// Applies a continuation to the task that will call <see cref="ReleaseWaiters"/> if the task is canceled. This method may not be called while holding the sync lock.
/// </summary>
/// <param name="task">The task to observe for cancellation.</param>
/// Asynchronously acquires the lock as a reader. Returns a disposable that releases the lock when disposed.
/// </summary>
/// <param name="cancellationToken">The cancellation token used to cancel the lock. If this is already set, then this method will attempt to take the lock immediately (succeeding if the lock is currently available).</param>
/// <returns>A disposable that releases the lock when disposed.</returns>
// Wait for the lock to become available or cancellation.
ret=_readerQueue.Enqueue(cancellationToken);
}
}
returnret;
}
/// <summary>
/// Synchronously acquires the lock as a reader. Returns a disposable that releases the lock when disposed. This method may block the calling thread.
/// </summary>
/// <param name="cancellationToken">The cancellation token used to cancel the lock. If this is already set, then this method will attempt to take the lock immediately (succeeding if the lock is currently available).</param>
/// <returns>A disposable that releases the lock when disposed.</returns>
// Wait for the lock to become available or cancellation.
ret=_readerQueue.Enqueue(cancellationToken);
}
returnret.WaitAndUnwrapException();
}
/// <summary>
/// Asynchronously acquires the lock as a reader. Returns a disposable that releases the lock when disposed.
/// </summary>
/// <returns>A disposable that releases the lock when disposed.</returns>
publicTask<IDisposable>ReaderLockAsync()
{
returnReaderLockAsync(CancellationToken.None);
}
/// <summary>
/// Synchronously acquires the lock as a reader. Returns a disposable that releases the lock when disposed. This method may block the calling thread.
/// </summary>
/// <returns>A disposable that releases the lock when disposed.</returns>
publicIDisposableReaderLock()
{
returnReaderLock(CancellationToken.None);
}
/// <summary>
/// Asynchronously acquires the lock as a writer. Returns a disposable that releases the lock when disposed.
/// </summary>
/// <param name="cancellationToken">The cancellation token used to cancel the lock. If this is already set, then this method will attempt to take the lock immediately (succeeding if the lock is currently available).</param>
/// <returns>A disposable that releases the lock when disposed.</returns>
// Wait for the lock to become available or cancellation.
ret=_writerQueue.Enqueue(cancellationToken);
}
}
ReleaseWaitersWhenCanceled(ret);
returnret;
}
/// <summary>
/// Synchronously acquires the lock as a writer. Returns a disposable that releases the lock when disposed. This method may block the calling thread.
/// </summary>
/// <param name="cancellationToken">The cancellation token used to cancel the lock. If this is already set, then this method will attempt to take the lock immediately (succeeding if the lock is currently available).</param>
/// <returns>A disposable that releases the lock when disposed.</returns>
// Wait for the lock to become available or cancellation.
ret=_writerQueue.Enqueue(cancellationToken);
}
ReleaseWaitersWhenCanceled(ret);
returnret.WaitAndUnwrapException();
}
/// <summary>
/// Asynchronously acquires the lock as a writer. Returns a disposable that releases the lock when disposed.
/// </summary>
/// <returns>A disposable that releases the lock when disposed.</returns>
publicTask<IDisposable>WriterLockAsync()
{
returnWriterLockAsync(CancellationToken.None);
}
/// <summary>
/// Asynchronously acquires the lock as a writer. Returns a disposable that releases the lock when disposed. This method may block the calling thread.
/// </summary>
/// <returns>A disposable that releases the lock when disposed.</returns>
publicIDisposableWriterLock()
{
returnWriterLock(CancellationToken.None);
}
/// <summary>
/// Asynchronously acquires the lock as a reader with the option to upgrade. Returns a key that can be used to upgrade and downgrade the lock, and releases the lock when disposed.
/// </summary>
/// <param name="cancellationToken">The cancellation token used to cancel the lock. If this is already set, then this method will attempt to take the lock immediately (succeeding if the lock is currently available).</param>
/// <returns>A key that can be used to upgrade and downgrade this lock, and releases the lock when disposed.</returns>
/// Synchronously acquires the lock as a reader with the option to upgrade. Returns a key that can be used to upgrade and downgrade the lock, and releases the lock when disposed. This method may block the calling thread.
/// </summary>
/// <param name="cancellationToken">The cancellation token used to cancel the lock. If this is already set, then this method will attempt to take the lock immediately (succeeding if the lock is currently available).</param>
/// <returns>A key that can be used to upgrade and downgrade this lock, and releases the lock when disposed.</returns>
/// Asynchronously acquires the lock as a reader with the option to upgrade. Returns a key that can be used to upgrade and downgrade the lock, and releases the lock when disposed.
/// </summary>
/// <returns>A key that can be used to upgrade and downgrade this lock, and releases the lock when disposed.</returns>
/// Synchronously acquires the lock as a reader with the option to upgrade. Returns a key that can be used to upgrade and downgrade the lock, and releases the lock when disposed. This method may block the calling thread.
/// </summary>
/// <returns>A key that can be used to upgrade and downgrade this lock, and releases the lock when disposed.</returns>
/// Upgrades the reader lock to a writer lock. Returns a disposable that downgrades the writer lock to a reader lock when disposed.
/// </summary>
/// <param name="cancellationToken">The cancellation token used to cancel the upgrade. If this is already set, then this method will attempt to upgrade immediately (succeeding if the lock is currently available).</param>
/// Synchronously upgrades the reader lock to a writer lock. Returns a disposable that downgrades the writer lock to a reader lock when disposed. This method may block the calling thread.
/// </summary>
/// <param name="cancellationToken">The cancellation token used to cancel the upgrade. If this is already set, then this method will attempt to upgrade immediately (succeeding if the lock is currently available).</param>
/// Upgrades the reader lock to a writer lock. Returns a disposable that downgrades the writer lock to a reader lock when disposed.
/// </summary>
publicTask<IDisposable>UpgradeAsync()
{
returnUpgradeAsync(CancellationToken.None);
}
/// <summary>
/// Synchronously upgrades the reader lock to a writer lock. Returns a disposable that downgrades the writer lock to a reader lock when disposed. This method may block the calling thread.