Semaphore class
Semaphore with async api
Signature:
export declare class Semaphore
Example
const sem = new Semaphore(5) const release = await sem.acquire() // do something release()
Constructors
Constructor | Modifiers | Description |
---|---|---|
(constructor)(permits) | constructs a new Semaphore with n permits |
Properties
Property | Modifiers | Type | Description |
---|---|---|---|
frozen? | Future<void> | (Optional) check if semaphore is frozen (non-undefined value), other uses are not guaranteed | |
isEmpty | boolean | Check if no task is using the semaphore' always return | |
isFull | boolean | Check if all permits are being used always return | |
permits | number | Get the number of total permits currently | |
remain | number | Get the number of remaining permits |
Methods
Method | Modifiers | Description |
---|---|---|
acquire(timeout) | Acquire a permit, resolve when resouce is available. | |
freeze() | Freeze this semaphore, calling NOTE: don't call this again if Semaphore.frozen, not supported yet | |
grant(permits) | Give n permits to the semaphore, will immediately start this number of waiting tasks if not frozen | |
revoke(permits) | Destroy n permits, effective until **note**: you may need to check if | |
schedule(fn) | Schedule a task to run when a permit is available and automatically release after run. | |
tryAcquire() | Try to synchronosly acquire if there's remaining permits | |
unfreeze() | unfreeze this semaphore, it is synchronos and the returned value should be ignored |