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.)
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 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.