Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Side by Side Diff: content/renderer/raster_worker_pool.cc

Issue 1666283002: Reland - Refactor signaling in RWP (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: x++ > ++x Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/renderer/raster_worker_pool.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/renderer/raster_worker_pool.h" 5 #include "content/renderer/raster_worker_pool.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/threading/thread_restrictions.h" 12 #include "base/threading/thread_restrictions.h"
13 #include "base/trace_event/trace_event.h" 13 #include "base/trace_event/trace_event.h"
14 #include "cc/base/math_util.h" 14 #include "cc/base/math_util.h"
15 #include "cc/raster/task_category.h" 15 #include "cc/raster/task_category.h"
16 16
17 namespace content { 17 namespace content {
18 namespace { 18 namespace {
19 19
20 // A thread which forwards to RasterWorkerPool::Run with the runnable 20 // A thread which forwards to RasterWorkerPool::Run with the runnable
21 // categories. 21 // categories.
22 class RasterWorkerPoolThread : public base::SimpleThread { 22 class RasterWorkerPoolThread : public base::SimpleThread {
23 public: 23 public:
24 explicit RasterWorkerPoolThread(const std::string& name_prefix, 24 RasterWorkerPoolThread(const std::string& name_prefix,
25 const Options& options, 25 const Options& options,
26 RasterWorkerPool* pool, 26 RasterWorkerPool* pool,
27 std::vector<cc::TaskCategory> categories) 27 std::vector<cc::TaskCategory> categories,
28 base::ConditionVariable* has_ready_to_run_tasks_cv)
28 : SimpleThread(name_prefix, options), 29 : SimpleThread(name_prefix, options),
29 pool_(pool), 30 pool_(pool),
30 categories_(categories) {} 31 categories_(categories),
Eric Willigers 2016/03/07 09:42:47 Perhaps categories_(std::move(categories)),
32 has_ready_to_run_tasks_cv_(has_ready_to_run_tasks_cv) {}
31 33
32 void Run() override { pool_->Run(categories_); } 34 void Run() override { pool_->Run(categories_, has_ready_to_run_tasks_cv_); }
33 35
34 private: 36 private:
35 RasterWorkerPool* const pool_; 37 RasterWorkerPool* const pool_;
36 const std::vector<cc::TaskCategory> categories_; 38 const std::vector<cc::TaskCategory> categories_;
39 base::ConditionVariable* const has_ready_to_run_tasks_cv_;
37 }; 40 };
38 41
39 } // namespace 42 } // namespace
40 43
41 // A sequenced task runner which posts tasks to a RasterWorkerPool. 44 // A sequenced task runner which posts tasks to a RasterWorkerPool.
42 class RasterWorkerPool::RasterWorkerPoolSequencedTaskRunner 45 class RasterWorkerPool::RasterWorkerPoolSequencedTaskRunner
43 : public base::SequencedTaskRunner { 46 : public base::SequencedTaskRunner {
44 public: 47 public:
45 explicit RasterWorkerPoolSequencedTaskRunner( 48 explicit RasterWorkerPoolSequencedTaskRunner(
46 cc::TaskGraphRunner* task_graph_runner) 49 cc::TaskGraphRunner* task_graph_runner)
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 cc::Task::Vector tasks_; 112 cc::Task::Vector tasks_;
110 // Graph object used for scheduling tasks. 113 // Graph object used for scheduling tasks.
111 cc::TaskGraph graph_; 114 cc::TaskGraph graph_;
112 // Cached vector to avoid allocation when getting the list of complete 115 // Cached vector to avoid allocation when getting the list of complete
113 // tasks. 116 // tasks.
114 cc::Task::Vector completed_tasks_; 117 cc::Task::Vector completed_tasks_;
115 }; 118 };
116 119
117 RasterWorkerPool::RasterWorkerPool() 120 RasterWorkerPool::RasterWorkerPool()
118 : namespace_token_(GetNamespaceToken()), 121 : namespace_token_(GetNamespaceToken()),
119 has_ready_to_run_tasks_cv_(&lock_), 122 has_ready_to_run_foreground_tasks_cv_(&lock_),
123 has_ready_to_run_background_tasks_cv_(&lock_),
120 has_namespaces_with_finished_running_tasks_cv_(&lock_), 124 has_namespaces_with_finished_running_tasks_cv_(&lock_),
121 shutdown_(false) {} 125 shutdown_(false) {}
122 126
123 void RasterWorkerPool::Start( 127 void RasterWorkerPool::Start(
124 int num_threads, 128 int num_threads,
125 const base::SimpleThread::Options& thread_options) { 129 const base::SimpleThread::Options& thread_options) {
126 DCHECK(threads_.empty()); 130 DCHECK(threads_.empty());
127 while (threads_.size() < static_cast<size_t>(num_threads)) {
128 // Determine the categories that each thread can run.
129 std::vector<cc::TaskCategory> task_categories;
130 131
131 // The first thread can run nonconcurrent tasks. 132 // Start |num_threads| threads for foreground work, including nonconcurrent
132 if (threads_.size() == 0) { 133 // foreground work.
133 task_categories.push_back(cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND); 134 std::vector<cc::TaskCategory> foreground_categories;
134 } 135 foreground_categories.push_back(cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND);
136 foreground_categories.push_back(cc::TASK_CATEGORY_FOREGROUND);
135 137
136 // All threads can run foreground tasks. 138 for (int i = 0; i < num_threads; i++) {
137 task_categories.push_back(cc::TASK_CATEGORY_FOREGROUND);
138
139 // The last thread can run background tasks.
140 if (threads_.size() == (static_cast<size_t>(num_threads) - 1)) {
141 task_categories.push_back(cc::TASK_CATEGORY_BACKGROUND);
142 }
143
144 scoped_ptr<base::SimpleThread> thread(new RasterWorkerPoolThread( 139 scoped_ptr<base::SimpleThread> thread(new RasterWorkerPoolThread(
145 base::StringPrintf("CompositorTileWorker%u", 140 base::StringPrintf("CompositorTileWorker%u",
146 static_cast<unsigned>(threads_.size() + 1)) 141 static_cast<unsigned>(threads_.size() + 1))
147 .c_str(), 142 .c_str(),
148 thread_options, this, task_categories)); 143 thread_options, this, foreground_categories,
144 &has_ready_to_run_foreground_tasks_cv_));
149 thread->Start(); 145 thread->Start();
150 threads_.push_back(std::move(thread)); 146 threads_.push_back(std::move(thread));
151 } 147 }
148
149 // Start a single thread for background work.
150 std::vector<cc::TaskCategory> background_categories;
151 background_categories.push_back(cc::TASK_CATEGORY_BACKGROUND);
152 scoped_ptr<base::SimpleThread> thread(new RasterWorkerPoolThread(
153 base::StringPrintf("CompositorTileWorker%u",
154 static_cast<unsigned>(threads_.size() + 1))
155 .c_str(),
156 thread_options, this, background_categories,
157 &has_ready_to_run_background_tasks_cv_));
158 thread->Start();
159 threads_.push_back(std::move(thread));
152 } 160 }
153 161
154 void RasterWorkerPool::Shutdown() { 162 void RasterWorkerPool::Shutdown() {
155 WaitForTasksToFinishRunning(namespace_token_); 163 WaitForTasksToFinishRunning(namespace_token_);
156 CollectCompletedTasks(namespace_token_, &completed_tasks_); 164 CollectCompletedTasks(namespace_token_, &completed_tasks_);
157 // Shutdown raster threads. 165 // Shutdown raster threads.
158 { 166 {
159 base::AutoLock lock(lock_); 167 base::AutoLock lock(lock_);
160 168
161 DCHECK(!work_queue_.HasReadyToRunTasks()); 169 DCHECK(!work_queue_.HasReadyToRunTasks());
162 DCHECK(!work_queue_.HasAnyNamespaces()); 170 DCHECK(!work_queue_.HasAnyNamespaces());
163 171
164 DCHECK(!shutdown_); 172 DCHECK(!shutdown_);
165 shutdown_ = true; 173 shutdown_ = true;
166 174
167 // Wake up all workers so they exit. 175 // Wake up all workers so they exit.
168 has_ready_to_run_tasks_cv_.Broadcast(); 176 has_ready_to_run_foreground_tasks_cv_.Broadcast();
177 has_ready_to_run_background_tasks_cv_.Broadcast();
169 } 178 }
170 while (!threads_.empty()) { 179 while (!threads_.empty()) {
171 threads_.back()->Join(); 180 threads_.back()->Join();
172 threads_.pop_back(); 181 threads_.pop_back();
173 } 182 }
174 } 183 }
175 184
176 // Overridden from base::TaskRunner: 185 // Overridden from base::TaskRunner:
177 bool RasterWorkerPool::PostDelayedTask( 186 bool RasterWorkerPool::PostDelayedTask(
178 const tracked_objects::Location& from_here, 187 const tracked_objects::Location& from_here,
(...skipping 25 matching lines...) Expand all
204 213
205 ScheduleTasksWithLockAcquired(namespace_token_, &graph_); 214 ScheduleTasksWithLockAcquired(namespace_token_, &graph_);
206 completed_tasks_.clear(); 215 completed_tasks_.clear();
207 return true; 216 return true;
208 } 217 }
209 218
210 bool RasterWorkerPool::RunsTasksOnCurrentThread() const { 219 bool RasterWorkerPool::RunsTasksOnCurrentThread() const {
211 return true; 220 return true;
212 } 221 }
213 222
214 void RasterWorkerPool::Run(const std::vector<cc::TaskCategory>& categories) { 223 void RasterWorkerPool::Run(const std::vector<cc::TaskCategory>& categories,
224 base::ConditionVariable* has_ready_to_run_tasks_cv) {
215 base::AutoLock lock(lock_); 225 base::AutoLock lock(lock_);
216 226
217 while (true) { 227 while (true) {
218 if (!RunTaskWithLockAcquired(categories)) { 228 if (!RunTaskWithLockAcquired(categories)) {
229 // We are no longer running tasks, which may allow another category to
230 // start running. Signal other worker threads.
231 SignalHasReadyToRunTasksWithLockAcquired();
232
219 // Exit when shutdown is set and no more tasks are pending. 233 // Exit when shutdown is set and no more tasks are pending.
220 if (shutdown_) 234 if (shutdown_)
221 break; 235 break;
222 236
223 // Wait for more tasks. 237 // Wait for more tasks.
224 has_ready_to_run_tasks_cv_.Wait(); 238 has_ready_to_run_tasks_cv->Wait();
225 continue; 239 continue;
226 } 240 }
227 } 241 }
228 } 242 }
229 243
230 void RasterWorkerPool::FlushForTesting() { 244 void RasterWorkerPool::FlushForTesting() {
231 base::AutoLock lock(lock_); 245 base::AutoLock lock(lock_);
232 246
233 while (!work_queue_.HasFinishedRunningTasksInAllNamespaces()) { 247 while (!work_queue_.HasFinishedRunningTasksInAllNamespaces()) {
234 has_namespaces_with_finished_running_tasks_cv_.Wait(); 248 has_namespaces_with_finished_running_tasks_cv_.Wait();
(...skipping 24 matching lines...) Expand all
259 } 273 }
260 274
261 void RasterWorkerPool::ScheduleTasksWithLockAcquired(cc::NamespaceToken token, 275 void RasterWorkerPool::ScheduleTasksWithLockAcquired(cc::NamespaceToken token,
262 cc::TaskGraph* graph) { 276 cc::TaskGraph* graph) {
263 DCHECK(token.IsValid()); 277 DCHECK(token.IsValid());
264 DCHECK(!cc::TaskGraphWorkQueue::DependencyMismatch(graph)); 278 DCHECK(!cc::TaskGraphWorkQueue::DependencyMismatch(graph));
265 DCHECK(!shutdown_); 279 DCHECK(!shutdown_);
266 280
267 work_queue_.ScheduleTasks(token, graph); 281 work_queue_.ScheduleTasks(token, graph);
268 282
269 // If there is more work available, wake up the other worker threads. 283 // There may be more work available, so wake up another worker thread.
270 if (work_queue_.HasReadyToRunTasks()) 284 SignalHasReadyToRunTasksWithLockAcquired();
271 has_ready_to_run_tasks_cv_.Broadcast();
272 } 285 }
273 286
274 void RasterWorkerPool::WaitForTasksToFinishRunning(cc::NamespaceToken token) { 287 void RasterWorkerPool::WaitForTasksToFinishRunning(cc::NamespaceToken token) {
275 TRACE_EVENT0("disabled-by-default-cc.debug", 288 TRACE_EVENT0("disabled-by-default-cc.debug",
276 "RasterWorkerPool::WaitForTasksToFinishRunning"); 289 "RasterWorkerPool::WaitForTasksToFinishRunning");
277 290
278 DCHECK(token.IsValid()); 291 DCHECK(token.IsValid());
279 292
280 { 293 {
281 base::AutoLock lock(lock_); 294 base::AutoLock lock(lock_);
282 base::ThreadRestrictions::ScopedAllowWait allow_wait; 295 base::ThreadRestrictions::ScopedAllowWait allow_wait;
283 296
284 auto* task_namespace = work_queue_.GetNamespaceForToken(token); 297 auto* task_namespace = work_queue_.GetNamespaceForToken(token);
285 298
286 if (!task_namespace) 299 if (!task_namespace)
287 return; 300 return;
288 301
289 while (!work_queue_.HasFinishedRunningTasksInNamespace(task_namespace)) 302 while (!work_queue_.HasFinishedRunningTasksInNamespace(task_namespace))
290 has_namespaces_with_finished_running_tasks_cv_.Wait(); 303 has_namespaces_with_finished_running_tasks_cv_.Wait();
304
305 // There may be other namespaces that have finished running tasks, so wake
306 // up another origin thread.
307 has_namespaces_with_finished_running_tasks_cv_.Signal();
291 } 308 }
292 } 309 }
293 310
294 void RasterWorkerPool::CollectCompletedTasks( 311 void RasterWorkerPool::CollectCompletedTasks(
295 cc::NamespaceToken token, 312 cc::NamespaceToken token,
296 cc::Task::Vector* completed_tasks) { 313 cc::Task::Vector* completed_tasks) {
297 TRACE_EVENT0("disabled-by-default-cc.debug", 314 TRACE_EVENT0("disabled-by-default-cc.debug",
298 "RasterWorkerPool::CollectCompletedTasks"); 315 "RasterWorkerPool::CollectCompletedTasks");
299 316
300 { 317 {
301 base::AutoLock lock(lock_); 318 base::AutoLock lock(lock_);
302 CollectCompletedTasksWithLockAcquired(token, completed_tasks); 319 CollectCompletedTasksWithLockAcquired(token, completed_tasks);
303 } 320 }
304 } 321 }
305 322
306 void RasterWorkerPool::CollectCompletedTasksWithLockAcquired( 323 void RasterWorkerPool::CollectCompletedTasksWithLockAcquired(
307 cc::NamespaceToken token, 324 cc::NamespaceToken token,
308 cc::Task::Vector* completed_tasks) { 325 cc::Task::Vector* completed_tasks) {
309 DCHECK(token.IsValid()); 326 DCHECK(token.IsValid());
310 work_queue_.CollectCompletedTasks(token, completed_tasks); 327 work_queue_.CollectCompletedTasks(token, completed_tasks);
311 } 328 }
312 329
313 bool RasterWorkerPool::RunTaskWithLockAcquired( 330 bool RasterWorkerPool::RunTaskWithLockAcquired(
314 const std::vector<cc::TaskCategory>& categories) { 331 const std::vector<cc::TaskCategory>& categories) {
315 for (const auto& category : categories) { 332 for (const auto& category : categories) {
316 if (work_queue_.HasReadyToRunTasksForCategory(category)) { 333 if (ShouldRunTaskForCategoryWithLockAcquired(category)) {
317 RunTaskInCategoryWithLockAcquired(category); 334 RunTaskInCategoryWithLockAcquired(category);
318 return true; 335 return true;
319 } 336 }
320 } 337 }
321 return false; 338 return false;
322 } 339 }
323 340
324 void RasterWorkerPool::RunTaskInCategoryWithLockAcquired( 341 void RasterWorkerPool::RunTaskInCategoryWithLockAcquired(
325 cc::TaskCategory category) { 342 cc::TaskCategory category) {
326 TRACE_EVENT0("toplevel", "TaskGraphRunner::RunTask"); 343 TRACE_EVENT0("toplevel", "TaskGraphRunner::RunTask");
327 344
328 lock_.AssertAcquired(); 345 lock_.AssertAcquired();
329 346
330 auto prioritized_task = work_queue_.GetNextTaskToRun(category); 347 auto prioritized_task = work_queue_.GetNextTaskToRun(category);
331 cc::Task* task = prioritized_task.task; 348 cc::Task* task = prioritized_task.task;
332 349
350 // There may be more work available, so wake up another worker thread.
351 SignalHasReadyToRunTasksWithLockAcquired();
352
333 // Call WillRun() before releasing |lock_| and running task. 353 // Call WillRun() before releasing |lock_| and running task.
334 task->WillRun(); 354 task->WillRun();
335 355
336 { 356 {
337 base::AutoUnlock unlock(lock_); 357 base::AutoUnlock unlock(lock_);
338 358
339 task->RunOnWorkerThread(); 359 task->RunOnWorkerThread();
340 } 360 }
341 361
342 // This will mark task as finished running. 362 // This will mark task as finished running.
343 task->DidRun(); 363 task->DidRun();
344 364
345 work_queue_.CompleteTask(prioritized_task); 365 work_queue_.CompleteTask(prioritized_task);
346 366
347 // We may have just dequeued more tasks, wake up the other worker threads.
348 if (work_queue_.HasReadyToRunTasks())
349 has_ready_to_run_tasks_cv_.Broadcast();
350
351 // If namespace has finished running all tasks, wake up origin threads. 367 // If namespace has finished running all tasks, wake up origin threads.
352 if (work_queue_.HasFinishedRunningTasksInNamespace( 368 if (work_queue_.HasFinishedRunningTasksInNamespace(
353 prioritized_task.task_namespace)) 369 prioritized_task.task_namespace))
354 has_namespaces_with_finished_running_tasks_cv_.Broadcast(); 370 has_namespaces_with_finished_running_tasks_cv_.Signal();
371 }
372
373 bool RasterWorkerPool::ShouldRunTaskForCategoryWithLockAcquired(
374 cc::TaskCategory category) {
375 lock_.AssertAcquired();
376
377 if (!work_queue_.HasReadyToRunTasksForCategory(category))
378 return false;
379
380 if (category == cc::TASK_CATEGORY_BACKGROUND) {
381 // Only run background tasks if there are no foreground tasks running or
382 // ready to run.
383 size_t num_running_foreground_tasks =
384 work_queue_.NumRunningTasksForCategory(
385 cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND) +
386 work_queue_.NumRunningTasksForCategory(cc::TASK_CATEGORY_FOREGROUND);
387 bool has_ready_to_run_foreground_tasks =
388 work_queue_.HasReadyToRunTasksForCategory(
389 cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND) ||
390 work_queue_.HasReadyToRunTasksForCategory(cc::TASK_CATEGORY_FOREGROUND);
391
392 if (num_running_foreground_tasks > 0 || has_ready_to_run_foreground_tasks)
393 return false;
394 }
395
396 // Enforce that only one nonconcurrent task runs at a time.
397 if (category == cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND &&
398 work_queue_.NumRunningTasksForCategory(
399 cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND) > 0) {
400 return false;
401 }
402
403 return true;
404 }
405
406 void RasterWorkerPool::SignalHasReadyToRunTasksWithLockAcquired() {
407 lock_.AssertAcquired();
408
409 if (ShouldRunTaskForCategoryWithLockAcquired(cc::TASK_CATEGORY_FOREGROUND) ||
410 ShouldRunTaskForCategoryWithLockAcquired(
411 cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND)) {
412 has_ready_to_run_foreground_tasks_cv_.Signal();
413 }
414
415 if (ShouldRunTaskForCategoryWithLockAcquired(cc::TASK_CATEGORY_BACKGROUND)) {
416 has_ready_to_run_background_tasks_cv_.Signal();
417 }
355 } 418 }
356 419
357 RasterWorkerPool::ClosureTask::ClosureTask(const base::Closure& closure) 420 RasterWorkerPool::ClosureTask::ClosureTask(const base::Closure& closure)
358 : closure_(closure) {} 421 : closure_(closure) {}
359 422
360 // Overridden from cc::Task: 423 // Overridden from cc::Task:
361 void RasterWorkerPool::ClosureTask::RunOnWorkerThread() { 424 void RasterWorkerPool::ClosureTask::RunOnWorkerThread() {
362 closure_.Run(); 425 closure_.Run();
363 closure_.Reset(); 426 closure_.Reset();
364 } 427 }
365 428
366 RasterWorkerPool::ClosureTask::~ClosureTask() {} 429 RasterWorkerPool::ClosureTask::~ClosureTask() {}
367 430
368 } // namespace content 431 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/raster_worker_pool.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698