Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
CMCDragonkai committed Apr 1, 2022
1 parent a644cda commit 3c1b288
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 131 deletions.
9 changes: 6 additions & 3 deletions src/Lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import type { MutexInterface } from 'async-mutex';
import type { ResourceAcquire } from '@matrixai/resources';
import { Mutex, withTimeout } from 'async-mutex';
import { withF, withG } from '@matrixai/resources';
import { sleep } from './utils';
import { ErrorAsyncLocksTimeout } from './errors';

class Lock {
protected _lock: Mutex = new Mutex();
protected release: MutexInterface.Releaser;
protected _count: number = 0;

public lock(timeout?: number): ResourceAcquire<Lock> {
Expand All @@ -16,16 +16,19 @@ class Lock {
if (timeout != null) {
lock = withTimeout(this._lock, timeout, new ErrorAsyncLocksTimeout());
}
let release: MutexInterface.Releaser;
try {
this.release = await lock.acquire();
release = await lock.acquire();
} catch (e) {
--this._count;
throw e;
}
return [
async () => {
--this._count;
this.release();
release();
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
await sleep(0);
},
this,
];
Expand Down
15 changes: 5 additions & 10 deletions src/RWLockReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { MutexInterface } from 'async-mutex';
import type { ResourceAcquire } from '@matrixai/resources';
import { Mutex, withTimeout } from 'async-mutex';
import { withF, withG } from '@matrixai/resources';
import { sleep } from './utils';
import { ErrorAsyncLocksTimeout } from './errors';

/**
Expand All @@ -23,24 +24,21 @@ class RWLockReader {
lock = withTimeout(this.lock, timeout, new ErrorAsyncLocksTimeout());
}
try {
console.log('ATTEMPT READ');
this.release = await lock.acquire();
console.log('ACQUIRED READ');

} catch (e) {
console.log('READ LOCK TIMEOUT', e.name);
--this._readerCount;
throw e;
}
}
return [
async () => {
console.log('RELEASE READ LOCK');
const readerCount = --this._readerCount;
// The last reader unlocks
if (readerCount === 0) {
this.release();
}
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
await sleep(0);
},
this,
];
Expand All @@ -56,20 +54,17 @@ class RWLockReader {
}
let release: MutexInterface.Releaser;
try {
console.log('ATTEMPT WRITE');
release = await lock.acquire();
console.log('ACQUIRED WRITE');

} catch (e) {
console.log('WRITE LOCK TIMEOUT', e.name);
--this._writerCount;
throw e;
}
return [
async () => {
console.log('RELEASE WRITE LOCK');
release();
--this._writerCount;
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
await sleep(0);
},
this,
];
Expand Down
7 changes: 7 additions & 0 deletions src/RWLockWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ResourceAcquire } from '@matrixai/resources';
import { performance } from 'perf_hooks';
import { Mutex, withTimeout } from 'async-mutex';
import { withF, withG } from '@matrixai/resources';
import { sleep } from './utils';
import { ErrorAsyncLocksTimeout } from './errors';

/**
Expand Down Expand Up @@ -67,6 +68,8 @@ class RWLockWriter {
// The last reader unlocks
if (readerCount === 0) {
this.readersRelease();
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
await sleep(0);
}
},
this,
Expand Down Expand Up @@ -99,13 +102,17 @@ class RWLockWriter {
} catch (e) {
writersRelease();
--this._writerCount;
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
await sleep(0);
throw e;
}
return [
async () => {
this.readersRelease();
writersRelease();
--this._writerCount;
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
await sleep(0);
},
this,
];
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export { default as Lock } from './Lock';
export { default as RWLockReader } from './RWLockReader';
export { default as RWLockWriter } from './RWLockWriter';
export * as errors from './errors';
export * as utils from './utils';
export * as types from './types';
File renamed without changes.
12 changes: 6 additions & 6 deletions tests/Lock.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { withF, withG } from '@matrixai/resources';
import Lock from '@/Lock';
import * as utils from '@/utils';
import * as errors from '@/errors';
import * as testUtils from './utils';

describe(Lock.name, () => {
test('withF', async () => {
Expand Down Expand Up @@ -97,7 +97,7 @@ describe(Lock.name, () => {
let value;
const p1 = withF([lock.lock()], async () => {
value = 'p1';
await testUtils.sleep(100);
await utils.sleep(100);
});
const p2 = lock.waitForUnlock().then(() => {
value = 'p2';
Expand All @@ -124,12 +124,12 @@ describe(Lock.name, () => {
await Promise.all([
lock.withF(async () => {
const value_ = value + 1;
await testUtils.sleep(100);
await utils.sleep(100);
value = value_;
}),
lock.withF(async () => {
const value_ = value + 1;
await testUtils.sleep(100);
await utils.sleep(100);
value = value_;
}),
]);
Expand All @@ -139,7 +139,7 @@ describe(Lock.name, () => {
(async () => {
const g = lock.withG(async function* (): AsyncGenerator {
const value_ = value + 1;
await testUtils.sleep(100);
await utils.sleep(100);
value = value_;
return 'last';
});
Expand All @@ -149,7 +149,7 @@ describe(Lock.name, () => {
(async () => {
const g = lock.withG(async function* (): AsyncGenerator {
const value_ = value + 1;
await testUtils.sleep(100);
await utils.sleep(100);
value = value_;
return 'last';
});
Expand Down
Loading

0 comments on commit 3c1b288

Please sign in to comment.