Simple, dependency-free Mutex tools for ES6.
npm install @debonet/es6mutex
es6mutex can be used as either a class or a library:
const Mutex = require( "es6mutex" );
const mutex = new Mutex();
async function f() {
await mutex.lock();
await wait(1000);
console.log( "Here" );
mutex.unlock();
}
// these will run serially, one per second
f();
f();
f();
f();
const Mutex = require( "es6mutex" );
async function sendAndReceive( serialport ) {
await Mutex.lock( serialport );
serialport.send( "some long message ");
let s = await serialport.read( );
mutex.unlock( serialport );
return s;
}
// these will run serially
sendAndReceive( serialport )
sendAndReceive( serialport )
const Mutex = require( "es6mutex" );
async function f( s ) {
await wait(1000);
console.log( s );
}
// these will run serially, one per second
Mutex.runExclusively( f, "Hello," );
Mutex.runExclusively( f, "World! );
const Mutex = require( "es6mutex" );
async function f( s ) {
await wait(1000);
console.log( s );
}
const exclusiveFunc = Mutex.makeExclusive ( f );
// these will run serially, one per second
exclusiveFunc( "Hello," ),
exclusiveFunc( "World!" ),
Returns a promise which is resolved when control over the mutex is obtained.
Use with await
const mutex = new Mutex();
await mutex.get();
// do whatever
``
or with Promise chains:
```javascript
mutex.get().then( /* do whatever */ );
Releases control over the mutex object allowing the next tasks waiting for control to proceed.
const mutex = new Mutex();
await mutex.get();
// do whatever
mutex.release();
``
### Mutex.prototpye.runExclusively( f, ...args )
### Mutex.prototpye.criticalSection( f, ...args )
### Mutex.prototpye.run( f, ...args )
### Mutex.prototpye.fpRunExclusively( f, ...args )
Gets control over the mutex, waits for resolution of the passed in function `f` called with the arguments `...args`, releases the mutex.
Note, with this form, the lock is bound to the mutex instance, so the function could be called from elsewhere without the lock or with a different lock.
```javascript
mutex.run( somePromiseFunction, arg1, arg2 );
// do whatever
``
## Library API
### Mutex.get( o )
### Mutex.lock( o )
### Mutex.fpGet( o )
Links a mutex to the object `o`, and returns a promise which is resolved
when control over the mutex is obtained.
Use with await
```javascript
const o = {};
await Mutex.get( o );
// do whatever
``
or with Promise chains:
```javascript
const o = {};
mutex.get( o ).then( /* do whatever */ );
Unlocks a mutex which has been bound to the given object, immediately allowing any tasks waiting for control of that mutex to proceed.
const o = {};
await Mutex.get( o );
// do whatever
Mutex.release( o );
Binds a mutex to the given function if one has not already been attached, and then waits for control before executing the function with the provided args
Mutex.run( somePromiseFunction, arg1, arg2 );
Mutex.run( somePromiseFunction, arg1, arg2 );
Returns a version of the provided function which can only be run exclusively (non-reentrant)
const f = Mutex.makeExclusive( somePromiseFunction );
f( arg1, arg2 );
f( arg1, arg2 );