Threading Threading
Threading Threading
DocFX + Singulink = ♥

Search Results for

    Class AsyncReadWriteLock

    A reader/writer lock for asynchronous code that can be held across an await.

    Inheritance
    object
    AsyncReadWriteLock
    Namespace: Singulink.Threading
    Assembly: Singulink.Threading.dll
    Syntax
    public sealed class AsyncReadWriteLock
    Remarks

    Any number of callers can hold the lock in read mode together, or one caller can hold it in write mode. The lock is entered through guards, like ReadWriteLock: Enter…GuardAsync methods wait until the lock is entered, TryEnter…GuardAsync methods give up after a time limit, and TryEnter…Guard methods do not wait at all. The TryEnter methods return a guard whose IsEntered property says whether the lock was entered. Disposing a guard that did not enter the lock does nothing, so a result can always be assigned to a using declaration:

    using var guard = await rwLock.TryEnterReadGuardAsync(TimeSpan.FromSeconds(5), cancellationToken);
    
    if (!guard.IsEntered)
        return false;
    
    await ReadDataAsync();
    return true;

    The lock has no thread affinity and no owner: a guard can be disposed on any thread, by any code that has it, so it can be held across an await. For the same reason the lock is not reentrant and cannot tell that a caller already holds it. A caller that holds it in write mode and enters it again, or holds it in read mode and enters it in write mode, waits for itself, and without a time limit that wait never ends. Entering read mode again while holding it in read mode is a second hold that depends on the Preference: it never waits under Readers, and under the other two it waits behind a waiting writer, which is waiting for the first hold, so it never ends either.

    The Preference given at construction decides who enters first when readers and writers both want the lock, and with that who can be kept waiting forever; see AsyncReadWriteLockPreference. Writers always enter in the order they arrived. A waiter that gives up, because its time limit passed or it was canceled, leaves the queue at once, and the callers it was holding up enter if they then can.

    Entering the lock without a wait and leaving it allocate nothing, and take one interlocked operation each while nothing is waiting for the lock; a caller whose release lets a waiter in also takes a short internal lock. A call that has to wait allocates one object for the wait, a timer if it has a time limit, and what registering with its cancellation token costs. The code that was waiting never continues on the thread that released the lock, canceled the wait or ran the timer: its continuation is queued to the thread pool, or posted to the SynchronizationContext or TaskScheduler that the await captured.

    The asynchronous methods throw directly only for an argument that is out of range. Every other failure is in the returned task: a canceled token, too many read holds, or an exception that the TimeProvider throws when it creates the timer of a time limit. A call that fails holds nothing and is not left waiting.

    Time limits are measured with the TimeProvider given at construction, System by default. The lock holds no resources between calls and is not disposable; a wait is ended early by canceling its token. There is no upgradeable read mode, no way to turn a write hold into a read hold, and no method that blocks the calling thread until the lock is entered.

    A failure in resuming one waiter never reaches another caller. If the continuation of a wait cannot be scheduled when the lock completes the wait, because the SynchronizationContext or TaskScheduler it was captured on throws, the exception is thrown to nobody: not to the caller that released the lock, not to the code that canceled a token, and not on the thread of a timer. If that wait had been granted the lock, the lock takes the hold back and passes it to whoever is next, so the lock is never left held by code that will not run.

    The code that was awaiting the lock then normally never continues, as with any awaitable whose context refuses its continuation. A context that runs or queues the continuation and throws all the same lets the code continue: it then either has the lock as usual, or, if the lock took the hold back first, its await throws an InvalidOperationException. A timer that throws when it is disposed is ignored in the same way.

    These guarantees cover a wait that the lock completes. If a wait is completed in the moment between the awaiting code's check for completion and the registration of its continuation, the continuation is scheduled by the awaiting code's own thread, and a context that throws then fails as it does with any other awaitable: the exception is rethrown on the thread pool, and a hold that the wait was granted is not released.

    Constructors

    Name Description
    AsyncReadWriteLock()

    Initializes a new instance of the AsyncReadWriteLock class that grants the lock in arrival order and measures time limits with System.

    AsyncReadWriteLock(AsyncReadWriteLockPreference)

    Initializes a new instance of the AsyncReadWriteLock class with the specified preference that measures time limits with System.

    AsyncReadWriteLock(AsyncReadWriteLockPreference, TimeProvider)

    Initializes a new instance of the AsyncReadWriteLock class with the specified preference and time provider.

    Properties

    Name Description
    CurrentReadCount

    Gets the number of holds on the lock in read mode.

    IsHeldForRead

    Gets a value indicating whether the lock is held in read mode by one or more callers.

    IsHeldForWrite

    Gets a value indicating whether the lock is held in write mode.

    Preference

    Gets who enters first when readers and writers both want the lock.

    WaitingReadCount

    Gets the number of callers that are waiting to enter the lock in read mode.

    WaitingWriteCount

    Gets the number of callers that are waiting to enter the lock in write mode.

    Methods

    Name Description
    EnterReadGuardAsync(CancellationToken)

    Asynchronously enters the lock in read mode and returns a guard that leaves it when it is disposed.

    EnterWriteGuardAsync(CancellationToken)

    Asynchronously enters the lock in write mode and returns a guard that leaves it when it is disposed.

    TryEnterReadGuard()

    Tries to enter the lock in read mode without waiting and returns a guard whose IsEntered property says whether it did. Disposing the guard leaves the lock, or does nothing if the lock was not entered.

    TryEnterReadGuardAsync(TimeSpan, CancellationToken)

    Asynchronously tries to enter the lock in read mode within a time limit and returns a guard whose IsEntered property says whether it did. Disposing the guard leaves the lock, or does nothing if the lock was not entered.

    TryEnterWriteGuard()

    Tries to enter the lock in write mode without waiting and returns a guard whose IsEntered property says whether it did. Disposing the guard leaves the lock, or does nothing if the lock was not entered.

    TryEnterWriteGuardAsync(TimeSpan, CancellationToken)

    Asynchronously tries to enter the lock in write mode within a time limit and returns a guard whose IsEntered property says whether it did. Disposing the guard leaves the lock, or does nothing if the lock was not entered.

    © Singulink. All rights reserved.