Options
All
  • Public
  • Public/Protected
  • All
Menu

A ReentrantLock can be used to synchronize async code and/or threads (WebWorkers). Multiple ReentrantLock instances can be grouped, and only one instance at a time can be locked.

An instance can be locked by calling lock() on the instance. The call blocks until no other instance inside the group is locked and increments holdCount by one. After the call returns, the instance is guaranteed to be the only one inside the group that is locked. Since lock() is a blocking operation, it is not available on the main thread. See lockAsync() for an alternative.

To unlock an instance, unlock() can be called on the instance. The invocation decrements holdCount of the instance by one if holdCount is greater than 0. The instance is unlocked if holdCount reaches zero during the invocation.

Multiple ReentrantLock instances can be grouped by providing TypedArray instances based on the same SharedArrayBuffer instance to the ReentrantLock constructor on creation.

This implementation is based on @mogoe1/semaphore and uses SharedArrayBuffers and Atomics for synchronization.

Hierarchy

  • ReentrantLock

Index

Constructors

Properties

Accessors

Methods

Constructors

constructor

  • Create a new ReentrantLock instance. The given Int32Array should be based on a SharedArrayBuffer. Multiple ReentrantLock instances initialized with the same SharedArrayBuffer are grouped.

    Parameters

    • i32Array: Int32Array

      A TypedArray used to store whether or not any instance inside a group of ReentrantLock instances is locked. It has to have a length of one, and it has to be based on a SharedArrayBuffer.

    Returns ReentrantLock

Properties

Private _holdCount

_holdCount: number

How often lock() has been called without a later call to unlock().

Private _semaphore

_semaphore: Semaphore

The underlying @mogoe1/semaphore used by this lock.

Accessors

buffer

  • get buffer(): SharedArrayBuffer
  • SharedArrayBuffer used by this ReentrantLocks group.

    Returns SharedArrayBuffer

holdCount

  • get holdCount(): number

isLocked

  • get isLocked(): boolean

Methods

lock

  • lock(): boolean
  • Locks this ReentrantLock instance and increments holdCount by one. Blocks until this instance is locked and no other instance inside the group is locked.

    If Atomics.wait is not availabe (eg. on the main thread) use lockAsync() instead.

    Returns boolean

    False if this instance was already locked prior to the function call. True otherwise.

lockAsync

  • lockAsync(): Promise<boolean>
  • Locks this ReentrantLock instance and increments holdCount by one. Immediately returns a promise that resolves once this instance is locked and no other instance inside the group is locked.

    Returns Promise<boolean>

    A promise that resolves to false if the instance was already locked prior to the function call. Otherwise it resolves to true.

unlock

  • unlock(): boolean

Static create

  • Creates a new SharedArrayBuffer and a new ReentrantLock instance based on the created buffer.

    Returns ReentrantLock

    The new ReentrantLock instance.

Legend

  • Constructor
  • Method
  • Private property
  • Static method

Generated using TypeDoc