| 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 { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 count_running_ = 0; | 94 count_running_ = 0; |
| 95 ASSERT(count_started_ == count_stopped_); | 95 ASSERT(count_started_ == count_stopped_); |
| 96 } | 96 } |
| 97 // Release ThreadPool::mutex_ before calling Worker functions. | 97 // Release ThreadPool::mutex_ before calling Worker functions. |
| 98 | 98 |
| 99 { | 99 { |
| 100 MonitorLocker eml(&exit_monitor_); | 100 MonitorLocker eml(&exit_monitor_); |
| 101 | 101 |
| 102 // First tell all the workers to shut down. | 102 // First tell all the workers to shut down. |
| 103 Worker* current = saved; | 103 Worker* current = saved; |
| 104 ThreadId id = OSThread::GetCurrentThreadId(); | 104 OSThread* os_thread = OSThread::Current(); |
| 105 ASSERT(os_thread != NULL); |
| 106 ThreadId id = os_thread->id(); |
| 105 while (current != NULL) { | 107 while (current != NULL) { |
| 106 Worker* next = current->all_next_; | 108 Worker* next = current->all_next_; |
| 107 ThreadId currentId = current->id(); | 109 ThreadId currentId = current->id(); |
| 108 if (currentId != id) { | 110 if (currentId != id) { |
| 109 AddWorkerToShutdownList(current); | 111 AddWorkerToShutdownList(current); |
| 110 } | 112 } |
| 111 current->Shutdown(); | 113 current->Shutdown(); |
| 112 current = next; | 114 current = next; |
| 113 } | 115 } |
| 114 saved = NULL; | 116 saved = NULL; |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 // Remove from idle list. | 240 // Remove from idle list. |
| 239 if (!RemoveWorkerFromIdleList(worker)) { | 241 if (!RemoveWorkerFromIdleList(worker)) { |
| 240 return false; | 242 return false; |
| 241 } | 243 } |
| 242 // Remove from all list. | 244 // Remove from all list. |
| 243 bool found = RemoveWorkerFromAllList(worker); | 245 bool found = RemoveWorkerFromAllList(worker); |
| 244 ASSERT(found); | 246 ASSERT(found); |
| 245 | 247 |
| 246 // The thread for worker will exit. Add its ThreadId to the join_list_ | 248 // The thread for worker will exit. Add its ThreadId to the join_list_ |
| 247 // so that we can join on it at the next opportunity. | 249 // so that we can join on it at the next opportunity. |
| 248 JoinList::AddLocked(OSThread::GetCurrentThreadJoinId(), &join_list_); | 250 OSThread* os_thread = OSThread::Current(); |
| 251 ASSERT(os_thread != NULL); |
| 252 JoinList::AddLocked(os_thread->join_id(), &join_list_); |
| 249 count_stopped_++; | 253 count_stopped_++; |
| 250 count_idle_--; | 254 count_idle_--; |
| 251 return true; | 255 return true; |
| 252 } | 256 } |
| 253 | 257 |
| 254 | 258 |
| 255 // Only call while holding the exit_monitor_ | 259 // Only call while holding the exit_monitor_ |
| 256 void ThreadPool::AddWorkerToShutdownList(Worker* worker) { | 260 void ThreadPool::AddWorkerToShutdownList(Worker* worker) { |
| 257 worker->shutdown_next_ = shutting_down_workers_; | 261 worker->shutdown_next_ = shutting_down_workers_; |
| 258 shutting_down_workers_ = worker; | 262 shutting_down_workers_ = worker; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 | 330 |
| 327 | 331 |
| 328 void ThreadPool::Worker::StartThread() { | 332 void ThreadPool::Worker::StartThread() { |
| 329 #if defined(DEBUG) | 333 #if defined(DEBUG) |
| 330 // Must call SetTask before StartThread. | 334 // Must call SetTask before StartThread. |
| 331 { // NOLINT | 335 { // NOLINT |
| 332 MonitorLocker ml(&monitor_); | 336 MonitorLocker ml(&monitor_); |
| 333 ASSERT(task_ != NULL); | 337 ASSERT(task_ != NULL); |
| 334 } | 338 } |
| 335 #endif | 339 #endif |
| 336 int result = OSThread::Start(&Worker::Main, reinterpret_cast<uword>(this)); | 340 int result = OSThread::Start("Dart ThreadPool Worker", |
| 341 &Worker::Main, |
| 342 reinterpret_cast<uword>(this)); |
| 337 if (result != 0) { | 343 if (result != 0) { |
| 338 FATAL1("Could not start worker thread: result = %d.", result); | 344 FATAL1("Could not start worker thread: result = %d.", result); |
| 339 } | 345 } |
| 340 } | 346 } |
| 341 | 347 |
| 342 | 348 |
| 343 void ThreadPool::Worker::SetTask(Task* task) { | 349 void ThreadPool::Worker::SetTask(Task* task) { |
| 344 MonitorLocker ml(&monitor_); | 350 MonitorLocker ml(&monitor_); |
| 345 ASSERT(task_ == NULL); | 351 ASSERT(task_ == NULL); |
| 346 task_ = task; | 352 task_ = task; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 | 416 |
| 411 void ThreadPool::Worker::Shutdown() { | 417 void ThreadPool::Worker::Shutdown() { |
| 412 MonitorLocker ml(&monitor_); | 418 MonitorLocker ml(&monitor_); |
| 413 done_ = true; | 419 done_ = true; |
| 414 ml.Notify(); | 420 ml.Notify(); |
| 415 } | 421 } |
| 416 | 422 |
| 417 | 423 |
| 418 // static | 424 // static |
| 419 void ThreadPool::Worker::Main(uword args) { | 425 void ThreadPool::Worker::Main(uword args) { |
| 420 Thread::EnsureInit(); | |
| 421 Thread* thread = Thread::Current(); | |
| 422 thread->set_name("Dart ThreadPool Worker"); | |
| 423 Worker* worker = reinterpret_cast<Worker*>(args); | 426 Worker* worker = reinterpret_cast<Worker*>(args); |
| 424 ThreadId id = OSThread::GetCurrentThreadId(); | 427 OSThread* os_thread = OSThread::Current(); |
| 425 ThreadJoinId join_id = OSThread::GetCurrentThreadJoinId(); | 428 ASSERT(os_thread != NULL); |
| 429 ThreadId id = os_thread->id(); |
| 430 ThreadJoinId join_id = os_thread->join_id(); |
| 426 ThreadPool* pool; | 431 ThreadPool* pool; |
| 427 | 432 |
| 428 { | 433 { |
| 429 MonitorLocker ml(&worker->monitor_); | 434 MonitorLocker ml(&worker->monitor_); |
| 430 ASSERT(worker->task_); | 435 ASSERT(worker->task_); |
| 431 worker->id_ = id; | 436 worker->id_ = id; |
| 432 pool = worker->pool_; | 437 pool = worker->pool_; |
| 433 } | 438 } |
| 434 | 439 |
| 435 bool released = worker->Loop(); | 440 bool released = worker->Loop(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 // is not due to a ThreadPool Shutdown. Thus, we simply delete the worker. | 474 // is not due to a ThreadPool Shutdown. Thus, we simply delete the worker. |
| 470 // The worker's id is added to the thread pool's join list by | 475 // The worker's id is added to the thread pool's join list by |
| 471 // ReleaseIdleWorker, so in the case that the thread pool begins shutting | 476 // ReleaseIdleWorker, so in the case that the thread pool begins shutting |
| 472 // down immediately after returning from worker->Loop() above, we still | 477 // down immediately after returning from worker->Loop() above, we still |
| 473 // wait for the thread to exit by joining on it in Shutdown(). | 478 // wait for the thread to exit by joining on it in Shutdown(). |
| 474 delete worker; | 479 delete worker; |
| 475 } | 480 } |
| 476 } | 481 } |
| 477 | 482 |
| 478 } // namespace dart | 483 } // namespace dart |
| OLD | NEW |