1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | class Semaphore {
|
9 | |
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | constructor(available) {
|
16 | this.available = available;
|
17 |
|
18 | this.waiters = [];
|
19 |
|
20 | this._continue = this._continue.bind(this);
|
21 | }
|
22 |
|
23 | |
24 |
|
25 |
|
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;
|