setTimeout implemenetation

470 views
Skip to first unread message

Daniel Burchardt

unread,
Sep 8, 2016, 5:25:24 PM9/8/16
to v8-users
Hi,

I try implement setTimeout in v8. I have problem: how i can execute js code in other thread in this same context? My code:

v8::Handle<v8::Value> setTimeoutImpl(const v8::FunctionCallbackInfo<v8::Value>& info) {
            v8::Local<v8::Value> callback(info[0]);

           
if (callback->IsFunction()) {
                v8
::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(callback);

                v8
::Handle<v8::Value> args[0];
                v8
::Handle<v8::Value> result;
                result
= func->Call(info.GetIsolate()->GetCurrentContext()->Global(), 0, args);

                info
.GetReturnValue().Set(result);
           
}
}

I try create new thread like that:
                pthread_t tid;
                pthread_attr_t tattr;
                pthread_attr_init(&tattr);
                pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
                pthread_create(&tid, &tattr, thread_setTimeout, &ji);
                pthread_attr_destroy(&tattr);

but use v8 in thread is terminated with error segmentation fault... Any one can help me? I found example but use old api.

Ben Noordhuis

unread,
Sep 9, 2016, 3:53:58 AM9/9/16
to v8-users
You need to create a v8::Locker instance first before entering the
isolate and context with v8::Isolate::Scope and v8::Context::Scope
respectively. The timer thread will block until the main thread gives
it a chance to acquire the locker.

(If that all sounds like mumbo jumbo, you need to study v8.h a bit
more, particular the sections on v8::Locker and v8::Unlocker.)

Traditionally though, most embedders solve this by building their
application around an event loop instead of using threads.

Daniel Burchardt

unread,
Sep 9, 2016, 5:14:06 AM9/9/16
to v8-users
I know but i have problem with this:
        static void* thread_setTimeout( void * arg ) {
            v8
::Locker locker(v8::Isolate::GetCurrent());
            v8
::HandleScope handle_scope(v8::Isolate::GetCurrent());

            usleep
( 1000000 );

            v8::Unlocker unlocker(v8::Isolate::GetCurrent());
       
}

after run process is terminated by seg fault

0x00007ffff56c6b7f in v8::Locker::Initialize(v8::Isolate*) ()

Ben Noordhuis

unread,
Sep 9, 2016, 10:31:06 AM9/9/16
to v8-users
On Fri, Sep 9, 2016 at 11:14 AM, Daniel Burchardt
<danielb...@gmail.com> wrote:
> I know but i have problem with this:
> static void* thread_setTimeout( void * arg ) {
> v8::Locker locker(v8::Isolate::GetCurrent());
> v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
>
> usleep( 1000000 );
>
> v8::Unlocker unlocker(v8::Isolate::GetCurrent());
> }
>
> after run process is terminated by seg fault
>
> 0x00007ffff56c6b7f in v8::Locker::Initialize(v8::Isolate*) ()

You need to pass the isolate explicitly to your thread.
v8::Isolate::GetCurrent() won't work, it stores the current isolate in
thread-local storage.
Reply all
Reply to author
Forward
0 new messages