Sha256: 0ea0c865db18f8c90e518037d5456c4dee325f254754aee57d9af0d82fac40c8

Contents?: true

Size: 1007 Bytes

Versions: 27

Compression:

Stored size: 1007 Bytes

Contents

/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author Tobias Koppers @sokra
*/
"use strict";

class Semaphore {
	/**
	 * Creates an instance of Semaphore.
	 *
	 * @param {number} available the amount available number of "tasks"
	 * in the Semaphore
	 */
	constructor(available) {
		this.available = available;
		/** @type {(function(): void)[]} */
		this.waiters = [];
		/** @private */
		this._continue = this._continue.bind(this);
	}

	/**
	 * @param {function(): void} callback function block to capture and run
	 * @returns {void}
	 */
	acquire(callback) {
		if (this.available > 0) {
			this.available--;
			callback();
		} else {
			this.waiters.push(callback);
		}
	}

	release() {
		this.available++;
		if (this.waiters.length > 0) {
			process.nextTick(this._continue);
		}
	}

	_continue() {
		if (this.available > 0) {
			if (this.waiters.length > 0) {
				this.available--;
				const callback = this.waiters.pop();
				callback();
			}
		}
	}
}

module.exports = Semaphore;

Version data entries

27 entries across 26 versions & 9 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/webpack/lib/util/Semaphore.js
disco_app-0.18.0 test/dummy/node_modules/webpack/lib/util/Semaphore.js
disco_app-0.18.2 test/dummy/node_modules/webpack/lib/util/Semaphore.js
disco_app-0.16.1 test/dummy/node_modules/webpack/lib/util/Semaphore.js
disco_app-0.15.2 test/dummy/node_modules/webpack/lib/util/Semaphore.js
disco_app-0.18.4 test/dummy/node_modules/webpack/lib/util/Semaphore.js
disco_app-0.18.1 test/dummy/node_modules/webpack/lib/util/Semaphore.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/webpack/lib/util/Semaphore.js
disco_app-0.14.0 test/dummy/node_modules/webpack/lib/util/Semaphore.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/webpack/lib/util/Semaphore.js
tang-0.2.1 spec/tang_app/node_modules/webpack/lib/util/Semaphore.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/webpack/lib/util/Semaphore.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/webpack/lib/util/Semaphore.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/webpack/lib/util/Semaphore.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/webpack/lib/util/Semaphore.js
tang-0.2.0 spec/tang_app/node_modules/webpack/lib/util/Semaphore.js
tang-0.1.0 spec/tang_app/node_modules/webpack/lib/util/Semaphore.js
tang-0.0.9 spec/tang_app/node_modules/webpack/lib/util/Semaphore.js
enju_library-0.3.8 spec/dummy/node_modules/webpack/lib/util/Semaphore.js
jester-data-8.0.0 node_modules/webpack/lib/util/Semaphore.js