bare-atomics
Native synchronization primitives for JavaScript
bare-atomics — Native synchronization primitives for JavaScript. It is a native addon and requires Bare >=1.2.0.
npm i bare-atomicsAPI
Mutex
new Mutex(opts?: MutexOptions)
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
opts? | MutexOptions | — | Options; set recursive: true to let the owning thread lock the mutex more than once (default false). May also carry an existing handle to wrap. |
Mutex.destroy(): void
Source
Throws
- The mutex is still held.
Mutex.handle: SharedArrayBuffer
Source
A SharedArrayBuffer with the underlying mutex handle.
held: boolean
Source
Whether or not the current thread currently holds the mutex.
lock(): void
Source
Throws
- The mutex is already held and was not created with
recursive: true.
Mutex.from(handle: SharedArrayBuffer, opts?: MutexOptions): Mutex
Source
Create a Mutex from an existing handle; options are the same as new Mutex().
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
handle | SharedArrayBuffer | — | A SharedArrayBuffer holding an existing mutex, as exposed by Mutex.handle. |
opts? | MutexOptions | — | Options, the same as new Mutex(). |
Returns Mutex — A Mutex sharing the underlying handle.
recursive: boolean
Source
tryLock(): boolean
Source
Throws
- The mutex is already held and was not created with
recursive: true.
unlock(): void
Source
Throws
- The mutex is not currently held.
Semaphore
new Semaphore(value: any)
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
value | any | — | The initial value (permit count) of the semaphore. |
Semaphore.destroy(): void
Source
Semaphore.handle: SharedArrayBuffer
Source
A SharedArrayBuffer with the underlying mutex handle.
post(): void
Source
Semaphore.from(handle: SharedArrayBuffer): Semaphore
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
handle | SharedArrayBuffer | — | A SharedArrayBuffer holding an existing semaphore, as exposed by Semaphore.handle. |
Returns Semaphore — A Semaphore sharing the underlying handle.
tryWait(): boolean
Source
wait(): void
Source
Condition
broadcast(): void
Source
Condition.from(handle: SharedArrayBuffer): Condition
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
handle | SharedArrayBuffer | — | A SharedArrayBuffer holding an existing condition variable, as exposed by Condition.handle. |
Returns Condition — A Condition sharing the underlying handle.
Condition.destroy(): void
Source
Condition.handle: SharedArrayBuffer
Source
A SharedArrayBuffer with the underlying mutex handle.
signal(): void
Source
Condition.wait(): boolean
Source
Throws
- The associated mutex is not held by the current thread.
Barrier
new Barrier(count: number)
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
count | number | — | The number of threads that must reach the barrier (call wait()) before they are all released together. |
Barrier.from(handle: SharedArrayBuffer): Barrier
Source
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
handle | SharedArrayBuffer | — | A SharedArrayBuffer holding an existing barrier, as exposed by Barrier.handle. |
Returns Barrier — A Barrier sharing the underlying handle.
Barrier.destroy(): void
Source
Barrier.handle: SharedArrayBuffer
Source
A SharedArrayBuffer with the underlying mutex handle.
Barrier.wait(): boolean
Source
Types
MutexOptions
interface MutexOptions {
recursive?: boolean
}See also
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.