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 #include "base/task/task_queue_manager.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/debug/trace_event.h" | |
9 #include "base/task/task_queue_scheduler.h" | |
10 | |
11 namespace base { | |
12 | |
13 TaskQueueManager::TaskRunner::TaskRunner(TaskQueueManager* task_queue_manager, | |
14 size_t queue_index) | |
15 : task_queue_manager_(task_queue_manager), queue_index_(queue_index) { | |
16 } | |
17 | |
18 TaskQueueManager::TaskRunner::~TaskRunner() { | |
19 } | |
20 | |
21 bool TaskQueueManager::TaskRunner::RunsTasksOnCurrentThread() const { | |
22 return task_queue_manager_->RunsTasksOnCurrentThread(); | |
23 } | |
24 | |
25 bool TaskQueueManager::TaskRunner::PostDelayedTask( | |
26 const tracked_objects::Location& from_here, | |
27 const Closure& task, | |
28 TimeDelta delay) { | |
29 return task_queue_manager_->PostDelayedTask( | |
30 queue_index_, from_here, task, delay); | |
31 } | |
32 | |
33 bool TaskQueueManager::TaskRunner::PostNonNestableDelayedTask( | |
34 const tracked_objects::Location& from_here, | |
35 const Closure& task, | |
36 TimeDelta delay) { | |
37 return task_queue_manager_->PostNonNestableDelayedTask( | |
38 queue_index_, from_here, task, delay); | |
39 } | |
40 | |
41 TaskQueueManager::InternalTaskQueue::InternalTaskQueue(size_t index) | |
42 : index(index), auto_pump(true) { | |
43 } | |
picksi1
2014/10/15 09:07:38
Should auto_pump be called pump_when_empty?
Sami
2014/10/15 12:05:24
No, because it also involves how DoWork is schedul
| |
44 | |
45 TaskQueueManager::InternalTaskQueue::~InternalTaskQueue() { | |
46 } | |
47 | |
48 TaskQueueManager::TaskQueueManager( | |
49 size_t task_queue_count, | |
50 scoped_refptr<SingleThreadTaskRunner> main_task_runner, | |
51 TaskQueueScheduler* scheduler) | |
52 : main_task_runner_(main_task_runner), | |
53 scheduler_(scheduler), | |
54 weak_factory_(this) { | |
55 DCHECK(main_task_runner->RunsTasksOnCurrentThread()); | |
56 | |
57 for (size_t i = 0; i < task_queue_count; i++) { | |
58 scoped_ptr<InternalTaskQueue> queue(new InternalTaskQueue(i)); | |
59 queue->task_runner = make_scoped_refptr(new TaskRunner(this, i)); | |
60 queues_.push_back(queue.release()); | |
61 } | |
62 | |
63 std::vector<const TaskQueue*> work_queues; | |
64 for (size_t i = 0; i < queues_.size(); i++) | |
65 work_queues.push_back(&queues_[i]->work_queue); | |
66 scheduler_->RegisterWorkQueues(work_queues); | |
picksi1
2014/10/15 09:07:38
Are we exposing our inner workings (i.e. the work_
Sami
2014/10/15 12:05:24
I'm not sure I follow the last half of your sugges
| |
67 } | |
68 | |
69 TaskQueueManager::~TaskQueueManager() { | |
70 } | |
71 | |
72 scoped_refptr<SingleThreadTaskRunner> TaskQueueManager::TaskRunnerForQueue( | |
73 size_t queue_index) { | |
74 DCHECK_LT(queue_index, queues_.size()); | |
75 return queues_[queue_index]->task_runner; | |
76 } | |
77 | |
78 bool TaskQueueManager::PollQueue(size_t queue_index) { | |
79 DCHECK_LT(queue_index, queues_.size()); | |
80 if (!queues_[queue_index]->work_queue.empty()) | |
81 return true; | |
82 InternalTaskQueue* queue = queues_[queue_index]; | |
picksi1
2014/10/15 09:07:38
Getting a queue from an index is repeated a lot, s
Sami
2014/10/15 12:05:24
Good idea, done.
| |
83 AutoLock lock(queue->incoming_queue_lock); | |
84 return !queue->incoming_queue.empty(); | |
petrcermak
2014/10/15 10:05:40
Shouldn't this be "return queue->auto_pump && !que
alexclarke
2014/10/15 10:09:14
The TaskQueueScheduler is responsible for setting
petrcermak
2014/10/15 10:13:05
Acknowledged.
| |
85 } | |
86 | |
87 bool TaskQueueManager::ReloadWorkQueue(size_t queue_index) { | |
88 main_thread_checker_.CalledOnValidThread(); | |
89 DCHECK_LT(queue_index, queues_.size()); | |
90 InternalTaskQueue* queue = queues_[queue_index]; | |
91 DCHECK(queue->work_queue.empty()); | |
92 AutoLock lock(queue->incoming_queue_lock); | |
93 if (!queue->auto_pump) | |
94 return false; | |
95 queue->work_queue.Swap(&queue->incoming_queue); | |
96 return !queue->work_queue.empty(); | |
97 } | |
98 | |
99 void TaskQueueManager::EnqueueTask(size_t queue_index, | |
100 const PendingTask& pending_task) { | |
101 DCHECK_LT(queue_index, queues_.size()); | |
102 InternalTaskQueue* queue = queues_[queue_index]; | |
103 AutoLock lock(queue->incoming_queue_lock); | |
104 if (queue->auto_pump && queue->incoming_queue.empty()) | |
105 ScheduleWork(); | |
106 queue->incoming_queue.push(pending_task); | |
picksi1
2014/10/15 09:07:38
Is it possible for the incoming queue to be empty
Sami
2014/10/15 12:05:24
Yes, that's possible. This is written to be slight
| |
107 } | |
108 | |
109 void TaskQueueManager::SetAutoPump(size_t queue_index, bool auto_pump) { | |
110 DCHECK_LT(queue_index, queues_.size()); | |
111 InternalTaskQueue* queue = queues_[queue_index]; | |
112 { | |
113 AutoLock lock(queue->incoming_queue_lock); | |
114 queue->auto_pump = auto_pump; | |
115 } | |
116 if (auto_pump) | |
117 PumpQueue(queue_index); | |
118 } | |
picksi1
2014/10/15 09:07:38
Would you consider having two different functions
Sami
2014/10/15 12:05:24
Hmm, having two different functions makes this a l
| |
119 | |
120 void TaskQueueManager::PumpQueue(size_t queue_index) { | |
121 DCHECK_LT(queue_index, queues_.size()); | |
122 InternalTaskQueue* queue = queues_[queue_index]; | |
123 if (queue->work_queue.empty()) { | |
124 AutoLock lock(queue->incoming_queue_lock); | |
125 queue->work_queue.Swap(&queue->incoming_queue); | |
126 } | |
127 if (!queue->work_queue.empty()) | |
128 ScheduleWork(); | |
129 } | |
130 | |
131 void TaskQueueManager::ScheduleWork() { | |
132 main_task_runner_->PostTask( | |
133 FROM_HERE, Bind(&TaskQueueManager::DoWork, weak_factory_.GetWeakPtr())); | |
134 } | |
picksi1
2014/10/15 09:07:38
Is ScheduleWork the right name? Should it be more
Sami
2014/10/15 12:05:24
ScheduleWork/DoWork seems like a reasonable pairin
rmcilroy
2014/10/15 13:02:45
I somewhat agree with Simon that it's difficult to
Sami
2014/10/15 14:34:30
Ah, I was hoping it was now more clear since we ha
| |
135 | |
136 bool TaskQueueManager::ReloadWorkQueues() { | |
137 bool has_work = false; | |
138 for (auto& queue : queues_) { | |
139 if (!queue->work_queue.empty()) | |
140 has_work = true; | |
141 else if (ReloadWorkQueue(queue->index)) | |
142 has_work = true; | |
143 } | |
144 return has_work; | |
145 } | |
146 | |
147 void TaskQueueManager::DoWork() { | |
148 main_thread_checker_.CalledOnValidThread(); | |
149 if (!ReloadWorkQueues()) | |
150 return; | |
151 | |
152 size_t queue_index; | |
153 if (!scheduler_->ChooseWorkQueueToService(&queue_index)) | |
154 return; | |
155 ScheduleWork(); | |
156 RunTaskFromWorkQueue(queue_index); | |
157 } | |
158 | |
159 void TaskQueueManager::RunTaskFromWorkQueue(size_t queue_index) { | |
160 main_thread_checker_.CalledOnValidThread(); | |
161 DCHECK_LT(queue_index, queues_.size()); | |
162 InternalTaskQueue* queue = queues_[queue_index]; | |
163 DCHECK(!queue->work_queue.empty()); | |
164 PendingTask pending_task = queue->work_queue.front(); | |
165 queue->work_queue.pop(); | |
166 task_annotator_.RunTask( | |
167 "TaskQueueManager::PostTask", "TaskQueueManager::RunTask", pending_task); | |
168 } | |
169 | |
170 bool TaskQueueManager::RunsTasksOnCurrentThread() const { | |
171 return main_task_runner_->RunsTasksOnCurrentThread(); | |
172 } | |
173 | |
174 bool TaskQueueManager::PostDelayedTask( | |
175 size_t queue_index, | |
176 const tracked_objects::Location& from_here, | |
177 const Closure& task, | |
178 TimeDelta delay) { | |
179 int sequence_num = task_sequence_num_.GetNext(); | |
180 | |
181 PendingTask pending_task(from_here, task); | |
182 pending_task.sequence_num = sequence_num; | |
183 | |
184 task_annotator_.DidQueueTask("TaskQueueManager::PostTask", pending_task); | |
185 if (delay > TimeDelta()) { | |
186 return main_task_runner_->PostDelayedTask( | |
187 from_here, | |
188 Bind(&TaskQueueManager::EnqueueTask, | |
189 weak_factory_.GetWeakPtr(), | |
190 queue_index, | |
191 pending_task), | |
192 delay); | |
193 } | |
194 EnqueueTask(queue_index, pending_task); | |
195 return true; | |
196 } | |
197 | |
198 bool TaskQueueManager::PostNonNestableDelayedTask( | |
199 size_t queue_index, | |
200 const tracked_objects::Location& from_here, | |
201 const Closure& task, | |
202 TimeDelta delay) { | |
203 // Defer non-nestable work to the main task runner. | |
204 return main_task_runner_->PostNonNestableDelayedTask(from_here, task, delay); | |
205 } | |
206 | |
207 } // namespace content | |
OLD | NEW |