在V8中使用TerminateExecution [英] Use of TerminateExecution in V8

查看:226
本文介绍了在V8中使用TerminateExecution的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我正在尝试V8.我希望能够在一个线程中运行一些(可能是长时间运行的)javascript,然后能够随意从另一个线程优雅地"终止执行.

I'm experimenting with V8 at the moment. I want to be able to run some (possibly long-running) javascript in one thread and then be able to terminate the execution "gracefully" at will from another thread.

我已经编写了这个简单的代码片段来测试Lockers的概念以及TerminateExecution的用法:

I've written this simple snippet to test the concept of Lockers and the usage of TerminateExecution:

void breaker( Isolate* isolate, int tid ) {

    getchar();      //wait for keyboard input on stdin

    std::cout << "Breaking V8 execution" << std::endl;

    v8::Locker locker( isolate );       //lock the isolate
    v8::V8::TerminateExecution( tid );  //and terminate it
}

int main( int argc, char **argv ) {

    if( argc != 2 ) {
        std::cout << "No script name given" << std::endl;
        return 1;
    }

    Isolate* isolate = Isolate::New();              //create a new isolate
    Isolate::Scope isolateScope( isolate );         //enter it

    v8::Locker locker( isolate );                   //lock the isolate
    v8::Locker::StartPreemption( 100 );             //and start preemption

    v8::HandleScope handle_scope( isolate );        //open a new handle scope


    /*** inject a console object into the global context ***/
    v8::Handle<v8::ObjectTemplate> globalTemplate = v8::ObjectTemplate::New();

    Handle<Context> context = Context::New( NULL, globalTemplate );
    v8::Context::Scope context_scope( context );

    Console* console = new Console;
    Handle<Object> jsConsole = wrap_console( isolate, console );
    expose_property( jsConsole, String::New( "log" ), InvocationCallback( Console::consoleLog ) );
    context->Global()->Set( String::New( "console" ), jsConsole, v8::ReadOnly );
    /*******************************************************/


    //read a javascript file supplied via console
    std::string contents = read_file( argv[1] );
    v8::Handle<v8::String> js = v8::String::New( contents.c_str() );

    v8::TryCatch try_catch;
    v8::Handle<v8::Script> script = v8::Script::Compile( js );

    if( script.IsEmpty() ) {
        report_exception( try_catch );
    }

    //start the breaker thread
    std::thread th( breaker, isolate, v8::V8::GetCurrentThreadId() );

    log_info( "running script" );
    script->Run();
    log_info( "Script execution finished" );

    th.join();
}

但是,我总是在terminateExecution()调用中遇到段错误.我在这里做什么错了?

However, I always get a segfault on the terminateExecution() call. What am I doing wrong here?

感谢您的帮助

推荐答案

v8::V8::GetCurrentThreadId()v8::V8::TerminateExecution(int)方法已从V8 API中删除.我建议您不要使用它们.抢占功能对于这个世界来说可能还不长.

The v8::V8::GetCurrentThreadId() and v8::V8::TerminateExecution(int) methods have been removed from the V8 API. I recommend that you don't use them. The preemption feature is probably also not long for this world.

相反,只需调用v8::V8::TerminateExecution(v8::Isolate*).并且不要将隔离锁锁定在中断线程中,因为这样做会阻塞,直到您的运行器线程释放隔离锁为止,直到脚本执行完成后,隔离锁才会执行.

Instead, simply call v8::V8::TerminateExecution(v8::Isolate*). And don't lock the isolate in your breaker thread, as doing so will block until your runner thread releases the isolate, which it won't do until script execution is finished.

这篇关于在V8中使用TerminateExecution的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆