1 // Copyright 2017 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_SEQUENCE_MANAGER_THREAD_CONTROLLER_IMPL_H_ 6 #define BASE_TASK_SEQUENCE_MANAGER_THREAD_CONTROLLER_IMPL_H_ 7 8 #include "base/cancelable_callback.h" 9 #include "base/debug/task_annotator.h" 10 #include "base/macros.h" 11 #include "base/memory/weak_ptr.h" 12 #include "base/run_loop.h" 13 #include "base/sequence_checker.h" 14 #include "base/single_thread_task_runner.h" 15 #include "base/task/sequence_manager/thread_controller.h" 16 17 namespace base { 18 19 // TODO(kraynov): https://crbug.com/828835 20 // Consider going away from using MessageLoop in the renderer process. 21 class MessageLoop; 22 23 namespace sequence_manager { 24 namespace internal { 25 26 // TODO(kraynov): Rename to ThreadControllerWithMessageLoopImpl. 27 class BASE_EXPORT ThreadControllerImpl : public ThreadController, 28 public RunLoop::NestingObserver { 29 public: 30 ~ThreadControllerImpl() override; 31 32 static std::unique_ptr<ThreadControllerImpl> Create( 33 MessageLoop* message_loop, 34 const TickClock* time_source); 35 36 // ThreadController: 37 void SetWorkBatchSize(int work_batch_size) override; 38 void WillQueueTask(PendingTask* pending_task) override; 39 void ScheduleWork() override; 40 void SetNextDelayedDoWork(LazyNow* lazy_now, TimeTicks run_time) override; 41 void SetSequencedTaskSource(SequencedTaskSource* sequence) override; 42 bool RunsTasksInCurrentSequence() override; 43 const TickClock* GetClock() override; 44 void SetDefaultTaskRunner(scoped_refptr<SingleThreadTaskRunner>) override; 45 void RestoreDefaultTaskRunner() override; 46 void AddNestingObserver(RunLoop::NestingObserver* observer) override; 47 void RemoveNestingObserver(RunLoop::NestingObserver* observer) override; 48 49 // RunLoop::NestingObserver: 50 void OnBeginNestedRunLoop() override; 51 void OnExitNestedRunLoop() override; 52 53 protected: 54 ThreadControllerImpl(MessageLoop* message_loop, 55 scoped_refptr<SingleThreadTaskRunner> task_runner, 56 const TickClock* time_source); 57 58 // TODO(altimin): Make these const. Blocked on removing 59 // lazy initialisation support. 60 MessageLoop* message_loop_; 61 scoped_refptr<SingleThreadTaskRunner> task_runner_; 62 63 RunLoop::NestingObserver* nesting_observer_ = nullptr; 64 65 private: 66 enum class WorkType { kImmediate, kDelayed }; 67 68 void DoWork(WorkType work_type); 69 70 struct AnySequence { 71 AnySequence(); 72 ~AnySequence(); 73 74 int do_work_running_count = 0; 75 int nesting_depth = 0; 76 bool immediate_do_work_posted = false; 77 }; 78 79 mutable Lock any_sequence_lock_; 80 AnySequence any_sequence_; 81 any_sequence()82 struct AnySequence& any_sequence() { 83 any_sequence_lock_.AssertAcquired(); 84 return any_sequence_; 85 } any_sequence()86 const struct AnySequence& any_sequence() const { 87 any_sequence_lock_.AssertAcquired(); 88 return any_sequence_; 89 } 90 91 struct MainSequenceOnly { 92 MainSequenceOnly(); 93 ~MainSequenceOnly(); 94 95 int do_work_running_count = 0; 96 int nesting_depth = 0; 97 int work_batch_size_ = 1; 98 99 TimeTicks next_delayed_do_work = TimeTicks::Max(); 100 }; 101 102 SEQUENCE_CHECKER(sequence_checker_); 103 MainSequenceOnly main_sequence_only_; main_sequence_only()104 MainSequenceOnly& main_sequence_only() { 105 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 106 return main_sequence_only_; 107 } main_sequence_only()108 const MainSequenceOnly& main_sequence_only() const { 109 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 110 return main_sequence_only_; 111 } 112 113 scoped_refptr<SingleThreadTaskRunner> message_loop_task_runner_; 114 const TickClock* time_source_; 115 RepeatingClosure immediate_do_work_closure_; 116 RepeatingClosure delayed_do_work_closure_; 117 CancelableClosure cancelable_delayed_do_work_closure_; 118 SequencedTaskSource* sequence_ = nullptr; // Not owned. 119 debug::TaskAnnotator task_annotator_; 120 121 WeakPtrFactory<ThreadControllerImpl> weak_factory_; 122 123 DISALLOW_COPY_AND_ASSIGN(ThreadControllerImpl); 124 }; 125 126 } // namespace internal 127 } // namespace sequence_manager 128 } // namespace base 129 130 #endif // BASE_TASK_SEQUENCE_MANAGER_THREAD_CONTROLLER_IMPL_H_ 131