Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dimaamega/asynchronous-concurrency-primitives-js
Implementation of asynchronous Barrier, Mutex, RWMutex in javascript
https://github.com/dimaamega/asynchronous-concurrency-primitives-js
asyncronous coroutines javascript nodejs promise
Last synced: 2 days ago
JSON representation
Implementation of asynchronous Barrier, Mutex, RWMutex in javascript
- Host: GitHub
- URL: https://github.com/dimaamega/asynchronous-concurrency-primitives-js
- Owner: DimaAmega
- Created: 2021-08-18T22:27:42.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-07-28T08:13:43.000Z (over 1 year ago)
- Last Synced: 2024-10-12T16:08:52.467Z (about 1 month ago)
- Topics: asyncronous, coroutines, javascript, nodejs, promise
- Language: JavaScript
- Homepage:
- Size: 72.3 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# asynchronous concurrency primitives js
[![Node.js CI](https://github.com/DimaAmega/asynchronous-mutex-js/actions/workflows/tests.js.yml/badge.svg)](https://github.com/DimaAmega/asynchronous-mutex-js/actions/workflows/tests.js.yml)[![Open in Codeflow](https://developer.stackblitz.com/img/open_in_codeflow.svg)](https:///pr.new/DimaAmega/asynchronous-concurrency-primitives-js
)There are a few asynchronous concurrency primitives js:
- [barrier.js](./examples/simple-barrier.js)
- [mutex.js](./examples/simple-mutex.js)
- [rwmutex.js](./tests/rwmutex.test.js#L71)Also see [tests](./tests)
### Get started:
deps:
```shell
npm i
```run tests:
```shell
npm test
```### Barrier
```js
const Coroutines = [0, 1, 2, 3].map((n) => `coroutine ${n}`)// Global variables
const b = new Barrier()
const c = new ColorLog();(async () => {
c.log(`Lock barrier`, Coroutines[0])
await b.Lock()
c.log(`Done`, Coroutines[0])
})();(async () => {
c.log(`Sleep 1s ...`, Coroutines[1])
await sleep(1)
b.UnLock()
c.log(`barrier UnLock was invoked :)`, Coroutines[1])
})()
```### Mutex
```js
// setup
const CoroutineS_NUMBER = 3
const Coroutines = [...Array(CoroutineS_NUMBER).keys()].map(
(n) => `coroutine ${n}`
)
const sleepTimes = [...Array(CoroutineS_NUMBER).keys()]// Global variables
const m = new Mutex()
const c = new ColorLog()const CreateWorker = (sleepTime, workerNumber) => async () => {
c.log(`Lock mutex`, Coroutines[workerNumber])
await m.Lock()
c.log(`In CS`, Coroutines[workerNumber])
c.log(`Sleep ${sleepTime}s ...`, Coroutines[workerNumber])
await sleep(sleepTime)
c.log(`Out CS`, Coroutines[workerNumber])
m.UnLock()
c.log(`mutex UnLock was invoked :)`, Coroutines[workerNumber])
c.log(`Done`, Coroutines[workerNumber])
};(async () => {
const promises = sleepTimes
// create workers
.map((sleepTime, workerNumber) => CreateWorker(sleepTime, workerNumber))
// execute workers
.map((worker) => worker())
// wait all tasks
await Promise.all(promises)
})()
```