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 struct 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) const; | |
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| into the work | |
60 // queue, regardless of whether the work queue is empty or not. After this, | |
61 // this function ensures that the tasks in the work queue, if any, are | |
62 // scheduled for execution. | |
63 // | |
64 // This function only needs to be called if automatic pumping is disabled | |
65 // for |queue_index|. See |SetQueueAutoPump|. By default automatic pumping is | |
66 // enabled for all queues. | |
67 void PumpQueue(size_t queue_index); | |
68 | |
69 // Returns true if there no tasks in either the work or incoming task queue | |
70 // identified by |queue_index|. Note that this function involves taking a | |
71 // lock, so calling it has some overhead. | |
72 bool IsQueueEmpty(size_t queue_index); | |
73 | |
74 private: | |
75 friend class internal::TaskRunner; | |
76 | |
77 // Adds a task at the end of the incoming task queue for |queue_index| and | |
78 // schedules a call to DoWork() if the incoming queue was empty and automatic | |
79 // pumping is enabled. Can be called on an arbitrary thread. | |
80 void EnqueueTask(size_t queue_index, const base::PendingTask& pending_task); | |
81 | |
82 // Post a task to call DoWork() on the main task runner. | |
83 void PostDoWorkOnMainRunner(); | |
84 | |
85 // Use the selector to choose a pending task and run it. | |
86 void DoWork(); | |
87 | |
88 // Reloads any empty work queues which have automatic pumping enabled. | |
89 // Returns true if any work queue has tasks after doing this. | |
90 bool UpdateWorkQueues(); | |
91 | |
92 // Runs a single task from the work queue designated by |queue_index|. The | |
93 // queue must not be empty. | |
94 void RunTaskFromWorkQueue(size_t queue_index); | |
95 | |
96 bool RunsTasksOnCurrentThread() const; | |
97 bool PostDelayedTask(size_t queue_index, | |
98 const tracked_objects::Location& from_here, | |
99 const base::Closure& task, | |
100 base::TimeDelta delay); | |
101 bool PostNonNestableDelayedTask(size_t queue_index, | |
102 const tracked_objects::Location& from_here, | |
103 const base::Closure& task, | |
104 base::TimeDelta delay); | |
105 internal::TaskQueue* Queue(size_t queue_index) const; | |
jar (doing other things)
2014/10/29 21:14:07
I found this declaration, and its use, but didn't
Sami
2014/10/30 10:43:00
It's at line 113 of task_queue_manager.cc in this
| |
106 void PumpQueueLocked(internal::TaskQueue* queue); | |
107 | |
108 ScopedVector<internal::TaskQueue> queues_; | |
109 base::AtomicSequenceNumber task_sequence_num_; | |
110 base::debug::TaskAnnotator task_annotator_; | |
111 | |
112 base::ThreadChecker main_thread_checker_; | |
113 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | |
114 TaskQueueSelector* selector_; | |
115 | |
116 base::WeakPtrFactory<TaskQueueManager> weak_factory_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(TaskQueueManager); | |
119 }; | |
120 | |
121 } // namespace content | |
122 | |
123 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_MANAGER_H_ | |
OLD | NEW |