• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef BASE_TASK_SCHEDULER_TASK_SCHEDULER_IMPL_H_
6 #define BASE_TASK_SCHEDULER_TASK_SCHEDULER_IMPL_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/base_export.h"
12 #include "base/callback.h"
13 #include "base/logging.h"
14 #include "base/macros.h"
15 #include "base/memory/ptr_util.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/strings/string_piece.h"
18 #include "base/synchronization/atomic_flag.h"
19 #include "base/task_scheduler/delayed_task_manager.h"
20 #include "base/task_scheduler/environment_config.h"
21 #include "base/task_scheduler/scheduler_single_thread_task_runner_manager.h"
22 #include "base/task_scheduler/scheduler_worker_pool_impl.h"
23 #include "base/task_scheduler/single_thread_task_runner_thread_mode.h"
24 #include "base/task_scheduler/task_scheduler.h"
25 #include "base/task_scheduler/task_tracker.h"
26 #include "base/task_scheduler/task_traits.h"
27 #include "build/build_config.h"
28 
29 #if defined(OS_POSIX) && !defined(OS_NACL_SFI)
30 #include "base/task_scheduler/task_tracker_posix.h"
31 #endif
32 
33 #if defined(OS_WIN)
34 #include "base/win/com_init_check_hook.h"
35 #endif
36 
37 namespace base {
38 
39 class HistogramBase;
40 class Thread;
41 
42 namespace internal {
43 
44 // Default TaskScheduler implementation. This class is thread-safe.
45 class BASE_EXPORT TaskSchedulerImpl : public TaskScheduler {
46  public:
47   using TaskTrackerImpl =
48 #if defined(OS_POSIX) && !defined(OS_NACL_SFI)
49       TaskTrackerPosix;
50 #else
51       TaskTracker;
52 #endif
53 
54   // Creates a TaskSchedulerImpl with a production TaskTracker.
55   //|histogram_label| is used to label histograms, it must not be empty.
56   explicit TaskSchedulerImpl(StringPiece histogram_label);
57 
58   // For testing only. Creates a TaskSchedulerImpl with a custom TaskTracker.
59   TaskSchedulerImpl(StringPiece histogram_label,
60                     std::unique_ptr<TaskTrackerImpl> task_tracker);
61 
62   ~TaskSchedulerImpl() override;
63 
64   // TaskScheduler:
65   void Start(const TaskScheduler::InitParams& init_params,
66              SchedulerWorkerObserver* scheduler_worker_observer) override;
67   void PostDelayedTaskWithTraits(const Location& from_here,
68                                  const TaskTraits& traits,
69                                  OnceClosure task,
70                                  TimeDelta delay) override;
71   scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(
72       const TaskTraits& traits) override;
73   scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits(
74       const TaskTraits& traits) override;
75   scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits(
76       const TaskTraits& traits,
77       SingleThreadTaskRunnerThreadMode thread_mode) override;
78 #if defined(OS_WIN)
79   scoped_refptr<SingleThreadTaskRunner> CreateCOMSTATaskRunnerWithTraits(
80       const TaskTraits& traits,
81       SingleThreadTaskRunnerThreadMode thread_mode) override;
82 #endif  // defined(OS_WIN)
83   std::vector<const HistogramBase*> GetHistograms() const override;
84   int GetMaxConcurrentNonBlockedTasksWithTraitsDeprecated(
85       const TaskTraits& traits) const override;
86   void Shutdown() override;
87   void FlushForTesting() override;
88   void FlushAsyncForTesting(OnceClosure flush_callback) override;
89   void JoinForTesting() override;
90 
91  private:
92   // Returns the worker pool that runs Tasks with |traits|.
93   SchedulerWorkerPoolImpl* GetWorkerPoolForTraits(
94       const TaskTraits& traits) const;
95 
96   // Returns |traits|, with priority set to TaskPriority::USER_BLOCKING if
97   // |all_tasks_user_blocking_| is set.
98   TaskTraits SetUserBlockingPriorityIfNeeded(const TaskTraits& traits) const;
99 
100   const std::unique_ptr<TaskTrackerImpl> task_tracker_;
101   std::unique_ptr<Thread> service_thread_;
102   DelayedTaskManager delayed_task_manager_;
103   SchedulerSingleThreadTaskRunnerManager single_thread_task_runner_manager_;
104 
105   // Indicates that all tasks are handled as if they had been posted with
106   // TaskPriority::USER_BLOCKING. Since this is set in Start(), it doesn't apply
107   // to tasks posted before Start() or to tasks posted to TaskRunners created
108   // before Start().
109   //
110   // TODO(fdoray): Remove after experiment. https://crbug.com/757022
111   AtomicFlag all_tasks_user_blocking_;
112 
113   // Owns all the pools managed by this TaskScheduler.
114   std::vector<std::unique_ptr<SchedulerWorkerPoolImpl>> worker_pools_;
115 
116   // Maps an environment from EnvironmentType to a pool in |worker_pools_|.
117   SchedulerWorkerPoolImpl* environment_to_worker_pool_[static_cast<int>(
118       EnvironmentType::ENVIRONMENT_COUNT)];
119 
120 #if DCHECK_IS_ON()
121   // Set once JoinForTesting() has returned.
122   AtomicFlag join_for_testing_returned_;
123 #endif
124 
125 #if defined(OS_WIN) && defined(COM_INIT_CHECK_HOOK_ENABLED)
126   // Provides COM initialization verification for supported builds.
127   base::win::ComInitCheckHook com_init_check_hook_;
128 #endif
129 
130   DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImpl);
131 };
132 
133 }  // namespace internal
134 }  // namespace base
135 
136 #endif  // BASE_TASK_SCHEDULER_TASK_SCHEDULER_IMPL_H_
137