{"name":"rwlock","version":"2.1.0","description":"Read-write lock implementation for Node.","keywords":["read","write","lock","locker","reader","writer"],"homepage":"https://github.com/canvace/ReadWriteLock","bugs":"https://github.com/canvace/ReadWriteLock/issues","author":{"name":"Alberto La Rocca","email":"a71104@gmail.com","url":"https://github.com/71104"},"files":["rwlock.js"],"main":"rwlock.js","repository":{"type":"git","url":"https://github.com/canvace/ReadWriteLock.git"},"_id":"rwlock@2.1.0","dist":{"shasum":"d4c9e630a8b0a04f7178c2390ebcc19d1a1baeff","tarball":"http://registry-npm.ywwl.com/rwlock/download/rwlock-2.1.0.tgz","integrity":"sha512-WqRgmrHlPq2EqmurkJDKhq9tE8a+j5XBdUJ0dY1mT5Ik4GktbcbPetHtHyLNFWixEO4666l/uXjIFs3HgOt7hA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDEkBRQjN6D0xZ4Mzsk3LjdLa/8jbf1M6tOBJ2lZawDiQIhAKHaPTQgZlxnmiyiFmvFg+WXGw825V8JQKlo1ptzMTun"}],"size":910,"noattachment":false,"key":"/rwlock/-/rwlock-2.1.0.tgz"},"_npmVersion":"1.1.69","_npmUser":{"name":"71104","email":"a71104@gmail.com"},"maintainers":[{"name":"71104","email":"a71104@gmail.com"}],"directories":{},"readme":"rwlock\n======\n\nAsynchronous read/write lock implementation for Node.js.\n\nMain rules:\n- there may be zero or more readers at a time,\n- there may be only one writer at a time,\n- there may be no writer if there are one or more readers already.\n\nInstallation\n------------\n\nIt's on [npmjs](https://npmjs.org/package/rwlock):\n\n```bash\n$ npm install rwlock\n```\n\nBasic usage\n-----------\n\nRequiring the package, creating an instance:\n\n```javascript\nvar ReadWriteLock = require('rwlock');\n\nvar lock = new ReadWriteLock();\n```\n\nAcquiring a read lock:\n\n```javascript\nlock.readLock(function (release) {\n\t// do stuff\n\n\trelease();\n});\n```\n\nAcquiring a write lock:\n\n```javascript\nlock.writeLock(function (release) {\n\t// do stuff\n\n\trelease();\n});\n```\n\nLocks can be released later:\n\n```javascript\nlock.readLock(function (release) {\n\t// not ready to release yet\n\n\tsetTimeout(function () {\n\t\t// ok, now I'm ready\n\t\trelease();\n\t}, 1000);\n});\n```\n\nUpgrading to a write lock\n-------------------------\n\nReadWriteLock does not explicitly support upgrading but you can take advantage of the asynchronous-ness:\n\n```javascript\nlock.readLock(function (release) {\n\t// read stuff here\n\n\t// ok, I now realize I need to write\n\n\t// this will be queued\n\tlock.writeLock(function (release) {\n\t\t// you can write here\n\n\t\trelease();\n\n\t\t// everything is now released.\n\t});\n\n\t// release the read lock, this will activate the writer\n\trelease();\n});\n```\n\nDowngrading to a read lock\n--------------------------\n\nSimilar to upgrading:\n\n```javascript\nlock.writeLock(function (release) {\n\tlock.readLock(function (release) {\n\t\t// ...\n\t\trelease();\n\t});\n\trelease();\n});\n```\n\nKeys\n----\n\nEvery ReadWriteLock instance allows you to work on a virtually unlimited number of completely independent read/write locks.\n\nLocks are identified by names called \"keys\". Every exposed method has an optional \"key\" first argument indicating the lock to work on; if you don't specify a key, the default lock is used.\n\nExample:\n\n```javascript\nlock.writeLock('lock1', function (release) {\n\tconsole.log('writing 1...');\n\tlock.writeLock('lock2', function (release) {\n\t\tconsole.log('writing 2...');\n\t\trelease();\n\t\tconsole.log('done 2.');\n\t});\n\trelease();\n\tconsole.log('done 1.');\n});\n```\n\nThe previous example logs:\n\n```\nwriting 1...\nwriting 2...\ndone 2.\ndone 1.\n```\n\n[async](https://npmjs.org/package/async) compatibility\n------------------------------------------------------\n\nThe ReadWriteLock class does not return errors to your callbacks, but many APIs in Node do. The `async` module uses that as a convention: callbacks usually receive two arguments, a possibly `null` error object and the actual result in case there is no error.\n\nTo aid `async` compatibility, ReadWriteLock sends `null` errors if you specify the `async` flag like in the following example:\n\n```javascript\nlock.async.readLock(function (error, release) {\n\t// no need to check on error, it will always be null\n\n\t// do stuff here\n\n\trelease();\n});\n```\n\nYou can use `rwlock` and `async` together like in this example:\n\n```javascript\nvar releaseLock = null;\n\nasync.waterfall([function (next) {\n\tlock.async.writeLock(next);\n}, function (release, next) {\n\treleaseLock = release;\n\tfs.writeFile('file', 'content', next);\n}, function (next) {\n\treleaseLock();\n\tnext(null);\n}], function (error) {\n\tif (error) {\n\t\tif (releaseLock) {\n\t\t\treleaseLock();\n\t\t}\n\t\tconsole.dir(error);\n\t} else {\n\t\tconsole.log('done.');\n\t}\n});\n```\n\nBuilding from source and testing\n--------------------------------\n\nYou don't need this, but in case you want:\n\n```bash\n$ sudo npm install -g grunt-cli\n$ cd\n$ git clone https://github.com/71104/rwlock.git\n$ cd rwlock\n$ npm install\n$ grunt all\n```\n\nThe following folders will be generated:\n- **lib**, containing the minified ReadWriteLock class to `require` in Node.js;\n- **doc**, containing the API reference documentation in HTML format.\n\nLicense\n-------\n\nMIT. Copyright 2013 Alberto La Rocca\n","publish_time":1360886733910,"_cnpm_publish_time":1360886733910,"dist-tags":{"latest":"5.0.0"}}