Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active June 22, 2022 02:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CMCDragonkai/4de5c1526fc58dac259e321db8cf5331 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/4de5c1526fc58dac259e321db8cf5331 to your computer and use it in GitHub Desktop.
Read Write Lock #javascript #typescript #concurrency
import type { MutexInterface } from 'async-mutex';
import { Mutex } from 'async-mutex';
/**
* Single threaded read-preferring read write lock
*/
class RWLock {
protected _readerCount: number = 0;
protected _writerCount: number = 0;
protected lock: Mutex = new Mutex();
protected release: MutexInterface.Releaser;
public get readerCount(): number {
return this._readerCount;
}
public get writerCount(): number {
return this._writerCount;
}
public async withRead<T>(f: () => Promise<T>): Promise<T> {
const release = await this.acquireRead();
try {
return await f();
} finally {
release();
}
}
public async withWrite<T>(f: () => Promise<T>): Promise<T> {
const release = await this.acquireWrite();
try {
return await f();
} finally {
release();
}
}
public async acquireRead(): Promise<() => void> {
const readerCount = ++this._readerCount;
// The first reader locks
if (readerCount === 1) {
this.release = await this.lock.acquire();
}
return () => {
const readerCount = --this._readerCount;
// The last reader unlocks
if (readerCount === 0) {
this.release();
}
};
}
public async acquireWrite(): Promise<() => void> {
++this._writerCount;
this.release = await this.lock.acquire();
return () => {
--this._writerCount;
this.release();
};
}
public isLocked(): boolean {
return this.lock.isLocked();
}
public async waitForUnlock(): Promise<void> {
return this.lock.waitForUnlock();
}
}
import type { MutexInterface } from 'async-mutex';
import { Mutex } from 'async-mutex';
/**
* Single threaded write-preferring read write lock
*/
class RWLock {
protected readersLock: Mutex = new Mutex();
protected writersLock: Mutex = new Mutex();
protected readersRelease: MutexInterface.Releaser;
protected readerCountBlocked: number = 0;
protected _readerCount: number = 0;
protected _writerCount: number = 0;
public get readerCount(): number {
return this._readerCount + this.readerCountBlocked;
}
public get writerCount(): number {
return this._writerCount;
}
public async withRead<T>(f: () => Promise<T>): Promise<T> {
const release = await this.acquireRead();
try {
return await f();
} finally {
release();
}
}
public async withWrite<T>(f: () => Promise<T>): Promise<T> {
const release = await this.acquireWrite();
try {
return await f();
} finally {
release();
}
}
public async acquireRead(): Promise<() => void> {
if (this._writerCount > 0) {
++this.readerCountBlocked;
await this.writersLock.waitForUnlock();
--this.readerCountBlocked;
}
const readerCount = ++this._readerCount;
// The first reader locks
if (readerCount === 1) {
this.readersRelease = await this.readersLock.acquire();
}
return () => {
const readerCount = --this._readerCount;
// The last reader unlocks
if (readerCount === 0) {
this.readersRelease();
}
};
}
public async acquireWrite(): Promise<() => void> {
++this._writerCount;
const writersRelease = await this.writersLock.acquire();
this.readersRelease = await this.readersLock.acquire();
return () => {
this.readersRelease();
writersRelease();
--this._writerCount;
};
}
public isLocked(): boolean {
return this.readersLock.isLocked() || this.writersLock.isLocked();
}
public async waitForUnlock(): Promise<void> {
await Promise.all([
this.readersLock.waitForUnlock(),
this.writersLock.waitForUnlock()
]);
return;
}
}
@CMCDragonkai
Copy link
Author

CMCDragonkai commented Mar 28, 2022

I've turned this into a package: https://github.com/MatrixAI/js-async-locks. Bug fixes and new features go to the package. Don't use this snippet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment