-
-
Notifications
You must be signed in to change notification settings - Fork 13
Locking timeout #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Locking timeout #24
Changes from 1 commit
b46ab15
07c7387
f9099d4
364e6b7
ed212e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,21 +11,20 @@ const UNLOCKED = 1; | |
| let locks = null; // LockManager instance | ||
|
|
||
| class Lock { | ||
| constructor({ name, mode = 'exclusive', buffer = null, timeout = null }) { | ||
| constructor(name, mode = 'exclusive', buffer = null) { | ||
| this.name = name; | ||
| this.mode = mode; // 'exclusive' or 'shared' | ||
| this.queue = []; | ||
| this.owner = false; | ||
| this.trying = false; | ||
| this.timeout = timeout; | ||
| this.buffer = buffer ? buffer : new SharedArrayBuffer(4); | ||
| this.flag = new Int32Array(this.buffer, 0, 1); | ||
| if (!buffer) Atomics.store(this.flag, 0, UNLOCKED); | ||
| } | ||
|
|
||
| enter(handler) { | ||
| enter(handler, timeout) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That it much better, but we need to compare passing timeout and timer instance.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hmm, maybe it would be better to throw an error on timeout, similar to abort behavior? |
||
| return new Promise(resolve => { | ||
| this.queue.push({ handler, resolve }); | ||
| this.queue.push({ handler, resolve, timeout }); | ||
| this.trying = true; | ||
| setTimeout(() => { | ||
| this.tryEnter(); | ||
|
|
@@ -39,15 +38,15 @@ class Lock { | |
| if (prev === LOCKED) return; | ||
| this.owner = true; | ||
| this.trying = false; | ||
| const { handler, resolve } = this.queue.shift(); | ||
| const { handler, resolve, timeout } = this.queue.shift(); | ||
|
|
||
| const endWork = () => { | ||
|
anton-iskryzhytskyi marked this conversation as resolved.
Outdated
|
||
| this.leave(); | ||
| resolve(); | ||
| }; | ||
|
|
||
| if (typeof this.timeout === 'number') { | ||
| setTimeout(endWork, this.timeout); | ||
| if (timeout) { | ||
| setTimeout(endWork, timeout); | ||
| } | ||
| handler(this).finally(endWork); | ||
|
anton-iskryzhytskyi marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
@@ -100,14 +99,14 @@ class LockManager { | |
|
|
||
| let lock = this.collection.get(name); | ||
| if (!lock) { | ||
| lock = new Lock({ name, mode, timeout }); | ||
| lock = new Lock(name, mode); | ||
| this.collection.set(name, lock); | ||
| const { buffer } = lock; | ||
| const message = { webLocks: true, kind: 'create', name, mode, buffer }; | ||
| locks.send(message); | ||
| } | ||
|
|
||
| const finished = lock.enter(handler); | ||
| const finished = lock.enter(handler, timeout); | ||
| let aborted = null; | ||
| if (signal) { | ||
| aborted = new Promise((resolve, reject) => { | ||
|
|
@@ -154,9 +153,9 @@ class LockManager { | |
|
|
||
| receive(message) { | ||
| if (!message.webLocks) return; | ||
| const { kind, name, mode, buffer, timeout } = message; | ||
| const { kind, name, mode, buffer } = message; | ||
| if (kind === 'create') { | ||
| const lock = new Lock({ name, mode, buffer, timeout }); | ||
| const lock = new Lock(name, mode, buffer); | ||
| this.collection.set(name, lock); | ||
| return; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.