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

    Class Semaphore

    A counting semaphore implementation.

    A semaphore maintains a set of permits. Each acquire call blocks if necessary until a permit is available, and then takes it. Each release call adds a permit, potentially releasing a blocking acquirer.

    The semaphore is initialized with a given number of permits. The number of permits can be increased up to a maximum value.

    Index

    Constructors

    Accessors

    Methods

    Constructors

    • Creates a new semaphore with the given initial number of permits and maximum number of permits.

      Parameters

      • maxValue: number

        The maximum number of permits.

      • OptionalinitialValue: number

        The initial number of permits available. Defaults to maxValue.

      Returns Semaphore

    Accessors

    Methods

    • Acquires a permit from the semaphore, waiting if necessary until one is available.

      Parameters

      • count: number = 1
      • Optionalsignal: null | AbortSignal

        An optional AbortSignal to cancel the acquire operation.

      Returns Promise<Callable>

      A promise that resolves to a function that releases the acquired permit.

    • Tries to acquire a permit from the semaphore immediately, without waiting.

      Parameters

      • count: number = 1

        The number of permits to acquire.

      Returns boolean

      true if the permits were acquired, false otherwise.