{"name":"rwlock","version":"3.2.0","description":"A read/write lock implementation for Node.","homepage":"http://71104.github.io/rwlock","bugs":{"url":"https://github.com/71104/rwlock/issues"},"license":"MIT","author":{"name":"Alberto La Rocca","email":"a71104@gmail.com"},"files":["lib","LICENSE","README.md"],"main":"lib/lock.js","repository":{"type":"git","url":"https://github.com/71104/rwlock.git"},"devDependencies":{"grunt":"~0.4.2","grunt-contrib-clean":"~0.5.0","grunt-contrib-jshint":"~0.7.2","grunt-contrib-uglify":"~0.2.7","grunt-contrib-yuidoc":"~0.5.0","grunt-contrib-nodeunit":"~0.2.2"},"_id":"rwlock@3.2.0","dist":{"shasum":"7afbf7d0f32486cc895f7072d0bd169b3c28bde3","tarball":"http://registry-npm.ywwl.com/rwlock/download/rwlock-3.2.0.tgz","integrity":"sha512-L+yEhgzqmuUIUFMkisPkQ4n+5ivhWj0Q8ojdMOA7tDg6QJPACzy8aeKKG4l4hyoRkgfupYeajweOGveCxrXSHg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEKo9wF2eRSjBrG0BQLnP4s0sx1Icq3ayp4PAE5U5CTYAiBwiY3SFT2TpKU/6qg6d7Y0FYxIqbREO3xVXfOu49Zj+A=="}],"size":3288,"noattachment":false,"key":"/rwlock/-/rwlock-3.2.0.tgz"},"_from":".","_npmVersion":"1.3.5","_npmUser":{"name":"71104","email":"a71104@gmail.com"},"maintainers":[{"name":"71104","email":"a71104@gmail.com"}],"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":1387909483047,"_cnpm_publish_time":1387909483047,"dist-tags":{"latest":"5.0.0"}}