Skip to content

Latest commit

 

History

History
127 lines (78 loc) · 2.44 KB

semaphore.md

File metadata and controls

127 lines (78 loc) · 2.44 KB

Table of Contents

Mutex

Simple mutex class.

const mutex = new Mutex()
const release = await mutex.acquire()
release() // to release mutex

Constructor

Constructor takes no parameters.

available

Determine if the lock is available.

Returns boolean true if lock is available, false otherwise

acquireSync

Get a lock if available.

Returns () => void if the lock is available, returns a function to release it. Otherwise returns null. The release function can be called multiple times, it will only release once.

const mutex = new Mutex()
const release = mutex.acquireSync()
if (release) release()

acquire

Acquire a lock.

Returns PromiseLike<() => void> - returns a Promise that resolves to a release function. The release function can be called multiple times, it will only release once.

const mutex = new Mutex()
const release = await mutex.acquire()
release()

Semaphore

const semaphore = new Semaphore(3) // 3 locks
await semaphore.acquire()
semaphore.release()

Constructor

  • max (optional, default 1)

available

Returns whether a lock is available. If one is available, acquireSync will succeed.

Returns boolean true if a lock is available, false otherwise

acquireSync

Acquires a lock synchronously.

Returns boolean true if lock was acquired, false otherwise

acquire

Acquires a lock asynchronously.

Returns PromiseLike<void> promise resolves when a lock has been acquired.

release

Releases a lock so that it is available to be acquired. Each acquire or acquireSync call must be matched by exactly one release call.

Returns void