import { Awaitable } from 'justypes' import { once } from 'lodash-es' import { Deferred } from '@classes/deferred.js' import { UniqueQueue } from '@blackglory/structures' import { go } from '@blackglory/go' type Release = () => void export class KeyedMutex { protected lockedKeys: Set = new Set() private keyToUnlockDeferredQueue: Map>> = new Map() constructor(protected createKey: (val: T) => U) {} acquire(val: T, handler: () => Awaitable): Promise acquire(val: T): Promise async acquire(val: T, handler?: () => Awaitable) { const key = this.createKey(val) await this.lock(key) if (handler) { try { return await handler() } finally { this.unlock(key) } } else { return once(() => this.unlock(key)) } } // 虽然是异步函数, 但在无锁情况下, 由于循环体内的异步代码没有执行, 函数实际上会同步返回. private async lock(key: U): Promise { while (!this.tryLock(key)) await this.waitForUnlock(key) } private tryLock(key: U): boolean { if (this.lockedKeys.has(key)) { return false } else { this.lockedKeys.add(key) return true } } private async waitForUnlock(key: U): Promise { const queue = go(() => { const queue = this.keyToUnlockDeferredQueue.get(key) if (queue) { return queue } else { const queue = new UniqueQueue>() this.keyToUnlockDeferredQueue.set(key, queue) return queue } }) const unlockDeferred = new Deferred() queue.enqueue(unlockDeferred) try { await unlockDeferred } finally { queue.remove(unlockDeferred) if (queue.size === 0) { this.keyToUnlockDeferredQueue.delete(key) } } } private unlock(key: U): void { this.lockedKeys.delete(key) const queue = this.keyToUnlockDeferredQueue.get(key) if (queue) { queue.dequeue()?.resolve() if (queue.size === 0) { this.keyToUnlockDeferredQueue.delete(key) } } } }