File "Semaphore.js"

Full Path: /srv/www/www.cadoro.it/app/lib/locks/lib/Semaphore.js
File size: 445 bytes
MIME-type: text/plain
Charset: utf-8

function Semaphore(initialCount) {
	this._count = initialCount || 1;
	this._waiting = [];
}

module.exports = Semaphore;


Semaphore.prototype.wait = function (cb) {
	this._count -= 1;

	if (this._count < 0) {
		this._waiting.push(cb);
	} else {
		cb.call(this);
	}
};


Semaphore.prototype.signal = function () {
	this._count += 1;

	if (this._count <= 0) {
		var waiter = this._waiting.shift();
		if (waiter) {
			waiter.call(this);
		}
	}
};