Skip to content

WIP: Web Locks API initial implementation#417

Open
tshemsedinov wants to merge 3 commits intomasterfrom
web-losks-api
Open

WIP: Web Locks API initial implementation#417
tshemsedinov wants to merge 3 commits intomasterfrom
web-losks-api

Conversation

@tshemsedinov
Copy link
Copy Markdown
Member

Refs: #416

@belochub
Copy link
Copy Markdown
Member

@tshemsedinov isn't it supposed to be compliant with the spec for Web Locks API? It defines two interfaces: LockManager and Lock, both of which are not present in the implementation in this PR. Thus I don't think it should be called 'Web Locks API implementation' unless you implement the interfaces mentioned above.

@tshemsedinov
Copy link
Copy Markdown
Member Author

Yes, but it's just proof of concept and WIP. @belochub I'll implement also
options: { mode: "exclusive" | "shared", signal <AbortSignal>, ifAvailable <boolean> } and extend specification with timeout, recursive locking and locking queue in each thread, error handling in critical section, etc.

Comment thread lib/locks.js Outdated
Comment thread lib/locks.js Outdated
Comment thread lib/locks.js
const prev = Atomics.exchange(this.flag, 0, LOCKED);
if (prev === LOCKED) return;
this.owner = true;
this.trying = false;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this.trying needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use mutex.queue.length > 0 instead

Comment thread lib/locks.js
return this.tryEnter();
}

tryEnter() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can merge tryEnter and enterIfAvailable by adding lock argument and a few checks?

Comment thread lib/locks.js Outdated
enterIfAvailable(lock) {
if (this.owner) return lock.callback();
const prev = Atomics.exchange(this.flag, 0, LOCKED);
if (prev === LOCKED) return lock.callback();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://wicg.github.io/web-locks/#api-lock-manager-request when ifAvailable is true the callback must be passed a lock (boolean) argument that signifies whether it got the lock or not.

Comment thread lib/locks.js
@tshemsedinov tshemsedinov force-pushed the web-losks-api branch 4 times, most recently from 465e50b to 18bcd04 Compare April 8, 2019 22:17
@tshemsedinov
Copy link
Copy Markdown
Member Author

In new implementation:

  • Mutex class is merged into Lock class
  • Added: AbortError, AbortSignal, AbortController

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants