-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web Locks API initial implementation
Refs: #416
- Loading branch information
1 parent
f752f0d
commit aa2f980
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
dist | ||
locks.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
'use strict'; | ||
|
||
const { Worker, isMainThread, parentPort } = require('worker_threads'); | ||
|
||
const threads = new Set(); | ||
|
||
const LOCKED = 0; | ||
const UNLOCKED = 1; | ||
|
||
const sendMessage = message => { | ||
if (isMainThread) { | ||
for (const thread of threads) { | ||
thread.worker.postMessage(message); | ||
} | ||
} else { | ||
parentPort.postMessage(message); | ||
} | ||
}; | ||
|
||
class Mutex { | ||
constructor(resourceName, shared, initial = false) { | ||
this.resourceName = resourceName; | ||
this.lock = new Int32Array(shared, 0, 1); | ||
if (initial) Atomics.store(this.lock, 0, UNLOCKED); | ||
this.owner = false; | ||
this.trying = false; | ||
this.callback = null; | ||
} | ||
|
||
enter(callback) { | ||
this.callback = callback; | ||
this.trying = true; | ||
this.tryEnter(); | ||
} | ||
|
||
tryEnter() { | ||
if (!this.callback) return; | ||
const prev = Atomics.exchange(this.lock, 0, LOCKED); | ||
if (prev === UNLOCKED) { | ||
this.owner = true; | ||
this.trying = false; | ||
this.callback(this).then(() => { | ||
this.leave(); | ||
}); | ||
this.callback = null; | ||
} | ||
} | ||
|
||
leave() { | ||
if (!this.owner) return; | ||
Atomics.store(this.lock, 0, UNLOCKED); | ||
this.owner = false; | ||
sendMessage({ kind: 'leave', resourceName: this.resourceName }); | ||
} | ||
} | ||
|
||
const resources = new Map(); | ||
|
||
const request = (resourceName, callback) => { | ||
let lock = resources.get(resourceName); | ||
if (!lock) { | ||
const buffer = new SharedArrayBuffer(4); | ||
lock = new Mutex(resourceName, buffer, true); | ||
resources.set(resourceName, lock); | ||
sendMessage({ kind: 'create', resourceName, buffer }); | ||
} | ||
lock.enter(callback); | ||
return lock; | ||
}; | ||
|
||
const receiveMessage = message => { | ||
const { kind, resourceName, buffer } = message; | ||
if (kind === 'create') { | ||
const lock = new Mutex(resourceName, buffer); | ||
resources.set(resourceName, lock); | ||
} | ||
}; | ||
|
||
if (!isMainThread) { | ||
parentPort.on('message', receiveMessage); | ||
} | ||
|
||
class Thread { | ||
constructor() { | ||
const worker = new Worker(__filename); | ||
this.worker = worker; | ||
threads.add(this); | ||
worker.on('message', message => { | ||
for (const thread of threads) { | ||
if (thread.worker !== worker) { | ||
thread.worker.postMessage(message); | ||
} | ||
} | ||
receiveMessage(message); | ||
}); | ||
} | ||
} | ||
|
||
const locks = { resources, request, sendMessage, receiveMessage, Thread }; | ||
|
||
module.exports = { locks }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters