Skip to content

Commit

Permalink
feat(mutex): add lockWith and tryLockWith definitions
Browse files Browse the repository at this point in the history
start #2
  • Loading branch information
HugoMendes98 committed Jun 22, 2023
1 parent ed9ab74 commit 2bc30d7
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/mutex/mutex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class Mutex implements Synchronizer {
/**
* Locks this mutex
*
* [lockWith]{@link lockWith} is the preferred choice.
*
* @throws {ConcurrencyInterruptedException} when the mutex is interrupted
* @returns a promise when the lock has been set
*/
Expand All @@ -43,6 +45,8 @@ export class Mutex implements Synchronizer {
*
* Throws an error if the given time exceeds
*
* [tryLockWith]{@link tryLockWith} is the preferred choice.
*
* @param timeout maximum time (in ms) to lock
* @throws {ConcurrencyExceedTimeoutException} when the time limit exceeds
* @throws {ConcurrencyInterruptedException} when the mutex is interrupted
Expand All @@ -54,11 +58,39 @@ export class Mutex implements Synchronizer {

/**
* Unlocks this mutex
*
* [lockWith]{@link lockWith} or [tryLockWith]{@link tryLockWith} are the preferred choices.
*/
public unlock() {
this.semaphore.release();
}

/**
* Locks this mutex and run the critical section function
* Then, automatically unlocks it.
*
* @param cs function to run once the locked is acquired (critical section)
* @throws {ConcurrencyInterruptedException} when the mutex is interrupted
* @returns a Promise after the mutex locked and unlocked and what has been return from `fn`
*/
public lockWith<T = void>(cs: () => Promise<T> | T): Promise<T> {
return Promise.reject(new Error("Not implemented"));
}

/**
* Locks this mutex within a time limit and run the critical section function
* Then, automatically unlocks it.
*
* @param timeout maximum time (in ms) to lock
* @param cs function to run once the locked is acquired (critical section)
* @throws {ConcurrencyExceedTimeoutException} when the time limit exceeds
* @throws {ConcurrencyInterruptedException} when the mutex is interrupted
* @returns a Promise after the mutex locked and unlocked and what has been return from `fn`
*/
public tryLockWith<T = void>(timeout: number, cs: () => Promise<T> | T): Promise<T> {
return Promise.reject(new Error("Not implemented"));
}

/**
* Interrupts all awaiting "Threads" with an [exception]{@link ConcurrencyInterruptedException}.
*
Expand Down

0 comments on commit 2bc30d7

Please sign in to comment.