| 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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 void OnTaskCompleted(); | 117 void OnTaskCompleted(); |
| 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 void PostTask(scoped_ptr<internal::WorkerPoolTask> task, bool is_cheap); |
| 124 | 128 |
| 125 private: | 129 private: |
| 126 class NumPendingTasksComparator { | 130 class NumPendingTasksComparator { |
| 127 public: | 131 public: |
| 128 bool operator() (const Worker* a, const Worker* b) const { | 132 bool operator() (const Worker* a, const Worker* b) const { |
| 129 return a->num_pending_tasks() < b->num_pending_tasks(); | 133 return a->num_pending_tasks() < b->num_pending_tasks(); |
| 130 } | 134 } |
| 131 }; | 135 }; |
| 132 | 136 |
| 133 // Schedule a completed tasks check if not already pending. | 137 // Schedule a completed tasks check if not already pending. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 144 | 148 |
| 145 // Check for completed tasks and run reply callbacks. | 149 // Check for completed tasks and run reply callbacks. |
| 146 void CheckForCompletedTasks(); | 150 void CheckForCompletedTasks(); |
| 147 | 151 |
| 148 // Called when processing task completion. | 152 // Called when processing task completion. |
| 149 void OnTaskCompleted(); | 153 void OnTaskCompleted(); |
| 150 | 154 |
| 151 // Ensure workers are sorted by number of pending tasks. | 155 // Ensure workers are sorted by number of pending tasks. |
| 152 void SortWorkersIfNeeded(); | 156 void SortWorkersIfNeeded(); |
| 153 | 157 |
| 158 // Schedule running cheap tasks on the origin thread unless already pending. |
| 159 void ScheduleRunCheapTasks(); |
| 160 |
| 161 // Run pending cheap tasks on the origin thread. If the allotted time slot |
| 162 // for cheap tasks runs out, the remaining tasks are deferred to the thread |
| 163 // pool. |
| 164 void RunCheapTasks(); |
| 165 |
| 166 WorkerPool::Worker* GetWorkerForNextTask(); |
| 167 bool CanPostCheapTask() const; |
| 168 void PostCheapTask(scoped_ptr<internal::WorkerPoolTask> task); |
| 169 |
| 154 typedef std::vector<Worker*> WorkerVector; | 170 typedef std::vector<Worker*> WorkerVector; |
| 155 WorkerVector workers_; | 171 WorkerVector workers_; |
| 156 WorkerPoolClient* client_; | 172 WorkerPoolClient* client_; |
| 157 scoped_refptr<base::MessageLoopProxy> origin_loop_; | 173 scoped_refptr<base::MessageLoopProxy> origin_loop_; |
| 158 base::WeakPtrFactory<WorkerPool> weak_ptr_factory_; | 174 base::WeakPtrFactory<WorkerPool> weak_ptr_factory_; |
| 159 bool workers_need_sorting_; | 175 bool workers_need_sorting_; |
| 160 bool shutdown_; | 176 bool shutdown_; |
| 161 base::CancelableClosure check_for_completed_tasks_callback_; | 177 base::CancelableClosure check_for_completed_tasks_callback_; |
| 162 bool check_for_completed_tasks_pending_; | 178 base::TimeTicks check_for_completed_tasks_deadline_; |
| 163 base::Closure idle_callback_; | 179 base::Closure idle_callback_; |
| 180 base::Closure cheap_task_callback_; |
| 164 // Accessed from multiple threads. 0 when worker pool is idle. | 181 // Accessed from multiple threads. 0 when worker pool is idle. |
| 165 base::subtle::Atomic32 pending_task_count_; | 182 base::subtle::Atomic32 pending_task_count_; |
| 166 | 183 |
| 184 bool run_cheap_tasks_pending_; |
| 185 ScopedPtrDeque<internal::WorkerPoolTask> pending_cheap_tasks_; |
| 186 ScopedPtrDeque<internal::WorkerPoolTask> completed_cheap_tasks_; |
| 187 scoped_ptr<RenderingStats> cheap_rendering_stats_; |
| 188 |
| 167 DISALLOW_COPY_AND_ASSIGN(WorkerPool); | 189 DISALLOW_COPY_AND_ASSIGN(WorkerPool); |
| 168 }; | 190 }; |
| 169 | 191 |
| 170 } // namespace cc | 192 } // namespace cc |
| 171 | 193 |
| 172 #endif // CC_WORKER_POOL_H_ | 194 #endif // CC_WORKER_POOL_H_ |
| OLD | NEW |