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 "vm/thread_pool.h" | 5 #include "vm/thread_pool.h" |
6 | 6 |
7 #include "vm/flags.h" | 7 #include "vm/flags.h" |
8 #include "vm/lockers.h" | 8 #include "vm/lockers.h" |
9 | 9 |
10 namespace dart { | 10 namespace dart { |
11 | 11 |
12 DEFINE_FLAG(int, worker_timeout_millis, 5000, | 12 DEFINE_FLAG(int, worker_timeout_millis, 5000, |
13 "Free workers when they have been idle for this amount of time."); | 13 "Free workers when they have been idle for this amount of time."); |
14 | 14 |
15 Monitor* ThreadPool::exit_monitor_ = NULL; | |
16 int* ThreadPool::exit_count_ = NULL; | |
17 | |
18 ThreadPool::ThreadPool() | 15 ThreadPool::ThreadPool() |
19 : shutting_down_(false), | 16 : shutting_down_(false), |
20 all_workers_(NULL), | 17 all_workers_(NULL), |
21 idle_workers_(NULL), | 18 idle_workers_(NULL), |
22 count_started_(0), | 19 count_started_(0), |
23 count_stopped_(0), | 20 count_stopped_(0), |
24 count_running_(0), | 21 count_running_(0), |
25 count_idle_(0) { | 22 count_idle_(0), |
23 shutting_down_workers_(NULL), | |
24 join_list_(NULL) { | |
26 } | 25 } |
27 | 26 |
28 | 27 |
29 ThreadPool::~ThreadPool() { | 28 ThreadPool::~ThreadPool() { |
30 Shutdown(); | 29 Shutdown(); |
31 } | 30 } |
32 | 31 |
33 | 32 |
34 void ThreadPool::Run(Task* task) { | 33 bool ThreadPool::Run(Task* task) { |
35 Worker* worker = NULL; | 34 Worker* worker = NULL; |
36 bool new_worker = false; | 35 bool new_worker = false; |
37 { | 36 { |
38 // We need ThreadPool::mutex_ to access worker lists and other | 37 // We need ThreadPool::mutex_ to access worker lists and other |
39 // ThreadPool state. | 38 // ThreadPool state. |
40 MutexLocker ml(&mutex_); | 39 MutexLocker ml(&mutex_); |
41 if (shutting_down_) { | 40 if (shutting_down_) { |
42 return; | 41 return false; |
43 } | 42 } |
44 if (idle_workers_ == NULL) { | 43 if (idle_workers_ == NULL) { |
45 worker = new Worker(this); | 44 worker = new Worker(this); |
46 ASSERT(worker != NULL); | 45 ASSERT(worker != NULL); |
47 new_worker = true; | 46 new_worker = true; |
48 count_started_++; | 47 count_started_++; |
49 | 48 |
50 // Add worker to the all_workers_ list. | 49 // Add worker to the all_workers_ list. |
51 worker->all_next_ = all_workers_; | 50 worker->all_next_ = all_workers_; |
52 all_workers_ = worker; | 51 all_workers_ = worker; |
53 worker->owned_ = true; | 52 worker->owned_ = true; |
53 count_running_++; | |
54 } else { | 54 } else { |
55 // Get the first worker from the idle worker list. | 55 // Get the first worker from the idle worker list. |
56 worker = idle_workers_; | 56 worker = idle_workers_; |
57 idle_workers_ = worker->idle_next_; | 57 idle_workers_ = worker->idle_next_; |
58 worker->idle_next_ = NULL; | 58 worker->idle_next_ = NULL; |
59 count_idle_--; | 59 count_idle_--; |
60 count_running_++; | |
60 } | 61 } |
61 count_running_++; | |
62 } | 62 } |
63 | |
63 // Release ThreadPool::mutex_ before calling Worker functions. | 64 // Release ThreadPool::mutex_ before calling Worker functions. |
64 ASSERT(worker != NULL); | 65 ASSERT(worker != NULL); |
65 worker->SetTask(task); | 66 worker->SetTask(task); |
66 if (new_worker) { | 67 if (new_worker) { |
67 // Call StartThread after we've assigned the first task. | 68 // Call StartThread after we've assigned the first task. |
68 worker->StartThread(); | 69 worker->StartThread(); |
69 } | 70 } |
71 return true; | |
70 } | 72 } |
71 | 73 |
72 | 74 |
73 void ThreadPool::Shutdown() { | 75 void ThreadPool::Shutdown() { |
74 Worker* saved = NULL; | 76 Worker* saved = NULL; |
75 { | 77 { |
76 MutexLocker ml(&mutex_); | 78 MutexLocker ml(&mutex_); |
77 shutting_down_ = true; | 79 shutting_down_ = true; |
78 saved = all_workers_; | 80 saved = all_workers_; |
79 all_workers_ = NULL; | 81 all_workers_ = NULL; |
80 idle_workers_ = NULL; | 82 idle_workers_ = NULL; |
81 | 83 |
82 Worker* current = saved; | 84 Worker* current = saved; |
83 while (current != NULL) { | 85 while (current != NULL) { |
84 Worker* next = current->all_next_; | 86 Worker* next = current->all_next_; |
85 current->idle_next_ = NULL; | 87 current->idle_next_ = NULL; |
86 current->owned_ = false; | 88 current->owned_ = false; |
87 current = next; | 89 current = next; |
88 count_stopped_++; | 90 count_stopped_++; |
89 } | 91 } |
90 | 92 |
91 count_idle_ = 0; | 93 count_idle_ = 0; |
92 count_running_ = 0; | 94 count_running_ = 0; |
93 ASSERT(count_started_ == count_stopped_); | 95 ASSERT(count_started_ == count_stopped_); |
94 } | 96 } |
95 // Release ThreadPool::mutex_ before calling Worker functions. | 97 // Release ThreadPool::mutex_ before calling Worker functions. |
96 | 98 |
97 Worker* current = saved; | 99 { |
98 while (current != NULL) { | 100 MonitorLocker eml(&exit_monitor_); |
99 // We may access all_next_ without holding ThreadPool::mutex_ here | 101 |
100 // because the worker is no longer owned by the ThreadPool. | 102 // First tell all the workers to shut down. |
101 Worker* next = current->all_next_; | 103 Worker* current = saved; |
102 current->all_next_ = NULL; | 104 while (current != NULL) { |
103 current->Shutdown(); | 105 Worker* next = current->all_next_; |
104 current = next; | 106 if (current->id_ != OSThread::GetCurrentThreadId()) { |
Ivan Posva
2015/09/14 21:52:14
Cache the current thread it outside the loop?
zra
2015/09/14 22:59:01
Done.
| |
107 AddWorkerToShutdownList(current); | |
108 } | |
109 current->Shutdown(); | |
110 current = next; | |
111 } | |
112 saved = NULL; | |
113 | |
114 // Wait until all workers will exit. | |
115 while (shutting_down_workers_ != NULL) { | |
116 // Here, we are waiting for workers to exit. When a worker exits we will | |
117 // be notified. | |
118 eml.Wait(); | |
119 } | |
120 | |
121 // Join non-idle threads. | |
Ivan Posva
2015/09/14 21:52:13
Move outside the exit_monitor_ block.
Verify that
zra
2015/09/14 22:59:01
Done.
| |
122 JoinList::Join(&join_list_); | |
105 } | 123 } |
106 } | 124 } |
107 | 125 |
108 | 126 |
109 bool ThreadPool::IsIdle(Worker* worker) { | 127 bool ThreadPool::IsIdle(Worker* worker) { |
110 ASSERT(worker != NULL && worker->owned_); | 128 ASSERT(worker != NULL && worker->owned_); |
111 for (Worker* current = idle_workers_; | 129 for (Worker* current = idle_workers_; |
112 current != NULL; | 130 current != NULL; |
113 current = current->idle_next_) { | 131 current = current->idle_next_) { |
114 if (current == worker) { | 132 if (current == worker) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 ASSERT(worker != NULL && worker->owned_); | 167 ASSERT(worker != NULL && worker->owned_); |
150 if (all_workers_ == NULL) { | 168 if (all_workers_ == NULL) { |
151 return false; | 169 return false; |
152 } | 170 } |
153 | 171 |
154 // Special case head of list. | 172 // Special case head of list. |
155 if (all_workers_ == worker) { | 173 if (all_workers_ == worker) { |
156 all_workers_ = worker->all_next_; | 174 all_workers_ = worker->all_next_; |
157 worker->all_next_ = NULL; | 175 worker->all_next_ = NULL; |
158 worker->owned_ = false; | 176 worker->owned_ = false; |
159 worker->pool_ = NULL; | 177 worker->done_ = true; |
160 return true; | 178 return true; |
161 } | 179 } |
162 | 180 |
163 for (Worker* current = all_workers_; | 181 for (Worker* current = all_workers_; |
164 current->all_next_ != NULL; | 182 current->all_next_ != NULL; |
165 current = current->all_next_) { | 183 current = current->all_next_) { |
166 if (current->all_next_ == worker) { | 184 if (current->all_next_ == worker) { |
167 current->all_next_ = worker->all_next_; | 185 current->all_next_ = worker->all_next_; |
168 worker->all_next_ = NULL; | 186 worker->all_next_ = NULL; |
169 worker->owned_ = false; | 187 worker->owned_ = false; |
170 return true; | 188 return true; |
171 } | 189 } |
172 } | 190 } |
173 return false; | 191 return false; |
174 } | 192 } |
175 | 193 |
176 | 194 |
177 void ThreadPool::SetIdle(Worker* worker) { | 195 void ThreadPool::SetIdleAndReapExited(Worker* worker) { |
178 MutexLocker ml(&mutex_); | 196 JoinList* list = NULL; |
179 if (shutting_down_) { | 197 { |
180 return; | 198 MutexLocker ml(&mutex_); |
199 if (shutting_down_) { | |
200 return; | |
201 } | |
202 ASSERT(worker->owned_ && !IsIdle(worker)); | |
203 worker->idle_next_ = idle_workers_; | |
204 idle_workers_ = worker; | |
205 count_idle_++; | |
206 count_running_--; | |
207 list = join_list_; | |
208 join_list_ = NULL; | |
181 } | 209 } |
182 ASSERT(worker->owned_ && !IsIdle(worker)); | 210 JoinList::Join(&list); |
183 worker->idle_next_ = idle_workers_; | |
184 idle_workers_ = worker; | |
185 count_idle_++; | |
186 count_running_--; | |
187 } | 211 } |
188 | 212 |
189 | 213 |
190 bool ThreadPool::ReleaseIdleWorker(Worker* worker) { | 214 bool ThreadPool::ReleaseIdleWorker(Worker* worker) { |
191 MutexLocker ml(&mutex_); | 215 MutexLocker ml(&mutex_); |
192 if (shutting_down_) { | 216 if (shutting_down_) { |
193 return false; | 217 return false; |
194 } | 218 } |
195 // Remove from idle list. | 219 // Remove from idle list. |
196 if (!RemoveWorkerFromIdleList(worker)) { | 220 if (!RemoveWorkerFromIdleList(worker)) { |
197 return false; | 221 return false; |
198 } | 222 } |
199 // Remove from all list. | 223 // Remove from all list. |
200 bool found = RemoveWorkerFromAllList(worker); | 224 bool found = RemoveWorkerFromAllList(worker); |
201 ASSERT(found); | 225 ASSERT(found); |
202 | 226 |
227 // The thread for worker will exit. Add its ThreadId to the join_list_ | |
228 // so that we can join on it at the next opportunity. | |
229 JoinList::Add(worker->join_id_, &join_list_); | |
203 count_stopped_++; | 230 count_stopped_++; |
204 count_idle_--; | 231 count_idle_--; |
205 return true; | 232 return true; |
206 } | 233 } |
207 | 234 |
208 | 235 |
236 // Only call while holding the exit_monitor_ | |
237 void ThreadPool::AddWorkerToShutdownList(Worker* worker) { | |
238 worker->shutdown_next_ = shutting_down_workers_; | |
239 shutting_down_workers_ = worker; | |
240 } | |
241 | |
242 | |
243 // Only call while holding the exit_monitor_ | |
244 bool ThreadPool::RemoveWorkerFromShutdownList(Worker* worker) { | |
245 ASSERT(worker != NULL); | |
246 ASSERT(shutting_down_workers_ != NULL); | |
247 | |
248 // Special case head of list. | |
249 if (shutting_down_workers_ == worker) { | |
250 shutting_down_workers_ = worker->shutdown_next_; | |
251 worker->shutdown_next_ = NULL; | |
252 return true; | |
253 } | |
254 | |
255 for (Worker* current = shutting_down_workers_; | |
256 current->shutdown_next_ != NULL; | |
257 current = current->shutdown_next_) { | |
258 if (current->shutdown_next_ == worker) { | |
259 current->shutdown_next_ = worker->shutdown_next_; | |
260 worker->shutdown_next_ = NULL; | |
261 return true; | |
262 } | |
263 } | |
264 return false; | |
265 } | |
266 | |
267 | |
268 void ThreadPool::JoinList::Add(ThreadJoinId id, JoinList** list) { | |
Ivan Posva
2015/09/14 21:52:13
AddLocked(). Ideally we want to assert that we hol
zra
2015/09/14 22:59:01
Done.
| |
269 *list = new JoinList(id, *list); | |
270 } | |
271 | |
272 | |
273 void ThreadPool::JoinList::Join(JoinList** list) { | |
274 while (*list) { | |
Ivan Posva
2015/09/14 21:52:14
*list is not a bool
zra
2015/09/14 22:59:01
Done.
| |
275 JoinList* current = *list; | |
276 *list = current->next(); | |
277 OSThread::Join(current->id()); | |
278 delete current; | |
279 } | |
280 } | |
281 | |
282 | |
209 ThreadPool::Task::Task() { | 283 ThreadPool::Task::Task() { |
210 } | 284 } |
211 | 285 |
212 | 286 |
213 ThreadPool::Task::~Task() { | 287 ThreadPool::Task::~Task() { |
214 } | 288 } |
215 | 289 |
216 | 290 |
217 ThreadPool::Worker::Worker(ThreadPool* pool) | 291 ThreadPool::Worker::Worker(ThreadPool* pool) |
218 : pool_(pool), | 292 : pool_(pool), |
219 task_(NULL), | 293 task_(NULL), |
294 id_(OSThread::kInvalidThreadId), | |
295 join_id_(OSThread::kInvalidThreadJoinId), | |
296 done_(false), | |
220 owned_(false), | 297 owned_(false), |
221 all_next_(NULL), | 298 all_next_(NULL), |
222 idle_next_(NULL) { | 299 idle_next_(NULL), |
300 shutdown_next_(NULL) { | |
223 } | 301 } |
224 | 302 |
225 | 303 |
226 void ThreadPool::Worker::StartThread() { | 304 void ThreadPool::Worker::StartThread() { |
227 #if defined(DEBUG) | 305 #if defined(DEBUG) |
228 // Must call SetTask before StartThread. | 306 // Must call SetTask before StartThread. |
229 { // NOLINT | 307 { // NOLINT |
230 MonitorLocker ml(&monitor_); | 308 MonitorLocker ml(&monitor_); |
231 ASSERT(task_ != NULL); | 309 ASSERT(task_ != NULL); |
232 } | 310 } |
(...skipping 24 matching lines...) Expand all Loading... | |
257 // out. Give the worker one last desperate chance to live. We | 335 // out. Give the worker one last desperate chance to live. We |
258 // are merciful. | 336 // are merciful. |
259 return 1; | 337 return 1; |
260 } else { | 338 } else { |
261 return FLAG_worker_timeout_millis - waited; | 339 return FLAG_worker_timeout_millis - waited; |
262 } | 340 } |
263 } | 341 } |
264 } | 342 } |
265 | 343 |
266 | 344 |
267 void ThreadPool::Worker::Loop() { | 345 bool ThreadPool::Worker::Loop() { |
268 MonitorLocker ml(&monitor_); | 346 MonitorLocker ml(&monitor_); |
269 int64_t idle_start; | 347 int64_t idle_start; |
270 while (true) { | 348 while (true) { |
271 ASSERT(task_ != NULL); | 349 ASSERT(task_ != NULL); |
272 Task* task = task_; | 350 Task* task = task_; |
273 task_ = NULL; | 351 task_ = NULL; |
274 | 352 |
275 // Release monitor while handling the task. | 353 // Release monitor while handling the task. |
276 monitor_.Exit(); | 354 monitor_.Exit(); |
277 task->Run(); | 355 task->Run(); |
278 ASSERT(Isolate::Current() == NULL); | 356 ASSERT(Isolate::Current() == NULL); |
279 delete task; | 357 delete task; |
280 monitor_.Enter(); | 358 monitor_.Enter(); |
281 | 359 |
282 ASSERT(task_ == NULL); | 360 ASSERT(task_ == NULL); |
283 if (IsDone()) { | 361 if (IsDone()) { |
284 return; | 362 return false; |
285 } | 363 } |
286 ASSERT(pool_ != NULL); | 364 ASSERT(!done_); |
287 pool_->SetIdle(this); | 365 pool_->SetIdleAndReapExited(this); |
288 idle_start = OS::GetCurrentTimeMillis(); | 366 idle_start = OS::GetCurrentTimeMillis(); |
289 while (true) { | 367 while (true) { |
290 Monitor::WaitResult result = ml.Wait(ComputeTimeout(idle_start)); | 368 Monitor::WaitResult result = ml.Wait(ComputeTimeout(idle_start)); |
291 if (task_ != NULL) { | 369 if (task_ != NULL) { |
292 // We've found a task. Process it, regardless of whether the | 370 // We've found a task. Process it, regardless of whether the |
293 // worker is done_. | 371 // worker is done_. |
294 break; | 372 break; |
295 } | 373 } |
296 if (IsDone()) { | 374 if (IsDone()) { |
297 return; | 375 return false; |
298 } | 376 } |
299 if (result == Monitor::kTimedOut && | 377 if ((result == Monitor::kTimedOut) && pool_->ReleaseIdleWorker(this)) { |
300 pool_->ReleaseIdleWorker(this)) { | 378 return true; |
301 return; | |
302 } | 379 } |
303 } | 380 } |
304 } | 381 } |
305 UNREACHABLE(); | 382 UNREACHABLE(); |
383 return false; | |
306 } | 384 } |
307 | 385 |
308 | 386 |
309 void ThreadPool::Worker::Shutdown() { | 387 void ThreadPool::Worker::Shutdown() { |
310 MonitorLocker ml(&monitor_); | 388 MonitorLocker ml(&monitor_); |
311 pool_ = NULL; // Fail fast if someone tries to access pool_. | 389 done_ = true; |
312 ml.Notify(); | 390 ml.Notify(); |
313 } | 391 } |
314 | 392 |
315 | 393 |
316 // static | 394 // static |
317 void ThreadPool::Worker::Main(uword args) { | 395 void ThreadPool::Worker::Main(uword args) { |
318 Thread::EnsureInit(); | 396 Thread::EnsureInit(); |
319 Worker* worker = reinterpret_cast<Worker*>(args); | 397 Worker* worker = reinterpret_cast<Worker*>(args); |
320 worker->Loop(); | 398 |
399 { | |
400 MonitorLocker ml(&(worker->monitor_)); | |
401 ASSERT(worker->task_); | |
402 worker->id_ = OSThread::GetCurrentThreadId(); | |
403 worker->join_id_ = OSThread::GetCurrentThreadJoinId(); | |
404 } | |
405 | |
406 bool released = worker->Loop(); | |
321 | 407 |
322 // It should be okay to access these unlocked here in this assert. | 408 // It should be okay to access these unlocked here in this assert. |
323 ASSERT(!worker->owned_ && | 409 // worker->all_next_ is retained by the pool for shutdown monitoring. |
324 worker->all_next_ == NULL && | 410 ASSERT(!worker->owned_ && (worker->idle_next_ == NULL)); |
325 worker->idle_next_ == NULL); | |
326 | 411 |
327 // The exit monitor is only used during testing. | 412 if (!released) { |
328 if (ThreadPool::exit_monitor_) { | 413 // This worker is exiting because the thread pool is being shut down. |
329 MonitorLocker ml(ThreadPool::exit_monitor_); | 414 // Inform the thread pool that we are exiting. We remove this worker from |
330 (*ThreadPool::exit_count_)++; | 415 // shutting_down_workers_ list because there will be no need for the |
331 ml.Notify(); | 416 // ThreadPool to take action for this worker. |
417 MonitorLocker eml(&worker->pool_->exit_monitor_); | |
418 JoinList::Add(worker->join_id_, &worker->pool_->join_list_); | |
Ivan Posva
2015/09/14 21:52:14
JoinList should be consistent on the lock which pr
zra
2015/09/14 22:59:01
Done.
| |
419 worker->id_ = OSThread::kInvalidThreadId; | |
420 worker->join_id_ = OSThread::kInvalidThreadJoinId; | |
421 worker->pool_->RemoveWorkerFromShutdownList(worker); | |
422 delete worker; | |
423 eml.Notify(); | |
424 } else { | |
425 // This worker is going down because it was idle for too long. This case | |
426 // is not due to a ThreadPool Shutdown. Thus, we simply delete the worker. | |
Ivan Posva
2015/09/14 21:52:13
Please add comment: If there is a shutdown race, t
zra
2015/09/14 22:59:01
Done.
| |
427 delete worker; | |
332 } | 428 } |
333 delete worker; | |
334 #if defined(TARGET_OS_WINDOWS) | 429 #if defined(TARGET_OS_WINDOWS) |
335 Thread::CleanUp(); | 430 Thread::CleanUp(); |
336 #endif | 431 #endif |
337 } | 432 } |
338 | 433 |
339 } // namespace dart | 434 } // namespace dart |
OLD | NEW |