Class AsyncReentrantMutex
Extends AsyncMutex to support reentrancy.
Namespace: Neon.Tasks
Assembly: Neon.Common.dll
Syntax
public class AsyncReentrantMutex
Remarks
Unlike the AsyncMutex class, this class supports reentrency, meaning that once an asynchronous Task flow has acquired the mutex, any additional acquistions by nested operations will also be accepted.
var mutex = new AsyncReentrantMutex();
await mutex.ExecuteActionAsync(
async () =>
{
// Protected code
await mutex.ExecuteActionAsync( // <--- This doesn't block
async () =>
{
// More protected code
});
});</code></pre>
The ExecuteFuncAsync<TResult>(Func<Task<TResult>>) can be used to execute an async
function that returns a result instead.
AsyncReentrantMutex is disposable. Calling dispose will cause
ObjectDisposedException to be thrown on any tasks waiting
to acquire the mutex.
Constructors
AsyncReentrantMutex()
Constructor.
Declaration
public AsyncReentrantMutex()
Methods
Dispose()
Releases any important resources associated with the instance.
Declaration
public void Dispose()
Remarks
note
This method will cause a ObjectDisposedException to be thrown on any task waiting to acquire this mutex.
Dispose(bool)
Releases any important resources associated with the instance.
Declaration
protected void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing | Pass |
ExecuteActionAsync(Func<Task>)
Acquires the mutex and then invokes the asynchronous action passed. This method returns after the action completes.
Declaration
public Task ExecuteActionAsync(Func<Task> action)
Parameters
Type | Name | Description |
---|---|---|
Func<Task> | action | The asynchronous action. |
Returns
Type | Description |
---|---|
Task | The tracking Task. |
ExecuteFuncAsync<TResult>(Func<Task<TResult>>)
Acquires the mutex and then invokes the asynchronous function passed, returning the function's result.
Declaration
public Task<TResult> ExecuteFuncAsync<TResult>(Func<Task<TResult>> function)
Parameters
Type | Name | Description |
---|---|---|
Func<Task<TResult>> | function | The asynchronous function. |
Returns
Type | Description |
---|---|
Task<TResult> | The function result. |
Type Parameters
Name | Description |
---|---|
TResult | Specifies the result returned by the async function. |
~AsyncReentrantMutex()
Finalizer.
Declaration
protected ~AsyncReentrantMutex()