| 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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 OSThread* os_thread = OSThread::Current(); | 104 OSThread* os_thread = OSThread::Current(); |
| 105 ASSERT(os_thread != NULL); | 105 ASSERT(os_thread != NULL); |
| 106 ThreadId id = os_thread->id(); | 106 ThreadId id = os_thread->id(); |
| 107 while (current != NULL) { | 107 while (current != NULL) { |
| 108 Worker* next = current->all_next_; | 108 Worker* next = current->all_next_; |
| 109 ThreadId currentId = current->id(); | 109 ThreadId currentId = current->id(); |
| 110 if (currentId != id) { | 110 ASSERT(id != currentId); |
| 111 if (currentId == OSThread::kInvalidThreadId) { |
| 112 // If the thread id is invalid, it means the thread never started |
| 113 // because OSThread creation was disabled. Destroy the Task and Worker. |
| 114 delete current->task_; |
| 115 delete current; |
| 116 } else { |
| 111 AddWorkerToShutdownList(current); | 117 AddWorkerToShutdownList(current); |
| 118 current->Shutdown(); |
| 112 } | 119 } |
| 113 current->Shutdown(); | |
| 114 current = next; | 120 current = next; |
| 115 } | 121 } |
| 116 saved = NULL; | 122 saved = NULL; |
| 117 | 123 |
| 118 // Wait until all workers will exit. | 124 // Wait until all workers will exit. |
| 119 while (shutting_down_workers_ != NULL) { | 125 while (shutting_down_workers_ != NULL) { |
| 120 // Here, we are waiting for workers to exit. When a worker exits we will | 126 // Here, we are waiting for workers to exit. When a worker exits we will |
| 121 // be notified. | 127 // be notified. |
| 122 eml.Wait(); | 128 eml.Wait(); |
| 123 } | 129 } |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 ASSERT(os_thread != NULL); | 257 ASSERT(os_thread != NULL); |
| 252 JoinList::AddLocked(os_thread->join_id(), &join_list_); | 258 JoinList::AddLocked(os_thread->join_id(), &join_list_); |
| 253 count_stopped_++; | 259 count_stopped_++; |
| 254 count_idle_--; | 260 count_idle_--; |
| 255 return true; | 261 return true; |
| 256 } | 262 } |
| 257 | 263 |
| 258 | 264 |
| 259 // Only call while holding the exit_monitor_ | 265 // Only call while holding the exit_monitor_ |
| 260 void ThreadPool::AddWorkerToShutdownList(Worker* worker) { | 266 void ThreadPool::AddWorkerToShutdownList(Worker* worker) { |
| 267 ASSERT(exit_monitor_.IsOwnedByCurrentThread()); |
| 261 worker->shutdown_next_ = shutting_down_workers_; | 268 worker->shutdown_next_ = shutting_down_workers_; |
| 262 shutting_down_workers_ = worker; | 269 shutting_down_workers_ = worker; |
| 263 } | 270 } |
| 264 | 271 |
| 265 | 272 |
| 266 // Only call while holding the exit_monitor_ | 273 // Only call while holding the exit_monitor_ |
| 267 bool ThreadPool::RemoveWorkerFromShutdownList(Worker* worker) { | 274 bool ThreadPool::RemoveWorkerFromShutdownList(Worker* worker) { |
| 268 ASSERT(worker != NULL); | 275 ASSERT(worker != NULL); |
| 269 ASSERT(shutting_down_workers_ != NULL); | 276 ASSERT(shutting_down_workers_ != NULL); |
| 277 ASSERT(exit_monitor_.IsOwnedByCurrentThread()); |
| 270 | 278 |
| 271 // Special case head of list. | 279 // Special case head of list. |
| 272 if (shutting_down_workers_ == worker) { | 280 if (shutting_down_workers_ == worker) { |
| 273 shutting_down_workers_ = worker->shutdown_next_; | 281 shutting_down_workers_ = worker->shutdown_next_; |
| 274 worker->shutdown_next_ = NULL; | 282 worker->shutdown_next_ = NULL; |
| 275 return true; | 283 return true; |
| 276 } | 284 } |
| 277 | 285 |
| 278 for (Worker* current = shutting_down_workers_; | 286 for (Worker* current = shutting_down_workers_; |
| 279 current->shutdown_next_ != NULL; | 287 current->shutdown_next_ != NULL; |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 // is not due to a ThreadPool Shutdown. Thus, we simply delete the worker. | 482 // is not due to a ThreadPool Shutdown. Thus, we simply delete the worker. |
| 475 // The worker's id is added to the thread pool's join list by | 483 // The worker's id is added to the thread pool's join list by |
| 476 // ReleaseIdleWorker, so in the case that the thread pool begins shutting | 484 // ReleaseIdleWorker, so in the case that the thread pool begins shutting |
| 477 // down immediately after returning from worker->Loop() above, we still | 485 // down immediately after returning from worker->Loop() above, we still |
| 478 // wait for the thread to exit by joining on it in Shutdown(). | 486 // wait for the thread to exit by joining on it in Shutdown(). |
| 479 delete worker; | 487 delete worker; |
| 480 } | 488 } |
| 481 } | 489 } |
| 482 | 490 |
| 483 } // namespace dart | 491 } // namespace dart |
| OLD | NEW |