diff --git a/lock.ts b/lock.ts index 2b5d955..7afa1ee 100644 --- a/lock.ts +++ b/lock.ts @@ -1,4 +1,4 @@ -import { Mutex } from "./mutex.ts"; +import { RawSemaphore } from "./_raw_semaphore.ts"; /** * A mutual exclusion lock that provides safe concurrent access to a shared value. @@ -16,7 +16,7 @@ import { Mutex } from "./mutex.ts"; * ``` */ export class Lock { - #mu = new Mutex(); + #sem = new RawSemaphore(1); #value: T; /** @@ -32,7 +32,7 @@ export class Lock { * Returns true if the lock is currently locked, false otherwise. */ get locked(): boolean { - return this.#mu.locked; + return this.#sem.locked; } /** @@ -43,11 +43,11 @@ export class Lock { * @returns A Promise that resolves with the result of the function. */ async lock(fn: (value: T) => R | PromiseLike): Promise { - const lock = await this.#mu.acquire(); + await this.#sem.acquire(); try { return await fn(this.#value); } finally { - lock[Symbol.dispose](); + this.#sem.release(); } } }