ciorent
Version:
A lightweight, low-overhead concurrency library
38 lines (37 loc) • 1.07 kB
TypeScript
/**
* @module Semaphores
*/
/**
* Describe a singly linked list node
*/
export type QueueNode = [next: QueueNode | undefined, value: () => void];
/**
* Describe a semaphore
*/
export type Semaphore = [head: QueueNode, tail: QueueNode, remain: number, register: (cb: () => void) => void];
/**
* Create a semaphore that allows n accesses
*/
export declare const init: (n: number) => Semaphore;
/**
* Wait until the semaphore allows access
*/
export declare const acquire: (s: Semaphore) => Promise<void> | void;
/**
* Signal to the semaphore to release access
*/
export declare const release: (s: Semaphore) => void;
/**
* Control concurrency of a task with a semaphore
*/
export declare const control: <T extends (...args: any[]) => Promise<any>>(task: T, s: Semaphore) => T;
/**
* Set maximum concurrency for a task (fast path)
*/
export declare const permits: <T extends (...args: any[]) => Promise<any>>(task: T, permits: number) => T;
/**
* Queue a task
* @param s
* @param task
*/
export declare const queue: <R>(s: Semaphore, task: () => Promise<R>) => Promise<R>;