HighDots Forums  

SpiderMonkey Multithreading String Issues

Javascript JavaScript language (comp.lang.javascript)


Discuss SpiderMonkey Multithreading String Issues in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
phughes31@gmail.com
 
Posts: n/a

Default SpiderMonkey Multithreading String Issues - 10-31-2008 , 03:52 PM






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

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.