Table of Contents

Class AsyncReaderWriterResourceLock<TMoniker, TResource>

Namespace
Microsoft.VisualStudio.Threading
Assembly
Microsoft.VisualStudio.Threading.dll

A non-blocking lock that allows concurrent access, exclusive access, or concurrent with upgradeability to exclusive access, making special allowances for resources that must be prepared for concurrent or exclusive access.

public abstract class AsyncReaderWriterResourceLock<TMoniker, TResource> : AsyncReaderWriterLock, IHangReportContributor, IDisposable where TResource : class

Type Parameters

TMoniker

The type of the moniker that identifies a resource.

TResource

The type of resource issued for access by this lock.

Inheritance
AsyncReaderWriterResourceLock<TMoniker, TResource>
Implements
Inherited Members

Constructors

AsyncReaderWriterResourceLock()

Initializes a new instance of the AsyncReaderWriterResourceLock<TMoniker, TResource> class.

protected AsyncReaderWriterResourceLock()

AsyncReaderWriterResourceLock(JoinableTaskContext?, bool)

Initializes a new instance of the AsyncReaderWriterResourceLock<TMoniker, TResource> class.

protected AsyncReaderWriterResourceLock(JoinableTaskContext? joinableTaskContext, bool captureDiagnostics)

Parameters

joinableTaskContext JoinableTaskContext

A JoinableTaskContext to help resolve dead locks caused by interdependency between top read lock tasks when there is a pending write lock blocking one of them.

captureDiagnostics bool

true to spend additional resources capturing diagnostic details that can be used to analyze deadlocks or other issues.

AsyncReaderWriterResourceLock(bool)

Initializes a new instance of the AsyncReaderWriterResourceLock<TMoniker, TResource> class.

protected AsyncReaderWriterResourceLock(bool captureDiagnostics)

Parameters

captureDiagnostics bool

true to spend additional resources capturing diagnostic details that can be used to analyze deadlocks or other issues.

Methods

GetAggregateLockFlags()

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

protected AsyncReaderWriterResourceLock<TMoniker, TResource>.LockFlags GetAggregateLockFlags()

Returns

AsyncReaderWriterResourceLock<TMoniker, TResource>.LockFlags

GetResourceAsync(TMoniker, CancellationToken)

Retrieves the resource with the specified moniker.

protected abstract Task<TResource> GetResourceAsync(TMoniker resourceMoniker, CancellationToken cancellationToken)

Parameters

resourceMoniker TMoniker

The identifier for the desired resource.

cancellationToken CancellationToken

A token whose cancellation indicates lost interest in obtaining the resource.

Returns

Task<TResource>

A task whose result is the desired resource.

GetTaskSchedulerToPrepareResourcesForConcurrentAccess(TResource)

Gets a task scheduler to prepare a resource for concurrent access.

protected virtual TaskScheduler GetTaskSchedulerToPrepareResourcesForConcurrentAccess(TResource resource)

Parameters

resource TResource

The resource to prepare.

Returns

TaskScheduler

A TaskScheduler.

OnExclusiveLockReleasedAsync()

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

protected override Task OnExclusiveLockReleasedAsync()

Returns

Task

Remarks

This method is called while holding a private lock in order to block future lock consumers till this method is finished.

OnUpgradeableReadLockReleased()

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

protected override void OnUpgradeableReadLockReleased()

PrepareResourceForConcurrentAccessAsync(TResource, CancellationToken)

Prepares a resource for concurrent access.

protected abstract Task PrepareResourceForConcurrentAccessAsync(TResource resource, CancellationToken cancellationToken)

Parameters

resource TResource

The resource to prepare.

cancellationToken CancellationToken

The token whose cancellation signals lost interest in the resource.

Returns

Task

A task whose completion signals the resource has been prepared.

Remarks

This is invoked on a resource when it is initially requested for concurrent access, for both transitions from no access and exclusive access.

PrepareResourceForExclusiveAccessAsync(TResource, LockFlags, CancellationToken)

Prepares a resource for access by one thread.

protected abstract Task PrepareResourceForExclusiveAccessAsync(TResource resource, AsyncReaderWriterResourceLock<TMoniker, TResource>.LockFlags lockFlags, CancellationToken cancellationToken)

Parameters

resource TResource

The resource to prepare.

lockFlags AsyncReaderWriterResourceLock<TMoniker, TResource>.LockFlags

The aggregate of all flags from the active and nesting locks.

cancellationToken CancellationToken

The token whose cancellation signals lost interest in the resource.

Returns

Task

A task whose completion signals the resource has been prepared.

Remarks

This is invoked on a resource when it is initially access for exclusive access, but only when transitioning from no access -- it is not invoked when transitioning from concurrent access to exclusive access.

ReadLockAsync(CancellationToken)

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

public AsyncReaderWriterResourceLock<TMoniker, TResource>.ResourceAwaitable ReadLockAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A token whose cancellation indicates lost interest in obtaining the lock. A canceled token does not release a lock that has already been issued. But if the lock isn't immediately available, a canceled token will cause the code that is waiting for the lock to resume with an OperationCanceledException.

Returns

AsyncReaderWriterResourceLock<TMoniker, TResource>.ResourceAwaitable

An awaitable object whose result is the lock releaser.

SetAllResourcesToUnknownState()

Sets all the resources to be considered in an unknown state.

protected void SetAllResourcesToUnknownState()

SetResourceAsAccessed(Func<TResource, object?, bool>, object?)

Marks any loaded resources as having been retrieved under a lock if they satisfy some predicate.

protected bool SetResourceAsAccessed(Func<TResource, object?, bool> resourceCheck, object? state)

Parameters

resourceCheck Func<TResource, object, bool>

A function that returns true if the provided resource should be considered retrieved.

state object

The state object to pass as a second parameter to resourceCheck.

Returns

bool

true if the delegate returned true on any of the invocations.

SetResourceAsAccessed(TResource)

Marks a resource as having been retrieved under a lock.

protected void SetResourceAsAccessed(TResource resource)

Parameters

resource TResource

UpgradeableReadLockAsync(LockFlags, CancellationToken)

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

public AsyncReaderWriterResourceLock<TMoniker, TResource>.ResourceAwaitable UpgradeableReadLockAsync(AsyncReaderWriterResourceLock<TMoniker, TResource>.LockFlags options, CancellationToken cancellationToken = default)

Parameters

options AsyncReaderWriterResourceLock<TMoniker, TResource>.LockFlags

Modifications to normal lock behavior.

cancellationToken CancellationToken

A token whose cancellation indicates lost interest in obtaining the lock. A canceled token does not release a lock that has already been issued. But if the lock isn't immediately available, a canceled token will cause the code that is waiting for the lock to resume with an OperationCanceledException.

Returns

AsyncReaderWriterResourceLock<TMoniker, TResource>.ResourceAwaitable

An awaitable object whose result is the lock releaser.

UpgradeableReadLockAsync(CancellationToken)

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

public AsyncReaderWriterResourceLock<TMoniker, TResource>.ResourceAwaitable UpgradeableReadLockAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A token whose cancellation indicates lost interest in obtaining the lock. A canceled token does not release a lock that has already been issued. But if the lock isn't immediately available, a canceled token will cause the code that is waiting for the lock to resume with an OperationCanceledException.

Returns

AsyncReaderWriterResourceLock<TMoniker, TResource>.ResourceAwaitable

An awaitable object whose result is the lock releaser.

WriteLockAsync(LockFlags, CancellationToken)

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

public AsyncReaderWriterResourceLock<TMoniker, TResource>.ResourceAwaitable WriteLockAsync(AsyncReaderWriterResourceLock<TMoniker, TResource>.LockFlags options, CancellationToken cancellationToken = default)

Parameters

options AsyncReaderWriterResourceLock<TMoniker, TResource>.LockFlags

Modifications to normal lock behavior.

cancellationToken CancellationToken

A token whose cancellation indicates lost interest in obtaining the lock. A canceled token does not release a lock that has already been issued. But if the lock isn't immediately available, a canceled token will cause the code that is waiting for the lock to resume with an OperationCanceledException.

Returns

AsyncReaderWriterResourceLock<TMoniker, TResource>.ResourceAwaitable

An awaitable object whose result is the lock releaser.

WriteLockAsync(CancellationToken)

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

public AsyncReaderWriterResourceLock<TMoniker, TResource>.ResourceAwaitable WriteLockAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A token whose cancellation indicates lost interest in obtaining the lock. A canceled token does not release a lock that has already been issued. But if the lock isn't immediately available, a canceled token will cause the code that is waiting for the lock to resume with an OperationCanceledException.

Returns

AsyncReaderWriterResourceLock<TMoniker, TResource>.ResourceAwaitable

An awaitable object whose result is the lock releaser.