Threading and sharing of global objects.

View: New views
2 Messages — Rating Filter:   Alert me  

Threading and sharing of global objects.

by gurkhali :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

After a bit of reading of the docs and on the Mozilla site I had
couple of question with regards to threading and sharing of global
variables. I have execution model with multiple threads (each with its
own context) running precompiled scripts in a non web environment. I
am trying to build a security sandbox where local variables are
'private' to each of the requests/sessions but they have read/write
access to global variables.

Every thing appears to be working great so far but I am a bit unsure
on the atomicity of global variable modifications.

All of my JS_ calls are enclosed in JS_BeginRequest/End calls, but I
am wondering if they (JS_BeginRequest) provide just GC mutual
exclusion or thread synchronizations as well ' ? In another words if I
may have multiple threads trying to modify global variables what level
of synchronization do I get for free (if any). For example lets say
that I am trying to run the following script in a MT environment where
the variable 'i' is a global.

<script>
   if (typeof i == 'undefined') {
       var i = 0;
   }
   i = i + 1;
   print(i);
</script>

Is there any way to get read/write atomicity with global variables or
is this some thing that I will have to implement my self. If its some
thing that I need to implement, is there any examples or pointers?

Thanks,
Bhushan
_______________________________________________
dev-tech-js-engine mailing list
dev-tech-js-engine@...
https://lists.mozilla.org/listinfo/dev-tech-js-engine

Re: Threading and sharing of global objects.

by Jason Orendorff-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 11/02/2009 06:25 PM, gurkhali wrote:
> After a bit of reading of the docs and on the Mozilla site I had
> couple of question with regards to threading and sharing of global
> variables. I have execution model with multiple threads (each with its
> own context) running precompiled scripts in a non web environment. I
> am trying to build a security sandbox where local variables are
> 'private' to each of the requests/sessions but they have read/write
> access to global variables.

I advise you not to attempt this. The problems include:

   * In recent SpiderMonkey builds (anything after 1.7), sharing arrays
     among threads is no longer thread-safe, so simple JS code can easily
     cause crashes.

       https://bugzilla.mozilla.org/show_bug.cgi?id=419537

     This bug is sneaky. However hard you try to dodge it, if scripts
     in your system can share *any* object across threads it's quite
     likely that a determined script could find a way to crash.

   * In current SpiderMonkey builds (1.8.1+), sharing objects among
     threads disables the JIT at best, and can probably even crash.

   * If many threads are using the global object at once, there will be
     lock contention, which can make your application very slow.

   * The behavior you want probably contradicts the ECMAScript standard,
     which says that e.g. "x = 1" creates a global variable if nothing
     named "x" is in scope. Because of this, you will find it impossible
     to get SpiderMonkey to support the exact nonstandard "local global
     object" semantics you want without modifying the engine itself.

The browser avoids passing objects back and forth among threads
altogether.  Just passing objects from window to window (that is, across
global objects) involves some spectacular complication.

> All of my JS_ calls are enclosed in JS_BeginRequest/End calls, but I
> am wondering if they (JS_BeginRequest) provide just GC mutual
> exclusion or thread synchronizations as well ' ?

This is a long story, told in some detail here:

https://developer.mozilla.org/En/SpiderMonkey/Internals/Thread_Safety

You have to provide your own synchronization primitives. Property loads
and stores are atomic (and pretty strictly ordered, even on SMP
platforms that aren't as cache-coherent as x86). But in a statement like

    i = i + 1;

no guarantee is made about what might happen between the read and the
subsequent write.

-j
_______________________________________________
dev-tech-js-engine mailing list
dev-tech-js-engine@...
https://lists.mozilla.org/listinfo/dev-tech-js-engine