Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Semaphore<Max>

Counting semaphore.

author

Jeongho Nam - https://github.com/samchon

Type parameters

  • Max: number = number

Hierarchy

  • Semaphore

Index

Constructors

constructor

Methods

acquire

  • acquire(): Promise<void>
  • Acquires a section.

    Acquires a section until be released. If all of the sections in the semaphore already have been acquired by others, the function call would be blocked until one of them returns its acquisition by calling the release method.

    In same reason, if you don't call the release function after you business, the others who want to acquire a section from the semaphore would be fall into the forever sleep. Therefore, never forget to calling the release function or utilize the UniqueLock.lock function instead with Semaphore.get_lockable to ensure the safety.

    Returns Promise<void>

max

  • max(): Max
  • Get number of maximum sections lockable.

    Returns Max

    Number of maximum sections lockable.

release

  • release(n?: number): Promise<void>
  • Release sections.

    When you call this release method and there're someone who are currently blocked by attemping to acquire a section from this semaphore, n of them (FIFO; first-in-first-out) would acquire those released sections and continue their executions.

    Otherwise, there's not anyone who is acquiring the section or number of the blocked are less than n, the OutOfRange error would be thrown.

    As you know, when you succeeded to acquire a section, you don't have to forget to calling this release method after your business. If you forget it, it would be a terrible situation for the others who're attempting to acquire a section from this semaphore.

    However, if you utilize the UniqueLock with Semaphore.get_lockable, you don't need to consider about this release method. Just define your business into a callback function as a parameter of methods of the UniqueLock, then this release method would be automatically called by the UniqueLock after the business.

    throw

    OutOfRange when n is greater than currently acquired sections.

    Parameters

    • Default value n: number = 1

      Number of sections to be released. Default is 1.

    Returns Promise<void>

try_acquire

  • try_acquire(): Promise<boolean>
  • Tries to acquire a section.

    Attempts to acquire a section without blocking. If succeeded to acquire a section from the semaphore immediately, it returns true directly. Otherwise all of the sections in the semaphore are full, the function gives up the trial immediately and returns false directly.

    Note that, if you succeeded to acquire a section from the semaphore (returns `true) but do not call the release function after your business, the others who want to acquire a section from the semaphore would be fall into the forever sleep. Therefore, never forget to calling the release function or utilize the UniqueLock.try_lock function instead with Semaphore.get_lockable to ensure the safety.

    Returns Promise<boolean>

    Whether succeeded to acquire or not.

try_acquire_for

  • try_acquire_for(ms: number): Promise<boolean>
  • Tries to acquire a section until timeout.

    Attempts to acquire a section from the semaphore until timeout. If succeeded to acquire a section until the timeout, it returns true. Otherwise failed to acquiring a section in given the time, the function gives up the trial and returns false.

    Failed to acquiring a section in the given time (returns false), it means that there're someone who have already acquired sections and do not return them over the time expiration.

    Note that, if you succeeded to acquire a section from the semaphore (returns `true) but do not call the release function after your business, the others who want to acquire a section from the semaphore would be fall into the forever sleep. Therefore, never forget to calling the release function or utilize the {@link UniqueLock.try_acquire_for} function instead with Semaphore.get_lockable to ensure the safety.

    Parameters

    • ms: number

      The maximum miliseconds for waiting.

    Returns Promise<boolean>

    Whether succeded to acquire or not.

try_acquire_until

  • try_acquire_until(at: Date): Promise<boolean>
  • Tries to acquire a section until timeout.

    Attempts to acquire a section from the semaphore until time expiration. If succeeded to acquire a section until the time expiration, it returns true. Otherwise failed to acquiring a section in the given time, the function gives up the trial and returns false.

    Failed to acquiring a section in the given time (returns false), it means that there're someone who have already acquired sections and do not return them over the time expiration.

    Note that, if you succeeded to acquire a section from the semaphore (returns `true) but do not call the release function after your business, the others who want to acquire a section from the semaphore would be fall into the forever sleep. Therefore, never forget to calling the release function or utilize the {@link UniqueLock.try_acquire_until} function instead with Semaphore.get_lockable to ensure the safety.

    Parameters

    • at: Date

      The maximum time point to wait.

    Returns Promise<boolean>

    Whether succeded to acquire or not.

Static get_lockable

Generated using TypeDoc