Changeset 14638
- Timestamp:
- 11/17/12 15:20:45 (12 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/InterpreterV8.cc
r14637 r14638 119 119 } 120 120 121 void InterpreterV8::Thread Timeout(int &id, Persistent<Function> func, uint32_t ms)121 void InterpreterV8::Thread(int &id, Persistent<Function> func, uint32_t ms) 122 122 { 123 123 const Locker lock; … … 148 148 fThreadIds.erase(id); 149 149 150 if (!HandleException(exception, " dim.timeout"))150 if (!HandleException(exception, "thread")) 151 151 V8::TerminateExecution(fThreadId); 152 152 } 153 153 154 Handle<Value> InterpreterV8::FuncTimeout(const Arguments& args) 155 { 154 Handle<Value> InterpreterV8::FuncThread(const Arguments& args) 155 { 156 if (!args.IsConstructCall()) 157 return ThrowException(String::New("Thread must be called as constructor.")); 158 156 159 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.")); 161 164 162 165 if (!args[1]->IsFunction()) 163 166 return ThrowException(String::New("Argument 1 not a function.")); 164 167 165 if (!args[0]->IsUint32())166 return ThrowException(String::New("Argument 0 not an uint32."));168 //if (!args.IsConstructCall()) 169 // return Constructor(args); 167 170 168 171 const HandleScope handle_scope; … … 175 178 176 179 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. 179 183 const Unlocker unlock; 180 184 while (id==-2) … … 182 186 } 183 187 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(); 185 194 } 186 195 187 196 Handle<Value> InterpreterV8::FuncKill(const Arguments& args) 188 197 { 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)); 202 203 } 203 204 … … 674 675 Handle<Value> InterpreterV8::FuncDatabase(const Arguments &args) 675 676 { 677 if (!args.IsConstructCall()) 678 return ThrowException(String::New("Database must be called as constructor.")); 679 676 680 if (args.Length()!=1) 677 681 return ThrowException(String::New("Number of arguments must be 1.")); … … 1702 1706 dim->Set(String::New("getState"), FunctionTemplate::New(WrapGetState), ReadOnly); 1703 1707 dim->Set(String::New("sleep"), FunctionTemplate::New(WrapSleep), ReadOnly); 1704 dim->Set(String::New("timeout"), FunctionTemplate::New(WrapTimeout), ReadOnly);1705 1708 dim->Set(String::New("kill"), FunctionTemplate::New(WrapKill), ReadOnly); 1706 1709 dim->Set(String::New("subscribe"), FunctionTemplate::New(WrapSubscribe), ReadOnly); … … 1729 1732 db->InstanceTemplate()->SetInternalFieldCount(1); 1730 1733 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); 1731 1739 1732 1740 #ifdef HAVE_NOVA … … 1818 1826 const Unlocker unlock; 1819 1827 1820 for (auto it=fT imeout.begin(); it!=fTimeout.end(); it++)1828 for (auto it=fThreads.begin(); it!=fThreads.end(); it++) 1821 1829 it->join(); 1822 fT imeout.clear();1830 fThreads.clear(); 1823 1831 } 1824 1832 -
trunk/FACT++/src/InterpreterV8.h
r14637 r14638 36 36 std::map<std::string, v8::Persistent<v8::Object>> fStateCallbacks; 37 37 38 // List of all t imeout threads39 std::vector<std::thread> fT imeout;38 // List of all threads 39 std::vector<std::thread> fThreads; 40 40 41 41 // List of all states already set … … 55 55 v8::Handle<v8::Value> ExecuteInternal(const std::string &code); 56 56 57 void Thread Timeout(int &id, v8::Persistent<v8::Function> func, uint32_t ms);57 void Thread(int &id, v8::Persistent<v8::Function> func, uint32_t ms); 58 58 59 59 v8::Handle<v8::Value> FuncWait(const v8::Arguments& args); 60 60 v8::Handle<v8::Value> FuncSend(const v8::Arguments& args); 61 61 v8::Handle<v8::Value> FuncSleep(const v8::Arguments& args); 62 v8::Handle<v8::Value> FuncT imeout(const v8::Arguments& args);62 v8::Handle<v8::Value> FuncThread(const v8::Arguments& args); 63 63 v8::Handle<v8::Value> FuncKill(const v8::Arguments& args); 64 64 v8::Handle<v8::Value> FuncPrint(const v8::Arguments& args); … … 106 106 static v8::Handle<v8::Value> WrapSend(const v8::Arguments &args) { if (This) return This->FuncSend(args); else return v8::Undefined(); } 107 107 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> WrapT imeout(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(); } 109 109 static v8::Handle<v8::Value> WrapKill(const v8::Arguments &args) { if (This) return This->FuncKill(args); else return v8::Undefined(); } 110 110 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.