Class AsyncMutex
Serializes an asynchronous critical section. Callers await LockAsync(CancellationToken) and
release by disposing the returned handle, so a whole check-then-act sequence that spans
await points runs to completion before the next caller observes the state.
Implements
Inherited Members
Namespace: Serenity.Shared.Utils
Assembly: Serenity.Shared.dll
Syntax
public sealed class AsyncMutex : IDisposable
Remarks
The C# lock statement cannot span an await, which is exactly what a service
that reads a cache, writes to a store and then updates the cache needs. This is the
standard SemaphoreSlim(1,1) wrapper for that case: it is NOT reentrant, so a
method that already holds the mutex must call a private helper rather than a sibling public
method that would take it again.
Constructors
AsyncMutex()
Declaration
public AsyncMutex()
Methods
Dispose()
Releases the underlying semaphore.
Declaration
public void Dispose()
LockAsync(CancellationToken)
Waits for exclusive access and returns the handle that releases it on dispose.
Declaration
public Task<IDisposable> LockAsync(CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken | Token used to cancel the wait. |
Returns
| Type | Description |
|---|---|
| Task<IDisposable> | A handle whose disposal releases the mutex. |