hydrogen-web/src/utils/Lock.ts

103 lines
2.7 KiB
TypeScript
Raw Normal View History

/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
export class Lock {
2021-11-16 15:06:19 +05:30
private _promise: Promise<void> | null = null;
private _resolve: (() => void) | null = null;
2021-11-16 15:06:19 +05:30
tryTake(): boolean {
if (!this._promise) {
this._promise = new Promise(resolve => {
this._resolve = resolve;
});
return true;
}
return false;
}
2021-11-16 15:06:19 +05:30
async take(): Promise<void> {
while(!this.tryTake()) {
await this.released();
}
}
2021-11-16 15:06:19 +05:30
get isTaken(): boolean {
return !!this._promise;
}
2021-11-16 15:06:19 +05:30
release(): void {
if (this._resolve) {
this._promise = null;
const resolve = this._resolve;
this._resolve = null;
resolve();
}
}
2021-11-16 15:06:19 +05:30
released(): Promise<void> | null {
return this._promise;
}
}
export class MultiLock {
2021-11-16 15:06:19 +05:30
constructor(public readonly locks: Lock[]) {
}
2021-11-16 15:06:19 +05:30
release(): void {
for (const lock of this.locks) {
lock.release();
}
}
}
export function tests() {
return {
"taking a lock twice returns false": assert => {
const lock = new Lock();
assert.equal(lock.tryTake(), true);
assert.equal(lock.isTaken, true);
assert.equal(lock.tryTake(), false);
},
"can take a released lock again": assert => {
const lock = new Lock();
lock.tryTake();
lock.release();
assert.equal(lock.isTaken, false);
assert.equal(lock.tryTake(), true);
},
"2 waiting for lock, only first one gets it": async assert => {
const lock = new Lock();
lock.tryTake();
2020-09-03 15:47:01 +05:30
let first;
2021-11-16 15:06:19 +05:30
lock.released()!.then(() => first = lock.tryTake());
2020-09-03 15:47:01 +05:30
let second;
2021-11-16 15:06:19 +05:30
lock.released()!.then(() => second = lock.tryTake());
const promise = lock.released();
lock.release();
await promise;
2020-09-03 15:47:01 +05:30
assert.strictEqual(first, true);
assert.strictEqual(second, false);
},
"await non-taken lock": async assert => {
const lock = new Lock();
await lock.released();
assert(true);
}
}
}