| 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 #include "bin/thread.h" |
| 8 |
| 7 void TaskQueue::Insert(TaskQueueEntry* entry) { | 9 void TaskQueue::Insert(TaskQueueEntry* entry) { |
| 10 MonitorLocker monitor(&monitor_); |
| 8 monitor_.Enter(); | 11 monitor_.Enter(); |
| 9 if (head_ == NULL) { | 12 if (head_ == NULL) { |
| 10 head_ = entry; | 13 head_ = entry; |
| 11 tail_ = entry; | 14 tail_ = entry; |
| 12 monitor_.Notify(); | 15 monitor.Notify(); |
| 13 } else { | 16 } else { |
| 14 tail_->set_next(entry); | 17 tail_->set_next(entry); |
| 15 tail_ = entry; | 18 tail_ = entry; |
| 16 } | 19 } |
| 17 monitor_.Exit(); | |
| 18 } | 20 } |
| 19 | 21 |
| 20 | 22 |
| 21 TaskQueueEntry* TaskQueue::Remove() { | 23 TaskQueueEntry* TaskQueue::Remove() { |
| 22 monitor_.Enter(); | 24 MonitorLocker monitor(&monitor_); |
| 23 TaskQueueEntry* result = head_; | 25 TaskQueueEntry* result = head_; |
| 24 while (result == NULL) { | 26 while (result == NULL) { |
| 25 if (terminate_) { | 27 if (terminate_) { |
| 26 monitor_.Exit(); | |
| 27 return NULL; | 28 return NULL; |
| 28 } | 29 } |
| 29 monitor_.Wait(dart::Monitor::kNoTimeout); | 30 monitor.Wait(); |
| 30 if (terminate_) { | 31 if (terminate_) { |
| 31 monitor_.Exit(); | |
| 32 return NULL; | 32 return NULL; |
| 33 } | 33 } |
| 34 result = head_; | 34 result = head_; |
| 35 } | 35 } |
| 36 head_ = result->next(); | 36 head_ = result->next(); |
| 37 ASSERT(head_ != NULL || tail_ == result); | 37 ASSERT(head_ != NULL || tail_ == result); |
| 38 monitor_.Exit(); | |
| 39 return result; | 38 return result; |
| 40 } | 39 } |
| 41 | 40 |
| 42 | 41 |
| 43 void TaskQueue::Shutdown() { | 42 void TaskQueue::Shutdown() { |
| 44 monitor_.Enter(); | 43 MonitorLocker monitor(&monitor_); |
| 45 terminate_ = true; | 44 terminate_ = true; |
| 46 monitor_.NotifyAll(); | 45 monitor.NotifyAll(); |
| 47 monitor_.Exit(); | |
| 48 } | 46 } |
| 49 | 47 |
| 50 | 48 |
| 51 void ThreadPool::InsertTask(Task task) { | 49 void ThreadPool::InsertTask(Task task) { |
| 52 TaskQueueEntry* entry = new TaskQueueEntry(task); | 50 TaskQueueEntry* entry = new TaskQueueEntry(task); |
| 53 queue_.Insert(entry); | 51 queue_.Insert(entry); |
| 54 } | 52 } |
| 55 | 53 |
| 56 | 54 |
| 57 Task ThreadPool::WaitForTask() { | 55 Task ThreadPool::WaitForTask() { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 73 while (!pool->terminate_) { | 71 while (!pool->terminate_) { |
| 74 if (Dart_IsVMFlagSet("trace_thread_pool")) { | 72 if (Dart_IsVMFlagSet("trace_thread_pool")) { |
| 75 printf("Waiting for task\n"); | 73 printf("Waiting for task\n"); |
| 76 } | 74 } |
| 77 Task task = pool->WaitForTask(); | 75 Task task = pool->WaitForTask(); |
| 78 if (pool->terminate_) return NULL; | 76 if (pool->terminate_) return NULL; |
| 79 (*(pool->task_handler_))(task); | 77 (*(pool->task_handler_))(task); |
| 80 } | 78 } |
| 81 return NULL; | 79 return NULL; |
| 82 }; | 80 }; |
| OLD | NEW |