lock: remove nonsense from mutex.

This commit is contained in:
Christopher Jeffrey 2017-01-20 12:01:54 -08:00
parent 18c4a83ed3
commit 4b937de8a6
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -24,12 +24,10 @@ function Lock(named) {
this.named = named === true;
this.jobs = [];
this.waiting = [];
this.busy = false;
this.destroyed = false;
this.map = Object.create(null);
this.pending = 0;
this.current = null;
this.unlocker = this.unlock.bind(this);
@ -76,19 +74,6 @@ Lock.prototype.hasPending = function hasPending(name) {
return this.map[name] > 0;
};
/**
* Test whether the lock is
* busy with a named job.
* @returns {Boolean}
*/
Lock.prototype.isBusy = function isBusy() {
assert(this.named, 'Must use named jobs.');
if (this.current)
return true;
return this.pending > 0;
};
/**
* Lock the parent object and all its methods
* which use the lock. Begin to queue calls.
@ -124,7 +109,6 @@ Lock.prototype.lock = function lock(arg1, arg2) {
if (!this.map[name])
this.map[name] = 0;
this.map[name]++;
this.pending++;
}
return new Promise(function(resolve, reject) {
self.jobs.push(new Job(resolve, reject, name));
@ -150,11 +134,8 @@ Lock.prototype.unlock = function unlock() {
this.busy = false;
this.current = null;
if (this.jobs.length === 0) {
assert(this.pending === 0);
this.drain();
if (this.jobs.length === 0)
return;
}
assert(!this.destroyed);
@ -164,10 +145,6 @@ Lock.prototype.unlock = function unlock() {
assert(this.map[job.name] > 0);
if (--this.map[job.name] === 0)
delete this.map[job.name];
this.pending--;
} else {
if (this.pending === 0)
this.drain();
}
this.busy = true;
@ -176,50 +153,6 @@ Lock.prototype.unlock = function unlock() {
job.resolve(this.unlocker);
};
/**
* Wait for a drain (empty queue).
* @returns {Promise}
*/
Lock.prototype.wait = function wait() {
var self = this;
assert(this.named, 'Must use named jobs.');
if (this.destroyed)
return Promise.reject(new Error('Lock is destroyed.'));
if (!this.isBusy()) {
assert(this.waiting.length === 0);
return Promise.resolve();
}
return new Promise(function(resolve, reject) {
self.waiting.push(new Job(resolve, reject, null));
});
};
/**
* Notify drainers that the queue has emptied.
* @private
*/
Lock.prototype.drain = function drain() {
var i, jobs, job;
if (this.waiting.length === 0)
return;
jobs = this.waiting.slice();
this.waiting.length = 0;
for (i = 0; i < jobs.length; i++) {
job = jobs[i];
job.resolve();
}
};
/**
* Destroy the lock. Purge all pending calls.
*/
@ -237,22 +170,12 @@ Lock.prototype.destroy = function destroy() {
this.busy = false;
this.jobs.length = 0;
this.map = Object.create(null);
this.pending = 0;
this.current = null;
for (i = 0; i < jobs.length; i++) {
job = jobs[i];
job.reject(err);
}
jobs = this.waiting.slice();
this.waiting.length = 0;
for (i = 0; i < jobs.length; i++) {
job = jobs[i];
job.reject(err);
}
};
/**
@ -284,7 +207,8 @@ MappedLock.create = function create() {
};
/**
* Test whether the lock has a pending job by name.
* Test whether the lock has a pending
* job or a job in progress (by name).
* @param {String} name
* @returns {Boolean}
*/
@ -294,12 +218,14 @@ MappedLock.prototype.has = function has(name) {
};
/**
* Test whether the lock is busy .
* Test whether the lock has
* a pending job by name.
* @param {String} name
* @returns {Boolean}
*/
MappedLock.prototype.isBusy = function isBusy() {
return this.busy;
MappedLock.prototype.hasPending = function hasPending(name) {
return this.jobs[name] != null;
};
/**