Chromium Code Reviews| 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/dart.h" | 7 #include "vm/dart.h" |
| 8 #include "vm/flags.h" | 8 #include "vm/flags.h" |
| 9 #include "vm/lockers.h" | 9 #include "vm/lockers.h" |
| 10 | 10 |
| 11 namespace dart { | 11 namespace dart { |
| 12 | 12 |
| 13 DEFINE_FLAG(int, | 13 DEFINE_FLAG(int, |
| 14 worker_timeout_millis, | 14 worker_timeout_micros, |
| 15 5000, | 15 5000000, |
|
Cutch
2016/12/15 22:59:24
consider keeping the command line flag backwards c
rmacnak
2016/12/15 23:11:27
Done.
| |
| 16 "Free workers when they have been idle for this amount of time."); | 16 "Free workers when they have been idle for this amount of time."); |
| 17 | 17 |
| 18 ThreadPool::ThreadPool() | 18 ThreadPool::ThreadPool() |
| 19 : shutting_down_(false), | 19 : shutting_down_(false), |
| 20 all_workers_(NULL), | 20 all_workers_(NULL), |
| 21 idle_workers_(NULL), | 21 idle_workers_(NULL), |
| 22 count_started_(0), | 22 count_started_(0), |
| 23 count_stopped_(0), | 23 count_stopped_(0), |
| 24 count_running_(0), | 24 count_running_(0), |
| 25 count_idle_(0), | 25 count_idle_(0), |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 363 | 363 |
| 364 void ThreadPool::Worker::SetTask(Task* task) { | 364 void ThreadPool::Worker::SetTask(Task* task) { |
| 365 MonitorLocker ml(&monitor_); | 365 MonitorLocker ml(&monitor_); |
| 366 ASSERT(task_ == NULL); | 366 ASSERT(task_ == NULL); |
| 367 task_ = task; | 367 task_ = task; |
| 368 ml.Notify(); | 368 ml.Notify(); |
| 369 } | 369 } |
| 370 | 370 |
| 371 | 371 |
| 372 static int64_t ComputeTimeout(int64_t idle_start) { | 372 static int64_t ComputeTimeout(int64_t idle_start) { |
| 373 if (FLAG_worker_timeout_millis <= 0) { | 373 if (FLAG_worker_timeout_micros <= 0) { |
| 374 // No timeout. | 374 // No timeout. |
| 375 return 0; | 375 return 0; |
| 376 } else { | 376 } else { |
| 377 int64_t waited = OS::GetCurrentTimeMillis() - idle_start; | 377 int64_t waited = OS::GetCurrentMonotonicMicros() - idle_start; |
| 378 if (waited >= FLAG_worker_timeout_millis) { | 378 if (waited >= FLAG_worker_timeout_micros) { |
| 379 // We must have gotten a spurious wakeup just before we timed | 379 // We must have gotten a spurious wakeup just before we timed |
| 380 // out. Give the worker one last desperate chance to live. We | 380 // out. Give the worker one last desperate chance to live. We |
| 381 // are merciful. | 381 // are merciful. |
| 382 return 1; | 382 return 1; |
| 383 } else { | 383 } else { |
| 384 return FLAG_worker_timeout_millis - waited; | 384 return FLAG_worker_timeout_micros - waited; |
| 385 } | 385 } |
| 386 } | 386 } |
| 387 } | 387 } |
| 388 | 388 |
| 389 | 389 |
| 390 bool ThreadPool::Worker::Loop() { | 390 bool ThreadPool::Worker::Loop() { |
| 391 MonitorLocker ml(&monitor_); | 391 MonitorLocker ml(&monitor_); |
| 392 int64_t idle_start; | 392 int64_t idle_start; |
| 393 while (true) { | 393 while (true) { |
| 394 ASSERT(task_ != NULL); | 394 ASSERT(task_ != NULL); |
| 395 Task* task = task_; | 395 Task* task = task_; |
| 396 task_ = NULL; | 396 task_ = NULL; |
| 397 | 397 |
| 398 // Release monitor while handling the task. | 398 // Release monitor while handling the task. |
| 399 ml.Exit(); | 399 ml.Exit(); |
| 400 task->Run(); | 400 task->Run(); |
| 401 ASSERT(Isolate::Current() == NULL); | 401 ASSERT(Isolate::Current() == NULL); |
| 402 delete task; | 402 delete task; |
| 403 ml.Enter(); | 403 ml.Enter(); |
| 404 | 404 |
| 405 ASSERT(task_ == NULL); | 405 ASSERT(task_ == NULL); |
| 406 if (IsDone()) { | 406 if (IsDone()) { |
| 407 return false; | 407 return false; |
| 408 } | 408 } |
| 409 ASSERT(!done_); | 409 ASSERT(!done_); |
| 410 pool_->SetIdleAndReapExited(this); | 410 pool_->SetIdleAndReapExited(this); |
| 411 idle_start = OS::GetCurrentTimeMillis(); | 411 idle_start = OS::GetCurrentMonotonicMicros(); |
| 412 while (true) { | 412 while (true) { |
| 413 Monitor::WaitResult result = ml.Wait(ComputeTimeout(idle_start)); | 413 Monitor::WaitResult result = ml.WaitMicros(ComputeTimeout(idle_start)); |
| 414 if (task_ != NULL) { | 414 if (task_ != NULL) { |
| 415 // We've found a task. Process it, regardless of whether the | 415 // We've found a task. Process it, regardless of whether the |
| 416 // worker is done_. | 416 // worker is done_. |
| 417 break; | 417 break; |
| 418 } | 418 } |
| 419 if (IsDone()) { | 419 if (IsDone()) { |
| 420 return false; | 420 return false; |
| 421 } | 421 } |
| 422 if ((result == Monitor::kTimedOut) && pool_->ReleaseIdleWorker(this)) { | 422 if ((result == Monitor::kTimedOut) && pool_->ReleaseIdleWorker(this)) { |
| 423 return true; | 423 return true; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 498 } | 498 } |
| 499 | 499 |
| 500 // Call the thread exit hook here to notify the embedder that the | 500 // Call the thread exit hook here to notify the embedder that the |
| 501 // thread pool thread is exiting. | 501 // thread pool thread is exiting. |
| 502 if (Dart::thread_exit_callback() != NULL) { | 502 if (Dart::thread_exit_callback() != NULL) { |
| 503 (*Dart::thread_exit_callback())(); | 503 (*Dart::thread_exit_callback())(); |
| 504 } | 504 } |
| 505 } | 505 } |
| 506 | 506 |
| 507 } // namespace dart | 507 } // namespace dart |
| OLD | NEW |