P
phughes31
I am using SpiderMonkey Javascript to execute a script repeatedly
across multiple threads. The SpiderMonkey code was originally compiled
using JS_THREADSAFE. All threads share the same context and the same
compiled script. When there is only one thread the script runs
perfectly. When there are multiple threads the program crashes within
a few seconds.
/************** Initialization **************/
JSClass global_class =
{
"global",0,
JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,
JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub
};
JSRuntime* pRuntime = JS_NewRuntime(nMaxbytes);
m_pContext = JS_NewContext(pRuntime, 0x1000);
m_pGlobal = JS_NewObject(m_pContext, &global_class, NULL, NULL);
JS_InitStandardClasses(m_pContext, m_pGlobal);
JS_DefineFunction(m_pContext, m_pGlobal, "GetText", MyGetText, 0,
0); // Defines GetText function shown below
JS_ClearPendingException(m_pContext);
// m_sRunScript is a string containing javascript that needs to be
executed
std::wstring sRunScript = L"var x = GetText(); var y = GetText(); var
z = x + y;";
m_pRunScript = JS_CompileUCScript(m_pContext, m_pGlobal,
sRunScript.c_str(), sRunScript.length(), "_Main.js", 1);
/************ Run by multiple threads **************/
rbool = JS_EvaluateUCScript(m_pContext, m_pGlobal,
m_sRunScript.c_str(), m_sRunScript.length(), "MyScript.js", 1,
&rval);
/************ Definition of function MyGetText ****************/
JSBool MyGetText(JSContext *cx, JSObject *obj, uintN argc, jsval
*argv, jsval *rval)
{
std::wstring sRet = L"Some String";
int nLength = sRet.length();
wchar_t* szOut = (wchar_t*)JS_malloc(cx, sizeof(wchar_t)*(nLength +
1));
wcscpy(szOut, sRet.c_str());
JSString* jstr = JS_NewUCString(cx, szOut, wcslen(szOut));
*rval = (jsval)STRING_TO_JSVAL(jstr);
return JS_TRUE;
}
Anybody has any suggestions??
Thanks in advance,
-Pascal
across multiple threads. The SpiderMonkey code was originally compiled
using JS_THREADSAFE. All threads share the same context and the same
compiled script. When there is only one thread the script runs
perfectly. When there are multiple threads the program crashes within
a few seconds.
/************** Initialization **************/
JSClass global_class =
{
"global",0,
JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,
JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub
};
JSRuntime* pRuntime = JS_NewRuntime(nMaxbytes);
m_pContext = JS_NewContext(pRuntime, 0x1000);
m_pGlobal = JS_NewObject(m_pContext, &global_class, NULL, NULL);
JS_InitStandardClasses(m_pContext, m_pGlobal);
JS_DefineFunction(m_pContext, m_pGlobal, "GetText", MyGetText, 0,
0); // Defines GetText function shown below
JS_ClearPendingException(m_pContext);
// m_sRunScript is a string containing javascript that needs to be
executed
std::wstring sRunScript = L"var x = GetText(); var y = GetText(); var
z = x + y;";
m_pRunScript = JS_CompileUCScript(m_pContext, m_pGlobal,
sRunScript.c_str(), sRunScript.length(), "_Main.js", 1);
/************ Run by multiple threads **************/
rbool = JS_EvaluateUCScript(m_pContext, m_pGlobal,
m_sRunScript.c_str(), m_sRunScript.length(), "MyScript.js", 1,
&rval);
/************ Definition of function MyGetText ****************/
JSBool MyGetText(JSContext *cx, JSObject *obj, uintN argc, jsval
*argv, jsval *rval)
{
std::wstring sRet = L"Some String";
int nLength = sRet.length();
wchar_t* szOut = (wchar_t*)JS_malloc(cx, sizeof(wchar_t)*(nLength +
1));
wcscpy(szOut, sRet.c_str());
JSString* jstr = JS_NewUCString(cx, szOut, wcslen(szOut));
*rval = (jsval)STRING_TO_JSVAL(jstr);
return JS_TRUE;
}
Anybody has any suggestions??
Thanks in advance,
-Pascal