LockHold
Defined in: packages/async/src/synchro/helpers/lock-hold.ts:52
A hold on one or more locks.
This class represents a collection of locks that have been acquired. It provides methods to release all held locks in a safe manner.
Locks are acquired in a globally consistent order to prevent deadlocks.
When a LockHold is no longer needed, the unlock method should be called to
release all held locks. Alternatively, it can be used with the using
statement for automatic disposal.
Locks are released in the reverse order of acquisition to maintain proper locking semantics.
Examples
Section titled “Examples” const lockA = new Mutex(); const lockB = new Mutex();
async function criticalSection() { // Acquire both locks const lockHold = await LockHold.from([lockA, lockB]); try { // Critical section code goes here } finally { // Release the locks await lockHold.unlock(); } } ```;
Using with `using` statement ```ts const lockA = new Mutex(); const lockB = new Mutex();
async function criticalSection() { // Acquire both locks await using _ = await LockHold.from([lockA, lockB]); // Critical section code goes here } ```
## Implements
- `AsyncDisposable`
## Methods
### \[asyncDispose\]()
> **\[asyncDispose\]**(): `PromiseLike`\<`void`\>
Defined in: packages/async/src/synchro/helpers/lock-hold.ts:157
#### Returns
`PromiseLike`\<`void`\>
#### Implementation of
`AsyncDisposable.[asyncDispose]`
***
### unlock()
> **unlock**(): `Promise`\<`void`\>
Defined in: packages/async/src/synchro/helpers/lock-hold.ts:153
Releases all held locks.
#### Returns
`Promise`\<`void`\>
***
### from()
> `static` **from**(`locks`, `signal?`): `Promise`\<`LockHold`\>
Defined in: packages/async/src/synchro/helpers/lock-hold.ts:88
Acquires locks on all provided locks, waiting as necessary.
The locks are acquired in a globally consistent order to prevent deadlocks.
If any lock cannot be acquired, all previously acquired locks are releasedand the error is thrown.
#### Parameters
##### locks
`Iterable`\<[`ILock`](../interfaces/ILock.md)\>
The iterable of locks to acquire.
##### signal?
`AbortSignal` \| `null`
An optional AbortSignal to cancel the acquire operation.
#### Returns
`Promise`\<`LockHold`\>
A promise that resolves to a LockHold when all locks are acquired.
***
### tryFrom()
#### Call Signature
> `static` **tryFrom**(`locks`): `Promise`\<`LockHold` \| `null`\>
Defined in: packages/async/src/synchro/helpers/lock-hold.ts:123
Tries to acquire locks on all provided locks without waiting.
The locks are acquired in a globally consistent order to prevent deadlocks.
If any lock is already held, all previously acquired locks are released andthe function returns `null`. Otherwise, a LockHold is returned.
##### Parameters
###### locks
readonly [`ILock`](../interfaces/ILock.md)[]
The locks to acquire.
##### Returns
`Promise`\<`LockHold` \| `null`\>
A promise that resolves to a LockHold if all locks were acquired, or `null` if any lock could not be acquired.
#### Call Signature
> `static` **tryFrom**(...`locks`): `Promise`\<`LockHold` \| `null`\>
Defined in: packages/async/src/synchro/helpers/lock-hold.ts:124
Tries to acquire locks on all provided locks without waiting.
The locks are acquired in a globally consistent order to prevent deadlocks.
If any lock is already held, all previously acquired locks are released andthe function returns `null`. Otherwise, a LockHold is returned.
##### Parameters
###### locks
...[`ILock`](../interfaces/ILock.md)[]
The locks to acquire.
##### Returns
`Promise`\<`LockHold` \| `null`\>
A promise that resolves to a LockHold if all locks were acquired, or `null` if any lock could not be acquired.