multiple instance of v8 in one process

905 views
Skip to first unread message

Neha

unread,
Mar 8, 2013, 8:54:15 AM3/8/13
to v8-u...@googlegroups.com
Hi,

i have a C++ application that at run time receives javascript which is as follows:
if (condition 1 && condition 2)
  if(condition)
  {
     true;  
  }
   
   false;
}

I compile this javascript in a worker thread and update the datastructure that holds the compiled javascript.

In my main thread, i try to run the v8 compiled script.

I have to use locker and isolate as i have two threads running which are using v8. First thread to compile the javascript and main thread to run the script.
My application is a low latency application. The execution speed should be very fast. Because i am using isolate, the speed of execution reduces.

Is there any other way in which i can do this using v8?
Can i have two instance of v8 in the same process?

Any help would be appreciated. Thanks!


Neha

unread,
Mar 11, 2013, 5:59:19 AM3/11/13
to v8-u...@googlegroups.com
 
Hi,
 
Tried to create multiple instance of v8 in my process using isolate.
In Thread A i have the following code. (this is a worker thread and the following code is called only when there is update in the javascript)
 v8::Isolate* isolate2 = v8::Isolate::New();
        {
                v8::V8::Initialize();
                v8::Locker l(isolate2);
                v8::Isolate::Scope isolate_scope(isolate2);
                v8::HandleScope handle_scope;
                ....
               // script is compiled here
      }
 
In thread B
 
{
if(v8::Locker::IsActive())
{
   std:: cout << "Active" << std::endl;
}
 v8::HandleScope handle_scope;
//Context scope
//Run the script
}
 
I get following error
 
Fatal error in HandleScope::HandleScope
# Entering the V8 API without proper locking in place
The check for IsActive is true. Since these are two instance of v8. One with isolate2 and other with default isolate why is the lock active after the thread A is executed?

Sven Panne

unread,
Mar 11, 2013, 6:15:02 AM3/11/13
to v8-u...@googlegroups.com
v8::Locker::IsActive() is just telling you that a v8::Locker has been used before *somewhere* in your program, not necessarily in your thread or for your current Isolate. This means: IsActive is basically useless, and I consider it to be on death row for deprecation/removal. Apart from that API oddity, the assertion says: If you use a Locker somewhere in your process, you have to use it everywhere, so use it in thread B, too.

Sven Panne

unread,
Mar 11, 2013, 6:27:16 AM3/11/13
to v8-u...@googlegroups.com
Re-reading your original mail, I am not 100% sure what you are trying to achieve by using separate Isolates and threads: Sharing *anything* JavaScript-ish between different Isolates is by definition impossible (hence the name ;-). An Isolate is a complete separate instance of the VM, and of course you can have several of them in a s single process, Chrome uses this for e.g. web workers. If you are using multiple threads, at any point in time there can only be a single thread executing a given an Isolate, this is what v8::Locker is for. The other way round, several threads can use a single Isolate, but only one after the other guarded by a Locker.

Neha

unread,
Mar 11, 2013, 12:04:40 PM3/11/13
to v8-u...@googlegroups.com
Thanks Sven Panne for the information.

My requirement is that my application has two threads, worker thread (Thread A) and main thread (Thread B).

While the main thread at the start precompiles the scripts and runs it.
The worker thread , dynamically recieves the updated javascript and compiles it. This compiled updated script is later run by the main thread.

If i use isolate and locker for the above, it works fine but isolate and locker adds to the execution time to run the script.Mine is a low latency system so was trying to achieve the above without using isolate and locker.

So i want my worker thread(Thread A) to precompile javascript (updated javascript recieved by the application at run time).
I want to run this precompiled javascript in my main thread. 

Currently what i do store the compiled script, persistent context handle in which the script is compiled in the worker thread(A).
In my main thread i access the compiled script , enter the persistent context stored and run the script. For this i use locker and isolate. Since the time of execution along with isolate and locker is more, i am looking for a work around for this.

Any help would be appreciated. Thanks.

Sven Panne

unread,
Mar 11, 2013, 12:40:39 PM3/11/13
to v8-u...@googlegroups.com
What you are describing is again not a statement of the problem in itself, but an attempt to solve something. What in detail that is, is still unclear to me. I think you are mixing up the concepts of Context and Isolate: An Isolate a concept for strictly, well, isolating several V8 instances (including the heap etc.), where by definition nothing can be shared between those instances: No ScriptData, no Script, no Value, nothing. Contexts are a weaker form of separation (within the same Isolate), basically separating the global object and code. BTW: Asking how to avoid an Isolate is a misconception, you *always* use at least one Isolate behind the scenes.

As I said above, I don't know what exactly you are trying to achieve, so I can only be guessing: If you e.g. have some JavaScript that rarely/never changes and want to run this JavaScript code from various threads, just use a single Isolate, compile your code once and run it from various threads, of course using a Locker everywhere (this is e.g. how Chrome's proxy resolver is handling PAC scripts). If the various threads shouldn't see e.g. changes to the global object from other threads in that scenario, use several contexts within that single Isolate.
Reply all
Reply to author
Forward
0 new messages