| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "bin/thread_pool.h" | 5 #include "bin/thread_pool.h" |
| 6 | 6 |
| 7 void TaskQueue::Insert(TaskQueueEntry* entry) { |
| 8 monitor_.Enter(); |
| 9 if (head_ == NULL) { |
| 10 head_ = entry; |
| 11 tail_ = entry; |
| 12 monitor_.Notify(); |
| 13 } else { |
| 14 tail_->set_next(entry); |
| 15 tail_ = entry; |
| 16 } |
| 17 monitor_.Exit(); |
| 18 } |
| 19 |
| 20 |
| 21 TaskQueueEntry* TaskQueue::Remove() { |
| 22 monitor_.Enter(); |
| 23 TaskQueueEntry* result = head_; |
| 24 while (result == NULL) { |
| 25 if (terminate_) { |
| 26 monitor_.Exit(); |
| 27 return NULL; |
| 28 } |
| 29 monitor_.Wait(dart::Monitor::kNoTimeout); |
| 30 if (terminate_) { |
| 31 monitor_.Exit(); |
| 32 return NULL; |
| 33 } |
| 34 result = head_; |
| 35 } |
| 36 head_ = result->next(); |
| 37 ASSERT(head_ != NULL || tail_ == result); |
| 38 monitor_.Exit(); |
| 39 return result; |
| 40 } |
| 41 |
| 42 |
| 43 void TaskQueue::Shutdown() { |
| 44 monitor_.Enter(); |
| 45 terminate_ = true; |
| 46 monitor_.NotifyAll(); |
| 47 monitor_.Exit(); |
| 48 } |
| 49 |
| 50 |
| 7 void ThreadPool::InsertTask(Task task) { | 51 void ThreadPool::InsertTask(Task task) { |
| 8 TaskQueueEntry* entry = new TaskQueueEntry(task); | 52 TaskQueueEntry* entry = new TaskQueueEntry(task); |
| 9 queue_.Insert(entry); | 53 queue_.Insert(entry); |
| 10 } | 54 } |
| 11 | 55 |
| 12 | 56 |
| 13 Task ThreadPool::WaitForTask() { | 57 Task ThreadPool::WaitForTask() { |
| 14 TaskQueueEntry* entry = queue_.Remove(); | 58 TaskQueueEntry* entry = queue_.Remove(); |
| 15 if (entry == NULL) { | 59 if (entry == NULL) { |
| 16 return NULL; | 60 return NULL; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 29 while (!pool->terminate_) { | 73 while (!pool->terminate_) { |
| 30 if (Dart_IsVMFlagSet("trace_thread_pool")) { | 74 if (Dart_IsVMFlagSet("trace_thread_pool")) { |
| 31 printf("Waiting for task\n"); | 75 printf("Waiting for task\n"); |
| 32 } | 76 } |
| 33 Task task = pool->WaitForTask(); | 77 Task task = pool->WaitForTask(); |
| 34 if (pool->terminate_) return NULL; | 78 if (pool->terminate_) return NULL; |
| 35 (*(pool->task_handler_))(task); | 79 (*(pool->task_handler_))(task); |
| 36 } | 80 } |
| 37 return NULL; | 81 return NULL; |
| 38 }; | 82 }; |
| OLD | NEW |