Chromium Code Reviews| 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 "base/atomic_sequence_num.h" | |
| 9 #include "base/debug/task_annotator.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/scoped_vector.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "base/pending_task.h" | |
| 14 #include "base/single_thread_task_runner.h" | |
| 15 #include "base/synchronization/lock.h" | |
| 16 #include "base/threading/thread_checker.h" | |
| 17 #include "content/common/content_export.h" | |
| 18 | |
| 19 namespace content { | |
| 20 class TaskQueueSelector; | |
| 21 | |
| 22 // The task queue manager provides N task queues and a selector interface for | |
| 23 // choosing which task queue to service next. Each task queue consists of two | |
| 24 // sub queues: | |
| 25 // | |
| 26 // 1. Incoming task queue. Tasks that are posted get immediately appended here. | |
| 27 // When a task is appended into an empty incoming queue, the task manager | |
| 28 // work function (DoWork) is scheduled to run on the main task runner. | |
| 29 // | |
| 30 // 2. Work queue. If a work queue is empty when DoWork() is entered, tasks from | |
| 31 // the incoming task queue (if any) are moved here. The work queues are | |
| 32 // registered with the selector as input to the scheduling decision. | |
| 33 // | |
| 34 class CONTENT_EXPORT TaskQueueManager { | |
| 35 public: | |
| 36 // Create a task queue manager with |task_queue_count| task queues. | |
| 37 // |main_task_runner| identifies the thread on which where the tasks are | |
| 38 // eventually run. |selector| is used to choose which task queue to service. | |
| 39 // It should outlive this class. | |
| 40 TaskQueueManager(size_t task_queue_count, | |
| 41 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | |
| 42 TaskQueueSelector* selector); | |
| 43 ~TaskQueueManager(); | |
| 44 | |
| 45 // Returns the task runner which targets the queue selected by |queue_index|. | |
| 46 scoped_refptr<base::SingleThreadTaskRunner> TaskRunnerForQueue( | |
| 47 size_t queue_index); | |
| 48 | |
| 49 // If |auto_pump| is false, tasks posted to the given incoming queue will not | |
| 50 // be automatically scheduled for execution or transferred to the work queue. | |
| 51 // Instead, the selector should call PumpQueue() when necessary to bring in | |
| 52 // new tasks for execution. | |
| 53 void SetAutoPump(size_t queue_index, bool auto_pump); | |
| 54 | |
| 55 // Reloads new tasks from the incoming queue for |queue_index|. After this, | |
| 56 // this function ensures that the tasks in the work queue, if any, are | |
| 57 // scheduled for execution. | |
| 58 // | |
| 59 // This function only needs to be called if automatic pumping is disabled | |
| 60 // for |queue_index|. See |SetQueueAutoPump|. By default automatic pumping is | |
| 61 // enabled for all queues. | |
| 62 void PumpQueue(size_t queue_index); | |
| 63 | |
| 64 // Returns true if there are any tasks in either the work or incoming task | |
| 65 // queue identified by |queue_index|. Note that this function involves taking | |
| 66 // a lock, so calling it has some overhead. | |
| 67 bool PollQueue(size_t queue_index); | |
| 68 | |
| 69 private: | |
| 70 class TaskRunner : public base::SingleThreadTaskRunner { | |
|
jochen (gone - plz use gerrit)
2014/10/20 13:35:31
do TaskRunner and InternalTaskQueue need to live i
Sami
2014/10/20 15:29:56
Good point, looks like we can just forward declare
| |
| 71 public: | |
| 72 TaskRunner(base::WeakPtr<TaskQueueManager> task_queue_manager, | |
| 73 size_t queue_index); | |
| 74 | |
| 75 // base::SingleThreadTaskRunner implementation. | |
| 76 virtual bool RunsTasksOnCurrentThread() const override; | |
| 77 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | |
| 78 const base::Closure& task, | |
| 79 base::TimeDelta delay) override; | |
| 80 virtual bool PostNonNestableDelayedTask( | |
| 81 const tracked_objects::Location& from_here, | |
| 82 const base::Closure& task, | |
| 83 base::TimeDelta delay) override; | |
| 84 | |
| 85 private: | |
| 86 virtual ~TaskRunner(); | |
| 87 | |
| 88 base::WeakPtr<TaskQueueManager> task_queue_manager_; | |
| 89 const size_t queue_index_; | |
| 90 }; | |
|
jochen (gone - plz use gerrit)
2014/10/20 13:35:31
disallow copy & assign
Sami
2014/10/20 15:29:57
Done.
| |
| 91 friend class TaskRunner; | |
|
jochen (gone - plz use gerrit)
2014/10/20 13:35:31
internal classes are auto-friends, no?
Sami
2014/10/20 15:29:56
Oops, I keep forgetting they changed that after TR
| |
| 92 | |
| 93 // Adds a task at the end of the incoming task queue for |queue_index| and | |
| 94 // schedules a call to DoWork() if the incoming queue was empty and automatic | |
| 95 // pumping is enabled. Can be called on an arbitrary thread. | |
| 96 void EnqueueTask(size_t queue_index, const base::PendingTask& pending_task); | |
| 97 | |
| 98 // Post a task to call DoWork() on the main task runner. | |
| 99 void PostDoWorkOnMainRunner(); | |
| 100 | |
| 101 // Use the selector to choose a pending task and run it. | |
| 102 void DoWork(); | |
| 103 | |
| 104 // Reloads the work queue identified by |queue_index| and returns true if | |
| 105 // it ended up having tasks. The work queue must be empty when this function | |
| 106 // is called. | |
| 107 bool ReloadWorkQueue(size_t queue_index); | |
| 108 | |
| 109 // Reloads any empty work queues which have automatic pumping enabled. | |
| 110 // Returns true if any work queue has tasks after doing this. | |
| 111 bool UpdateWorkQueues(); | |
| 112 | |
| 113 // Runs a single task from the work queue designated by |queue_index|. The | |
| 114 // queue must not be empty. | |
| 115 void RunTaskFromWorkQueue(size_t queue_index); | |
| 116 | |
| 117 bool RunsTasksOnCurrentThread() const; | |
| 118 bool PostDelayedTask(size_t queue_index, | |
| 119 const tracked_objects::Location& from_here, | |
| 120 const base::Closure& task, | |
| 121 base::TimeDelta delay); | |
| 122 bool PostNonNestableDelayedTask(size_t queue_index, | |
| 123 const tracked_objects::Location& from_here, | |
| 124 const base::Closure& task, | |
| 125 base::TimeDelta delay); | |
| 126 struct InternalTaskQueue { | |
|
jochen (gone - plz use gerrit)
2014/10/20 13:35:31
this should be a class (and disallow copying)
Sami
2014/10/20 15:29:56
Done.
| |
| 127 InternalTaskQueue(); | |
| 128 ~InternalTaskQueue(); | |
| 129 | |
| 130 scoped_refptr<TaskRunner> task_runner; | |
| 131 | |
| 132 base::Lock incoming_queue_lock; | |
| 133 base::TaskQueue incoming_queue; | |
| 134 | |
| 135 bool auto_pump; | |
| 136 base::TaskQueue work_queue; | |
| 137 }; | |
| 138 | |
| 139 InternalTaskQueue* Queue(size_t queue_index) const; | |
| 140 void PumpQueueLocked(InternalTaskQueue* queue); | |
| 141 | |
| 142 ScopedVector<InternalTaskQueue> queues_; | |
| 143 base::AtomicSequenceNumber task_sequence_num_; | |
| 144 base::debug::TaskAnnotator task_annotator_; | |
| 145 | |
| 146 base::ThreadChecker main_thread_checker_; | |
| 147 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | |
| 148 TaskQueueSelector* selector_; | |
| 149 | |
| 150 base::WeakPtrFactory<TaskQueueManager> weak_factory_; | |
| 151 | |
| 152 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); | |
| 153 }; | |
| 154 | |
| 155 } // namespace content | |
| 156 | |
| 157 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ | |
| OLD | NEW |