Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1008 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | class Semaphore {
|
---|
| 9 | /**
|
---|
| 10 | * Creates an instance of Semaphore.
|
---|
| 11 | *
|
---|
| 12 | * @param {number} available the amount available number of "tasks"
|
---|
| 13 | * in the Semaphore
|
---|
| 14 | */
|
---|
| 15 | constructor(available) {
|
---|
| 16 | this.available = available;
|
---|
| 17 | /** @type {(function(): void)[]} */
|
---|
| 18 | this.waiters = [];
|
---|
| 19 | /** @private */
|
---|
| 20 | this._continue = this._continue.bind(this);
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * @param {function(): void} callback function block to capture and run
|
---|
| 25 | * @returns {void}
|
---|
| 26 | */
|
---|
| 27 | acquire(callback) {
|
---|
| 28 | if (this.available > 0) {
|
---|
| 29 | this.available--;
|
---|
| 30 | callback();
|
---|
| 31 | } else {
|
---|
| 32 | this.waiters.push(callback);
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | release() {
|
---|
| 37 | this.available++;
|
---|
| 38 | if (this.waiters.length > 0) {
|
---|
| 39 | process.nextTick(this._continue);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | _continue() {
|
---|
| 44 | if (this.available > 0) {
|
---|
| 45 | if (this.waiters.length > 0) {
|
---|
| 46 | this.available--;
|
---|
| 47 | const callback = this.waiters.pop();
|
---|
| 48 | callback();
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | module.exports = Semaphore;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.