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 BASE_TASK_TASK_QUEUE_MANAGER_H_ | |
| 6 #define BASE_TASK_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 | |
| 18 namespace base { | |
| 19 class TaskQueueScheduler; | |
| 20 | |
| 21 // The task queue manager provides N task queues and a scheduling interface for | |
| 22 // choosing which task queue to service next. Each task queue consists of two | |
| 23 // sub queues: | |
| 24 // | |
| 25 // 1. Incoming task queue. Tasks that are posted get immediately appended here. | |
| 26 // When a task is appended into an empty incoming queue, the task manager | |
| 27 // work function (DoWork) is scheduled to run on the main task runner. | |
| 28 // | |
| 29 // 2. Work queue. If a work queue is empty when DoWork() is entered, tasks from | |
| 30 // the incoming task queue (if any) are moved here. The work queues are | |
| 31 // registered with the scheduler as input to the scheduling decision. | |
| 32 // | |
| 33 class BASE_EXPORT TaskQueueManager { | |
| 34 public: | |
| 35 // Create a task queue manager with |task_queue_count| task queues. | |
| 36 // |main_task_runner| identifies the thread on which where the tasks are | |
| 37 // eventually run. |scheduler| is used to choose which task queue to service. | |
| 38 // It should outlive this class. | |
| 39 TaskQueueManager(size_t task_queue_count, | |
| 40 scoped_refptr<SingleThreadTaskRunner> main_task_runner, | |
| 41 TaskQueueScheduler* scheduler); | |
| 42 ~TaskQueueManager(); | |
| 43 | |
| 44 // Returns the task runner which targets the queue selected by |queue_index|. | |
| 45 scoped_refptr<SingleThreadTaskRunner> TaskRunnerForQueue(size_t queue_index); | |
| 46 | |
| 47 // If |auto_pump| is false, tasks posted to the given incoming queue will not | |
| 48 // be automatically scheduled for execution or transferred to the work queue. | |
| 49 // Instead, the scheduler should call PumpQueue() when necessary to bring in | |
| 50 // new tasks for execution. | |
| 51 void SetAutoPump(size_t queue_index, bool auto_pump); | |
|
alexclarke
2014/10/15 09:48:59
I suspect this method will only ever get called on
Sami
2014/10/15 12:05:24
I'm thinking it might be useful to keep it togglea
| |
| 52 | |
| 53 // If the work queue is empty, reloads new tasks from the incoming queue. | |
| 54 // After this, this function ensures any tasks in the work queue are | |
| 55 // scheduled for execution. | |
| 56 void PumpQueue(size_t queue_index); | |
|
petrcermak
2014/10/15 10:05:40
Shouldn't this method reload tasks whenever the in
Sami
2014/10/15 12:05:24
I think you're right. I had this this way because
| |
| 57 | |
| 58 // Returns true if there are any tasks in either the work or incoming task | |
| 59 // queue identified by |queue_index|. | |
| 60 bool PollQueue(size_t queue_index); | |
|
alexclarke
2014/10/15 09:48:59
How about IsQueueEmpty()?
Sami
2014/10/15 12:05:24
I prefer poll since it conveys that this operation
| |
| 61 | |
| 62 private: | |
| 63 class TaskRunner : public SingleThreadTaskRunner { | |
| 64 public: | |
| 65 TaskRunner(TaskQueueManager* task_queue_manager, size_t queue_index); | |
| 66 | |
| 67 // SingleThreadTaskRunner implementation. | |
| 68 virtual bool RunsTasksOnCurrentThread() const override; | |
| 69 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | |
| 70 const Closure& task, | |
| 71 TimeDelta delay) override; | |
| 72 virtual bool PostNonNestableDelayedTask( | |
| 73 const tracked_objects::Location& from_here, | |
| 74 const Closure& task, | |
| 75 TimeDelta delay) override; | |
| 76 | |
| 77 private: | |
| 78 virtual ~TaskRunner(); | |
| 79 | |
| 80 TaskQueueManager* task_queue_manager_; | |
| 81 const size_t queue_index_; | |
| 82 }; | |
| 83 friend class TaskRunner; | |
| 84 | |
| 85 // Adds a task at the end of the incoming task queue for |queue_index| and | |
| 86 // schedules a call to DoWork() if the incoming queue was empty and automatic | |
| 87 // pumping is enabled. Can be called on an arbitrary thread. | |
| 88 void EnqueueTask(size_t queue_index, const PendingTask& pending_task); | |
| 89 | |
| 90 // Post a task to call DoWork() on the main task runner. | |
| 91 void ScheduleWork(); | |
| 92 | |
| 93 // Use the scheduler to choose a pending task and run it. | |
| 94 void DoWork(); | |
| 95 | |
| 96 // Reloads the work queue identified by |queue_index| and returns true if | |
| 97 // it ended up having tasks. The work queue must be empty when this function | |
| 98 // is called. | |
| 99 bool ReloadWorkQueue(size_t queue_index); | |
| 100 | |
| 101 // Returns true if any work queue is non-empty. | |
| 102 bool ReloadWorkQueues(); | |
| 103 | |
| 104 // Runs a single task from the work queue designated by |queue_index|. The | |
| 105 // queue must not be empty. | |
| 106 void RunTaskFromWorkQueue(size_t queue_index); | |
| 107 | |
| 108 bool RunsTasksOnCurrentThread() const; | |
| 109 bool PostDelayedTask(size_t queue_index, | |
| 110 const tracked_objects::Location& from_here, | |
| 111 const Closure& task, | |
| 112 TimeDelta delay); | |
| 113 bool PostNonNestableDelayedTask(size_t queue_index, | |
| 114 const tracked_objects::Location& from_here, | |
| 115 const Closure& task, | |
| 116 TimeDelta delay); | |
| 117 | |
| 118 struct InternalTaskQueue { | |
| 119 InternalTaskQueue(size_t index); | |
| 120 ~InternalTaskQueue(); | |
| 121 | |
| 122 const size_t index; | |
|
alexclarke
2014/10/15 09:48:59
The only reason for this appears to be because of
Sami
2014/10/15 12:05:24
Heh, well spotted :) Removed.
| |
| 123 scoped_refptr<TaskRunner> task_runner; | |
| 124 | |
| 125 Lock incoming_queue_lock; | |
| 126 TaskQueue incoming_queue; | |
| 127 | |
| 128 bool auto_pump; | |
| 129 TaskQueue work_queue; | |
| 130 }; | |
| 131 | |
| 132 ScopedVector<InternalTaskQueue> queues_; | |
| 133 AtomicSequenceNumber task_sequence_num_; | |
| 134 debug::TaskAnnotator task_annotator_; | |
| 135 | |
| 136 base::ThreadChecker main_thread_checker_; | |
| 137 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | |
| 138 TaskQueueScheduler* scheduler_; | |
| 139 | |
| 140 base::WeakPtrFactory<TaskQueueManager> weak_factory_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); | |
| 143 }; | |
| 144 | |
| 145 } // namespace content | |
| 146 | |
| 147 #endif // BASE_TASK_TASK_QUEUE_MANAGER_H_ | |
| OLD | NEW |