2020-09-03 12:12:33 +02:00
|
|
|
/*
|
|
|
|
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-17 11:58:14 +01:00
|
|
|
private _promise?: Promise<void>;
|
|
|
|
private _resolve?: (() => void);
|
2020-09-03 12:12:33 +02:00
|
|
|
|
2021-11-16 10:36:19 +01:00
|
|
|
tryTake(): boolean {
|
2020-09-03 12:12:33 +02:00
|
|
|
if (!this._promise) {
|
|
|
|
this._promise = new Promise(resolve => {
|
|
|
|
this._resolve = resolve;
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-16 10:36:19 +01:00
|
|
|
async take(): Promise<void> {
|
2020-11-10 11:04:53 +01:00
|
|
|
while(!this.tryTake()) {
|
|
|
|
await this.released();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 10:36:19 +01:00
|
|
|
get isTaken(): boolean {
|
2020-09-03 12:12:33 +02:00
|
|
|
return !!this._promise;
|
|
|
|
}
|
|
|
|
|
2021-11-16 10:36:19 +01:00
|
|
|
release(): void {
|
2020-09-03 12:12:33 +02:00
|
|
|
if (this._resolve) {
|
2021-11-17 11:58:14 +01:00
|
|
|
this._promise = undefined;
|
2020-09-03 12:12:33 +02:00
|
|
|
const resolve = this._resolve;
|
2021-11-17 11:58:14 +01:00
|
|
|
this._resolve = undefined;
|
2020-09-03 12:12:33 +02:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 11:58:14 +01:00
|
|
|
released(): Promise<void> | undefined {
|
2020-09-03 12:12:33 +02:00
|
|
|
return this._promise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 15:04:45 +01:00
|
|
|
export class MultiLock {
|
2021-11-16 10:36:19 +01:00
|
|
|
|
|
|
|
constructor(public readonly locks: Lock[]) {
|
2021-03-01 15:04:45 +01:00
|
|
|
}
|
|
|
|
|
2021-11-16 10:36:19 +01:00
|
|
|
release(): void {
|
2021-03-01 15:04:45 +01:00
|
|
|
for (const lock of this.locks) {
|
|
|
|
lock.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 12:12:33 +02:00
|
|
|
export function tests() {
|
|
|
|
return {
|
|
|
|
"taking a lock twice returns false": assert => {
|
|
|
|
const lock = new Lock();
|
2020-11-10 11:04:53 +01:00
|
|
|
assert.equal(lock.tryTake(), true);
|
2020-09-03 12:12:33 +02:00
|
|
|
assert.equal(lock.isTaken, true);
|
2020-11-10 11:04:53 +01:00
|
|
|
assert.equal(lock.tryTake(), false);
|
2020-09-03 12:12:33 +02:00
|
|
|
},
|
|
|
|
"can take a released lock again": assert => {
|
|
|
|
const lock = new Lock();
|
2020-11-10 11:04:53 +01:00
|
|
|
lock.tryTake();
|
2020-09-03 12:12:33 +02:00
|
|
|
lock.release();
|
|
|
|
assert.equal(lock.isTaken, false);
|
2020-11-10 11:04:53 +01:00
|
|
|
assert.equal(lock.tryTake(), true);
|
2020-09-03 12:12:33 +02:00
|
|
|
},
|
|
|
|
"2 waiting for lock, only first one gets it": async assert => {
|
|
|
|
const lock = new Lock();
|
2020-11-10 11:04:53 +01:00
|
|
|
lock.tryTake();
|
2020-09-03 12:12:33 +02:00
|
|
|
|
2020-09-03 12:17:01 +02:00
|
|
|
let first;
|
2021-11-16 10:36:19 +01:00
|
|
|
lock.released()!.then(() => first = lock.tryTake());
|
2020-09-03 12:17:01 +02:00
|
|
|
let second;
|
2021-11-16 10:36:19 +01:00
|
|
|
lock.released()!.then(() => second = lock.tryTake());
|
2020-09-03 12:12:33 +02:00
|
|
|
const promise = lock.released();
|
|
|
|
lock.release();
|
|
|
|
await promise;
|
2020-09-03 12:17:01 +02:00
|
|
|
assert.strictEqual(first, true);
|
|
|
|
assert.strictEqual(second, false);
|
2020-09-03 12:12:33 +02:00
|
|
|
},
|
|
|
|
"await non-taken lock": async assert => {
|
|
|
|
const lock = new Lock();
|
|
|
|
await lock.released();
|
|
|
|
assert(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|