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