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