Status

()

Core
JavaScript Engine
--
enhancement
RESOLVED FIXED
6 years ago
6 years ago

People

(Reporter: sfink, Unassigned)

Tracking

unspecified
mozilla10
x86_64
Linux
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 attachment)

(Reporter)

Description

6 years ago
I've been doing a number of things within the JS engine where I'm integrating with external libraries that want a per-process initialization. For example, opagent's op_open_agent() ends up creating a file named after the current pid.

As far as I can tell, there is no way to do this sort of module initialization safely from within the JS engine. You can't use a global lock because NSPR (and probably some underlying thread libraries) does not provide a static initializer, so the creation of the global lock itself races.

If we had a JS_StartEngine() that was required to be called before starting up any more JSAPI-using threads, I could hook in there, but we don't.

I finally stumbled across NSPR's solution: PR_CallOnce. (I should've grepped for pthread_once in the first place; it was very hard to find.)

I think we should have a JS wrapper for the same functionality.

(I've been using a static initialization flag from within JS_NewRuntime, but there's no restriction as far as I know on a single runtime per process.)
(Reporter)

Comment 1

6 years ago
Created attachment 559784 [details] [diff] [review]
Add JS_CallOnce to the JSAPI for module initialization

I'm not crazy about the names in this patch, and I haven't actually tested the !JS_THREADSAFE case.
Attachment #559784 - Flags: review?(luke)

Comment 2

6 years ago
Comment on attachment 559784 [details] [diff] [review]
Add JS_CallOnce to the JSAPI for module initialization

r+ with beefy comment in jsapi.h explaining the interface, in particular, the 'once' arg.
Attachment #559784 - Flags: review?(luke) → review+
(Reporter)

Comment 3

6 years ago
https://hg.mozilla.org/integration/mozilla-inbound/rev/74c8bc479d72
https://hg.mozilla.org/mozilla-central/rev/74c8bc479d72
Status: NEW → RESOLVED
Last Resolved: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla10
You need to log in before you can comment on or make changes to this bug.