Skip to content

Instantly share code, notes, and snippets.

@mlhDevelopment
Forked from oivoodoo/mutex.js
Last active August 29, 2015 14:11
Show Gist options
  • Save mlhDevelopment/1e0b8c3a8bc41dc100af to your computer and use it in GitHub Desktop.
Save mlhDevelopment/1e0b8c3a8bc41dc100af to your computer and use it in GitHub Desktop.
Javascript Mutex, with encapsulation
var Mutex = function() {
var queues = [];
var locked = false;
this.isLocked = function() {
return locked;
};
// It was about this point where I realized I didn't really want a mutex. I wanted a lock check......
this.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