Last active
June 22, 2022 02:14
-
-
Save CMCDragonkai/4de5c1526fc58dac259e321db8cf5331 to your computer and use it in GitHub Desktop.
Read Write Lock #javascript #typescript #concurrency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
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
Interesting, however I'm not sure if the solution is very robust because like you said it relies on a number of event loop ticks... I'm not even sure if that's why it does it though.