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 3c1b288 commit 1d67c83
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/Lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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 { yieldMicro } from './utils';
import { ErrorAsyncLocksTimeout } from './errors';

class Lock {
Expand All @@ -28,7 +28,7 @@ class Lock {
--this._count;
release();
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
await sleep(0);
await yieldMicro();
},
this,
];
Expand Down
6 changes: 3 additions & 3 deletions src/RWLockReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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 { yieldMicro } from './utils';
import { ErrorAsyncLocksTimeout } from './errors';

/**
Expand Down Expand Up @@ -38,7 +38,7 @@ class RWLockReader {
this.release();
}
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
await sleep(0);
await yieldMicro();
},
this,
];
Expand All @@ -64,7 +64,7 @@ class RWLockReader {
release();
--this._writerCount;
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
await sleep(0);
await yieldMicro();
},
this,
];
Expand Down
16 changes: 5 additions & 11 deletions src/RWLockWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +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 { sleep, yieldMicro } from './utils';
import { ErrorAsyncLocksTimeout } from './errors';

/**
Expand All @@ -24,15 +24,9 @@ class RWLockWriter {
++this.readerCountBlocked;
if (timeout != null) {
let timedOut = false;
const timedOutP = new Promise<void>((resolve) => {
setTimeout(() => {
timedOut = true;
resolve();
}, timeout);
});
await Promise.race([
this.writersLock.waitForUnlock(),
timedOutP
sleep(timeout).then(() => { timedOut = true; })
]);
if (timedOut) {
--this.readerCountBlocked;
Expand Down Expand Up @@ -69,7 +63,7 @@ class RWLockWriter {
if (readerCount === 0) {
this.readersRelease();
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
await sleep(0);
await yieldMicro();
}
},
this,
Expand Down Expand Up @@ -103,7 +97,7 @@ class RWLockWriter {
writersRelease();
--this._writerCount;
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
await sleep(0);
await yieldMicro();
throw e;
}
return [
Expand All @@ -112,7 +106,7 @@ class RWLockWriter {
writersRelease();
--this._writerCount;
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
await sleep(0);
await yieldMicro();
},
this,
];
Expand Down
10 changes: 7 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
async function sleep(ms: number) {
return await new Promise((r) => setTimeout(r, ms));
async function sleep(ms: number): Promise<void> {
return await new Promise<void>((r) => setTimeout(r, ms));
}

export { sleep };
async function yieldMicro(): Promise<void> {
return await new Promise<void>((r) => queueMicrotask(r));
}

export { sleep, yieldMicro };

0 comments on commit 1d67c83

Please sign in to comment.