• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors
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_SEQUENCE_MANAGER_THREAD_CONTROLLER_IMPL_H_
6 #define BASE_TASK_SEQUENCE_MANAGER_THREAD_CONTROLLER_IMPL_H_
7 
8 #include <memory>
9 
10 #include "base/base_export.h"
11 #include "base/cancelable_callback.h"
12 #include "base/dcheck_is_on.h"
13 #include "base/memory/raw_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/run_loop.h"
16 #include "base/sequence_checker.h"
17 #include "base/task/common/task_annotator.h"
18 #include "base/task/sequence_manager/thread_controller.h"
19 #include "base/task/sequence_manager/work_deduplicator.h"
20 #include "base/task/single_thread_task_runner.h"
21 #include "build/build_config.h"
22 
23 namespace base {
24 namespace sequence_manager {
25 namespace internal {
26 class SequenceManagerImpl;
27 
28 // This is the interface between a SequenceManager which sits on top of an
29 // underlying SequenceManagerImpl or SingleThreadTaskRunner. Currently it's only
30 // used for workers in blink although we'd intend to migrate those to
31 // ThreadControllerWithMessagePumpImpl (https://crbug.com/948051). Long term we
32 // intend to use this for sequence funneling.
33 class BASE_EXPORT ThreadControllerImpl : public ThreadController,
34                                          public RunLoop::NestingObserver {
35  public:
36   ThreadControllerImpl(const ThreadControllerImpl&) = delete;
37   ThreadControllerImpl& operator=(const ThreadControllerImpl&) = delete;
38   ~ThreadControllerImpl() override;
39 
40   // TODO(https://crbug.com/948051): replace |funneled_sequence_manager| with
41   // |funneled_task_runner| when we sort out the workers
42   static std::unique_ptr<ThreadControllerImpl> Create(
43       SequenceManagerImpl* funneled_sequence_manager,
44       const TickClock* time_source);
45 
46   // ThreadController:
47   void SetWorkBatchSize(int work_batch_size) override;
48   void WillQueueTask(PendingTask* pending_task) override;
49   void ScheduleWork() override;
50   void BindToCurrentThread(std::unique_ptr<MessagePump> message_pump) override;
51   void SetNextDelayedDoWork(LazyNow* lazy_now,
52                             absl::optional<WakeUp> wake_up) override;
53   void SetSequencedTaskSource(SequencedTaskSource* sequence) override;
54   bool RunsTasksInCurrentSequence() override;
55   void SetDefaultTaskRunner(scoped_refptr<SingleThreadTaskRunner>) override;
56   scoped_refptr<SingleThreadTaskRunner> GetDefaultTaskRunner() override;
57   void RestoreDefaultTaskRunner() override;
58   void AddNestingObserver(RunLoop::NestingObserver* observer) override;
59   void RemoveNestingObserver(RunLoop::NestingObserver* observer) override;
60   void SetTaskExecutionAllowed(bool allowed) override;
61   bool IsTaskExecutionAllowed() const override;
62   MessagePump* GetBoundMessagePump() const override;
63 #if BUILDFLAG(IS_IOS) || BUILDFLAG(IS_ANDROID)
64   void AttachToMessagePump() override;
65 #endif
66 #if BUILDFLAG(IS_IOS)
67   void DetachFromMessagePump() override;
68 #endif
69   void PrioritizeYieldingToNative(base::TimeTicks prioritize_until) override;
70   bool ShouldQuitRunLoopWhenIdle() override;
71 
72   // RunLoop::NestingObserver:
73   void OnBeginNestedRunLoop() override;
74   void OnExitNestedRunLoop() override;
75 
76  protected:
77   ThreadControllerImpl(SequenceManagerImpl* sequence_manager,
78                        scoped_refptr<SingleThreadTaskRunner> task_runner,
79                        const TickClock* time_source);
80 
81   // TODO(altimin): Make these const. Blocked on removing
82   // lazy initialisation support.
83   raw_ptr<SequenceManagerImpl> funneled_sequence_manager_;
84   scoped_refptr<SingleThreadTaskRunner> task_runner_;
85 
86   raw_ptr<RunLoop::NestingObserver> nesting_observer_ = nullptr;
87 
88  private:
89   enum class WorkType { kImmediate, kDelayed };
90 
91   void DoWork(WorkType work_type);
92 
93   // TODO(scheduler-dev): Maybe fold this into the main class and use
94   // thread annotations.
95   struct MainSequenceOnly {
96     MainSequenceOnly();
97     ~MainSequenceOnly();
98 
99     int work_batch_size_ = 1;
100 
101     TimeTicks next_delayed_do_work = TimeTicks::Max();
102   };
103 
104   MainSequenceOnly main_sequence_only_;
main_sequence_only()105   MainSequenceOnly& main_sequence_only() {
106     DCHECK_CALLED_ON_VALID_SEQUENCE(associated_thread_->sequence_checker);
107     return main_sequence_only_;
108   }
main_sequence_only()109   const MainSequenceOnly& main_sequence_only() const {
110     DCHECK_CALLED_ON_VALID_SEQUENCE(associated_thread_->sequence_checker);
111     return main_sequence_only_;
112   }
113 
114   scoped_refptr<SingleThreadTaskRunner> message_loop_task_runner_;
115   RepeatingClosure immediate_do_work_closure_;
116   RepeatingClosure delayed_do_work_closure_;
117   CancelableRepeatingClosure cancelable_delayed_do_work_closure_;
118   raw_ptr<SequencedTaskSource> sequence_ = nullptr;  // Not owned.
119   TaskAnnotator task_annotator_;
120   WorkDeduplicator work_deduplicator_;
121 
122 #if DCHECK_IS_ON()
123   bool default_task_runner_set_ = false;
124 #endif
125 
126   WeakPtrFactory<ThreadControllerImpl> weak_factory_{this};
127 };
128 
129 }  // namespace internal
130 }  // namespace sequence_manager
131 }  // namespace base
132 
133 #endif  // BASE_TASK_SEQUENCE_MANAGER_THREAD_CONTROLLER_IMPL_H_
134