Ferrous API Reference

All Types / Ferrous.Concurrency

class AsyncMutex<T>

public sealed class AsyncMutex<T> : IDisposable

An async-capable data-owning mutual exclusion primitive inspired by tokio::sync::Mutex<T>. Compiled with .NET 11 runtime async (runtime-async=on) on net11.0.

Type Parameters
T
The type of the protected value.

Constructors

public AsyncMutex(T initialValue)

Initializes a new AsyncMutex<T> owning initialValue.

Parameters
initialValue
The initial protected value.

Methods

public ValueTask<AsyncMutexGuard<T>> LockAsync(CancellationToken cancellationToken = default)
[MustUse]

Asynchronously acquires the mutex and returns an AsyncMutexGuard<T>. Uses a zero-allocation fast path when uncontended.

Parameters
cancellationToken
A token to observe while waiting to acquire the mutex.

Returns: A ValueTask<TResult> producing the acquired AsyncMutexGuard<T>.

public bool TryLock(out AsyncMutexGuard<T> guard)
[MustUse]

Attempts to acquire the mutex synchronously without blocking.

Parameters
guard
When this method returns true, contains the acquired AsyncMutexGuard<T>; otherwise, the default (unacquired) guard.

Returns: true if the lock was acquired; otherwise, false.

public void Dispose()

Releases the underlying SemaphoreSlim.