blob: 1d6fc04ca6719ed8fd22f34093b7a35d3c8b4814 [file] [log] [blame]
dcastagna4517a182015-08-05 19:51:031// Copyright 2015 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_RASTER_WORKER_POOL_H_
6#define CONTENT_RENDERER_RASTER_WORKER_POOL_H_
7
8#include "base/callback.h"
9#include "base/containers/hash_tables.h"
avi1023d012015-12-25 02:39:1410#include "base/macros.h"
dcastagna4517a182015-08-05 19:51:0311#include "base/memory/scoped_vector.h"
12#include "base/sequenced_task_runner.h"
ericrk4e3aa5a2015-12-01 03:53:5613#include "base/synchronization/condition_variable.h"
dcastagna4517a182015-08-05 19:51:0314#include "base/task_runner.h"
15#include "base/threading/simple_thread.h"
ericrk7795c7002016-01-21 22:49:1816#include "cc/raster/task_category.h"
dcastagna4517a182015-08-05 19:51:0317#include "cc/raster/task_graph_runner.h"
ericrk4e3aa5a2015-12-01 03:53:5618#include "cc/raster/task_graph_work_queue.h"
dcastagna4517a182015-08-05 19:51:0319#include "content/common/content_export.h"
20
21namespace content {
22
23// A pool of threads used to run raster work.
24// Work can be scheduled on the threads using different interfaces.
25// The pool itself implements TaskRunner interface and tasks posted via that
26// interface might run in parallel.
27// CreateSequencedTaskRunner creates a sequenced task runner that might run in
28// parallel with other instances of sequenced task runners.
29// It's also possible to get the underlying TaskGraphRunner to schedule a graph
30// of tasks with their dependencies.
ericrk7795c7002016-01-21 22:49:1831class CONTENT_EXPORT RasterWorkerPool : public base::TaskRunner,
32 public cc::TaskGraphRunner {
dcastagna4517a182015-08-05 19:51:0333 public:
34 RasterWorkerPool();
35
36 // Overridden from base::TaskRunner:
37 bool PostDelayedTask(const tracked_objects::Location& from_here,
38 const base::Closure& task,
39 base::TimeDelta delay) override;
40 bool RunsTasksOnCurrentThread() const override;
41
ericrk4e3aa5a2015-12-01 03:53:5642 // Overridden from cc::TaskGraphRunner:
43 cc::NamespaceToken GetNamespaceToken() override;
44 void ScheduleTasks(cc::NamespaceToken token, cc::TaskGraph* graph) override;
45 void WaitForTasksToFinishRunning(cc::NamespaceToken token) override;
46 void CollectCompletedTasks(cc::NamespaceToken token,
47 cc::Task::Vector* completed_tasks) override;
48
ericrk7795c7002016-01-21 22:49:1849 // Runs a task from one of the provided categories. Categories listed first
50 // have higher priority.
51 void Run(const std::vector<cc::TaskCategory>& categories);
dcastagna4517a182015-08-05 19:51:0352
ericrk4e3aa5a2015-12-01 03:53:5653 void FlushForTesting();
54
dcastagna4517a182015-08-05 19:51:0355 // Spawn |num_threads| number of threads and start running work on the
56 // worker threads.
57 void Start(int num_threads,
58 const base::SimpleThread::Options& thread_options);
59
60 // Finish running all the posted tasks (and nested task posted by those tasks)
61 // of all the associated task runners.
62 // Once all the tasks are executed the method blocks until the threads are
63 // terminated.
64 void Shutdown();
65
ericrk4e3aa5a2015-12-01 03:53:5666 cc::TaskGraphRunner* GetTaskGraphRunner() { return this; }
dcastagna4517a182015-08-05 19:51:0367
68 // Create a new sequenced task graph runner.
69 scoped_refptr<base::SequencedTaskRunner> CreateSequencedTaskRunner();
70
71 protected:
72 ~RasterWorkerPool() override;
73
74 private:
75 class RasterWorkerPoolSequencedTaskRunner;
76 friend class RasterWorkerPoolSequencedTaskRunner;
77
ericrk7795c7002016-01-21 22:49:1878 // Runs a task from one of the provided categories. Categories listed first
79 // have higher priority. Returns false if there were no tasks to run.
80 bool RunTaskWithLockAcquired(const std::vector<cc::TaskCategory>& categories);
81
82 // Run next task for the given category. Caller must acquire |lock_| prior to
83 // calling this function and make sure at least one task is ready to run.
84 void RunTaskInCategoryWithLockAcquired(cc::TaskCategory category);
ericrk4e3aa5a2015-12-01 03:53:5685
dcastagna4517a182015-08-05 19:51:0386 // Simple Task for the TaskGraphRunner that wraps a closure.
87 // This class is used to schedule TaskRunner tasks on the
88 // |task_graph_runner_|.
89 class ClosureTask : public cc::Task {
90 public:
91 explicit ClosureTask(const base::Closure& closure);
92
93 // Overridden from cc::Task:
94 void RunOnWorkerThread() override;
95
96 protected:
97 ~ClosureTask() override;
98
99 private:
100 base::Closure closure_;
101
102 DISALLOW_COPY_AND_ASSIGN(ClosureTask);
103 };
104
ericrk4e3aa5a2015-12-01 03:53:56105 void ScheduleTasksWithLockAcquired(cc::NamespaceToken token,
106 cc::TaskGraph* graph);
107 void CollectCompletedTasksWithLockAcquired(cc::NamespaceToken token,
108 cc::Task::Vector* completed_tasks);
109
dcastagna4517a182015-08-05 19:51:03110 // The actual threads where work is done.
ericrk7795c7002016-01-21 22:49:18111 std::vector<scoped_ptr<base::SimpleThread>> threads_;
dcastagna4517a182015-08-05 19:51:03112
113 // Lock to exclusively access all the following members that are used to
ericrk4e3aa5a2015-12-01 03:53:56114 // implement the TaskRunner and TaskGraphRunner interfaces.
dcastagna4517a182015-08-05 19:51:03115 base::Lock lock_;
ericrk4e3aa5a2015-12-01 03:53:56116 // Stores the tasks to be run, sorted by priority.
117 cc::TaskGraphWorkQueue work_queue_;
dcastagna4517a182015-08-05 19:51:03118 // Namespace used to schedule tasks in the task graph runner.
119 cc::NamespaceToken namespace_token_;
120 // List of tasks currently queued up for execution.
121 cc::Task::Vector tasks_;
122 // Graph object used for scheduling tasks.
123 cc::TaskGraph graph_;
124 // Cached vector to avoid allocation when getting the list of complete
125 // tasks.
126 cc::Task::Vector completed_tasks_;
ericrk4e3aa5a2015-12-01 03:53:56127 // Condition variable that is waited on by Run() until new tasks are ready to
128 // run or shutdown starts.
129 base::ConditionVariable has_ready_to_run_tasks_cv_;
130 // Condition variable that is waited on by origin threads until a namespace
131 // has finished running all associated tasks.
132 base::ConditionVariable has_namespaces_with_finished_running_tasks_cv_;
133 // Set during shutdown. Tells Run() to return when no more tasks are pending.
134 bool shutdown_;
dcastagna4517a182015-08-05 19:51:03135};
136
137} // namespace content
138
139#endif // CONTENT_RENDERER_RASTER_WORKER_POOL_H_