JavaScript信号量/测试和设置/锁? [英] Javascript semaphore / test-and-set / lock?

查看:1281
本文介绍了JavaScript信号量/测试和设置/锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有像JavaScript中的原子测试和设置,信号量或锁的东西?



我有javascript通过自定义协议调用异步后台进程(后台进程在一个单独的进程中运行,与浏览器无关)。我相信我遇到了竞争条件;后台进程在我的测试和我的设置之间返回,在javascript一边拧紧。我需要一个测试和设置操作,使其成为一个真正的信号量。



以下是尝试检测后台进程并将其排队的javascript代码:

  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是一个实现队列的单例;任何想要调用外部进程的人都会调用Call.call(something)。



任何想法?

解决方案

JavaScript没有锁定语义,因为JS不是多线程语言。多线程只能在完全不同的上下文中同时操作 - 例如。 HTML5 Worker线程,或者像JavaScriptCore API的上下文对象的多个实例(我假设SpiderMonkey有一个类似的概念)。他们不能有共享状态,所以在本质上所有的执行都是原子的。



好吧,你现在提供了一些代码,我假设你有类似的东西:

 外部处理:
< JSObject> .isRunning = true;
doSomething()
< JSObject> .done()

(使用适当的API)。在这种情况下,我希望JS引擎阻止如果JS在你的js对象的上下文中执行(这是JavaScriptCore会做的),失败,你可能需要在js执行的地方放一个手动锁。 p>

您使用什么引擎来完成所有这些操作?我问,因为根据你的描述,它听起来像你使用该语言提供的C / C ++ API从一个非JS语言设置一个标志,并且大多数JS引擎假设任何状态操作通过API将发生在单个线程上,通常是发生所有执行的同一线程。


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?

解决方案

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.

这篇关于JavaScript信号量/测试和设置/锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆