|
Last change
on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.7 KB
|
| Rev | Line | |
|---|
| [a762898] | 1 | /**
|
|---|
| 2 | * A Mutex (mutual exclusion lock) for async functions.
|
|---|
| 3 | * It allows only one async task to access a critical section at a time.
|
|---|
| 4 | *
|
|---|
| 5 | * @example
|
|---|
| 6 | * const mutex = new Mutex();
|
|---|
| 7 | *
|
|---|
| 8 | * async function criticalSection() {
|
|---|
| 9 | * await mutex.acquire();
|
|---|
| 10 | * try {
|
|---|
| 11 | * // This code section cannot be executed simultaneously
|
|---|
| 12 | * } finally {
|
|---|
| 13 | * mutex.release();
|
|---|
| 14 | * }
|
|---|
| 15 | * }
|
|---|
| 16 | *
|
|---|
| 17 | * criticalSection();
|
|---|
| 18 | * criticalSection(); // This call will wait until the first call releases the mutex.
|
|---|
| 19 | */
|
|---|
| 20 | declare class Mutex {
|
|---|
| 21 | private semaphore;
|
|---|
| 22 | /**
|
|---|
| 23 | * Checks if the mutex is currently locked.
|
|---|
| 24 | * @returns {boolean} True if the mutex is locked, false otherwise.
|
|---|
| 25 | *
|
|---|
| 26 | * @example
|
|---|
| 27 | * const mutex = new Mutex();
|
|---|
| 28 | * console.log(mutex.isLocked); // false
|
|---|
| 29 | * await mutex.acquire();
|
|---|
| 30 | * console.log(mutex.isLocked); // true
|
|---|
| 31 | * mutex.release();
|
|---|
| 32 | * console.log(mutex.isLocked); // false
|
|---|
| 33 | */
|
|---|
| 34 | get isLocked(): boolean;
|
|---|
| 35 | /**
|
|---|
| 36 | * Acquires the mutex, blocking if necessary until it is available.
|
|---|
| 37 | * @returns {Promise<void>} A promise that resolves when the mutex is acquired.
|
|---|
| 38 | *
|
|---|
| 39 | * @example
|
|---|
| 40 | * const mutex = new Mutex();
|
|---|
| 41 | * await mutex.acquire();
|
|---|
| 42 | * try {
|
|---|
| 43 | * // This code section cannot be executed simultaneously
|
|---|
| 44 | * } finally {
|
|---|
| 45 | * mutex.release();
|
|---|
| 46 | * }
|
|---|
| 47 | */
|
|---|
| 48 | acquire(): Promise<void>;
|
|---|
| 49 | /**
|
|---|
| 50 | * Releases the mutex, allowing another waiting task to proceed.
|
|---|
| 51 | *
|
|---|
| 52 | * @example
|
|---|
| 53 | * const mutex = new Mutex();
|
|---|
| 54 | * await mutex.acquire();
|
|---|
| 55 | * try {
|
|---|
| 56 | * // This code section cannot be executed simultaneously
|
|---|
| 57 | * } finally {
|
|---|
| 58 | * mutex.release(); // Allows another waiting task to proceed.
|
|---|
| 59 | * }
|
|---|
| 60 | */
|
|---|
| 61 | release(): void;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | export { Mutex };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.