| 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> |
| 11 | 11 |
| 12 #include "base/atomicops.h" | 12 #include "base/atomicops.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
| 15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 18 #include "base/sequence_token.h" | 18 #include "base/sequence_token.h" |
| 19 #include "base/sequenced_task_runner.h" | 19 #include "base/sequenced_task_runner.h" |
| 20 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| 21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 22 #include "base/task_runner.h" |
| 22 #include "base/task_scheduler/delayed_task_manager.h" | 23 #include "base/task_scheduler/delayed_task_manager.h" |
| 23 #include "base/task_scheduler/task_tracker.h" | 24 #include "base/task_scheduler/task_tracker.h" |
| 24 #include "base/threading/platform_thread.h" | 25 #include "base/threading/platform_thread.h" |
| 25 #include "base/threading/thread_local.h" | 26 #include "base/threading/thread_local.h" |
| 26 #include "base/threading/thread_restrictions.h" | 27 #include "base/threading/thread_restrictions.h" |
| 27 #include "base/time/time.h" | 28 #include "base/time/time.h" |
| 28 | 29 |
| 29 namespace base { | 30 namespace base { |
| 30 namespace internal { | 31 namespace internal { |
| 31 | 32 |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 | 335 |
| 335 DCHECK(!join_for_testing_returned_.IsSignaled()); | 336 DCHECK(!join_for_testing_returned_.IsSignaled()); |
| 336 join_for_testing_returned_.Signal(); | 337 join_for_testing_returned_.Signal(); |
| 337 } | 338 } |
| 338 | 339 |
| 339 void SchedulerWorkerPoolImpl::DisallowWorkerDetachmentForTesting() { | 340 void SchedulerWorkerPoolImpl::DisallowWorkerDetachmentForTesting() { |
| 340 worker_detachment_disallowed_.Set(); | 341 worker_detachment_disallowed_.Set(); |
| 341 } | 342 } |
| 342 | 343 |
| 343 scoped_refptr<TaskRunner> SchedulerWorkerPoolImpl::CreateTaskRunnerWithTraits( | 344 scoped_refptr<TaskRunner> SchedulerWorkerPoolImpl::CreateTaskRunnerWithTraits( |
| 344 const TaskTraits& traits, | 345 const TaskTraits& traits) { |
| 345 ExecutionMode execution_mode) { | 346 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this)); |
| 346 switch (execution_mode) { | 347 } |
| 347 case ExecutionMode::PARALLEL: | |
| 348 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this)); | |
| 349 | 348 |
| 350 case ExecutionMode::SEQUENCED: | 349 scoped_refptr<SequencedTaskRunner> |
| 351 return make_scoped_refptr(new SchedulerSequencedTaskRunner(traits, this)); | 350 SchedulerWorkerPoolImpl::CreateSequencedTaskRunnerWithTraits( |
| 351 const TaskTraits& traits) { |
| 352 return make_scoped_refptr(new SchedulerSequencedTaskRunner(traits, this)); |
| 353 } |
| 352 | 354 |
| 353 case ExecutionMode::SINGLE_THREADED: { | 355 scoped_refptr<SingleThreadTaskRunner> |
| 354 // TODO(fdoray): Find a way to take load into account when assigning a | 356 SchedulerWorkerPoolImpl::CreateSingleThreadTaskRunnerWithTraits( |
| 355 // SchedulerWorker to a SingleThreadTaskRunner. Also, this code | 357 const TaskTraits& traits) { |
| 356 // assumes that all SchedulerWorkers are alive. Eventually, we might | 358 // TODO(fdoray): Find a way to take load into account when assigning a |
| 357 // decide to tear down threads that haven't run tasks for a long time. | 359 // SchedulerWorker to a SingleThreadTaskRunner. |
| 358 size_t worker_index; | 360 size_t worker_index; |
| 359 { | 361 { |
| 360 AutoSchedulerLock auto_lock(next_worker_index_lock_); | 362 AutoSchedulerLock auto_lock(next_worker_index_lock_); |
| 361 worker_index = next_worker_index_; | 363 worker_index = next_worker_index_; |
| 362 next_worker_index_ = (next_worker_index_ + 1) % workers_.size(); | 364 next_worker_index_ = (next_worker_index_ + 1) % workers_.size(); |
| 363 } | |
| 364 return make_scoped_refptr(new SchedulerSingleThreadTaskRunner( | |
| 365 traits, this, workers_[worker_index].get())); | |
| 366 } | |
| 367 } | 365 } |
| 368 | 366 return make_scoped_refptr(new SchedulerSingleThreadTaskRunner( |
| 369 NOTREACHED(); | 367 traits, this, workers_[worker_index].get())); |
| 370 return nullptr; | |
| 371 } | 368 } |
| 372 | 369 |
| 373 void SchedulerWorkerPoolImpl::ReEnqueueSequence( | 370 void SchedulerWorkerPoolImpl::ReEnqueueSequence( |
| 374 scoped_refptr<Sequence> sequence, | 371 scoped_refptr<Sequence> sequence, |
| 375 const SequenceSortKey& sequence_sort_key) { | 372 const SequenceSortKey& sequence_sort_key) { |
| 376 shared_priority_queue_.BeginTransaction()->Push(std::move(sequence), | 373 shared_priority_queue_.BeginTransaction()->Push(std::move(sequence), |
| 377 sequence_sort_key); | 374 sequence_sort_key); |
| 378 | 375 |
| 379 // The thread calling this method just ran a Task from |sequence| and will | 376 // The thread calling this method just ran a Task from |sequence| and will |
| 380 // soon try to get another Sequence from which to run a Task. If the thread | 377 // soon try to get another Sequence from which to run a Task. If the thread |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 796 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); | 793 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); |
| 797 idle_workers_stack_.Remove(worker); | 794 idle_workers_stack_.Remove(worker); |
| 798 } | 795 } |
| 799 | 796 |
| 800 bool SchedulerWorkerPoolImpl::CanWorkerDetachForTesting() { | 797 bool SchedulerWorkerPoolImpl::CanWorkerDetachForTesting() { |
| 801 return !worker_detachment_disallowed_.IsSet(); | 798 return !worker_detachment_disallowed_.IsSet(); |
| 802 } | 799 } |
| 803 | 800 |
| 804 } // namespace internal | 801 } // namespace internal |
| 805 } // namespace base | 802 } // namespace base |
| OLD | NEW |