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