Table of Contents

Class KeyedLock

Namespace
Excalibur.Dispatch.Threading
Assembly
Excalibur.Dispatch.dll

Default implementation of IKeyedLock backed by a per-key SemaphoreSlim.

public sealed class KeyedLock : IKeyedLock
Inheritance
KeyedLock
Implements
Inherited Members
Extension Methods

Remarks

Each key maps to an KeyedLock.Entry holding its semaphore and a reference count of the acquirers and waiters currently using it. The semaphore is removed and disposed only when the last reference is released (reference count returns to zero), which keeps memory bounded without ever disposing a semaphore that a concurrent waiter still references. The reference count is mutated exclusively under Excalibur.Dispatch.Threading.KeyedLock._lockObj, so "remove a key's semaphore while it is still referenced" is structurally inexpressible — closing the classic keyed-semaphore cleanup race (a waiter observing ObjectDisposedException, or two holders for one key).

Methods

AcquireAsync(string, CancellationToken)

Acquires a lock for the specified key asynchronously, creating a new semaphore if one doesn't exist.

public Task<IDisposable> AcquireAsync(string key, CancellationToken cancellationToken)

Parameters

key string

The key to lock on.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<IDisposable>

A disposable lock handle that releases the lock when disposed.