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 |
(...skipping 352 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 int64_t worker_timeout_micros = |
| 374 FLAG_worker_timeout_millis * kMicrosecondsPerMillisecond; |
| 375 if (worker_timeout_micros <= 0) { |
374 // No timeout. | 376 // No timeout. |
375 return 0; | 377 return 0; |
376 } else { | 378 } else { |
377 int64_t waited = OS::GetCurrentTimeMillis() - idle_start; | 379 int64_t waited = OS::GetCurrentMonotonicMicros() - idle_start; |
378 if (waited >= FLAG_worker_timeout_millis) { | 380 if (waited >= worker_timeout_micros) { |
379 // We must have gotten a spurious wakeup just before we timed | 381 // We must have gotten a spurious wakeup just before we timed |
380 // out. Give the worker one last desperate chance to live. We | 382 // out. Give the worker one last desperate chance to live. We |
381 // are merciful. | 383 // are merciful. |
382 return 1; | 384 return 1; |
383 } else { | 385 } else { |
384 return FLAG_worker_timeout_millis - waited; | 386 return worker_timeout_micros - waited; |
385 } | 387 } |
386 } | 388 } |
387 } | 389 } |
388 | 390 |
389 | 391 |
390 bool ThreadPool::Worker::Loop() { | 392 bool ThreadPool::Worker::Loop() { |
391 MonitorLocker ml(&monitor_); | 393 MonitorLocker ml(&monitor_); |
392 int64_t idle_start; | 394 int64_t idle_start; |
393 while (true) { | 395 while (true) { |
394 ASSERT(task_ != NULL); | 396 ASSERT(task_ != NULL); |
395 Task* task = task_; | 397 Task* task = task_; |
396 task_ = NULL; | 398 task_ = NULL; |
397 | 399 |
398 // Release monitor while handling the task. | 400 // Release monitor while handling the task. |
399 ml.Exit(); | 401 ml.Exit(); |
400 task->Run(); | 402 task->Run(); |
401 ASSERT(Isolate::Current() == NULL); | 403 ASSERT(Isolate::Current() == NULL); |
402 delete task; | 404 delete task; |
403 ml.Enter(); | 405 ml.Enter(); |
404 | 406 |
405 ASSERT(task_ == NULL); | 407 ASSERT(task_ == NULL); |
406 if (IsDone()) { | 408 if (IsDone()) { |
407 return false; | 409 return false; |
408 } | 410 } |
409 ASSERT(!done_); | 411 ASSERT(!done_); |
410 pool_->SetIdleAndReapExited(this); | 412 pool_->SetIdleAndReapExited(this); |
411 idle_start = OS::GetCurrentTimeMillis(); | 413 idle_start = OS::GetCurrentMonotonicMicros(); |
412 while (true) { | 414 while (true) { |
413 Monitor::WaitResult result = ml.Wait(ComputeTimeout(idle_start)); | 415 Monitor::WaitResult result = ml.WaitMicros(ComputeTimeout(idle_start)); |
414 if (task_ != NULL) { | 416 if (task_ != NULL) { |
415 // We've found a task. Process it, regardless of whether the | 417 // We've found a task. Process it, regardless of whether the |
416 // worker is done_. | 418 // worker is done_. |
417 break; | 419 break; |
418 } | 420 } |
419 if (IsDone()) { | 421 if (IsDone()) { |
420 return false; | 422 return false; |
421 } | 423 } |
422 if ((result == Monitor::kTimedOut) && pool_->ReleaseIdleWorker(this)) { | 424 if ((result == Monitor::kTimedOut) && pool_->ReleaseIdleWorker(this)) { |
423 return true; | 425 return true; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 } | 500 } |
499 | 501 |
500 // Call the thread exit hook here to notify the embedder that the | 502 // Call the thread exit hook here to notify the embedder that the |
501 // thread pool thread is exiting. | 503 // thread pool thread is exiting. |
502 if (Dart::thread_exit_callback() != NULL) { | 504 if (Dart::thread_exit_callback() != NULL) { |
503 (*Dart::thread_exit_callback())(); | 505 (*Dart::thread_exit_callback())(); |
504 } | 506 } |
505 } | 507 } |
506 | 508 |
507 } // namespace dart | 509 } // namespace dart |
OLD | NEW |