Share via


AsyncReaderWriterLock Class

Definition

A non-blocking lock that allows concurrent access, exclusive access, or concurrent with upgradeability to exclusive access.

public ref class AsyncReaderWriterLock : IDisposable, Microsoft::VisualStudio::Threading::IHangReportContributor
public class AsyncReaderWriterLock : IDisposable, Microsoft.VisualStudio.Threading.IHangReportContributor
type AsyncReaderWriterLock = class
    interface IHangReportContributor
    interface IDisposable
Public Class AsyncReaderWriterLock
Implements IDisposable, IHangReportContributor
Inheritance
AsyncReaderWriterLock
Derived
Implements

Remarks

We have to use a custom awaitable rather than simply returning Task{LockReleaser} because we have to set CallContext data in the context of the person receiving the lock, which requires that we get to execute code at the start of the continuation (whether we yield or not).

Constructors

AsyncReaderWriterLock()

Initializes a new instance of the AsyncReaderWriterLock class.

AsyncReaderWriterLock(Boolean)

Initializes a new instance of the AsyncReaderWriterLock class.

AsyncReaderWriterLock(JoinableTaskContext, Boolean)

Initializes a new instance of the AsyncReaderWriterLock class.

Properties

AmbientLock

Gets the lock held by the caller's execution context.

CanCurrentThreadHoldActiveLock

Gets a value indicating whether the current thread is allowed to hold an active lock.

CaptureDiagnostics

Gets or sets a value indicating whether additional resources should be spent to collect information that would be useful in diagnosing deadlocks, etc.

Completion

Gets a task whose completion signals that this lock will no longer issue locks.

DeadlockCheckTimeout

Gets a time delay to check whether pending writer lock and reader locks forms a deadlock.

IsAnyLockHeld

Gets a value indicating whether any kind of lock is held by the caller and can be immediately used given the caller's context.

IsAnyPassiveLockHeld

Gets a value indicating whether any kind of lock is held by the caller without regard to the lock compatibility of the caller's context.

IsPassiveReadLockHeld

Gets a value indicating whether a read lock is held by the caller without regard to the lock compatibility of the caller's context.

IsPassiveUpgradeableReadLockHeld

Gets a value indicating whether an upgradeable read lock is held by the caller without regard to the lock compatibility of the caller's context.

IsPassiveWriteLockHeld

Gets a value indicating whether a write lock is held by the caller without regard to the lock compatibility of the caller's context.

IsReadLockHeld

Gets a value indicating whether the caller holds a read lock.

IsUnsupportedSynchronizationContext

Gets a value indicating whether the current SynchronizationContext is one that is not supported by this lock.

IsUpgradeableReadLockHeld

Gets a value indicating whether the caller holds an upgradeable read lock.

IsWriteLockHeld

Gets a value indicating whether the caller holds a write lock.

NoMessagePumpSynchronizationContext

Gets a SynchronizationContext which, when applied, suppresses any message pump that may run during synchronous blocks of the calling thread.

SyncObject

Gets the object used to synchronize access to this instance's fields.

Methods

Complete()

Causes new top-level lock requests to be rejected and the Completion task to transition to a completed state after any issued locks have been released.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

Dispose(Boolean)

Disposes managed and unmanaged resources held by this instance.

GetAggregateLockFlags()

Returns the aggregate of the lock flags for all nested locks.

GetHangReport()

Contributes data for a hang report.

GetTaskSchedulerForReadLockRequest()

Get the task scheduler to execute the continuation when the lock is acquired. AsyncReaderWriterLock uses a special SynchronizationContext to handle exclusive locks, and will ignore task scheduler provided, so this is only used in a read lock scenario. This method is called within the execution context to wait the read lock, so it can pick up TaskScheduler based on the current execution context. Note: the task scheduler is only used, when the lock is issued later. If the lock is issued immediately when CanCurrentThreadHoldActiveLock returns true, it will be ignored.

HideLocks()

Prevents use or visibility of the caller's lock(s) until the returned value is disposed.

LockStackContains(AsyncReaderWriterLock+LockFlags, AsyncReaderWriterLock+LockHandle)

Checks whether the aggregated flags from all locks in the lock stack satisfy the specified flag(s).

OnBeforeExclusiveLockReleasedAsync()

Fired when the last write lock is about to be released.

OnBeforeLockReleasedAsync(Boolean, AsyncReaderWriterLock+LockHandle)

Fired when any lock is being released.

OnBeforeWriteLockReleased(Func<Task>)

Registers a callback to be invoked when the write lock held by the caller is about to be ultimately released (outermost write lock).

OnCriticalFailure(Exception)

Invoked when the lock detects an internal error or illegal usage pattern that indicates a serious flaw that should be immediately reported to the application and/or bring down the process to avoid hangs or data corruption.

OnCriticalFailure(String)

Invoked when the lock detects an internal error or illegal usage pattern that indicates a serious flaw that should be immediately reported to the application and/or bring down the process to avoid hangs or data corruption.

OnExclusiveLockReleasedAsync()

Invoked after an exclusive lock is released but before anyone has a chance to enter the lock.

OnUpgradeableReadLockReleased()

Invoked when a top-level upgradeable read lock is released, leaving no remaining (write) lock.

ReadLockAsync(CancellationToken)

Obtains a read lock, asynchronously awaiting for the lock if it is not immediately available.

UpgradeableReadLockAsync(AsyncReaderWriterLock+LockFlags, CancellationToken)

Obtains a read lock, asynchronously awaiting for the lock if it is not immediately available.

UpgradeableReadLockAsync(CancellationToken)

Obtains an upgradeable read lock, asynchronously awaiting for the lock if it is not immediately available.

WriteLockAsync(AsyncReaderWriterLock+LockFlags, CancellationToken)

Obtains a write lock, asynchronously awaiting for the lock if it is not immediately available.

WriteLockAsync(CancellationToken)

Obtains a write lock, asynchronously awaiting for the lock if it is not immediately available.

Explicit Interface Implementations

IHangReportContributor.GetHangReport()

Contributes data for a hang report.

Applies to

Thread Safety

This type is thread-safe for all members.