-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initialized package
- Loading branch information
Showing
12 changed files
with
7,892 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"root": true, | ||
"parser": "@typescript-eslint/parser", | ||
"ignorePatterns": ["example/**", "/*.*"], | ||
"plugins": [ | ||
"@typescript-eslint" | ||
], | ||
"extends": [ | ||
"standard", | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/eslint-recommended", | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"rules": { | ||
"prefer-template": "error", | ||
"quotes": [ | ||
"error", | ||
"double" | ||
], | ||
"indent": [ | ||
"error", | ||
4 | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
], | ||
"space-before-function-paren": [ | ||
"error", | ||
"never" | ||
], | ||
"no-trailing-spaces": [ | ||
"error", | ||
{ | ||
"ignoreComments": true | ||
} | ||
], | ||
"@typescript-eslint/type-annotation-spacing": "error", | ||
"no-useless-return": "warn" | ||
} | ||
} |
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,3 @@ | ||
node_modules | ||
/docs | ||
/lib |
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 @@ | ||
14.17.3 |
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,56 @@ | ||
# ReentrantLock | ||
# ReentrantLock | ||
A lock designed for multi-threaded browser environments. | ||
|
||
## Installing | ||
<strike> `npm install @mogoe1/reentrant-lock` </strike> (we are not on npm yet) | ||
|
||
npm install --save git+https://github.com/mogoe1/reentrant-lock.git | ||
|
||
## Notes | ||
* The implementation relies on SharedArrayBuffers. Make sure your site meets the [security requirements](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements). | ||
* The implementation relies on Atomics.waitAsync which is implemented by [v8](https://v8.dev/features/atomics) but not documented at mdn yet. You might want to proivide a [polyfill](https://github.com/tc39/proposal-atomics-wait-async/blob/master/polyfill.js). | ||
|
||
## Quickstart | ||
The following example shows how a reentrant-lock can be used to make sure only one thrad at a time has access to a shared ressource. | ||
|
||
```js | ||
// file: main.js | ||
import { ReentrantLock } from "@mogoe1/reentrant-lock"; | ||
|
||
const lock = ReentrantLock.create(); | ||
const worker = new Worker("worker.js", { type: "module" }); | ||
|
||
lock.lockAsync().then(_ => { | ||
worker.postMessage(lock.buffer); | ||
|
||
// exclusive access to resource | ||
console.log("locked!"); | ||
lock.unlock(); | ||
console.log("unlocked"); | ||
}); | ||
``` | ||
|
||
```js | ||
// file: worker.js | ||
import { ReentrantLock } from "@mogoe1/reentrant-lock"; | ||
|
||
self.addEventListener("message", message => { | ||
const buffer = message.data; | ||
const lock = new ReentrantLock(new Int32Array(buffer)); | ||
|
||
let newlyLocked = lock.lock(); // this blocks the worker thread untill the main thread unlocks it's ReentrantLock instance! | ||
console.log(newlyLocked); // true | ||
newlyLocked = lock.lock(); // lock can be called multiple times without blocking more than once | ||
console.log(newlyLocked); // false | ||
|
||
// exclusive access to resource here | ||
|
||
let stillOwned = lock.unlock(); | ||
console.log(stillOwned); // true, because lock was called twice without unlock | ||
stillOwned = lock.unlock(); | ||
console.log(stillOwned); // false, because unlock was called as often as lock | ||
}); | ||
``` | ||
|
||
## API | ||
The API is documented at https://mogoe1.github.io/reentrant-lock/classes/ReentrantLock.html. |
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,3 @@ | ||
<body> | ||
<script type="module" src="example/main.js"></script> | ||
</body> |
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,14 @@ | ||
// file: main.js | ||
import { ReentrantLock } from "../lib/ReentrantLock.js"; | ||
|
||
const lock = ReentrantLock.create(); | ||
const worker = new Worker("./example/worker.js", { type: "module" }); | ||
|
||
lock.lockAsync().then(_ => { | ||
worker.postMessage(lock.buffer); | ||
|
||
// exclusive access to resource | ||
console.log("locked!"); | ||
lock.unlock(); | ||
console.log("unlocked"); | ||
}); |
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,18 @@ | ||
// file: worker.js | ||
import { ReentrantLock } from "../lib/ReentrantLock.js"; | ||
|
||
self.addEventListener("message", message => { | ||
const buffer = message.data; | ||
const lock = new ReentrantLock(new Int32Array(buffer)); | ||
|
||
lock.lock(); // this blocks the worker thread untill the main thread unlocks it's ReentrantLock instance! | ||
const newlyLocked = lock.lock(); // lock can be called multiple times without blocking more than once | ||
console.log(newlyLocked); // false, because this instance was locked prior to the previous line | ||
|
||
// exclusive access to resource | ||
|
||
let stillOwned = lock.unlock(); | ||
console.log(stillOwned); // true, because lock was called twice without unlock | ||
stillOwned = lock.unlock(); | ||
console.log(stillOwned); // false, because unlock was called as often as lock | ||
}); |
Oops, something went wrong.