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 namespace internal { |
| 21 class TaskRunner; |
| 22 class TaskQueue; |
| 23 } |
| 24 class TaskQueueSelector; |
| 25 |
| 26 // The task queue manager provides N task queues and a selector interface for |
| 27 // choosing which task queue to service next. Each task queue consists of two |
| 28 // sub queues: |
| 29 // |
| 30 // 1. Incoming task queue. Tasks that are posted get immediately appended here. |
| 31 // When a task is appended into an empty incoming queue, the task manager |
| 32 // work function (DoWork) is scheduled to run on the main task runner. |
| 33 // |
| 34 // 2. Work queue. If a work queue is empty when DoWork() is entered, tasks from |
| 35 // the incoming task queue (if any) are moved here. The work queues are |
| 36 // registered with the selector as input to the scheduling decision. |
| 37 // |
| 38 class CONTENT_EXPORT TaskQueueManager { |
| 39 public: |
| 40 // Create a task queue manager with |task_queue_count| task queues. |
| 41 // |main_task_runner| identifies the thread on which where the tasks are |
| 42 // eventually run. |selector| is used to choose which task queue to service. |
| 43 // It should outlive this class. |
| 44 TaskQueueManager(size_t task_queue_count, |
| 45 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
| 46 TaskQueueSelector* selector); |
| 47 ~TaskQueueManager(); |
| 48 |
| 49 // Returns the task runner which targets the queue selected by |queue_index|. |
| 50 scoped_refptr<base::SingleThreadTaskRunner> TaskRunnerForQueue( |
| 51 size_t queue_index); |
| 52 |
| 53 // If |auto_pump| is false, tasks posted to the given incoming queue will not |
| 54 // be automatically scheduled for execution or transferred to the work queue. |
| 55 // Instead, the selector should call PumpQueue() when necessary to bring in |
| 56 // new tasks for execution. |
| 57 void SetAutoPump(size_t queue_index, bool auto_pump); |
| 58 |
| 59 // Reloads new tasks from the incoming queue for |queue_index|. After this, |
| 60 // this function ensures that the tasks in the work queue, if any, are |
| 61 // scheduled for execution. |
| 62 // |
| 63 // This function only needs to be called if automatic pumping is disabled |
| 64 // for |queue_index|. See |SetQueueAutoPump|. By default automatic pumping is |
| 65 // enabled for all queues. |
| 66 void PumpQueue(size_t queue_index); |
| 67 |
| 68 // Returns true if there are any tasks in either the work or incoming task |
| 69 // queue identified by |queue_index|. Note that this function involves taking |
| 70 // a lock, so calling it has some overhead. |
| 71 bool PollQueue(size_t queue_index); |
| 72 |
| 73 private: |
| 74 friend class internal::TaskRunner; |
| 75 |
| 76 // Adds a task at the end of the incoming task queue for |queue_index| and |
| 77 // schedules a call to DoWork() if the incoming queue was empty and automatic |
| 78 // pumping is enabled. Can be called on an arbitrary thread. |
| 79 void EnqueueTask(size_t queue_index, const base::PendingTask& pending_task); |
| 80 |
| 81 // Post a task to call DoWork() on the main task runner. |
| 82 void PostDoWorkOnMainRunner(); |
| 83 |
| 84 // Use the selector to choose a pending task and run it. |
| 85 void DoWork(); |
| 86 |
| 87 // Reloads the work queue identified by |queue_index| and returns true if |
| 88 // it ended up having tasks. The work queue must be empty when this function |
| 89 // is called. |
| 90 bool ReloadWorkQueue(size_t queue_index); |
| 91 |
| 92 // Reloads any empty work queues which have automatic pumping enabled. |
| 93 // Returns true if any work queue has tasks after doing this. |
| 94 bool UpdateWorkQueues(); |
| 95 |
| 96 // Runs a single task from the work queue designated by |queue_index|. The |
| 97 // queue must not be empty. |
| 98 void RunTaskFromWorkQueue(size_t queue_index); |
| 99 |
| 100 bool RunsTasksOnCurrentThread() const; |
| 101 bool PostDelayedTask(size_t queue_index, |
| 102 const tracked_objects::Location& from_here, |
| 103 const base::Closure& task, |
| 104 base::TimeDelta delay); |
| 105 bool PostNonNestableDelayedTask(size_t queue_index, |
| 106 const tracked_objects::Location& from_here, |
| 107 const base::Closure& task, |
| 108 base::TimeDelta delay); |
| 109 internal::TaskQueue* Queue(size_t queue_index) const; |
| 110 void PumpQueueLocked(internal::TaskQueue* queue); |
| 111 |
| 112 ScopedVector<internal::TaskQueue> queues_; |
| 113 base::AtomicSequenceNumber task_sequence_num_; |
| 114 base::debug::TaskAnnotator task_annotator_; |
| 115 |
| 116 base::ThreadChecker main_thread_checker_; |
| 117 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
| 118 TaskQueueSelector* selector_; |
| 119 |
| 120 base::WeakPtrFactory<TaskQueueManager> weak_factory_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); |
| 123 }; |
| 124 |
| 125 } // namespace content |
| 126 |
| 127 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ |
OLD | NEW |