Options
All
  • Public
  • Public/Protected
  • All
Menu

General counting semaphore implementation.

Not thread-safe! Not safe with regard to pre-emptive multitasking (e.g. possible with some native code Node.js extensions).

Hierarchy

  • Semaphore

Implements

Index

Constructors

Properties

Methods

Constructors

constructor

  • Initialize.

    Parameters

    • counter: number = 0

      Initial value for the counter. E.g. if you provide 10, the first 10 takes will fulfill "immediately" (for some informal value of immediately).

    Returns Semaphore

Properties

Private resolvers

resolvers: (() => void)[] = ...

Methods

free

  • free(): void
  • Free or 'V' operation.

    If there are any promises returned by {@link take()} still awaiting their fulfillment, exactly of them is fulfilled. The order of fulfillment is implementation-defined.

    Returns void

take

  • take(): Promise<void>
  • Take or 'P' operation.

    Returns Promise<void>

    A promise which is fulfilled when >= 1 of the resource represented by the internal counter is available. This might be the case either if free is called or if the semaphore is initialized with a value > 0.

tryTake

  • tryTake(): boolean
  • Take ('P') if a resource is still available (<=> counter >= 1). Otherwise, do not block (as take does) and return false.

    Returns boolean

    True if a resource could be taken, False otherwise.

tryTakeWithin

  • tryTakeWithin(millis: number): Promise<boolean>
  • Try taking ('P') a resource within the next millis milliseconds.

    Parameters

    • millis: number

    Returns Promise<boolean>

    A promise fulfilling with true if a resource could be taken or false otherwise.

Static Private wait

  • wait(millis: number): Promise<void>

Generated using TypeDoc