| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/task_scheduler/scheduler_worker_pool_impl.h" | 5 #include "base/task_scheduler/scheduler_worker_pool_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 histograms->push_back(num_tasks_between_waits_histogram_); | 406 histograms->push_back(num_tasks_between_waits_histogram_); |
| 407 } | 407 } |
| 408 | 408 |
| 409 void SchedulerWorkerPoolImpl::WaitForAllWorkersIdleForTesting() { | 409 void SchedulerWorkerPoolImpl::WaitForAllWorkersIdleForTesting() { |
| 410 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); | 410 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); |
| 411 while (idle_workers_stack_.Size() < workers_.size()) | 411 while (idle_workers_stack_.Size() < workers_.size()) |
| 412 idle_workers_stack_cv_for_testing_->Wait(); | 412 idle_workers_stack_cv_for_testing_->Wait(); |
| 413 } | 413 } |
| 414 | 414 |
| 415 void SchedulerWorkerPoolImpl::JoinForTesting() { | 415 void SchedulerWorkerPoolImpl::JoinForTesting() { |
| 416 DCHECK(!CanWorkerDetachForTesting() || suggested_reclaim_time_.is_max()) | |
| 417 << "Workers can detach during join."; | |
| 418 for (const auto& worker : workers_) | 416 for (const auto& worker : workers_) |
| 419 worker->JoinForTesting(); | 417 worker->JoinForTesting(); |
| 420 | 418 |
| 421 DCHECK(!join_for_testing_returned_.IsSignaled()); | 419 DCHECK(!join_for_testing_returned_.IsSignaled()); |
| 422 join_for_testing_returned_.Signal(); | 420 join_for_testing_returned_.Signal(); |
| 423 } | 421 } |
| 424 | 422 |
| 425 void SchedulerWorkerPoolImpl::DisallowWorkerDetachmentForTesting() { | |
| 426 worker_detachment_disallowed_.Set(); | |
| 427 } | |
| 428 | |
| 429 size_t SchedulerWorkerPoolImpl::NumberOfAliveWorkersForTesting() { | 423 size_t SchedulerWorkerPoolImpl::NumberOfAliveWorkersForTesting() { |
| 430 size_t num_alive_workers = 0; | 424 size_t num_alive_workers = 0; |
| 431 for (const auto& worker : workers_) { | 425 for (const auto& worker : workers_) { |
| 432 if (worker->ThreadAliveForTesting()) | 426 if (worker->ThreadAliveForTesting()) |
| 433 ++num_alive_workers; | 427 ++num_alive_workers; |
| 434 } | 428 } |
| 435 return num_alive_workers; | 429 return num_alive_workers; |
| 436 } | 430 } |
| 437 | 431 |
| 438 SchedulerWorkerPoolImpl::SchedulerSingleThreadTaskRunner:: | 432 SchedulerWorkerPoolImpl::SchedulerSingleThreadTaskRunner:: |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 bool SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::CanDetach( | 597 bool SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::CanDetach( |
| 604 SchedulerWorker* worker) { | 598 SchedulerWorker* worker) { |
| 605 // It's not an issue if |num_single_threaded_runners_| is incremented after | 599 // It's not an issue if |num_single_threaded_runners_| is incremented after |
| 606 // this because the newly created SingleThreadTaskRunner (from which no task | 600 // this because the newly created SingleThreadTaskRunner (from which no task |
| 607 // has run yet) will simply run all its tasks on the next physical thread | 601 // has run yet) will simply run all its tasks on the next physical thread |
| 608 // created by the worker. | 602 // created by the worker. |
| 609 const bool can_detach = | 603 const bool can_detach = |
| 610 !idle_start_time_.is_null() && | 604 !idle_start_time_.is_null() && |
| 611 (TimeTicks::Now() - idle_start_time_) > outer_->suggested_reclaim_time_ && | 605 (TimeTicks::Now() - idle_start_time_) > outer_->suggested_reclaim_time_ && |
| 612 worker != outer_->PeekAtIdleWorkersStack() && | 606 worker != outer_->PeekAtIdleWorkersStack() && |
| 613 !subtle::NoBarrier_Load(&num_single_threaded_runners_) && | 607 !subtle::NoBarrier_Load(&num_single_threaded_runners_); |
| 614 outer_->CanWorkerDetachForTesting(); | |
| 615 return can_detach; | 608 return can_detach; |
| 616 } | 609 } |
| 617 | 610 |
| 618 void SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::OnDetach() { | 611 void SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::OnDetach() { |
| 619 DCHECK(!did_detach_since_last_get_work_); | 612 DCHECK(!did_detach_since_last_get_work_); |
| 620 outer_->num_tasks_before_detach_histogram_->Add(num_tasks_since_last_detach_); | 613 outer_->num_tasks_before_detach_histogram_->Add(num_tasks_since_last_detach_); |
| 621 num_tasks_since_last_detach_ = 0; | 614 num_tasks_since_last_detach_ = 0; |
| 622 did_detach_since_last_get_work_ = true; | 615 did_detach_since_last_get_work_ = true; |
| 623 last_detach_time_ = TimeTicks::Now(); | 616 last_detach_time_ = TimeTicks::Now(); |
| 624 } | 617 } |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 748 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); | 741 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); |
| 749 return idle_workers_stack_.Peek(); | 742 return idle_workers_stack_.Peek(); |
| 750 } | 743 } |
| 751 | 744 |
| 752 void SchedulerWorkerPoolImpl::RemoveFromIdleWorkersStack( | 745 void SchedulerWorkerPoolImpl::RemoveFromIdleWorkersStack( |
| 753 SchedulerWorker* worker) { | 746 SchedulerWorker* worker) { |
| 754 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); | 747 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); |
| 755 idle_workers_stack_.Remove(worker); | 748 idle_workers_stack_.Remove(worker); |
| 756 } | 749 } |
| 757 | 750 |
| 758 bool SchedulerWorkerPoolImpl::CanWorkerDetachForTesting() { | |
| 759 return !worker_detachment_disallowed_.IsSet(); | |
| 760 } | |
| 761 | |
| 762 } // namespace internal | 751 } // namespace internal |
| 763 } // namespace base | 752 } // namespace base |
| OLD | NEW |