Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating different v8 contexts that are clones of another

Using Google's v8 c++ library, I am wanting to create a context where I have several templates, variables, and globals defined and ready for use by several places in code that may run on different threads, each with its own isolate, where they should also each have their own local copy of the context so that any changes to the global variables in one thread will not impact the others.

I can do this by explicitly setting up all my templates, variables, and globals each and every time I want a new context, but I am wondering if there is a more efficient way. Assume that I already have a global v8::Isolate pointer and v8::Persistent that represent the master state. What do I then need to do, if I want to create a brand new isolate in its own thread,and make a new context that is essentially a clone of the master? I know I can wrap a mutex around accesses to the master to ensure that different threads do not access it at the same time if necessary. I just don't know how to efficiently copy information that was made in one isolate to another without recreating its entire contents from scratch.

like image 248
markt1964 Avatar asked Aug 13 '14 19:08

markt1964


1 Answers

You cannot share objects between Isolates. From here

Isolate represents an isolated instance of the V8 engine. V8 isolates have completely separate states. Objects from one isolate must not be used in other isolates. The embedder can create multiple isolates and use them in parallel in multiple threads. An isolate can be entered by at most one thread at any given time. The Locker/Unlocker API must be used to synchronize.

like image 77
smirnoff Avatar answered Sep 30 '22 11:09

smirnoff