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_BASE_WORKER_POOL_H_ | 5 #ifndef CC_BASE_WORKER_POOL_H_ |
6 #define CC_BASE_WORKER_POOL_H_ | 6 #define CC_BASE_WORKER_POOL_H_ |
7 | 7 |
8 #include <deque> | |
8 #include <string> | 9 #include <string> |
9 | 10 |
10 #include "base/cancelable_callback.h" | 11 #include "base/cancelable_callback.h" |
12 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
13 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
14 #include "cc/base/cc_export.h" | 16 #include "cc/base/cc_export.h" |
15 #include "cc/base/scoped_ptr_deque.h" | |
16 | 17 |
17 namespace cc { | 18 namespace cc { |
18 | 19 |
19 namespace internal { | 20 namespace internal { |
20 | 21 |
21 class WorkerPoolTask { | 22 class CC_EXPORT WorkerPoolTask |
23 : public base::RefCountedThreadSafe<WorkerPoolTask> { | |
22 public: | 24 public: |
25 virtual void RunOnThread(unsigned thread_index) = 0; | |
26 virtual void DispatchCompletionCallback() = 0; | |
27 | |
28 void DidSchedule(); | |
29 void WillRun(); | |
30 void DidRun(); | |
31 void DidComplete(); | |
32 | |
33 bool HasFinished() const; | |
34 | |
35 protected: | |
36 friend class base::RefCountedThreadSafe<WorkerPoolTask>; | |
37 | |
38 WorkerPoolTask(); | |
23 virtual ~WorkerPoolTask(); | 39 virtual ~WorkerPoolTask(); |
24 | 40 |
25 virtual void RunOnThread(unsigned thread_index) = 0; | 41 private: |
42 bool did_schedule_; | |
43 bool did_run_; | |
44 bool did_complete_; | |
45 }; | |
26 | 46 |
nduca
2013/05/21 00:31:13
I guess i'm still puzzled why we have dependency s
reveman
2013/05/21 01:22:36
There's a 1-N relationship between tasks and depen
| |
27 void DidComplete(); | 47 class CC_EXPORT WorkerPoolTaskDependency { |
48 public: | |
49 // Depth-first traversal of dependency graph. Parent nodes are visited | |
50 // before children. | |
51 class Iterator { | |
52 public: | |
53 explicit Iterator(const WorkerPoolTaskDependency* root); | |
54 ~Iterator(); | |
55 | |
56 const WorkerPoolTaskDependency* operator->() const { | |
57 DCHECK(current_); | |
58 return current_; | |
59 } | |
60 | |
61 const WorkerPoolTaskDependency* operator*() const { | |
62 DCHECK(current_); | |
63 return current_; | |
64 } | |
65 | |
66 Iterator& operator++() { | |
67 if (current_->first_child_) { | |
68 current_ = current_->first_child_.get(); | |
69 return *this; | |
70 } | |
71 | |
72 while (!current_->next_sibling_ && current_ != root_) | |
73 current_ = current_->parent_; | |
74 if (current_ == root_) | |
75 return *this; | |
76 | |
77 current_ = current_->next_sibling_.get(); | |
78 return *this; | |
79 } | |
80 | |
81 operator bool() const { | |
82 return current_ != root_; | |
83 } | |
84 | |
85 private: | |
86 const WorkerPoolTaskDependency* current_; | |
87 const WorkerPoolTaskDependency* root_; | |
88 }; | |
89 | |
90 // Visits each task for all direct children. | |
91 class TaskIterator { | |
92 public: | |
93 explicit TaskIterator(const WorkerPoolTaskDependency* parent); | |
94 ~TaskIterator(); | |
95 | |
96 WorkerPoolTask* operator->() const { | |
97 DCHECK(current_); | |
98 return current_->task_.get(); | |
99 } | |
100 | |
101 WorkerPoolTask* operator*() const { | |
102 DCHECK(current_); | |
103 return current_->task_.get(); | |
104 } | |
105 | |
106 TaskIterator& operator++() { | |
107 current_ = current_->next_sibling_.get(); | |
108 return *this; | |
109 } | |
110 | |
111 operator bool() const { | |
112 return !!current_; | |
113 } | |
114 | |
115 private: | |
116 const WorkerPoolTaskDependency* current_; | |
117 }; | |
118 | |
119 WorkerPoolTaskDependency(); | |
120 ~WorkerPoolTaskDependency(); | |
121 | |
122 static void Create(WorkerPoolTask* task, | |
123 WorkerPoolTaskDependency* parent, | |
124 WorkerPoolTaskDependency* dependencies); | |
125 | |
126 void Swap(WorkerPoolTaskDependency* other); | |
127 | |
128 WorkerPoolTask* task() const { return task_.get(); } | |
28 | 129 |
29 protected: | 130 protected: |
30 explicit WorkerPoolTask(const base::Closure& reply); | 131 // |parent_| is only set when "parent_->last_child_ == this". |
132 // This enough to traverse graph in O(n) time and O(1) space complexity, | |
133 // while also allowing Swap() to be implemented in O(1) time. | |
134 WorkerPoolTaskDependency* parent_; | |
135 WorkerPoolTaskDependency* last_child_; | |
136 scoped_ptr<WorkerPoolTaskDependency> first_child_; | |
137 scoped_ptr<WorkerPoolTaskDependency> next_sibling_; | |
138 scoped_refptr<WorkerPoolTask> task_; | |
139 }; | |
31 | 140 |
32 const base::Closure reply_; | 141 typedef WorkerPoolTaskDependency WorkerPoolTaskGraph; |
33 }; | |
34 | 142 |
35 } // namespace internal | 143 } // namespace internal |
36 | 144 |
37 class CC_EXPORT WorkerPoolClient { | 145 class CC_EXPORT WorkerPoolClient { |
38 public: | 146 public: |
39 virtual void DidFinishDispatchingWorkerPoolCompletionCallbacks() = 0; | 147 virtual void DidFinishDispatchingWorkerPoolCompletionCallbacks() = 0; |
40 | 148 |
41 protected: | 149 protected: |
42 virtual ~WorkerPoolClient() {} | 150 virtual ~WorkerPoolClient() {} |
43 }; | 151 }; |
44 | 152 |
45 // A worker thread pool that runs rendering tasks and guarantees completion | 153 // A worker thread pool that runs tasks provided by task graph and |
46 // of all pending tasks at shutdown. | 154 // guarantees completion of all pending tasks at shutdown. |
47 class CC_EXPORT WorkerPool { | 155 class CC_EXPORT WorkerPool { |
48 public: | 156 public: |
49 typedef base::Callback<void()> Callback; | |
50 | |
51 virtual ~WorkerPool(); | 157 virtual ~WorkerPool(); |
52 | 158 |
53 static scoped_ptr<WorkerPool> Create( | |
54 size_t num_threads, | |
55 base::TimeDelta check_for_completed_tasks_delay, | |
56 const std::string& thread_name_prefix) { | |
57 return make_scoped_ptr(new WorkerPool(num_threads, | |
58 check_for_completed_tasks_delay, | |
59 thread_name_prefix)); | |
60 } | |
61 | |
62 // Tells the worker pool to shutdown and returns once all pending tasks have | 159 // Tells the worker pool to shutdown and returns once all pending tasks have |
63 // completed. | 160 // completed. |
64 void Shutdown(); | 161 virtual void Shutdown(); |
65 | |
66 // Posts |task| to worker pool. On completion, |reply| | |
67 // is posted to the thread that called PostTaskAndReply(). | |
68 void PostTaskAndReply(const Callback& task, const base::Closure& reply); | |
69 | 162 |
70 // Set a new client. | 163 // Set a new client. |
71 void SetClient(WorkerPoolClient* client) { | 164 void SetClient(WorkerPoolClient* client) { |
72 client_ = client; | 165 client_ = client; |
73 } | 166 } |
74 | 167 |
75 protected: | 168 protected: |
76 WorkerPool(size_t num_threads, | 169 WorkerPool(size_t num_threads, |
77 base::TimeDelta check_for_completed_tasks_delay, | 170 base::TimeDelta check_for_completed_tasks_delay, |
78 const std::string& thread_name_prefix); | 171 const std::string& thread_name_prefix); |
79 | 172 |
80 void PostTask(scoped_ptr<internal::WorkerPoolTask> task); | 173 void ScheduleTasks(internal::WorkerPoolTaskGraph* task_graph); |
81 | 174 |
82 private: | 175 private: |
83 class Inner; | 176 class Inner; |
84 friend class Inner; | 177 friend class Inner; |
85 | 178 |
86 void OnTaskCompleted(); | 179 typedef std::deque<scoped_refptr<internal::WorkerPoolTask> > TaskDeque; |
87 void OnIdle(); | 180 |
181 void OnIdle(TaskDeque* completed_tasks); | |
88 void ScheduleCheckForCompletedTasks(); | 182 void ScheduleCheckForCompletedTasks(); |
89 void CheckForCompletedTasks(); | 183 void CheckForCompletedTasks(); |
90 void DispatchCompletionCallbacks(); | 184 void DispatchCompletionCallbacks(TaskDeque* completed_tasks); |
91 | 185 |
92 WorkerPoolClient* client_; | 186 WorkerPoolClient* client_; |
93 scoped_refptr<base::MessageLoopProxy> origin_loop_; | 187 scoped_refptr<base::MessageLoopProxy> origin_loop_; |
94 base::WeakPtrFactory<WorkerPool> weak_ptr_factory_; | 188 base::CancelableClosure check_for_completed_tasks_callback_; |
95 base::TimeDelta check_for_completed_tasks_delay_; | 189 base::TimeDelta check_for_completed_tasks_delay_; |
96 bool check_for_completed_tasks_pending_; | 190 bool check_for_completed_tasks_pending_; |
97 | 191 |
98 // Holds all completed tasks for which we have not yet dispatched | |
99 // reply callbacks. | |
100 ScopedPtrDeque<internal::WorkerPoolTask> completed_tasks_; | |
101 | |
102 // Hide the gory details of the worker pool in |inner_|. | 192 // Hide the gory details of the worker pool in |inner_|. |
103 const scoped_ptr<Inner> inner_; | 193 const scoped_ptr<Inner> inner_; |
104 | |
105 DISALLOW_COPY_AND_ASSIGN(WorkerPool); | |
106 }; | 194 }; |
107 | 195 |
108 } // namespace cc | 196 } // namespace cc |
109 | 197 |
110 #endif // CC_BASE_WORKER_POOL_H_ | 198 #endif // CC_BASE_WORKER_POOL_H_ |
OLD | NEW |