JS_BeginRequest 编辑

Indicates to the JS engine that the calling thread is entering a region of code that may call into the JSAPI but does not block.

Syntax

void JS_BeginRequest(JSContext *cx);

void JS_EndRequest(JSContext *cx);
NameTypeDescription
cxJSContext *The context in which the calling thread intends to call JSAPI functions.

Description

When your multithreaded application wants to use a JSContext, it must use JS_BeginRequest and JS_EndRequest to bracket maximal non-blocking hunks of native code that call the JSAPI. This "request model" is necessary to interlock with the global garbage collector.

In a JS_THREADSAFE build, many JSAPI functions must only be called from within a request. In this reference, the cx parameter of such functions is documented with the phrase “Requires request”, like this:

NameTypeDescription
cxJSContext *The context to use. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.

In a DEBUG build, this is enforced with assertions.

Requests constrain garbage collection. If any thread is in a requests, garbage collection can happen only when that thread calls into the JSAPI. If one thread needs garbage collection, it blocks until each other thread makes a JSAPI call. It is therefore imperative that native code executing within an active request on cx not block, or simply take too long, outside the JSAPI. Any blocking native call, or lengthy computation that can race safely with the garbage collector, within a request, must be bracketed with JS_SuspendRequest and JS_ResumeRequest.

It is safe to nest calls to JS_BeginRequest so long as each call is balanced by a matching call to JS_EndRequest.

JSAPI 1.7 and earlier JS_BeginRequest and JS_EndRequest are available only in JS_THREADSAFE builds. In SpiderMonkey 1.8 and later, these functions are present, but do nothing, in non-JS_THREADSAFE builds.

MXR ID Search for JS_BeginRequest
MXR ID Search for JS_EndRequest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:122 次

字数:3695

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文