diff --git a/lib/chain/chain.js b/lib/chain/chain.js index c5ea5d15..2c92dce8 100644 --- a/lib/chain/chain.js +++ b/lib/chain/chain.js @@ -73,7 +73,7 @@ function Chain(options) { this.total = 0; this.currentBlock = null; this.orphanLimit = options.orphanLimit || (20 << 20); - this.locker = new bcoin.locker(this, this.add); + this.locker = new bcoin.locker(true); this.invalid = {}; this.bestHeight = -1; this.tip = null; diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 63307d91..4c6a5214 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -40,7 +40,7 @@ function RPC(node) { this.walletdb = node.walletdb; this.logger = node.logger; - this.locker = new bcoin.locker(this); + this.locker = new bcoin.locker(); this.feeRate = null; this.mining = false; diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index b0939c4f..9ba87138 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -75,7 +75,7 @@ function Mempool(options) { this.logger = options.logger || this.chain.logger; this.loaded = false; - this.locker = new bcoin.locker(this, this.addTX); + this.locker = new bcoin.locker(true); this.size = 0; this.totalOrphans = 0; diff --git a/lib/net/peer.js b/lib/net/peer.js index 78b941ad..8739c378 100644 --- a/lib/net/peer.js +++ b/lib/net/peer.js @@ -82,7 +82,7 @@ function Peer(pool, addr, socket) { this.chain = this.pool.chain; this.mempool = this.pool.mempool; this.network = this.chain.network; - this.locker = new bcoin.locker(this); + this.locker = new bcoin.locker(); this.version = null; this.destroyed = false; this.ack = false; diff --git a/lib/net/pool.js b/lib/net/pool.js index ae6c6cab..c566dfa4 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -103,7 +103,7 @@ function Pool(options) { this.connected = false; this.uid = 0; this.createServer = null; - this.locker = new bcoin.locker(this); + this.locker = new bcoin.locker(); this.proxyServer = null; this.auth = null; this.identityKey = null; diff --git a/lib/utils/locker.js b/lib/utils/locker.js index 05b88605..c5de5011 100644 --- a/lib/utils/locker.js +++ b/lib/utils/locker.js @@ -15,23 +15,22 @@ var assert = utils.assert; * Represents a mutex lock for locking asynchronous object methods. * @exports Locker * @constructor - * @param {Function} parent - Parent object. * @param {Function?} add - `add` method (whichever method is queuing data). */ -function Locker(parent, add) { +function Locker(add) { if (!(this instanceof Locker)) - return Locker.create(parent, add); + return Locker.create(add); EventEmitter.call(this); - this.parent = parent; + this.add = add === true; + this.jobs = []; this.busy = false; this.pending = []; this.pendingMap = {}; - this.add = add; this.unlocker = this.unlock.bind(this); } @@ -40,15 +39,14 @@ utils.inherits(Locker, EventEmitter); /** * Create a closure scoped locker. - * @param {Function} parent - Parent object. * @param {Function?} add * @returns {Function} Lock method. */ -Locker.create = function create(parent, add) { - var locker = new Locker(parent, add); - return function lock(func, args, force) { - return locker.lock(func, args, force); +Locker.create = function create(add) { + var locker = new Locker(add); + return function lock(arg1, arg2) { + return locker.lock(arg1, arg2); }; }; @@ -138,9 +136,10 @@ Locker.prototype.unlock = function unlock() { */ Locker.prototype.destroy = function destroy() { + this.jobs.length = 0; + this.busy = false; this.pending.length = 0; this.pendingMap = {}; - this.jobs.length = 0; }; /** @@ -166,28 +165,25 @@ Locker.prototype.onDrain = function onDrain() { * Locks methods according to passed-in key. * @exports MappedLock * @constructor - * @param {Function} parent - Parent object. */ -function MappedLock(parent) { +function MappedLock() { if (!(this instanceof MappedLock)) - return MappedLock.create(parent); + return MappedLock.create(); - this.parent = parent; this.jobs = {}; this.busy = {}; } /** * Create a closure scoped locker. - * @param {Function} parent - Parent object. * @returns {Function} Lock method. */ -MappedLock.create = function create(parent) { - var locker = new MappedLock(parent); - return function lock(key, func, args, force) { - return locker.lock(key, func, args, force); +MappedLock.create = function create() { + var locker = new MappedLock(); + return function lock(key, force) { + return locker.lock(key, force); }; }; @@ -255,10 +251,20 @@ MappedLock.prototype.unlock = function unlock(key) { }; }; +/** + * Destroy the locker. Purge all pending calls. + */ + +MappedLock.prototype.destroy = function destroy() { + this.jobs = {}; + this.busy = {}; +}; + /* * Expose */ exports = Locker; exports.mapped = MappedLock; + module.exports = exports; diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 35a0c171..8027f3cc 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -211,7 +211,7 @@ function TXDB(wallet) { this.options = wallet.db.options; this.locked = {}; - this.locker = new bcoin.locker(this); + this.locker = new bcoin.locker(); this.coinCache = new bcoin.lru(10000); this.current = null; diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 68ab4c4b..dc1d9a3e 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -56,8 +56,8 @@ function Wallet(db, options) { this.db = db; this.network = db.network; this.logger = db.logger; - this.writeLock = new bcoin.locker(this); - this.fundLock = new bcoin.locker(this); + this.writeLock = new bcoin.locker(); + this.fundLock = new bcoin.locker(); this.wid = 0; this.id = null; diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 0193eaa8..fd4bea00 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -135,9 +135,9 @@ function WalletDB(options) { // We need one read lock for `get` and `create`. // It will hold locks specific to wallet ids. - this.readLock = new bcoin.locker.mapped(this); - this.writeLock = new bcoin.locker(this); - this.txLock = new bcoin.locker(this); + this.readLock = new bcoin.locker.mapped(); + this.writeLock = new bcoin.locker(); + this.txLock = new bcoin.locker(); this.walletCache = new bcoin.lru(10000); this.accountCache = new bcoin.lru(10000);