@ac-essentials/misc-util
    Preparing search index...

    Class LockHold

    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.

    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();
    }
    }
    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
    Index

    Methods

    • 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 released and the error is thrown.

      Parameters

      • locks: Iterable<ILock>

        The iterable of locks to acquire.

      • Optionalsignal: 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.

    • 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 and the function returns null. Otherwise, a LockHold is returned.

      Parameters

      • ...locks: ILock[]

        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.