thread_controller_impl.h 5.1 KB

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