OLD | NEW |
| (Empty) |
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 | |
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 <memory> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/containers/hash_tables.h" | |
12 #include "base/macros.h" | |
13 #include "base/sequenced_task_runner.h" | |
14 #include "base/synchronization/condition_variable.h" | |
15 #include "base/task_runner.h" | |
16 #include "base/threading/simple_thread.h" | |
17 #include "cc/raster/task_category.h" | |
18 #include "cc/raster/task_graph_runner.h" | |
19 #include "cc/raster/task_graph_work_queue.h" | |
20 #include "content/common/content_export.h" | |
21 | |
22 namespace content { | |
23 | |
24 // A pool of threads used to run raster work. | |
25 // Work can be scheduled on the threads using different interfaces. | |
26 // The pool itself implements TaskRunner interface and tasks posted via that | |
27 // interface might run in parallel. | |
28 // CreateSequencedTaskRunner creates a sequenced task runner that might run in | |
29 // parallel with other instances of sequenced task runners. | |
30 // It's also possible to get the underlying TaskGraphRunner to schedule a graph | |
31 // of tasks with their dependencies. | |
32 class CONTENT_EXPORT RasterWorkerPool : public base::TaskRunner, | |
33 public cc::TaskGraphRunner { | |
34 public: | |
35 RasterWorkerPool(); | |
36 | |
37 // Overridden from base::TaskRunner: | |
38 bool PostDelayedTask(const tracked_objects::Location& from_here, | |
39 const base::Closure& task, | |
40 base::TimeDelta delay) override; | |
41 bool RunsTasksOnCurrentThread() const override; | |
42 | |
43 // Overridden from cc::TaskGraphRunner: | |
44 cc::NamespaceToken GetNamespaceToken() override; | |
45 void ScheduleTasks(cc::NamespaceToken token, cc::TaskGraph* graph) override; | |
46 void WaitForTasksToFinishRunning(cc::NamespaceToken token) override; | |
47 void CollectCompletedTasks(cc::NamespaceToken token, | |
48 cc::Task::Vector* completed_tasks) override; | |
49 | |
50 // Runs a task from one of the provided categories. Categories listed first | |
51 // have higher priority. | |
52 void Run(const std::vector<cc::TaskCategory>& categories, | |
53 base::ConditionVariable* has_ready_to_run_tasks_cv); | |
54 | |
55 void FlushForTesting(); | |
56 | |
57 // Spawn |num_threads| number of threads and start running work on the | |
58 // worker threads. | |
59 void Start(int num_threads); | |
60 | |
61 // Finish running all the posted tasks (and nested task posted by those tasks) | |
62 // of all the associated task runners. | |
63 // Once all the tasks are executed the method blocks until the threads are | |
64 // terminated. | |
65 void Shutdown(); | |
66 | |
67 cc::TaskGraphRunner* GetTaskGraphRunner() { return this; } | |
68 | |
69 // Create a new sequenced task graph runner. | |
70 scoped_refptr<base::SequencedTaskRunner> CreateSequencedTaskRunner(); | |
71 | |
72 protected: | |
73 ~RasterWorkerPool() override; | |
74 | |
75 private: | |
76 class RasterWorkerPoolSequencedTaskRunner; | |
77 friend class RasterWorkerPoolSequencedTaskRunner; | |
78 | |
79 // Simple Task for the TaskGraphRunner that wraps a closure. | |
80 // This class is used to schedule TaskRunner tasks on the | |
81 // |task_graph_runner_|. | |
82 class ClosureTask : public cc::Task { | |
83 public: | |
84 explicit ClosureTask(const base::Closure& closure); | |
85 | |
86 // Overridden from cc::Task: | |
87 void RunOnWorkerThread() override; | |
88 | |
89 protected: | |
90 ~ClosureTask() override; | |
91 | |
92 private: | |
93 base::Closure closure_; | |
94 | |
95 DISALLOW_COPY_AND_ASSIGN(ClosureTask); | |
96 }; | |
97 | |
98 void ScheduleTasksWithLockAcquired(cc::NamespaceToken token, | |
99 cc::TaskGraph* graph); | |
100 void CollectCompletedTasksWithLockAcquired(cc::NamespaceToken token, | |
101 cc::Task::Vector* completed_tasks); | |
102 | |
103 // Runs a task from one of the provided categories. Categories listed first | |
104 // have higher priority. Returns false if there were no tasks to run. | |
105 bool RunTaskWithLockAcquired(const std::vector<cc::TaskCategory>& categories); | |
106 | |
107 // Run next task for the given category. Caller must acquire |lock_| prior to | |
108 // calling this function and make sure at least one task is ready to run. | |
109 void RunTaskInCategoryWithLockAcquired(cc::TaskCategory category); | |
110 | |
111 // Helper function which signals worker threads if tasks are ready to run. | |
112 void SignalHasReadyToRunTasksWithLockAcquired(); | |
113 | |
114 // Determines if we should run a new task for the given category. This factors | |
115 // in whether a task is available and whether the count of running tasks is | |
116 // low enough to start a new one. | |
117 bool ShouldRunTaskForCategoryWithLockAcquired(cc::TaskCategory category); | |
118 | |
119 // The actual threads where work is done. | |
120 std::vector<std::unique_ptr<base::SimpleThread>> threads_; | |
121 | |
122 // Lock to exclusively access all the following members that are used to | |
123 // implement the TaskRunner and TaskGraphRunner interfaces. | |
124 base::Lock lock_; | |
125 // Stores the tasks to be run, sorted by priority. | |
126 cc::TaskGraphWorkQueue work_queue_; | |
127 // Namespace used to schedule tasks in the task graph runner. | |
128 cc::NamespaceToken namespace_token_; | |
129 // List of tasks currently queued up for execution. | |
130 cc::Task::Vector tasks_; | |
131 // Graph object used for scheduling tasks. | |
132 cc::TaskGraph graph_; | |
133 // Cached vector to avoid allocation when getting the list of complete | |
134 // tasks. | |
135 cc::Task::Vector completed_tasks_; | |
136 // Condition variables for foreground and background tasks. | |
137 base::ConditionVariable has_ready_to_run_foreground_tasks_cv_; | |
138 base::ConditionVariable has_ready_to_run_background_tasks_cv_; | |
139 // Condition variable that is waited on by origin threads until a namespace | |
140 // has finished running all associated tasks. | |
141 base::ConditionVariable has_namespaces_with_finished_running_tasks_cv_; | |
142 // Set during shutdown. Tells Run() to return when no more tasks are pending. | |
143 bool shutdown_; | |
144 }; | |
145 | |
146 } // namespace content | |
147 | |
148 #endif // CONTENT_RENDERER_RASTER_WORKER_POOL_H_ | |
OLD | NEW |