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" | 7 #include "bin/thread.h" |
8 | 8 |
9 void TaskQueue::Insert(TaskQueueEntry* entry) { | 9 void TaskQueue::Insert(TaskQueueEntry* entry) { |
10 MonitorLocker monitor(&monitor_); | 10 MonitorLocker monitor(&monitor_); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 TaskQueueEntry* entry = queue_.Remove(); | 56 TaskQueueEntry* entry = queue_.Remove(); |
57 if (entry == NULL) { | 57 if (entry == NULL) { |
58 return NULL; | 58 return NULL; |
59 } | 59 } |
60 Task task = entry->task(); | 60 Task task = entry->task(); |
61 delete entry; | 61 delete entry; |
62 return task; | 62 return task; |
63 } | 63 } |
64 | 64 |
65 | 65 |
66 void* ThreadPool::Main(void* args) { | 66 void ThreadPool::Start() { |
67 MonitorLocker monitor(&monitor_); | |
Ivan Posva
2012/02/02 07:53:24
It is very, very confusing to have two different M
Søren Gjesse
2012/02/02 08:46:08
They are in different namespaces though. I will tr
| |
68 for (int i = 0; i < size_; i++) { | |
69 int result = dart::Thread::Start(&ThreadPool::Main, | |
70 reinterpret_cast<uword>(this)); | |
71 if (result != 0) { | |
72 FATAL1("Failed to start thread pool thread %d", result); | |
73 } | |
74 } | |
75 } | |
76 | |
77 | |
78 void ThreadPool::Shutdown() { | |
79 terminate_ = true; | |
80 queue_.Shutdown(); | |
81 MonitorLocker monitor(&monitor_); | |
82 while (size_ > 0) { | |
83 monitor.Wait(); | |
84 } | |
85 } | |
86 | |
87 | |
88 void ThreadPool::ThreadTerminated() { | |
89 MonitorLocker monitor(&monitor_); | |
90 size_--; | |
91 monitor.Notify(); | |
92 } | |
93 | |
94 | |
95 void ThreadPool::Main(uword args) { | |
67 if (Dart_IsVMFlagSet("trace_thread_pool")) { | 96 if (Dart_IsVMFlagSet("trace_thread_pool")) { |
68 printf("Thread pool thread started\n"); | 97 printf("Thread pool thread started\n"); |
69 } | 98 } |
70 ThreadPool* pool = reinterpret_cast<ThreadPool*>(args); | 99 ThreadPool* pool = reinterpret_cast<ThreadPool*>(args); |
71 while (!pool->terminate_) { | 100 while (!pool->terminate_) { |
72 if (Dart_IsVMFlagSet("trace_thread_pool")) { | 101 if (Dart_IsVMFlagSet("trace_thread_pool")) { |
73 printf("Waiting for task\n"); | 102 printf("Waiting for task\n"); |
74 } | 103 } |
75 Task task = pool->WaitForTask(); | 104 Task task = pool->WaitForTask(); |
76 if (pool->terminate_) return NULL; | 105 if (pool->terminate_) break; |
77 (*(pool->task_handler_))(task); | 106 (*(pool->task_handler_))(task); |
78 } | 107 } |
79 return NULL; | 108 pool->ThreadTerminated(); |
80 }; | 109 }; |
OLD | NEW |