Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript semaphore / test-and-set / lock?

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?

like image 300
Parand Avatar asked Feb 17 '09 00:02

Parand


People also ask

Is Semaphore same as lock?

Lock vs SemaphoreLocks cannot be shared between more than one thread processes but semaphores can have multiple processes of the same thread. Only one thread works with the entire buffer at a given instance of time but semaphores can work on different buffers at a given time.

How lock is implemented in JavaScript?

B raises an onclick event. If B is in event-state the event waits for B to be in ready-state before propagating. If B is in ready-state , B is locked and is set to event-state , then the event propagates. When the event's propagation is complete, B is set to ready-state .

Is mutex same as lock?

A mutex is the same as a lock but it can be system wide (shared by multiple processes). A semaphore does the same as a mutex but allows x number of threads to enter, this can be used for example to limit the number of cpu, io or ram intensive tasks running at the same time.

Does JavaScript have mutex?

Race conditions are a well-known problem from any language that supports for concurrency, and often a mutex is used as a tool to synchronize such operations. This article shows how to apply the concept of mutexes to JavaScript in order to avoid race conditions.


1 Answers

JavaScript has no locking semantics because JS is not a multi threaded language. Multiple threads can only operate concurrently in completely distinct contexts -- eg. HTML5 Worker threads, or in things like multiple instances of JavaScriptCore API's context object (I assume SpiderMonkey has a similar concept). They can't have shared state, so in essence all execution is atomic.

Okay, as you have now provided some of your code i assume you have something akin to:

External Process: <JSObject>.isRunning = true; doSomething() <JSObject>.done() 

Or some such (using appropriate APIs). In which case I would expect the JS engine to block if JS is executing in the context of your js object (which is what JavaScriptCore would do), failing that you will probably need to put a manual lock in place around js execution.

What engine are you using to do all of this? I ask because based on your description it sounds like you're setting a flag from a secondary thread from a non-JS language using the C/C++ API provided by that language, and most JS engines assume that any state manipulation made via the API will be occurring on a single thread, typically the same thread that all execution occurs on.

like image 99
olliej Avatar answered Sep 22 '22 10:09

olliej