| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 COMPONENTS_SCHEDULER_CHILD_SCHEDULER_HELPER_H_ | |
| 6 #define COMPONENTS_SCHEDULER_CHILD_SCHEDULER_HELPER_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/time/tick_clock.h" | |
| 12 #include "components/scheduler/base/task_queue_manager.h" | |
| 13 #include "components/scheduler/base/task_queue_selector.h" | |
| 14 #include "components/scheduler/scheduler_export.h" | |
| 15 | |
| 16 namespace base { | |
| 17 class TickClock; | |
| 18 } | |
| 19 | |
| 20 namespace scheduler { | |
| 21 | |
| 22 class SchedulerTqmDelegate; | |
| 23 | |
| 24 // Common scheduler functionality for default tasks. | |
| 25 class SCHEDULER_EXPORT SchedulerHelper | |
| 26 : public TaskQueueManager::Observer { | |
| 27 public: | |
| 28 // Category strings must have application lifetime (statics or | |
| 29 // literals). They may not include " chars. | |
| 30 SchedulerHelper( | |
| 31 scoped_refptr<SchedulerTqmDelegate> task_queue_manager_delegate, | |
| 32 const char* tracing_category, | |
| 33 const char* disabled_by_default_tracing_category, | |
| 34 const char* disabled_by_default_verbose_tracing_category); | |
| 35 ~SchedulerHelper() override; | |
| 36 | |
| 37 // TaskQueueManager::Observer implementation: | |
| 38 void OnUnregisterTaskQueue(const scoped_refptr<TaskQueue>& queue) override; | |
| 39 void OnTriedToExecuteBlockedTask(const TaskQueue& queue, | |
| 40 const base::PendingTask& task) override; | |
| 41 | |
| 42 // Returns the default task runner. | |
| 43 scoped_refptr<TaskQueue> DefaultTaskRunner(); | |
| 44 | |
| 45 // Returns the control task runner. Tasks posted to this runner are executed | |
| 46 // with the highest priority. Care must be taken to avoid starvation of other | |
| 47 // task queues. | |
| 48 scoped_refptr<TaskQueue> ControlTaskRunner(); | |
| 49 | |
| 50 // Returns the control task after wakeup runner. Tasks posted to this runner | |
| 51 // are executed with the highest priority but do not cause the scheduler to | |
| 52 // wake up. Care must be taken to avoid starvation of other task queues. | |
| 53 scoped_refptr<TaskQueue> ControlAfterWakeUpTaskRunner(); | |
| 54 | |
| 55 // Adds or removes a task observer from the scheduler. The observer will be | |
| 56 // notified before and after every executed task. These functions can only be | |
| 57 // called on the thread this class was created on. | |
| 58 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer); | |
| 59 void RemoveTaskObserver(base::MessageLoop::TaskObserver* task_observer); | |
| 60 | |
| 61 void SetTaskTimeTracker(TaskTimeTracker* task_time_tracker) { | |
| 62 if (task_queue_manager_) | |
| 63 task_queue_manager_->SetTaskTimeTracker(task_time_tracker); | |
| 64 } | |
| 65 | |
| 66 // Shuts down the scheduler by dropping any remaining pending work in the work | |
| 67 // queues. After this call any work posted to the task runners will be | |
| 68 // silently dropped. | |
| 69 void Shutdown(); | |
| 70 | |
| 71 // Returns true if Shutdown() has been called. Otherwise returns false. | |
| 72 bool IsShutdown() const { return !task_queue_manager_.get(); } | |
| 73 | |
| 74 void CheckOnValidThread() const { | |
| 75 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 76 } | |
| 77 | |
| 78 // Creates a new TaskQueue with the given |spec|. | |
| 79 scoped_refptr<TaskQueue> NewTaskQueue(const TaskQueue::Spec& spec); | |
| 80 | |
| 81 class SCHEDULER_EXPORT Observer { | |
| 82 public: | |
| 83 virtual ~Observer() {} | |
| 84 | |
| 85 // Called when |queue| is unregistered. | |
| 86 virtual void OnUnregisterTaskQueue( | |
| 87 const scoped_refptr<TaskQueue>& queue) = 0; | |
| 88 | |
| 89 // Called when the scheduler tried to execute a task from a disabled | |
| 90 // queue. See TaskQueue::Spec::SetShouldReportWhenExecutionBlocked. | |
| 91 virtual void OnTriedToExecuteBlockedTask(const TaskQueue& queue, | |
| 92 const base::PendingTask& task) = 0; | |
| 93 }; | |
| 94 | |
| 95 // Called once to set the Observer. This function is called on the main | |
| 96 // thread. If |observer| is null, then no callbacks will occur. | |
| 97 // Note |observer| is expected to outlive the SchedulerHelper. | |
| 98 void SetObserver(Observer* observer); | |
| 99 | |
| 100 // Accessor methods. | |
| 101 RealTimeDomain* real_time_domain() const; | |
| 102 void RegisterTimeDomain(TimeDomain* time_domain); | |
| 103 void UnregisterTimeDomain(TimeDomain* time_domain); | |
| 104 const scoped_refptr<SchedulerTqmDelegate>& scheduler_tqm_delegate() const; | |
| 105 bool GetAndClearSystemIsQuiescentBit(); | |
| 106 TaskQueue* CurrentlyExecutingTaskQueue() const; | |
| 107 | |
| 108 // Test helpers. | |
| 109 void SetWorkBatchSizeForTesting(size_t work_batch_size); | |
| 110 TaskQueueManager* GetTaskQueueManagerForTesting(); | |
| 111 | |
| 112 private: | |
| 113 friend class SchedulerHelperTest; | |
| 114 | |
| 115 base::ThreadChecker thread_checker_; | |
| 116 scoped_refptr<SchedulerTqmDelegate> task_queue_manager_delegate_; | |
| 117 std::unique_ptr<TaskQueueManager> task_queue_manager_; | |
| 118 scoped_refptr<TaskQueue> control_task_runner_; | |
| 119 scoped_refptr<TaskQueue> control_after_wakeup_task_runner_; | |
| 120 scoped_refptr<TaskQueue> default_task_runner_; | |
| 121 | |
| 122 Observer* observer_; // NOT OWNED | |
| 123 const char* tracing_category_; | |
| 124 const char* disabled_by_default_tracing_category_; | |
| 125 | |
| 126 DISALLOW_COPY_AND_ASSIGN(SchedulerHelper); | |
| 127 }; | |
| 128 | |
| 129 } // namespace scheduler | |
| 130 | |
| 131 #endif // COMPONENTS_SCHEDULER_CHILD_SCHEDULER_HELPER_H_ | |
| OLD | NEW |