Changeset 14638 for trunk


Ignore:
Timestamp:
11/17/12 15:20:45 (12 years ago)
Author:
tbretz
Message:
Converted dim.timeout into a Thread object.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/InterpreterV8.cc

    r14637 r14638  
    119119}
    120120
    121 void InterpreterV8::ThreadTimeout(int &id, Persistent<Function> func, uint32_t ms)
     121void InterpreterV8::Thread(int &id, Persistent<Function> func, uint32_t ms)
    122122{
    123123    const Locker lock;
     
    148148    fThreadIds.erase(id);
    149149
    150     if (!HandleException(exception, "dim.timeout"))
     150    if (!HandleException(exception, "thread"))
    151151        V8::TerminateExecution(fThreadId);
    152152}
    153153
    154 Handle<Value> InterpreterV8::FuncTimeout(const Arguments& args)
    155 {
     154Handle<Value> InterpreterV8::FuncThread(const Arguments& args)
     155{
     156    if (!args.IsConstructCall())
     157        return ThrowException(String::New("Thread must be called as constructor."));
     158
    156159    if (args.Length()!=2)
    157         return ThrowException(String::New("Number of arguments must be at least 1."));
    158 
    159     if (args.Length()==0)
    160         return ThrowException(String::New("Number of arguments must be at least 1."));
     160        return ThrowException(String::New("Number of arguments must be two."));
     161
     162    if (!args[0]->IsUint32())
     163        return ThrowException(String::New("Argument 0 not an uint32."));
    161164
    162165    if (!args[1]->IsFunction())
    163166        return ThrowException(String::New("Argument 1 not a function."));
    164167
    165     if (!args[0]->IsUint32())
    166         return ThrowException(String::New("Argument 0 not an uint32."));
     168    //if (!args.IsConstructCall())
     169    //    return Constructor(args);
    167170
    168171    const HandleScope handle_scope;
     
    175178
    176179    int id=-2;
    177     fTimeout.push_back(thread(bind(&InterpreterV8::ThreadTimeout, this, ref(id), func, ms)));
    178     {
     180    fThreads.push_back(thread(bind(&InterpreterV8::Thread, this, ref(id), func, ms)));
     181    {
     182        // Allow the thread to lock, so we can get the thread id.
    179183        const Unlocker unlock;
    180184        while (id==-2)
     
    182186    }
    183187
    184     return Integer::New(id);
     188    Handle<Object> This = args.This();
     189
     190    This->SetInternalField(0, Integer::New(id));
     191    This->Set(String::New("kill"), FunctionTemplate::New(WrapKill)->GetFunction(), ReadOnly);
     192
     193    return Undefined();
    185194}
    186195
    187196Handle<Value> InterpreterV8::FuncKill(const Arguments& args)
    188197{
    189     for (int i=0; i<args.Length(); i++)
    190         if (!args[i]->IsInt32())
    191             return ThrowException(String::New("All arguments must be int32."));
    192 
    193     uint32_t cnt = 0;
    194 
    195     for (int i=0; i<args.Length(); i++)
    196     {
    197         V8::TerminateExecution(args[i]->Int32Value());
    198         cnt += fThreadIds.erase(args[i]->Int32Value());
    199     }
    200 
    201     return Integer::New(cnt);
     198    const int id = args.This()->GetInternalField(0)->Int32Value();
     199
     200    V8::TerminateExecution(id);
     201
     202    return Boolean::New(fThreadIds.erase(id));
    202203}
    203204
     
    674675Handle<Value> InterpreterV8::FuncDatabase(const Arguments &args)
    675676{
     677    if (!args.IsConstructCall())
     678        return ThrowException(String::New("Database must be called as constructor."));
     679
    676680    if (args.Length()!=1)
    677681        return ThrowException(String::New("Number of arguments must be 1."));
     
    17021706    dim->Set(String::New("getState"),  FunctionTemplate::New(WrapGetState),  ReadOnly);
    17031707    dim->Set(String::New("sleep"),     FunctionTemplate::New(WrapSleep),     ReadOnly);
    1704     dim->Set(String::New("timeout"),   FunctionTemplate::New(WrapTimeout),   ReadOnly);
    17051708    dim->Set(String::New("kill"),      FunctionTemplate::New(WrapKill),      ReadOnly);
    17061709    dim->Set(String::New("subscribe"), FunctionTemplate::New(WrapSubscribe), ReadOnly);
     
    17291732    db->InstanceTemplate()->SetInternalFieldCount(1);
    17301733    global->Set(String::New("Database"), db, ReadOnly);
     1734
     1735    Handle<FunctionTemplate> thread = FunctionTemplate::New(WrapThread);
     1736    thread->SetClassName(String::New("Thread"));
     1737    thread->InstanceTemplate()->SetInternalFieldCount(1);
     1738    global->Set(String::New("Thread"), thread, ReadOnly);
    17311739
    17321740#ifdef HAVE_NOVA
     
    18181826        const Unlocker unlock;
    18191827
    1820         for (auto it=fTimeout.begin(); it!=fTimeout.end(); it++)
     1828        for (auto it=fThreads.begin(); it!=fThreads.end(); it++)
    18211829            it->join();
    1822         fTimeout.clear();
     1830        fThreads.clear();
    18231831    }
    18241832
  • trunk/FACT++/src/InterpreterV8.h

    r14637 r14638  
    3636    std::map<std::string, v8::Persistent<v8::Object>> fStateCallbacks;
    3737
    38     // List of all timeout threads
    39     std::vector<std::thread> fTimeout;
     38    // List of all threads
     39    std::vector<std::thread> fThreads;
    4040
    4141    // List of all states already set
     
    5555    v8::Handle<v8::Value> ExecuteInternal(const std::string &code);
    5656
    57     void ThreadTimeout(int &id, v8::Persistent<v8::Function> func, uint32_t ms);
     57    void Thread(int &id, v8::Persistent<v8::Function> func, uint32_t ms);
    5858
    5959    v8::Handle<v8::Value> FuncWait(const v8::Arguments& args);
    6060    v8::Handle<v8::Value> FuncSend(const v8::Arguments& args);
    6161    v8::Handle<v8::Value> FuncSleep(const v8::Arguments& args);
    62     v8::Handle<v8::Value> FuncTimeout(const v8::Arguments& args);
     62    v8::Handle<v8::Value> FuncThread(const v8::Arguments& args);
    6363    v8::Handle<v8::Value> FuncKill(const v8::Arguments& args);
    6464    v8::Handle<v8::Value> FuncPrint(const v8::Arguments& args);
     
    106106    static v8::Handle<v8::Value> WrapSend(const v8::Arguments &args)     { if (This) return This->FuncSend(args);     else return v8::Undefined(); }
    107107    static v8::Handle<v8::Value> WrapSleep(const v8::Arguments &args)    { if (This) return This->FuncSleep(args);    else return v8::Undefined(); }
    108     static v8::Handle<v8::Value> WrapTimeout(const v8::Arguments &args)  { if (This) return This->FuncTimeout(args);  else return v8::Undefined(); }
     108    static v8::Handle<v8::Value> WrapThread(const v8::Arguments &args)   { if (This) return This->FuncThread(args);   else return v8::Undefined(); }
    109109    static v8::Handle<v8::Value> WrapKill(const v8::Arguments &args)     { if (This) return This->FuncKill(args);     else return v8::Undefined(); }
    110110    static v8::Handle<v8::Value> WrapExit(const v8::Arguments &args)     { if (This) return This->FuncExit(args);     else return v8::Undefined(); }
Note: See TracChangeset for help on using the changeset viewer.