初始化fy

This commit is contained in:
yziiy
2025-08-11 11:51:38 +08:00
parent 98ce20e897
commit 7e21160e13
19770 changed files with 3108698 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
var Class = require('./Class');
var Queue = require('./Queue');
exports = Class({
initialize: function Semaphore() {
var counter =
arguments.length > 0 && arguments[0] !== undefined
? arguments[0]
: 1;
this._counter = counter;
this._tasks = new Queue();
},
wait: function(fn) {
if (this._counter > 0) {
this._counter--;
return fn();
}
this._tasks.enqueue(fn);
},
signal: function() {
var task = this._tasks.dequeue();
if (task) {
return task();
}
this._counter++;
}
});
module.exports = exports;