Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: runtime/vm/thread_pool.cc

Issue 1177153005: Enables clean VM shutdown. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Fix error propagation Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/vm/thread_pool.h ('k') | runtime/vm/thread_pool_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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) {
26 } 24 }
27 25
28 26
29 ThreadPool::~ThreadPool() { 27 ThreadPool::~ThreadPool() {
30 Shutdown(); 28 Shutdown();
31 } 29 }
32 30
33 31
34 void ThreadPool::Run(Task* task) { 32 void ThreadPool::Run(Task* task) {
35 Worker* worker = NULL; 33 Worker* worker = NULL;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 current = next; 85 current = next;
88 count_stopped_++; 86 count_stopped_++;
89 } 87 }
90 88
91 count_idle_ = 0; 89 count_idle_ = 0;
92 count_running_ = 0; 90 count_running_ = 0;
93 ASSERT(count_started_ == count_stopped_); 91 ASSERT(count_started_ == count_stopped_);
94 } 92 }
95 // Release ThreadPool::mutex_ before calling Worker functions. 93 // Release ThreadPool::mutex_ before calling Worker functions.
96 94
97 Worker* current = saved; 95 {
98 while (current != NULL) { 96 MonitorLocker eml(&exit_monitor_);
99 // We may access all_next_ without holding ThreadPool::mutex_ here 97
100 // because the worker is no longer owned by the ThreadPool. 98 // First tell all the workers to shut down.
101 Worker* next = current->all_next_; 99 Worker* current = saved;
102 current->all_next_ = NULL; 100 while (current != NULL) {
103 current->Shutdown(); 101 Worker* next = current->all_next_;
104 current = next; 102 if (current->id_ != OSThread::GetCurrentThreadId()) {
103 AddWorkerToShutdownList(current);
104 }
105 current->Shutdown();
106 current = next;
107 }
108 saved = NULL;
109
110 // Wait until all workers have exited.
111 while (shutting_down_workers_ != NULL) {
112 // Here, we are waiting for workers to exit. When a worker exits we will
113 // be notified.
114 eml.Wait();
115 }
105 } 116 }
106 } 117 }
107 118
108 119
109 bool ThreadPool::IsIdle(Worker* worker) { 120 bool ThreadPool::IsIdle(Worker* worker) {
110 ASSERT(worker != NULL && worker->owned_); 121 ASSERT(worker != NULL && worker->owned_);
111 for (Worker* current = idle_workers_; 122 for (Worker* current = idle_workers_;
112 current != NULL; 123 current != NULL;
113 current = current->idle_next_) { 124 current = current->idle_next_) {
114 if (current == worker) { 125 if (current == worker) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 if (all_workers_ == NULL) { 161 if (all_workers_ == NULL) {
151 return false; 162 return false;
152 } 163 }
153 164
154 // Special case head of list. 165 // Special case head of list.
155 if (all_workers_ == worker) { 166 if (all_workers_ == worker) {
156 all_workers_ = worker->all_next_; 167 all_workers_ = worker->all_next_;
157 worker->all_next_ = NULL; 168 worker->all_next_ = NULL;
158 worker->owned_ = false; 169 worker->owned_ = false;
159 worker->pool_ = NULL; 170 worker->pool_ = NULL;
171 worker->done_ = true;
160 return true; 172 return true;
161 } 173 }
162 174
163 for (Worker* current = all_workers_; 175 for (Worker* current = all_workers_;
164 current->all_next_ != NULL; 176 current->all_next_ != NULL;
165 current = current->all_next_) { 177 current = current->all_next_) {
166 if (current->all_next_ == worker) { 178 if (current->all_next_ == worker) {
167 current->all_next_ = worker->all_next_; 179 current->all_next_ = worker->all_next_;
168 worker->all_next_ = NULL; 180 worker->all_next_ = NULL;
169 worker->owned_ = false; 181 worker->owned_ = false;
(...skipping 29 matching lines...) Expand all
199 // Remove from all list. 211 // Remove from all list.
200 bool found = RemoveWorkerFromAllList(worker); 212 bool found = RemoveWorkerFromAllList(worker);
201 ASSERT(found); 213 ASSERT(found);
202 214
203 count_stopped_++; 215 count_stopped_++;
204 count_idle_--; 216 count_idle_--;
205 return true; 217 return true;
206 } 218 }
207 219
208 220
221 // Only call while holding the exit_monitor_
222 void ThreadPool::AddWorkerToShutdownList(Worker* worker) {
223 worker->shutdown_next_ = shutting_down_workers_;
224 shutting_down_workers_ = worker;
225 }
226
227
228 // Only call while holding the exit_monitor_
229 bool ThreadPool::RemoveWorkerFromShutdownList(Worker* worker) {
230 ASSERT(worker != NULL);
231 ASSERT(shutting_down_workers_ != NULL);
232
233 // Special case head of list.
234 if (shutting_down_workers_ == worker) {
235 shutting_down_workers_ = worker->shutdown_next_;
236 worker->shutdown_next_ = NULL;
237 return true;
238 }
239
240 for (Worker* current = shutting_down_workers_;
241 current->shutdown_next_ != NULL;
242 current = current->shutdown_next_) {
243 if (current->shutdown_next_ == worker) {
244 current->shutdown_next_ = worker->shutdown_next_;
245 worker->shutdown_next_ = NULL;
246 return true;
247 }
248 }
249 return false;
250 }
251
252
209 ThreadPool::Task::Task() { 253 ThreadPool::Task::Task() {
210 } 254 }
211 255
212 256
213 ThreadPool::Task::~Task() { 257 ThreadPool::Task::~Task() {
214 } 258 }
215 259
216 260
217 ThreadPool::Worker::Worker(ThreadPool* pool) 261 ThreadPool::Worker::Worker(ThreadPool* pool)
218 : pool_(pool), 262 : pool_(pool),
263 done_(false),
219 task_(NULL), 264 task_(NULL),
265 id_(OSThread::kInvalidThreadId),
266 started_(false),
220 owned_(false), 267 owned_(false),
221 all_next_(NULL), 268 all_next_(NULL),
222 idle_next_(NULL) { 269 idle_next_(NULL),
270 shutdown_next_(NULL) {
223 } 271 }
224 272
225 273
226 void ThreadPool::Worker::StartThread() { 274 void ThreadPool::Worker::StartThread() {
227 #if defined(DEBUG) 275 #if defined(DEBUG)
228 // Must call SetTask before StartThread. 276 // Must call SetTask before StartThread.
229 { // NOLINT 277 { // NOLINT
230 MonitorLocker ml(&monitor_); 278 MonitorLocker ml(&monitor_);
231 ASSERT(task_ != NULL); 279 ASSERT(task_ != NULL);
232 } 280 }
(...skipping 24 matching lines...) Expand all
257 // out. Give the worker one last desperate chance to live. We 305 // out. Give the worker one last desperate chance to live. We
258 // are merciful. 306 // are merciful.
259 return 1; 307 return 1;
260 } else { 308 } else {
261 return FLAG_worker_timeout_millis - waited; 309 return FLAG_worker_timeout_millis - waited;
262 } 310 }
263 } 311 }
264 } 312 }
265 313
266 314
267 void ThreadPool::Worker::Loop() { 315 bool ThreadPool::Worker::Loop() {
268 MonitorLocker ml(&monitor_); 316 MonitorLocker ml(&monitor_);
269 int64_t idle_start; 317 int64_t idle_start;
270 while (true) { 318 while (true) {
271 ASSERT(task_ != NULL); 319 ASSERT(task_ != NULL);
272 Task* task = task_; 320 Task* task = task_;
273 task_ = NULL; 321 task_ = NULL;
274 322
275 // Release monitor while handling the task. 323 // Release monitor while handling the task.
276 monitor_.Exit(); 324 monitor_.Exit();
277 task->Run(); 325 task->Run();
278 ASSERT(Isolate::Current() == NULL); 326 ASSERT(Isolate::Current() == NULL);
279 delete task; 327 delete task;
280 monitor_.Enter(); 328 monitor_.Enter();
281 329
282 ASSERT(task_ == NULL); 330 ASSERT(task_ == NULL);
283 if (IsDone()) { 331 if (IsDone()) {
284 return; 332 return false;
285 } 333 }
286 ASSERT(pool_ != NULL); 334 ASSERT(!done_);
287 pool_->SetIdle(this); 335 pool_->SetIdle(this);
288 idle_start = OS::GetCurrentTimeMillis(); 336 idle_start = OS::GetCurrentTimeMillis();
289 while (true) { 337 while (true) {
290 Monitor::WaitResult result = ml.Wait(ComputeTimeout(idle_start)); 338 Monitor::WaitResult result = ml.Wait(ComputeTimeout(idle_start));
291 if (task_ != NULL) { 339 if (task_ != NULL) {
292 // We've found a task. Process it, regardless of whether the 340 // We've found a task. Process it, regardless of whether the
293 // worker is done_. 341 // worker is done_.
294 break; 342 break;
295 } 343 }
296 if (IsDone()) { 344 if (IsDone()) {
297 return; 345 return false;
298 } 346 }
299 if (result == Monitor::kTimedOut && 347 if ((result == Monitor::kTimedOut) && pool_->ReleaseIdleWorker(this)) {
300 pool_->ReleaseIdleWorker(this)) { 348 return true;
301 return;
302 } 349 }
303 } 350 }
304 } 351 }
305 UNREACHABLE(); 352 UNREACHABLE();
353 return false;
306 } 354 }
307 355
308 356
309 void ThreadPool::Worker::Shutdown() { 357 void ThreadPool::Worker::Shutdown() {
310 MonitorLocker ml(&monitor_); 358 MonitorLocker ml(&monitor_);
311 pool_ = NULL; // Fail fast if someone tries to access pool_. 359 done_ = true;
312 ml.Notify(); 360 ml.Notify();
313 } 361 }
314 362
315 363
316 // static 364 // static
317 void ThreadPool::Worker::Main(uword args) { 365 void ThreadPool::Worker::Main(uword args) {
318 Thread::EnsureInit(); 366 Thread::EnsureInit();
319 Worker* worker = reinterpret_cast<Worker*>(args); 367 Worker* worker = reinterpret_cast<Worker*>(args);
320 worker->Loop(); 368 bool delete_self = false;
369
370 {
371 MonitorLocker ml(&(worker->monitor_));
372 if (worker->IsDone()) {
373 // id_ hasn't been set yet, but the ThreadPool is being shutdown.
374 // Delete the task, and return.
375 ASSERT(worker->task_);
376 delete worker->task_;
377 worker->task_ = NULL;
378 delete_self = true;
379 } else {
380 worker->id_ = OSThread::GetCurrentThreadId();
381 worker->started_ = true;
382 }
383 }
384
385 // We aren't able to delete the worker while holding the worker's monitor.
386 // Now that we have released it, and we know that ThreadPool::Shutdown
387 // won't touch it again, we can delete it and return.
388 if (delete_self) {
389 MonitorLocker eml(&worker->pool_->exit_monitor_);
390 worker->pool_->RemoveWorkerFromShutdownList(worker);
391 delete worker;
392 eml.Notify();
393 return;
394 }
395
396 bool released = worker->Loop();
321 397
322 // It should be okay to access these unlocked here in this assert. 398 // It should be okay to access these unlocked here in this assert.
323 ASSERT(!worker->owned_ && 399 // worker->all_next_ is retained by the pool for shutdown monitoring.
324 worker->all_next_ == NULL && 400 ASSERT(!worker->owned_ && (worker->idle_next_ == NULL));
325 worker->idle_next_ == NULL);
326 401
327 // The exit monitor is only used during testing. 402 if (!released) {
328 if (ThreadPool::exit_monitor_) { 403 // This worker is exiting because the thread pool is being shut down.
329 MonitorLocker ml(ThreadPool::exit_monitor_); 404 // Inform the thread pool that we are exiting. We remove this worker from
330 (*ThreadPool::exit_count_)++; 405 // shutting_down_workers_ list because there will be no need for the
331 ml.Notify(); 406 // ThreadPool to take action for this worker.
407 MonitorLocker eml(&worker->pool_->exit_monitor_);
408 worker->id_ = OSThread::kInvalidThreadId;
409 worker->pool_->RemoveWorkerFromShutdownList(worker);
410 delete worker;
411 eml.Notify();
412 } else {
413 // This worker is going down because it was idle for too long. This case
414 // is not due to a ThreadPool Shutdown. Thus, we simply delete the worker.
415 delete worker;
332 } 416 }
333 delete worker;
334 #if defined(TARGET_OS_WINDOWS) 417 #if defined(TARGET_OS_WINDOWS)
335 Thread::CleanUp(); 418 Thread::CleanUp();
336 #endif 419 #endif
337 } 420 }
338 421
339 } // namespace dart 422 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/thread_pool.h ('k') | runtime/vm/thread_pool_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698