thread_controller.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2018 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_H_
  5. #define BASE_TASK_SEQUENCE_MANAGER_THREAD_CONTROLLER_H_
  6. #include "base/message_loop/message_pump.h"
  7. #include "base/run_loop.h"
  8. #include "base/single_thread_task_runner.h"
  9. #include "base/task/sequence_manager/lazy_now.h"
  10. #include "base/time/time.h"
  11. #include "build/build_config.h"
  12. namespace base {
  13. class MessageLoopBase;
  14. class TickClock;
  15. struct PendingTask;
  16. namespace sequence_manager {
  17. namespace internal {
  18. class AssociatedThreadId;
  19. class SequencedTaskSource;
  20. // Implementation of this interface is used by SequenceManager to schedule
  21. // actual work to be run. Hopefully we can stop using MessageLoop and this
  22. // interface will become more concise.
  23. class ThreadController {
  24. public:
  25. virtual ~ThreadController() = default;
  26. // Sets the number of tasks executed in a single invocation of DoWork.
  27. // Increasing the batch size can reduce the overhead of yielding back to the
  28. // main message loop.
  29. virtual void SetWorkBatchSize(int work_batch_size = 1) = 0;
  30. // Notifies that |pending_task| is about to be enqueued. Needed for tracing
  31. // purposes. The impl may use this opportunity add metadata to |pending_task|
  32. // before it is moved into the queue.
  33. virtual void WillQueueTask(PendingTask* pending_task,
  34. const char* task_queue_name) = 0;
  35. // Notify the controller that its associated sequence has immediate work
  36. // to run. Shortly after this is called, the thread associated with this
  37. // controller will run a task returned by sequence->TakeTask(). Can be called
  38. // from any sequence.
  39. //
  40. // TODO(altimin): Change this to "the thread associated with this
  41. // controller will run tasks returned by sequence->TakeTask() until it
  42. // returns null or sequence->DidRunTask() returns false" once the
  43. // code is changed to work that way.
  44. virtual void ScheduleWork() = 0;
  45. // Notify the controller that SequencedTaskSource will have a delayed work
  46. // ready to be run at |run_time|. This call cancels any previously
  47. // scheduled delayed work. Can only be called from the main sequence.
  48. // NOTE: DelayTillNextTask might return a different value as it also takes
  49. // immediate work into account.
  50. // TODO(kraynov): Remove |lazy_now| parameter.
  51. virtual void SetNextDelayedDoWork(LazyNow* lazy_now, TimeTicks run_time) = 0;
  52. // Sets the sequenced task source from which to take tasks after
  53. // a Schedule*Work() call is made.
  54. // Must be called before the first call to Schedule*Work().
  55. virtual void SetSequencedTaskSource(SequencedTaskSource*) = 0;
  56. // Requests desired timer precision from the OS.
  57. // Has no effect on some platforms.
  58. virtual void SetTimerSlack(TimerSlack timer_slack) = 0;
  59. // Completes delayed initialization of unbound ThreadControllers.
  60. // BindToCurrentThread(MessageLoopBase*) or BindToCurrentThread(MessagePump*)
  61. // may only be called once.
  62. virtual void BindToCurrentThread(
  63. std::unique_ptr<MessagePump> message_pump) = 0;
  64. // Explicitly allow or disallow task execution. Implicitly disallowed when
  65. // entering a nested runloop.
  66. virtual void SetTaskExecutionAllowed(bool allowed) = 0;
  67. // Whether task execution is allowed or not.
  68. virtual bool IsTaskExecutionAllowed() const = 0;
  69. // Returns the MessagePump we're bound to if any.
  70. virtual MessagePump* GetBoundMessagePump() const = 0;
  71. // Returns true if the current run loop should quit when idle.
  72. virtual bool ShouldQuitRunLoopWhenIdle() = 0;
  73. #if defined(OS_IOS) || defined(OS_ANDROID)
  74. // On iOS, the main message loop cannot be Run(). Instead call
  75. // AttachToMessagePump(), which connects this ThreadController to the
  76. // UI thread's CFRunLoop and allows PostTask() to work.
  77. virtual void AttachToMessagePump() = 0;
  78. #endif
  79. #if defined(OS_IOS)
  80. // Detaches this ThreadController from the message pump, allowing the
  81. // controller to be shut down cleanly.
  82. virtual void DetachFromMessagePump() = 0;
  83. #endif
  84. // TODO(altimin): Get rid of the methods below.
  85. // These methods exist due to current integration of SequenceManager
  86. // with MessageLoop.
  87. virtual bool RunsTasksInCurrentSequence() = 0;
  88. virtual const TickClock* GetClock() = 0;
  89. virtual void SetDefaultTaskRunner(scoped_refptr<SingleThreadTaskRunner>) = 0;
  90. virtual scoped_refptr<SingleThreadTaskRunner> GetDefaultTaskRunner() = 0;
  91. virtual void RestoreDefaultTaskRunner() = 0;
  92. virtual void AddNestingObserver(RunLoop::NestingObserver* observer) = 0;
  93. virtual void RemoveNestingObserver(RunLoop::NestingObserver* observer) = 0;
  94. virtual const scoped_refptr<AssociatedThreadId>& GetAssociatedThread()
  95. const = 0;
  96. };
  97. } // namespace internal
  98. } // namespace sequence_manager
  99. } // namespace base
  100. #endif // BASE_TASK_SEQUENCE_MANAGER_THREAD_CONTROLLER_H_