Subj : Threads and different SpiderMonkey Runtimes To : netscape.public.mozilla.jseng From : Mads Bondo Dydensborg Date : Wed Sep 21 2005 03:45 pm Hi I have a problem with using SpiderMonkey in my multithreaded C++ program. I want to use one Runtime/Context/Object pr. thread. That is, I have no intention of sharing any JS state at all between the different runtimes. Was it not for other communication that my threads wish to perform, I could happily fork different processes for each javascript environment. I have compiled SpiderMonkey using JS_THREADSAFE and nspr. In my application I create a number of threads. Each thread creates runtimes, etc, as needed. So, each thread has its own private Runtime. However, they crash at many points, most often when creating or destroying a context. Only when running with multiple threads does this happen. The code is unfortunately wrapped in a layer of C++ wrappers, so posting it here does not really make sense. It works without a hint of problems for singlethreaded applications, and valgrind does not report any errors. Obviously I am doing something wrong, but I am hard pressed for explanations. An important point: it is my understanding that one does not need to use the JS_BeginRequest/EndRequest calls, when there are different runtimes. Is that correct? There is only ever the same thread accessing any runtime, contexts, etc. I realize it says the garbage collector is global - but JS_GC takes a context as an argument, so I have been hoping that there were no state changes/conflicts across runtimes. Information on this subject is a bit ambigious - the tutorial says one runtime pr. process, the embedders guide says that you can have multiple runtimes pr. application. A note on using SpiderMonkey in multithreaded applications would be great, IMHO. Here is an example gdb stacktrace of a segmentation fault: #0 0xb74d2951 in pthread_mutex_lock () from /lib/tls/i686/cmov/libpthread.so.0 #1 0xb7384522 in PR_Lock () from /usr/lib/libnspr4.so #2 0xb738a137 in PR_Select () from /usr/lib/libnspr4.so #3 0xb738a75e in PR_GetCurrentThread () from /usr/lib/libnspr4.so #4 0xb7eac143 in js_InitContextForLocking (cx=0x80e2790) at jslock.c:877 #5 0xb7e68747 in js_NewContext (rt=0x80dec48, stackChunkSize=8192) at jscntxt.c:103 #6 0xb7e58716 in JS_NewContext (rt=0x80dec48, stackChunkSize=8192) at jsapi.c:937 #7 0x08058f8b in ContextImpl (this=0x80ddf68, runtime=@0xb712d250, stacksize=8192) at jslib_wrappers.cc:70 #8 0x08055edc in Context (this=0xb712d240, runtime=@0xb712d250, stacksize=8192) at jslib_wrappers.cc:92 #9 0x08052f06 in worker::run (this=0x80dda90) at ptest.cc:146 #10 0x0806f078 in super_thread::Start_Thread__ (this=0x80dda90) at thread_class.cc:117 #11 0x0806ed35 in Static_do_start (MyThis=0x80dda90) at thread_class.cc:32 #12 0xb74d0ca3 in start_thread () from /lib/tls/i686/cmov/libpthread.so.0 #13 0xb746ef5a in clone () from /lib/tls/i686/cmov/libc.so.6 Any help most appreciated. Thanks in advance Mads .