OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ |
| 6 #define CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ |
| 7 |
| 8 #include <map> |
| 9 |
| 10 #include "base/atomic_sequence_num.h" |
| 11 #include "base/debug/task_annotator.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/pending_task.h" |
| 16 #include "base/synchronization/lock.h" |
| 17 #include "base/threading/thread_checker.h" |
| 18 #include "components/scheduler/child/task_queue_impl.h" |
| 19 #include "components/scheduler/child/task_queue_selector.h" |
| 20 #include "components/scheduler/scheduler_export.h" |
| 21 |
| 22 namespace base { |
| 23 class TickClock; |
| 24 |
| 25 namespace trace_event { |
| 26 class ConvertableToTraceFormat; |
| 27 class TracedValue; |
| 28 } // namespace trace_event |
| 29 } // namespace base |
| 30 |
| 31 namespace scheduler { |
| 32 namespace internal { |
| 33 class LazyNow; |
| 34 class TaskQueueImpl; |
| 35 } // namespace internal |
| 36 |
| 37 class NestableSingleThreadTaskRunner; |
| 38 |
| 39 // The task queue manager provides N task queues and a selector interface for |
| 40 // choosing which task queue to service next. Each task queue consists of two |
| 41 // sub queues: |
| 42 // |
| 43 // 1. Incoming task queue. Tasks that are posted get immediately appended here. |
| 44 // When a task is appended into an empty incoming queue, the task manager |
| 45 // work function (DoWork) is scheduled to run on the main task runner. |
| 46 // |
| 47 // 2. Work queue. If a work queue is empty when DoWork() is entered, tasks from |
| 48 // the incoming task queue (if any) are moved here. The work queues are |
| 49 // registered with the selector as input to the scheduling decision. |
| 50 // |
| 51 class SCHEDULER_EXPORT TaskQueueManager |
| 52 : public internal::TaskQueueSelector::Observer { |
| 53 public: |
| 54 // Create a task queue manager where |main_task_runner| identifies the thread |
| 55 // on which where the tasks are eventually run. Category strings must have |
| 56 // application lifetime (statics or literals). They may not include " chars. |
| 57 TaskQueueManager( |
| 58 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner, |
| 59 const char* disabled_by_default_tracing_category, |
| 60 const char* disabled_by_default_verbose_tracing_category); |
| 61 ~TaskQueueManager() override; |
| 62 |
| 63 // Returns the time of the next pending delayed task in any queue. Ignores |
| 64 // any delayed tasks whose delay has expired. Returns a null TimeTicks object |
| 65 // if no tasks are pending. NOTE this is somewhat expensive since every queue |
| 66 // will get locked. |
| 67 base::TimeTicks NextPendingDelayedTaskRunTime(); |
| 68 |
| 69 // Set the number of tasks executed in a single invocation of the task queue |
| 70 // manager. Increasing the batch size can reduce the overhead of yielding |
| 71 // back to the main message loop -- at the cost of potentially delaying other |
| 72 // tasks posted to the main loop. The batch size is 1 by default. |
| 73 void SetWorkBatchSize(int work_batch_size); |
| 74 |
| 75 // These functions can only be called on the same thread that the task queue |
| 76 // manager executes its tasks on. |
| 77 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer); |
| 78 void RemoveTaskObserver(base::MessageLoop::TaskObserver* task_observer); |
| 79 |
| 80 void SetTimeSourceForTesting(scoped_ptr<base::TickClock> time_source); |
| 81 |
| 82 // Returns true if any task from a monitored task queue was was run since the |
| 83 // last call to GetAndClearSystemIsQuiescentBit. |
| 84 bool GetAndClearSystemIsQuiescentBit(); |
| 85 |
| 86 // Creates a task queue with the given |spec|. Must be called on the thread |
| 87 // this class was created on. |
| 88 scoped_refptr<internal::TaskQueueImpl> NewTaskQueue( |
| 89 const TaskQueue::Spec& spec); |
| 90 |
| 91 class SCHEDULER_EXPORT Observer { |
| 92 public: |
| 93 virtual ~Observer() {} |
| 94 |
| 95 // Called when |queue| is unregistered. |
| 96 virtual void OnUnregisterTaskQueue( |
| 97 const scoped_refptr<internal::TaskQueueImpl>& queue) = 0; |
| 98 }; |
| 99 |
| 100 // Called once to set the Observer. This function is called on the main |
| 101 // thread. If |observer| is null, then no callbacks will occur. |
| 102 // Note |observer| is expected to outlive the SchedulerHelper. |
| 103 void SetObserver(Observer* observer); |
| 104 |
| 105 private: |
| 106 friend class internal::LazyNow; |
| 107 friend class internal::TaskQueueImpl; |
| 108 friend class TaskQueueManagerTest; |
| 109 |
| 110 class DeletionSentinel : public base::RefCounted<DeletionSentinel> { |
| 111 private: |
| 112 friend class base::RefCounted<DeletionSentinel>; |
| 113 ~DeletionSentinel() {} |
| 114 }; |
| 115 |
| 116 // Unregisters a TaskQueue previously created by |NewTaskQueue()|. |
| 117 // NOTE we have to flush the queue from |newly_updatable_| which means as a |
| 118 // side effect MoveNewlyUpdatableQueuesIntoUpdatableQueueSet is called by this |
| 119 // function. |
| 120 void UnregisterTaskQueue(scoped_refptr<internal::TaskQueueImpl> task_queue); |
| 121 |
| 122 // TaskQueueSelector::Observer implementation: |
| 123 void OnTaskQueueEnabled(internal::TaskQueueImpl* queue) override; |
| 124 |
| 125 // Called by the task queue to register a new pending task. |
| 126 void DidQueueTask(const internal::TaskQueueImpl::Task& pending_task); |
| 127 |
| 128 // Post a task to call DoWork() on the main task runner. Only one pending |
| 129 // DoWork is allowed from the main thread, to prevent an explosion of pending |
| 130 // DoWorks. |
| 131 void MaybePostDoWorkOnMainRunner(); |
| 132 |
| 133 // Use the selector to choose a pending task and run it. |
| 134 void DoWork(bool decrement_pending_dowork_count); |
| 135 |
| 136 // Delayed Tasks with run_times <= Now() are enqueued onto the work queue. |
| 137 // Reloads any empty work queues which have automatic pumping enabled and |
| 138 // which are eligible to be auto pumped based on the |previous_task| which was |
| 139 // run and |should_trigger_wakeup|. Call with an empty |previous_task| if no |
| 140 // task was just run. |
| 141 void UpdateWorkQueues(bool should_trigger_wakeup, |
| 142 const internal::TaskQueueImpl::Task* previous_task); |
| 143 |
| 144 // Chooses the next work queue to service. Returns true if |out_queue| |
| 145 // indicates the queue from which the next task should be run, false to |
| 146 // avoid running any tasks. |
| 147 bool SelectQueueToService(internal::TaskQueueImpl** out_queue); |
| 148 |
| 149 // Runs a single nestable task from the |queue|. On exit, |out_task| will |
| 150 // contain the task which was executed. Non-nestable task are reposted on the |
| 151 // run loop. The queue must not be empty. |
| 152 enum class ProcessTaskResult { |
| 153 DEFERRED, |
| 154 EXECUTED, |
| 155 TASK_QUEUE_MANAGER_DELETED |
| 156 }; |
| 157 ProcessTaskResult ProcessTaskFromWorkQueue( |
| 158 internal::TaskQueueImpl* queue, |
| 159 internal::TaskQueueImpl::Task* out_previous_task); |
| 160 |
| 161 bool RunsTasksOnCurrentThread() const; |
| 162 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 163 const base::Closure& task, |
| 164 base::TimeDelta delay); |
| 165 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| 166 const base::Closure& task, |
| 167 base::TimeDelta delay); |
| 168 |
| 169 base::TimeTicks Now() const; |
| 170 |
| 171 int GetNextSequenceNumber(); |
| 172 |
| 173 scoped_refptr<base::trace_event::ConvertableToTraceFormat> |
| 174 AsValueWithSelectorResult(bool should_run, |
| 175 internal::TaskQueueImpl* selected_queue) const; |
| 176 |
| 177 // Causes DoWork to start calling UpdateWorkQueue for |queue|. Can be called |
| 178 // from any thread. |
| 179 void RegisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); |
| 180 |
| 181 // Prevents DoWork from calling UpdateWorkQueue for |queue|. Must be called |
| 182 // from the thread the TaskQueueManager was created on. |
| 183 void UnregisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); |
| 184 |
| 185 // Schedule a call to DelayedDoWork at |delayed_run_time| which will call |
| 186 // TaskQueueImpl::MoveReadyDelayedTasksToIncomingQueue for |queue|. |
| 187 // Can be called from any thread. |
| 188 void ScheduleDelayedWork(internal::TaskQueueImpl* queue, |
| 189 base::TimeTicks delayed_run_time, |
| 190 internal::LazyNow* lazy_now); |
| 191 |
| 192 // Function calling ScheduleDelayedWork that's suitable for use in base::Bind. |
| 193 void ScheduleDelayedWorkTask(scoped_refptr<internal::TaskQueueImpl> queue, |
| 194 base::TimeTicks delayed_run_time); |
| 195 |
| 196 // Calls WakeupReadyDelayedQueues followed by DoWork so that ready delayed |
| 197 // tasks are enqueued and run. Must be called from the main thread. |
| 198 void DelayedDoWork(); |
| 199 |
| 200 // Call TaskQueueImpl::MoveReadyDelayedTasksToIncomingQueue for each |
| 201 // registered queue for which the delay has elapsed. |
| 202 void WakeupReadyDelayedQueues(internal::LazyNow* lazy_now); |
| 203 |
| 204 void MoveNewlyUpdatableQueuesIntoUpdatableQueueSet(); |
| 205 |
| 206 std::set<scoped_refptr<internal::TaskQueueImpl>> queues_; |
| 207 |
| 208 // We have to be careful when deleting a queue because some of the code uses |
| 209 // raw pointers and doesn't expect the rug to be pulled out from underneath. |
| 210 std::set<scoped_refptr<internal::TaskQueueImpl>> queues_to_delete_; |
| 211 |
| 212 // This lock guards only |newly_updatable_|. It's not expected to be heavily |
| 213 // contended. |
| 214 base::Lock newly_updatable_lock_; |
| 215 std::vector<internal::TaskQueueImpl*> newly_updatable_; |
| 216 |
| 217 // Set of task queues with avaliable work on the incoming queue. This should |
| 218 // only be accessed from the main thread. |
| 219 std::set<internal::TaskQueueImpl*> updatable_queue_set_; |
| 220 |
| 221 typedef std::multimap<base::TimeTicks, internal::TaskQueueImpl*> |
| 222 DelayedWakeupMultimap; |
| 223 |
| 224 DelayedWakeupMultimap delayed_wakeup_map_; |
| 225 |
| 226 base::AtomicSequenceNumber task_sequence_num_; |
| 227 base::debug::TaskAnnotator task_annotator_; |
| 228 |
| 229 base::ThreadChecker main_thread_checker_; |
| 230 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner_; |
| 231 internal::TaskQueueSelector selector_; |
| 232 |
| 233 base::Closure do_work_from_main_thread_closure_; |
| 234 base::Closure do_work_from_other_thread_closure_; |
| 235 base::Closure delayed_queue_wakeup_closure_; |
| 236 |
| 237 bool task_was_run_on_quiescence_monitored_queue_; |
| 238 |
| 239 // The pending_dowork_count_ is only tracked on the main thread since that's |
| 240 // where re-entrant problems happen. |
| 241 int pending_dowork_count_; |
| 242 |
| 243 int work_batch_size_; |
| 244 |
| 245 scoped_ptr<base::TickClock> time_source_; |
| 246 |
| 247 base::ObserverList<base::MessageLoop::TaskObserver> task_observers_; |
| 248 |
| 249 const char* disabled_by_default_tracing_category_; |
| 250 const char* disabled_by_default_verbose_tracing_category_; |
| 251 |
| 252 Observer* observer_; // NOT OWNED |
| 253 scoped_refptr<DeletionSentinel> deletion_sentinel_; |
| 254 base::WeakPtrFactory<TaskQueueManager> weak_factory_; |
| 255 |
| 256 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); |
| 257 }; |
| 258 |
| 259 } // namespace scheduler |
| 260 |
| 261 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ |
OLD | NEW |