OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 CC_RESOURCES_WORKER_POOL_H_ | 5 #ifndef CC_RESOURCES_WORKER_POOL_H_ |
6 #define CC_RESOURCES_WORKER_POOL_H_ | 6 #define CC_RESOURCES_WORKER_POOL_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/cancelable_callback.h" | 12 #include "base/cancelable_callback.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
17 #include "cc/base/cc_export.h" | 17 #include "cc/base/cc_export.h" |
18 #include "cc/base/scoped_ptr_hash_map.h" | 18 #include "cc/base/scoped_ptr_hash_map.h" |
19 | 19 |
20 namespace cc { | 20 namespace cc { |
21 namespace internal { | 21 namespace internal { |
22 | 22 |
23 class CC_EXPORT WorkerPoolTask | 23 class CC_EXPORT WorkerPoolTask |
24 : public base::RefCountedThreadSafe<WorkerPoolTask> { | 24 : public base::RefCountedThreadSafe<WorkerPoolTask> { |
25 public: | 25 public: |
26 typedef std::vector<scoped_refptr<WorkerPoolTask> > TaskVector; | |
27 | |
28 virtual void RunOnThread(unsigned thread_index) = 0; | 26 virtual void RunOnThread(unsigned thread_index) = 0; |
29 virtual void DispatchCompletionCallback() = 0; | 27 virtual void DispatchCompletionCallback() = 0; |
30 | 28 |
31 void DidSchedule(); | 29 void DidSchedule(); |
32 void WillRun(); | 30 void WillRun(); |
33 void DidRun(); | 31 void DidRun(); |
34 void DidComplete(); | 32 void DidComplete(); |
35 | 33 |
36 bool HasFinishedRunning() const; | 34 bool HasFinishedRunning() const; |
37 bool HasCompleted() const; | 35 bool HasCompleted() const; |
38 | 36 |
39 TaskVector& dependencies() { return dependencies_; } | |
40 | |
41 protected: | 37 protected: |
42 friend class base::RefCountedThreadSafe<WorkerPoolTask>; | 38 friend class base::RefCountedThreadSafe<WorkerPoolTask>; |
43 | 39 |
44 WorkerPoolTask(); | 40 WorkerPoolTask(); |
45 explicit WorkerPoolTask(TaskVector* dependencies); | |
46 virtual ~WorkerPoolTask(); | 41 virtual ~WorkerPoolTask(); |
47 | 42 |
48 private: | 43 private: |
49 bool did_schedule_; | 44 bool did_schedule_; |
50 bool did_run_; | 45 bool did_run_; |
51 bool did_complete_; | 46 bool did_complete_; |
52 TaskVector dependencies_; | |
53 }; | 47 }; |
54 | 48 |
55 } // namespace internal | 49 } // namespace internal |
56 } // namespace cc | 50 } // namespace cc |
57 | 51 |
58 #if defined(COMPILER_GCC) | 52 #if defined(COMPILER_GCC) |
59 namespace BASE_HASH_NAMESPACE { | 53 namespace BASE_HASH_NAMESPACE { |
60 template <> struct hash<cc::internal::WorkerPoolTask*> { | 54 template <> struct hash<cc::internal::WorkerPoolTask*> { |
61 size_t operator()(cc::internal::WorkerPoolTask* ptr) const { | 55 size_t operator()(cc::internal::WorkerPoolTask* ptr) const { |
62 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); | 56 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); |
(...skipping 15 matching lines...) Expand all Loading... |
78 virtual void Shutdown(); | 72 virtual void Shutdown(); |
79 | 73 |
80 // Force a check for completed tasks. | 74 // Force a check for completed tasks. |
81 virtual void CheckForCompletedTasks(); | 75 virtual void CheckForCompletedTasks(); |
82 | 76 |
83 protected: | 77 protected: |
84 class CC_EXPORT GraphNode { | 78 class CC_EXPORT GraphNode { |
85 public: | 79 public: |
86 typedef std::vector<GraphNode*> Vector; | 80 typedef std::vector<GraphNode*> Vector; |
87 | 81 |
88 explicit GraphNode(internal::WorkerPoolTask* task); | 82 GraphNode(); |
89 ~GraphNode(); | 83 ~GraphNode(); |
90 | 84 |
| 85 void set_task(internal::WorkerPoolTask* task) { task_ = task; } |
91 internal::WorkerPoolTask* task() { return task_; } | 86 internal::WorkerPoolTask* task() { return task_; } |
92 | 87 |
93 void add_dependent(GraphNode* dependent) { | 88 void add_dependent(GraphNode* dependent) { |
| 89 DCHECK(dependent); |
94 dependents_.push_back(dependent); | 90 dependents_.push_back(dependent); |
95 } | 91 } |
96 const Vector& dependents() const { | 92 const Vector& dependents() const { |
97 return dependents_; | 93 return dependents_; |
98 } | 94 } |
99 | 95 |
100 void set_priority(unsigned priority) { priority_ = priority; } | 96 void set_priority(unsigned priority) { priority_ = priority; } |
101 unsigned priority() const { return priority_; } | 97 unsigned priority() const { return priority_; } |
102 | 98 |
103 unsigned num_dependencies() const { | 99 unsigned num_dependencies() const { |
104 return num_dependencies_; | 100 return num_dependencies_; |
105 } | 101 } |
106 void add_dependency() { ++num_dependencies_; } | 102 void add_dependency() { ++num_dependencies_; } |
107 void remove_dependency() { | 103 void remove_dependency() { |
108 DCHECK(num_dependencies_); | 104 DCHECK(num_dependencies_); |
109 --num_dependencies_; | 105 --num_dependencies_; |
110 } | 106 } |
111 | 107 |
112 private: | 108 private: |
113 internal::WorkerPoolTask* task_; | 109 internal::WorkerPoolTask* task_; |
114 Vector dependents_; | 110 Vector dependents_; |
115 unsigned priority_; | 111 unsigned priority_; |
116 unsigned num_dependencies_; | 112 unsigned num_dependencies_; |
117 | 113 |
118 DISALLOW_COPY_AND_ASSIGN(GraphNode); | 114 DISALLOW_COPY_AND_ASSIGN(GraphNode); |
119 }; | 115 }; |
| 116 // A task graph contains a unique set of tasks with edges between |
| 117 // dependencies pointing in the direction of the dependents. Each task |
| 118 // need to be assigned a unique priority and a run count that matches |
| 119 // the number of dependencies. |
120 typedef ScopedPtrHashMap<internal::WorkerPoolTask*, GraphNode> GraphNodeMap; | 120 typedef ScopedPtrHashMap<internal::WorkerPoolTask*, GraphNode> GraphNodeMap; |
121 typedef GraphNodeMap TaskGraph; | 121 typedef GraphNodeMap TaskGraph; |
122 | 122 |
123 WorkerPool(size_t num_threads, const std::string& thread_name_prefix); | 123 WorkerPool(size_t num_threads, const std::string& thread_name_prefix); |
124 | 124 |
125 // Schedule running of tasks in |graph|. Any previously scheduled tasks | 125 // Schedule running of tasks in |graph|. Any previously scheduled tasks |
126 // that are not already running will be canceled. Canceled tasks don't run | 126 // that are not already running will be canceled. Canceled tasks don't run |
127 // but completion of them is still processed. | 127 // but completion of them is still processed. |
128 void SetTaskGraph(TaskGraph* graph); | 128 void SetTaskGraph(TaskGraph* graph); |
129 | 129 |
130 // BuildTaskGraph() takes a task tree as input and constructs a | |
131 // unique set of tasks with edges between dependencies pointing in | |
132 // the direction of the dependents. Each task is given a unique priority | |
133 // which is currently the same as the DFS traversal order. | |
134 // | |
135 // Input: Output: | |
136 // | |
137 // root task4 Task | Priority (lower is better) | |
138 // / \ / \ -------+--------------------------- | |
139 // task1 task2 task3 task2 root | 4 | |
140 // | | | | task1 | 2 | |
141 // task3 | task1 | task2 | 3 | |
142 // | | \ / task3 | 1 | |
143 // task4 task4 root task4 | 0 | |
144 // | |
145 // The output can be used to efficiently maintain a queue of | |
146 // "ready to run" tasks. | |
147 static unsigned BuildTaskGraphRecursive( | |
148 internal::WorkerPoolTask* task, | |
149 GraphNode* dependent, | |
150 unsigned priority, | |
151 TaskGraph* graph); | |
152 static void BuildTaskGraph( | |
153 internal::WorkerPoolTask* root, TaskGraph* graph); | |
154 | |
155 private: | 130 private: |
156 class Inner; | 131 class Inner; |
157 friend class Inner; | 132 friend class Inner; |
158 | 133 |
159 typedef std::deque<scoped_refptr<internal::WorkerPoolTask> > TaskDeque; | 134 typedef std::deque<scoped_refptr<internal::WorkerPoolTask> > TaskDeque; |
160 | 135 |
161 void DispatchCompletionCallbacks(TaskDeque* completed_tasks); | 136 void DispatchCompletionCallbacks(TaskDeque* completed_tasks); |
162 | 137 |
163 bool in_dispatch_completion_callbacks_; | 138 bool in_dispatch_completion_callbacks_; |
164 | 139 |
165 // Hide the gory details of the worker pool in |inner_|. | 140 // Hide the gory details of the worker pool in |inner_|. |
166 const scoped_ptr<Inner> inner_; | 141 const scoped_ptr<Inner> inner_; |
167 }; | 142 }; |
168 | 143 |
169 } // namespace cc | 144 } // namespace cc |
170 | 145 |
171 #endif // CC_RESOURCES_WORKER_POOL_H_ | 146 #endif // CC_RESOURCES_WORKER_POOL_H_ |
OLD | NEW |