Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed mutex issue #80

Merged
merged 18 commits into from
Jun 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions src/mutex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ import { ErrnoError, Errno } from './error.js';
* @internal
*/
export class Mutex {
protected locks: Map<string, (() => void)[]> = new Map();
protected locks: Map<string, { isLocked: boolean; queue: (() => void)[] }> = new Map();
terryluan12 marked this conversation as resolved.
Show resolved Hide resolved

public lock(path: string): Promise<void> {
if (!this.locks.has(path)) {
this.locks.set(path, { isLocked: false, queue: [] });
terryluan12 marked this conversation as resolved.
Show resolved Hide resolved
}
const queue = this.locks.get(path);

return new Promise(resolve => {
if (this.locks.has(path)) {
this.locks.get(path)!.push(resolve);
} else {
this.locks.set(path, [resolve]);
}
queue!.queue.push(resolve);
james-pre marked this conversation as resolved.
Show resolved Hide resolved
this.dispatch(path);
});
}

public unlock(path: string): void {
if (!this.locks.has(path)) {
throw new ErrnoError(Errno.EPERM, 'Can not unlock an already unlocked path', path);
}

const next = this.locks.get(path)?.shift();
this.locks.get(path)!.isLocked = false;
james-pre marked this conversation as resolved.
Show resolved Hide resolved
/*
don't unlock - we want to queue up next for the
end of the current task execution, but we don't
Expand All @@ -31,24 +32,34 @@ export class Mutex {
behavior that an unlock immediately followed by a
lock won't cause starvation.
*/
if (next) {
setTimeout(next);
return;
}

this.locks.delete(path);
setTimeout(() => this.dispatch(path));
}

public tryLock(path: string): boolean {
if (this.locks.has(path)) {
return false;
}

this.locks.set(path, []);
this.locks.set(path, { isLocked: false, queue: [] });
terryluan12 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

public isLocked(path: string): boolean {
return this.locks.has(path);
}

private dispatch(path: string) {
const queue = this.locks.get(path);
if (queue!.isLocked) {
terryluan12 marked this conversation as resolved.
Show resolved Hide resolved
return;
}

const next = queue!.queue.shift();
terryluan12 marked this conversation as resolved.
Show resolved Hide resolved
if (!next) {
return;
}

queue!.isLocked = true;
terryluan12 marked this conversation as resolved.
Show resolved Hide resolved
next();
}
}
Loading