co-mutex
Mutex primitive for generator flow control.
Installation
Node.js
co-mutex
is available through npm:
npm install co-mutex
Example
var co = ;var Mutex = Mutex; var queue = ;var DONE = false; /** * Produce a line of data simulating async when * mutex has given the producer access. Wait * if the queue has reached its high watermark. * * @param */ { while true mutex; if DONE break; } /** * Consume a line of data simulating async when * the mutex has given the consumer access. Wait * if the queue is empty. * * @param */ { while true mutex; if DONE && !queuelength break; } /** * Create a mutex to be shared between the * consumer and the producer. Spawn the coroutines * then wait 10 seconds before signalling shutdown. */ ; /*! * Random data generator */ { return Math * 10;}
Usage
Mutex implementation that uses a closure to ensure
of lock release on error. Can use optional Condvar
to drop then re-aquire lock on future state change.
.locked
Get the lock status of this mutex.
- @return {Boolean} locked
if mutexlocked // ...
.lock(blk)
Acquire access to mutex resources in closure. Will release access on return or error before forwarding result to yield.
- @param {Generator} closure*(cond)
var result = mutex;
.acquire()
Acquire access and lock the mutex. Cannot safegaurd against deadlock scenarios.
mutex;
.release()
Relase access and unlock the mutex. Cannot safegaurd against deadlock scenarios.
mutex;
License
(The MIT License)
Copyright (c) 2014 Jake Luer jake@alogicalparadox.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.