Skip to content

Commit 2bc30d7

Browse files
committed
feat(mutex): add lockWith and tryLockWith definitions
start #2
1 parent ed9ab74 commit 2bc30d7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/mutex/mutex.ts

+32
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export class Mutex implements Synchronizer {
3131
/**
3232
* Locks this mutex
3333
*
34+
* [lockWith]{@link lockWith} is the preferred choice.
35+
*
3436
* @throws {ConcurrencyInterruptedException} when the mutex is interrupted
3537
* @returns a promise when the lock has been set
3638
*/
@@ -43,6 +45,8 @@ export class Mutex implements Synchronizer {
4345
*
4446
* Throws an error if the given time exceeds
4547
*
48+
* [tryLockWith]{@link tryLockWith} is the preferred choice.
49+
*
4650
* @param timeout maximum time (in ms) to lock
4751
* @throws {ConcurrencyExceedTimeoutException} when the time limit exceeds
4852
* @throws {ConcurrencyInterruptedException} when the mutex is interrupted
@@ -54,11 +58,39 @@ export class Mutex implements Synchronizer {
5458

5559
/**
5660
* Unlocks this mutex
61+
*
62+
* [lockWith]{@link lockWith} or [tryLockWith]{@link tryLockWith} are the preferred choices.
5763
*/
5864
public unlock() {
5965
this.semaphore.release();
6066
}
6167

68+
/**
69+
* Locks this mutex and run the critical section function
70+
* Then, automatically unlocks it.
71+
*
72+
* @param cs function to run once the locked is acquired (critical section)
73+
* @throws {ConcurrencyInterruptedException} when the mutex is interrupted
74+
* @returns a Promise after the mutex locked and unlocked and what has been return from `fn`
75+
*/
76+
public lockWith<T = void>(cs: () => Promise<T> | T): Promise<T> {
77+
return Promise.reject(new Error("Not implemented"));
78+
}
79+
80+
/**
81+
* Locks this mutex within a time limit and run the critical section function
82+
* Then, automatically unlocks it.
83+
*
84+
* @param timeout maximum time (in ms) to lock
85+
* @param cs function to run once the locked is acquired (critical section)
86+
* @throws {ConcurrencyExceedTimeoutException} when the time limit exceeds
87+
* @throws {ConcurrencyInterruptedException} when the mutex is interrupted
88+
* @returns a Promise after the mutex locked and unlocked and what has been return from `fn`
89+
*/
90+
public tryLockWith<T = void>(timeout: number, cs: () => Promise<T> | T): Promise<T> {
91+
return Promise.reject(new Error("Not implemented"));
92+
}
93+
6294
/**
6395
* Interrupts all awaiting "Threads" with an [exception]{@link ConcurrencyInterruptedException}.
6496
*

0 commit comments

Comments
 (0)