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 TaskQueueSelector; |
| 20 |
| 21 // The task queue manager provides N task queues and a selector 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 selector 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. |selector| 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 TaskQueueSelector* selector); |
| 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 selector should call PumpQueue() when necessary to bring in |
| 50 // new tasks for execution. |
| 51 void SetAutoPump(size_t queue_index, bool auto_pump); |
| 52 |
| 53 // Reloads new tasks from the incoming queue for |queue_index|. After this, |
| 54 // this function ensures that the tasks in the work queue, if any, are |
| 55 // scheduled for execution. |
| 56 // |
| 57 // This function only needs to be called if automatic pumping is disabled |
| 58 // for |queue_index|. See |SetQueueAutoPump|. By default automatic pumping is |
| 59 // enabled for all queues. |
| 60 void PumpQueue(size_t queue_index); |
| 61 |
| 62 // Returns true if there are any tasks in either the work or incoming task |
| 63 // queue identified by |queue_index|. Note that this function involves taking |
| 64 // a lock, so calling it has some overhead. |
| 65 bool PollQueue(size_t queue_index); |
| 66 |
| 67 private: |
| 68 class TaskRunner : public SingleThreadTaskRunner { |
| 69 public: |
| 70 TaskRunner(TaskQueueManager* task_queue_manager, size_t queue_index); |
| 71 |
| 72 // SingleThreadTaskRunner implementation. |
| 73 virtual bool RunsTasksOnCurrentThread() const override; |
| 74 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 75 const Closure& task, |
| 76 TimeDelta delay) override; |
| 77 virtual bool PostNonNestableDelayedTask( |
| 78 const tracked_objects::Location& from_here, |
| 79 const Closure& task, |
| 80 TimeDelta delay) override; |
| 81 |
| 82 private: |
| 83 virtual ~TaskRunner(); |
| 84 |
| 85 TaskQueueManager* task_queue_manager_; |
| 86 const size_t queue_index_; |
| 87 }; |
| 88 friend class TaskRunner; |
| 89 |
| 90 // Adds a task at the end of the incoming task queue for |queue_index| and |
| 91 // schedules a call to DoWork() if the incoming queue was empty and automatic |
| 92 // pumping is enabled. Can be called on an arbitrary thread. |
| 93 void EnqueueTask(size_t queue_index, const PendingTask& pending_task); |
| 94 |
| 95 // Post a task to call DoWork() on the main task runner. |
| 96 void ScheduleWork(); |
| 97 |
| 98 // Use the selector to choose a pending task and run it. |
| 99 void DoWork(); |
| 100 |
| 101 // Reloads the work queue identified by |queue_index| and returns true if |
| 102 // it ended up having tasks. The work queue must be empty when this function |
| 103 // is called. |
| 104 bool ReloadWorkQueue(size_t queue_index); |
| 105 |
| 106 // Returns true if any work queue is non-empty. |
| 107 bool ReloadWorkQueues(); |
| 108 |
| 109 // Runs a single task from the work queue designated by |queue_index|. The |
| 110 // queue must not be empty. |
| 111 void RunTaskFromWorkQueue(size_t queue_index); |
| 112 |
| 113 bool RunsTasksOnCurrentThread() const; |
| 114 bool PostDelayedTask(size_t queue_index, |
| 115 const tracked_objects::Location& from_here, |
| 116 const Closure& task, |
| 117 TimeDelta delay); |
| 118 bool PostNonNestableDelayedTask(size_t queue_index, |
| 119 const tracked_objects::Location& from_here, |
| 120 const Closure& task, |
| 121 TimeDelta delay); |
| 122 struct InternalTaskQueue { |
| 123 InternalTaskQueue(); |
| 124 ~InternalTaskQueue(); |
| 125 |
| 126 scoped_refptr<TaskRunner> task_runner; |
| 127 |
| 128 Lock incoming_queue_lock; |
| 129 TaskQueue incoming_queue; |
| 130 |
| 131 bool auto_pump; |
| 132 TaskQueue work_queue; |
| 133 }; |
| 134 |
| 135 InternalTaskQueue* Queue(size_t queue_index) const; |
| 136 void PumpQueueLocked(InternalTaskQueue* queue); |
| 137 |
| 138 ScopedVector<InternalTaskQueue> queues_; |
| 139 AtomicSequenceNumber task_sequence_num_; |
| 140 debug::TaskAnnotator task_annotator_; |
| 141 |
| 142 base::ThreadChecker main_thread_checker_; |
| 143 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
| 144 TaskQueueSelector* selector_; |
| 145 |
| 146 base::WeakPtrFactory<TaskQueueManager> weak_factory_; |
| 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); |
| 149 }; |
| 150 |
| 151 } // namespace content |
| 152 |
| 153 #endif // BASE_TASK_TASK_QUEUE_MANAGER_H_ |
OLD | NEW |