Table of Contents

Class AsyncMutex

Namespace
LfrlAnvil.Async
Assembly
LfrlAnvil.Core.dll

Represents a source of a fair asynchronous mutex primitive.

public sealed class AsyncMutex
Inheritance
AsyncMutex
Inherited Members
Extension Methods

Remarks

Lock is not reentrant.

Constructors

AsyncMutex()

Creates a new AsyncMutex instance.

public AsyncMutex()

Properties

Participants

Returns the total number of lock participants, which includes current lock holder and all waiters.

public int Participants { get; }

Property Value

int

Methods

EnterAsync(CancellationToken)

Asynchronously acquires an exclusive lock from this mutex.

public ValueTask<AsyncMutexToken> EnterAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Optional CancellationToken that can be used to cancel pending mutex acquisition.

Returns

ValueTask<AsyncMutexToken>

New ValueTask<TResult> instance which returns an AsyncMutexToken value.

Exceptions

OperationCanceledException

When provided cancellationToken was cancelled before the lock was acquired.

TrimExcess()

Attempts to discard unused resources.

public void TrimExcess()

TryEnter(out bool)

Attempts to synchronously acquire an exclusive lock from this mutex.

public AsyncMutexToken TryEnter(out bool entered)

Parameters

entered bool

out parameter which specifies whether the lock was acquired.

Returns

AsyncMutexToken

New AsyncMutexToken value. When entered is false, then returned instanced will be a default value.