Skip to content

Instantly share code, notes, and snippets.

@lrewega
Created June 9, 2011 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lrewega/1017581 to your computer and use it in GitHub Desktop.
Save lrewega/1017581 to your computer and use it in GitHub Desktop.
// Initially zero. When decremented to zero, fires callback
function semaphore( callback ) {
var fn = callback,
count = 0;
return {
increment: function() {
count++;
},
decrement: function() {
if( count > 0 ) {
if(--count == 0 && fn) {
fn();
fn = null;
}
} else {
throw new Error("Semaphore decremented more than incremented!");
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment