OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_D8_H_ | 5 #ifndef V8_D8_H_ |
6 #define V8_D8_H_ | 6 #define V8_D8_H_ |
7 | 7 |
8 #ifndef V8_SHARED | 8 #ifndef V8_SHARED |
9 #include "src/allocation.h" | 9 #include "src/allocation.h" |
10 #include "src/hashmap.h" | 10 #include "src/hashmap.h" |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 base::Mutex mutex_; | 214 base::Mutex mutex_; |
215 i::List<SerializationData*> data_; | 215 i::List<SerializationData*> data_; |
216 }; | 216 }; |
217 | 217 |
218 | 218 |
219 class Worker { | 219 class Worker { |
220 public: | 220 public: |
221 Worker(); | 221 Worker(); |
222 ~Worker(); | 222 ~Worker(); |
223 | 223 |
224 void StartExecuteInThread(Isolate* isolate, const char* script); | 224 // Run the given script on this Worker. This function should only be called |
| 225 // once, and should only be called by the thread that created the Worker. |
| 226 void StartExecuteInThread(const char* script); |
225 // Post a message to the worker's incoming message queue. The worker will | 227 // Post a message to the worker's incoming message queue. The worker will |
226 // take ownership of the SerializationData. | 228 // take ownership of the SerializationData. |
| 229 // This function should only be called by the thread that created the Worker. |
227 void PostMessage(SerializationData* data); | 230 void PostMessage(SerializationData* data); |
228 // Synchronously retrieve messages from the worker's outgoing message queue. | 231 // Synchronously retrieve messages from the worker's outgoing message queue. |
229 // If there is no message in the queue, block until a message is available. | 232 // If there is no message in the queue, block until a message is available. |
230 // If there are no messages in the queue and the worker is no longer running, | 233 // If there are no messages in the queue and the worker is no longer running, |
231 // return nullptr. | 234 // return nullptr. |
| 235 // This function should only be called by the thread that created the Worker. |
232 SerializationData* GetMessage(); | 236 SerializationData* GetMessage(); |
233 // Terminate the worker's event loop. Messages from the worker that have been | 237 // Terminate the worker's event loop. Messages from the worker that have been |
234 // queued can still be read via GetMessage(). | 238 // queued can still be read via GetMessage(). |
| 239 // This function can be called by any thread. |
235 void Terminate(); | 240 void Terminate(); |
236 // Terminate and join the thread. | 241 // Terminate and join the thread. |
| 242 // This function can be called by any thread. |
237 void WaitForThread(); | 243 void WaitForThread(); |
238 | 244 |
239 private: | 245 private: |
240 class WorkerThread : public base::Thread { | 246 class WorkerThread : public base::Thread { |
241 public: | 247 public: |
242 explicit WorkerThread(Worker* worker) | 248 explicit WorkerThread(Worker* worker) |
243 : base::Thread(base::Thread::Options("WorkerThread")), | 249 : base::Thread(base::Thread::Options("WorkerThread")), |
244 worker_(worker) {} | 250 worker_(worker) {} |
245 | 251 |
246 virtual void Run() { worker_->ExecuteInThread(); } | 252 virtual void Run() { worker_->ExecuteInThread(); } |
247 | 253 |
248 private: | 254 private: |
249 Worker* worker_; | 255 Worker* worker_; |
250 }; | 256 }; |
251 | 257 |
252 enum State { | |
253 IDLE, // The worker thread hasn't been started yet | |
254 RUNNING, // The worker thread is running and hasn't been terminated | |
255 TERMINATED // The worker thread has been terminated and will soon finish | |
256 }; | |
257 | |
258 void ExecuteInThread(); | 258 void ExecuteInThread(); |
259 static void PostMessageOut(const v8::FunctionCallbackInfo<v8::Value>& args); | 259 static void PostMessageOut(const v8::FunctionCallbackInfo<v8::Value>& args); |
260 | 260 |
261 base::Semaphore in_semaphore_; | 261 base::Semaphore in_semaphore_; |
262 base::Semaphore out_semaphore_; | 262 base::Semaphore out_semaphore_; |
263 SerializationDataQueue in_queue_; | 263 SerializationDataQueue in_queue_; |
264 SerializationDataQueue out_queue_; | 264 SerializationDataQueue out_queue_; |
265 base::Thread* thread_; | 265 base::Thread* thread_; |
266 char* script_; | 266 char* script_; |
267 base::Atomic32 state_; | 267 base::Atomic32 running_; |
268 base::Atomic32 join_called_; | |
269 }; | 268 }; |
270 #endif // !V8_SHARED | 269 #endif // !V8_SHARED |
271 | 270 |
272 | 271 |
273 class ShellOptions { | 272 class ShellOptions { |
274 public: | 273 public: |
275 ShellOptions() | 274 ShellOptions() |
276 : script_executed(false), | 275 : script_executed(false), |
277 last_run(true), | 276 last_run(true), |
278 send_idle_notification(false), | 277 send_idle_notification(false), |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 static void RunShell(Isolate* isolate); | 467 static void RunShell(Isolate* isolate); |
469 static bool SetOptions(int argc, char* argv[]); | 468 static bool SetOptions(int argc, char* argv[]); |
470 static Local<ObjectTemplate> CreateGlobalTemplate(Isolate* isolate); | 469 static Local<ObjectTemplate> CreateGlobalTemplate(Isolate* isolate); |
471 }; | 470 }; |
472 | 471 |
473 | 472 |
474 } // namespace v8 | 473 } // namespace v8 |
475 | 474 |
476 | 475 |
477 #endif // V8_D8_H_ | 476 #endif // V8_D8_H_ |
OLD | NEW |