Is it possible to implement N:M schedule model using isolate and native thread?

53 views
Skip to first unread message

Ran Aizen

unread,
Aug 11, 2020, 3:38:25 AM8/11/20
to v8-users
I saw the v8 description says:

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.

According to my understanding, it seems one isolate can be run in multiple thread (not in same time), and multiple isolate can be managed in one thread.

Because I want to use Isolate as the VM for my program in multi-tenant case, so I read some of the v8 API, I found that `v8::Isolate` has some APIs like:

- `Isolate::enter`, `Isolate::exit`
- `Isolate::TerminateExecution`
- `Isolate::CancelTerminateExecution`
- `Isolate::RequestInterrupt`

So, is it possible to "schedule" massive isolates among multiple threads using v8 public APIs like above or does v8 support to achieve the "schedule" ?

The "schedule" here means maybe:

1. work stealing: let a thread run an isolate which was created and init by other thread to balanced the isolate distribution.
2. round-robin: one thread created multiple isolate, when an special event  happend (maybe recv an os signal), one of the running isolate could be paused the execution progress, save the execution context and switch into other isolate, after other isolate finished, resume the previous isolate's execution.

Ben Noordhuis

unread,
Aug 11, 2020, 6:26:25 AM8/11/20
to v8-users
I believe your question is whether V8 supports preemptive
rescheduling, i.e., without cooperation from the executing script?

It doesn't. It's allowed to call isolate->RequestInterrupt() and then
call isolate->TerminateExecution() from the callback but not
isolate->Exit().

Ran Aizen

unread,
Aug 11, 2020, 11:56:36 AM8/11/20
to v8-users
> I believe your question is whether V8 supports preemptive
> rescheduling, i.e., without cooperation from the executing script?

Yes, whether it support preemptive scheduling or not, is an aspect of my question.

To simplify, what I want to know is "Can we treat a v8 Isolate as a user-space thread, if we can, does v8 permit embedder implement user-space scheduler to support preemptive scheduling between different isolates?"

> It doesn't. It's allowed to call isolate->RequestInterrupt() and then
> call isolate->TerminateExecution() from the callback but not
> isolate->Exit().

This is also what I want to ask, as we all know the given isolate will terminate the previous execution when invoking `isolate->TerminateExecution()` inside the callback of `isolate->RequestInterrupt()`. Will v8 isolate save and restore the execution context (not v8::Context) so that the execution context of the current isolate can be restored and we can resume it in the future, just like what os scheduler do (context switch).

From your answer, I guess that v8 no longer maintaining the execution context after "isolate->TerminateExecution" and cleanup the stack frame, so we can't continue the execution from where we stopped?

Ben Noordhuis

unread,
Aug 11, 2020, 12:45:36 PM8/11/20
to v8-users
On Tue, Aug 11, 2020 at 5:56 PM Ran Aizen <abbshr...@gmail.com> wrote:
> From your answer, I guess that v8 no longer maintaining the execution context after "isolate->TerminateExecution" and cleanup the stack frame, so we can't continue the execution from where we stopped?

That's right. Think of it like this: your C++ code calls into the VM
and the VM can call out to your code. It's not safe to call
isolate->Exit() as long as there are VM stack frames on the call
stack.

isolate->TerminateExecution() works because it unwinds the VM stack
and returns control to your C++ code.

Ran Aizen

unread,
Aug 11, 2020, 11:08:43 PM8/11/20
to v8-users
Thank you Ben~

I just read the a little code implementation, but I still confused
when should I use isolate->enter() and isolate->exit() ?
What's the use case of them?

Ben Noordhuis

unread,
Aug 12, 2020, 5:13:47 AM8/12/20
to v8-users
On Wed, Aug 12, 2020 at 5:08 AM Ran Aizen <abbshr...@gmail.com> wrote:
>
> Thank you Ben~
>
> I just read the a little code implementation, but I still confused
> when should I use isolate->enter() and isolate->exit() ?
> What's the use case of them?

You use them for switching between isolates (at points where it's safe
to do so, of course.)

Rather than juggling enters and exits manually, I recommend using
v8::Isolate::Scope.
Reply all
Reply to author
Forward
0 new messages