| // Copyright 2016 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef BASE_TASK_SCHEDULER_TASK_SCHEDULER_IMPL_H_ |
| #define BASE_TASK_SCHEDULER_TASK_SCHEDULER_IMPL_H_ |
| |
| #include <memory> |
| #include <vector> |
| |
| #include "base/base_export.h" |
| #include "base/callback.h" |
| #include "base/logging.h" |
| #include "base/macros.h" |
| #include "base/memory/ref_counted.h" |
| #include "base/synchronization/atomic_flag.h" |
| #include "base/task_scheduler/scheduler_worker_pool_impl.h" |
| #include "base/task_scheduler/sequence.h" |
| #include "base/task_scheduler/task_scheduler.h" |
| #include "base/task_scheduler/task_traits.h" |
| #include "base/threading/thread.h" |
| #include "build/build_config.h" |
| |
| namespace base { |
| |
| class HistogramBase; |
| |
| namespace internal { |
| |
| class DelayedTaskManager; |
| class SchedulerSingleThreadTaskRunnerManager; |
| class TaskTracker; |
| |
| // Default TaskScheduler implementation. This class is thread-safe. |
| class BASE_EXPORT TaskSchedulerImpl : public TaskScheduler { |
| public: |
| // Creates and returns an initialized TaskSchedulerImpl. CHECKs on failure. |
| // |name| is used to label threads and histograms. It should identify the |
| // component that creates the TaskScheduler. |init_params| contains params to |
| // initialize worker pools. |
| // |
| // Note: The names and priority hints in |init_params| are ignored. |
| // https://crbug.com/690706 |
| static std::unique_ptr<TaskSchedulerImpl> Create( |
| const std::string& name, |
| const TaskScheduler::InitParams& init_params); |
| |
| ~TaskSchedulerImpl() override; |
| |
| // TaskScheduler: |
| void PostDelayedTaskWithTraits(const tracked_objects::Location& from_here, |
| const TaskTraits& traits, |
| OnceClosure task, |
| TimeDelta delay) override; |
| scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
| const TaskTraits& traits) override; |
| scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits( |
| const TaskTraits& traits) override; |
| scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits( |
| const TaskTraits& traits) override; |
| #if defined(OS_WIN) |
| scoped_refptr<SingleThreadTaskRunner> CreateCOMSTATaskRunnerWithTraits( |
| const TaskTraits& traits) override; |
| #endif // defined(OS_WIN) |
| std::vector<const HistogramBase*> GetHistograms() const override; |
| int GetMaxConcurrentTasksWithTraitsDeprecated( |
| const TaskTraits& traits) const override; |
| void Shutdown() override; |
| void FlushForTesting() override; |
| void JoinForTesting() override; |
| |
| private: |
| explicit TaskSchedulerImpl(const std::string& name); |
| |
| void Initialize(const TaskScheduler::InitParams& init_params); |
| |
| // Returns the worker pool that runs Tasks with |traits|. |
| SchedulerWorkerPoolImpl* GetWorkerPoolForTraits( |
| const TaskTraits& traits) const; |
| |
| // Callback invoked when a non-single-thread |sequence| isn't empty after a |
| // worker pops a Task from it. |
| void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence); |
| |
| const std::string name_; |
| Thread service_thread_; |
| std::unique_ptr<TaskTracker> task_tracker_; |
| std::unique_ptr<DelayedTaskManager> delayed_task_manager_; |
| std::unique_ptr<SchedulerSingleThreadTaskRunnerManager> |
| single_thread_task_runner_manager_; |
| |
| // There are 4 SchedulerWorkerPoolImpl in this array to match the 4 |
| // SchedulerWorkerPoolParams in TaskScheduler::InitParams. |
| std::unique_ptr<SchedulerWorkerPoolImpl> worker_pools_[4]; |
| |
| #if DCHECK_IS_ON() |
| // Set once JoinForTesting() has returned. |
| AtomicFlag join_for_testing_returned_; |
| #endif |
| |
| DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImpl); |
| }; |
| |
| } // namespace internal |
| } // namespace base |
| |
| #endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_IMPL_H_ |