Skip to content

Instantly share code, notes, and snippets.

@cdmz
Forked from oivoodoo/mutex.js
Created October 22, 2015 01:11
Show Gist options
  • Save cdmz/1a3cbb12ded9c435d528 to your computer and use it in GitHub Desktop.
Save cdmz/1a3cbb12ded9c435d528 to your computer and use it in GitHub Desktop.
mutex
var Mutex = function() {
this.queues = [];
this.locked = false;
};
Mutex.prototype = {
push: function(callback) {
var self = this;
this.queues.push(callback);
if (!this.locked) {
this.locked = true;
var f = this.queues.pop();
try {
f(function() {
self.locked = false;
});
} catch(ex) {
self.locked = false;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment