Last Comment Bug 686230 - JSCallOnce
: JSCallOnce
Product: Core
Classification: Components
Component: JavaScript Engine (show other bugs)
: unspecified
: x86_64 Linux
-- enhancement (vote)
: mozilla10
Assigned To: general
: Jason Orendorff [:jorendorff]
Depends on:
  Show dependency treegraph
Reported: 2011-09-11 11:33 PDT by Steve Fink [:sfink] [:s:]
Modified: 2011-10-12 03:11 PDT (History)
5 users (show)
See Also:
Crash Signature:
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---

Add JS_CallOnce to the JSAPI for module initialization (3.48 KB, patch)
2011-09-11 11:36 PDT, Steve Fink [:sfink] [:s:]
luke: review+
Details | Diff | Splinter Review

Description User image Steve Fink [:sfink] [:s:] 2011-09-11 11:33:08 PDT
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.)
Comment 1 User image Steve Fink [:sfink] [:s:] 2011-09-11 11:36:27 PDT
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.
Comment 2 User image Luke Wagner [:luke] 2011-09-15 16:41:01 PDT
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.
Comment 3 User image Steve Fink [:sfink] [:s:] 2011-10-11 13:32:48 PDT
Comment 4 User image Marco Bonardo [::mak] 2011-10-12 03:11:14 PDT

Note You need to log in before you can comment on or make changes to this bug.