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_WORKER_POOL_H_ | 5 #ifndef CC_WORKER_POOL_H_ |
6 #define CC_WORKER_POOL_H_ | 6 #define CC_WORKER_POOL_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/cancelable_callback.h" | 12 #include "base/cancelable_callback.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
16 #include "cc/rendering_stats.h" | 16 #include "cc/rendering_stats.h" |
17 #include "cc/scoped_ptr_deque.h" | 17 #include "cc/scoped_ptr_deque.h" |
18 | 18 |
19 namespace cc { | 19 namespace cc { |
20 namespace internal { | 20 namespace internal { |
21 | 21 |
22 class WorkerPoolTask { | 22 class WorkerPoolTask { |
23 public: | 23 public: |
24 virtual ~WorkerPoolTask(); | 24 virtual ~WorkerPoolTask(); |
25 | 25 |
| 26 // Called when the task is scheduled to run on a thread that isn't the |
| 27 // origin thread. |
| 28 virtual void WillRunOnThread(base::Thread* thread) = 0; |
| 29 |
26 virtual void Run(RenderingStats* rendering_stats) = 0; | 30 virtual void Run(RenderingStats* rendering_stats) = 0; |
27 | 31 |
28 bool HasCompleted(); | 32 bool HasCompleted(); |
29 void DidComplete(); | 33 void DidComplete(); |
30 | 34 |
31 protected: | 35 protected: |
32 WorkerPoolTask(const base::Closure& reply); | 36 WorkerPoolTask(const base::Closure& reply); |
33 | 37 |
34 const base::Closure reply_; | 38 const base::Closure reply_; |
35 | 39 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 118 |
115 WorkerPool* worker_pool_; | 119 WorkerPool* worker_pool_; |
116 ScopedPtrDeque<internal::WorkerPoolTask> pending_tasks_; | 120 ScopedPtrDeque<internal::WorkerPoolTask> pending_tasks_; |
117 scoped_ptr<RenderingStats> rendering_stats_; | 121 scoped_ptr<RenderingStats> rendering_stats_; |
118 bool record_rendering_stats_; | 122 bool record_rendering_stats_; |
119 }; | 123 }; |
120 | 124 |
121 WorkerPool(WorkerPoolClient* client, size_t num_threads); | 125 WorkerPool(WorkerPoolClient* client, size_t num_threads); |
122 | 126 |
123 WorkerPool::Worker* GetWorkerForNextTask(); | 127 WorkerPool::Worker* GetWorkerForNextTask(); |
| 128 bool CanPostCheapTask() const; |
| 129 void PostCheapTask(scoped_ptr<internal::WorkerPoolTask> task); |
124 | 130 |
125 private: | 131 private: |
126 class NumPendingTasksComparator { | 132 class NumPendingTasksComparator { |
127 public: | 133 public: |
128 bool operator() (const Worker* a, const Worker* b) const { | 134 bool operator() (const Worker* a, const Worker* b) const { |
129 return a->num_pending_tasks() < b->num_pending_tasks(); | 135 return a->num_pending_tasks() < b->num_pending_tasks(); |
130 } | 136 } |
131 }; | 137 }; |
132 | 138 |
133 // Schedule a completed tasks check if not already pending. | 139 // Schedule a completed tasks check if not already pending. |
(...skipping 10 matching lines...) Expand all Loading... |
144 | 150 |
145 // Check for completed tasks and run reply callbacks. | 151 // Check for completed tasks and run reply callbacks. |
146 void CheckForCompletedTasks(); | 152 void CheckForCompletedTasks(); |
147 | 153 |
148 // Called when processing task completion. | 154 // Called when processing task completion. |
149 void OnTaskCompleted(); | 155 void OnTaskCompleted(); |
150 | 156 |
151 // Ensure workers are sorted by number of pending tasks. | 157 // Ensure workers are sorted by number of pending tasks. |
152 void SortWorkersIfNeeded(); | 158 void SortWorkersIfNeeded(); |
153 | 159 |
| 160 // Schedule running cheap tasks on the origin thread unless already pending. |
| 161 void ScheduleRunCheapTasks(); |
| 162 |
| 163 // Run pending cheap tasks on the origin thread. If the allotted time slot |
| 164 // for cheap tasks runs out, the remaining tasks are deferred to the thread |
| 165 // pool. |
| 166 void RunCheapTasks(); |
| 167 |
154 typedef std::vector<Worker*> WorkerVector; | 168 typedef std::vector<Worker*> WorkerVector; |
155 WorkerVector workers_; | 169 WorkerVector workers_; |
156 WorkerPoolClient* client_; | 170 WorkerPoolClient* client_; |
157 scoped_refptr<base::MessageLoopProxy> origin_loop_; | 171 scoped_refptr<base::MessageLoopProxy> origin_loop_; |
158 base::WeakPtrFactory<WorkerPool> weak_ptr_factory_; | 172 base::WeakPtrFactory<WorkerPool> weak_ptr_factory_; |
159 bool workers_need_sorting_; | 173 bool workers_need_sorting_; |
160 bool shutdown_; | 174 bool shutdown_; |
161 base::CancelableClosure check_for_completed_tasks_callback_; | 175 base::CancelableClosure check_for_completed_tasks_callback_; |
162 bool check_for_completed_tasks_pending_; | 176 base::TimeTicks check_for_completed_tasks_deadline_; |
163 base::Closure idle_callback_; | 177 base::Closure idle_callback_; |
| 178 base::Closure cheap_task_callback_; |
164 // Accessed from multiple threads. 0 when worker pool is idle. | 179 // Accessed from multiple threads. 0 when worker pool is idle. |
165 base::subtle::Atomic32 pending_task_count_; | 180 base::subtle::Atomic32 pending_task_count_; |
166 | 181 |
| 182 bool run_cheap_tasks_pending_; |
| 183 ScopedPtrDeque<internal::WorkerPoolTask> pending_cheap_tasks_; |
| 184 ScopedPtrDeque<internal::WorkerPoolTask> completed_cheap_tasks_; |
| 185 scoped_ptr<RenderingStats> cheap_rendering_stats_; |
| 186 |
167 DISALLOW_COPY_AND_ASSIGN(WorkerPool); | 187 DISALLOW_COPY_AND_ASSIGN(WorkerPool); |
168 }; | 188 }; |
169 | 189 |
170 } // namespace cc | 190 } // namespace cc |
171 | 191 |
172 #endif // CC_WORKER_POOL_H_ | 192 #endif // CC_WORKER_POOL_H_ |
OLD | NEW |