Simple mutex class.
const mutex = new Mutex()
const release = await mutex.acquire()
release() // to release mutex
Constructor takes no parameters.
Determine if the lock is available.
Returns boolean true if lock is available, false otherwise
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 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()
const semaphore = new Semaphore(3) // 3 locks
await semaphore.acquire()
semaphore.release()
max
(optional, default1
)
Returns whether a lock is available. If one is available, acquireSync will succeed.
Returns boolean true if a lock is available, false otherwise
Acquires a lock synchronously.
Returns boolean true if lock was acquired, false otherwise
Acquires a lock asynchronously.
Returns PromiseLike<void> promise resolves when a lock has been acquired.
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