Initial commit

This commit is contained in:
jiapengyu
2026-07-27 13:37:48 +08:00
commit ecba71e2a5
39123 changed files with 5989154 additions and 0 deletions
+28
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;