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/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
15 #include "cc/rendering_stats.h" | 16 #include "cc/rendering_stats.h" |
16 #include "cc/scoped_ptr_deque.h" | 17 #include "cc/scoped_ptr_deque.h" |
17 | 18 |
18 namespace cc { | 19 namespace cc { |
19 namespace internal { | 20 namespace internal { |
20 | 21 |
21 class WorkerPoolTask { | 22 class WorkerPoolTask { |
22 public: | 23 public: |
23 virtual ~WorkerPoolTask(); | 24 virtual ~WorkerPoolTask(); |
24 | 25 |
25 virtual void Run(RenderingStats* rendering_stats) = 0; | 26 virtual void Run(RenderingStats* rendering_stats) = 0; |
26 | 27 |
27 void Completed(); | 28 bool IsPending(); |
nduca
2013/02/13 08:29:32
Can you say HasCompleted? So we dont mix completed
reveman
2013/02/13 08:46:38
Done.
| |
29 void Completed(bool more_tasks_completed); | |
28 | 30 |
29 protected: | 31 protected: |
30 WorkerPoolTask(const base::Closure& reply); | 32 WorkerPoolTask(const base::Callback<void(bool)>& reply); |
31 | 33 |
32 base::Closure reply_; | 34 base::Callback<void(bool)> reply_; |
35 | |
36 // Accessed from multiple threads. Set to 1 when task has completed. | |
37 base::subtle::Atomic32 completed_; | |
33 }; | 38 }; |
34 | 39 |
35 } // namespace internal | 40 } // namespace internal |
36 | 41 |
37 // A worker thread pool that runs rendering tasks and guarantees completion | 42 // A worker thread pool that runs rendering tasks and guarantees completion |
38 // of all pending tasks at shutdown. | 43 // of all pending tasks at shutdown. |
39 class WorkerPool { | 44 class WorkerPool { |
40 public: | 45 public: |
41 typedef base::Callback<void(RenderingStats*)> Callback; | 46 typedef base::Callback<void(RenderingStats*)> Callback; |
47 typedef base::Callback<void(bool)> Reply; | |
42 | 48 |
43 virtual ~WorkerPool(); | 49 virtual ~WorkerPool(); |
44 | 50 |
45 static scoped_ptr<WorkerPool> Create(size_t num_threads) { | 51 static scoped_ptr<WorkerPool> Create(size_t num_threads) { |
46 return make_scoped_ptr(new WorkerPool(num_threads)); | 52 return make_scoped_ptr(new WorkerPool(num_threads)); |
47 } | 53 } |
48 | 54 |
49 // Tells the worker pool to shutdown and returns once all pending tasks have | 55 // Tells the worker pool to shutdown and returns once all pending tasks have |
50 // completed. | 56 // completed. |
51 void Shutdown(); | 57 void Shutdown(); |
52 | 58 |
53 // Posts |task| to worker pool. On completion, |reply| | 59 // Posts |task| to worker pool. On completion, |reply| |
54 // is posted to the thread that called PostTaskAndReply(). | 60 // is posted to the thread that called PostTaskAndReply(). |
55 void PostTaskAndReply(const Callback& task, const base::Closure& reply); | 61 void PostTaskAndReply(const Callback& task, const Reply& reply); |
56 | |
57 // Returns true when worker pool has reached its internal limit for number | |
58 // of pending tasks. | |
nduca
2013/02/13 08:29:32
I wonder, can you shift the raster throttle stuff
reveman
2013/02/13 08:46:38
my current throttle values are derived from the kM
| |
59 bool IsBusy(); | |
60 | 62 |
61 // Toggle rendering stats collection. | 63 // Toggle rendering stats collection. |
62 void SetRecordRenderingStats(bool record_rendering_stats); | 64 void SetRecordRenderingStats(bool record_rendering_stats); |
63 | 65 |
64 // Collect rendering stats all completed tasks. | 66 // Collect rendering stats all completed tasks. |
65 void GetRenderingStats(RenderingStats* stats); | 67 void GetRenderingStats(RenderingStats* stats); |
66 | 68 |
67 protected: | 69 protected: |
68 class Worker : public base::Thread { | 70 class Worker : public base::Thread { |
69 public: | 71 public: |
70 Worker(WorkerPool* worker_pool, const std::string name); | 72 Worker(WorkerPool* worker_pool, const std::string name); |
71 virtual ~Worker(); | 73 virtual ~Worker(); |
72 | 74 |
73 // This must be called before the destructor. | 75 // This must be called before the destructor. |
74 void StopAfterCompletingAllPendingTasks(); | 76 void StopAfterCompletingAllPendingTasks(); |
75 | 77 |
76 // Posts a task to the worker thread. | 78 // Posts a task to the worker thread. |
77 void PostTask(scoped_ptr<internal::WorkerPoolTask> task); | 79 void PostTask(scoped_ptr<internal::WorkerPoolTask> task); |
78 | 80 |
81 // Check for completed tasks and run reply callbacks. | |
82 void CheckForCompletedTasks(); | |
83 | |
79 int num_pending_tasks() const { return pending_tasks_.size(); } | 84 int num_pending_tasks() const { return pending_tasks_.size(); } |
80 void set_record_rendering_stats(bool record_rendering_stats) { | 85 void set_record_rendering_stats(bool record_rendering_stats) { |
81 record_rendering_stats_ = record_rendering_stats; | 86 record_rendering_stats_ = record_rendering_stats; |
82 } | 87 } |
83 const RenderingStats* rendering_stats() const { | 88 const RenderingStats* rendering_stats() const { |
84 return rendering_stats_.get(); | 89 return rendering_stats_.get(); |
85 } | 90 } |
86 | 91 |
87 // Overridden from base::Thread: | 92 // Overridden from base::Thread: |
88 virtual void Init() OVERRIDE; | 93 virtual void Init() OVERRIDE; |
89 | 94 |
90 private: | 95 private: |
91 static void RunTask( | 96 static void RunTask( |
92 internal::WorkerPoolTask* task, RenderingStats* rendering_stats); | 97 internal::WorkerPoolTask* task, |
98 WorkerPool* worker_pool, | |
99 RenderingStats* rendering_stats); | |
93 | 100 |
94 void OnTaskCompleted(); | 101 void OnTaskCompleted(); |
95 | 102 |
96 WorkerPool* worker_pool_; | 103 WorkerPool* worker_pool_; |
97 base::WeakPtrFactory<Worker> weak_ptr_factory_; | |
98 ScopedPtrDeque<internal::WorkerPoolTask> pending_tasks_; | 104 ScopedPtrDeque<internal::WorkerPoolTask> pending_tasks_; |
99 scoped_ptr<RenderingStats> rendering_stats_; | 105 scoped_ptr<RenderingStats> rendering_stats_; |
100 bool record_rendering_stats_; | 106 bool record_rendering_stats_; |
101 }; | 107 }; |
102 | 108 |
103 explicit WorkerPool(size_t num_threads); | 109 explicit WorkerPool(size_t num_threads); |
104 | 110 |
105 WorkerPool::Worker* GetWorkerForNextTask(); | 111 WorkerPool::Worker* GetWorkerForNextTask(); |
106 | 112 |
107 private: | 113 private: |
108 class NumPendingTasksComparator { | 114 class NumPendingTasksComparator { |
109 public: | 115 public: |
110 bool operator() (const Worker* a, const Worker* b) const { | 116 bool operator() (const Worker* a, const Worker* b) const { |
111 return a->num_pending_tasks() < b->num_pending_tasks(); | 117 return a->num_pending_tasks() < b->num_pending_tasks(); |
112 } | 118 } |
113 }; | 119 }; |
114 | 120 |
115 void DidNumPendingTasksChange(); | 121 // Schedule a completed tasks check if not already pending. |
122 void ScheduleCheckForCompletedTasks(); | |
123 | |
124 // Called on origin thread before posting task to worker. | |
125 void WillPostWorkTask(); | |
126 | |
127 // Called on worker thread after completing work. | |
128 void OnWorkCompletedOnWorkerThread(); | |
129 | |
130 // Called on origin thread after becoming idle. | |
131 void OnIdle(); | |
132 | |
133 // Check for completed tasks and run reply callbacks. | |
134 void CheckForCompletedTasks(); | |
135 | |
136 // Called when processing task completion. | |
137 void OnTaskCompleted(); | |
138 | |
139 // Returns true when work has completed in worker thread but | |
140 // task completion has not yet been processed. | |
141 bool MoreTasksCompleted(); | |
142 | |
143 // Ensure workers are sorted by number of pending tasks. | |
116 void SortWorkersIfNeeded(); | 144 void SortWorkersIfNeeded(); |
117 | 145 |
118 typedef std::vector<Worker*> WorkerVector; | 146 typedef std::vector<Worker*> WorkerVector; |
119 WorkerVector workers_; | 147 WorkerVector workers_; |
148 scoped_refptr<base::MessageLoopProxy> origin_loop_; | |
149 base::WeakPtrFactory<WorkerPool> weak_ptr_factory_; | |
120 bool workers_need_sorting_; | 150 bool workers_need_sorting_; |
151 int task_count_; | |
121 bool shutdown_; | 152 bool shutdown_; |
153 base::CancelableClosure check_for_completed_tasks_callback_; | |
154 bool check_for_completed_tasks_pending_; | |
155 base::Closure idle_callback_; | |
156 // Accessed from multiple threads. 0 when worker pool is idle. | |
157 base::subtle::Atomic32 work_count_; | |
nduca
2013/02/13 08:29:32
better name
| |
122 | 158 |
123 DISALLOW_COPY_AND_ASSIGN(WorkerPool); | 159 DISALLOW_COPY_AND_ASSIGN(WorkerPool); |
124 }; | 160 }; |
125 | 161 |
126 } // namespace cc | 162 } // namespace cc |
127 | 163 |
128 #endif // CC_WORKER_POOL_H_ | 164 #endif // CC_WORKER_POOL_H_ |
OLD | NEW |