Is there such a thing as an atomic test-and-set, semaphore, or lock in Javascript?
I have javascript invoking async background processes via a custom protocol (the background process literally runs in a separate process, unrelated to the browser). I believe I'm running into a race condition; the background process returns between my test and my set, screwing things up on the javascript side. I need a test-and-set operation to make it a real semaphore.
Here's the javascript code that attempts to detect background processes and queue them up:
Call = function () {
var isRunning = true,
queue = [];
return {
// myPublicProperty: "something",
call: function (method) {
if (isRunning) {
console.log("Busy, pushing " + method);
queue.push(method);
} else {
isRunning = true;
objccall(method);
}
},
done: function() {
isRunning = false;
if (queue.length > 0) {
Call.call(queue.shift());
}
}
};
}();
Call is a singleton that implements the queuing; anybody that wants to invoke an external process does Call.call("something") .
Any ideas?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…