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" | |
14 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
15 #include "cc/rendering_stats.h" | 14 #include "cc/rendering_stats.h" |
16 #include "cc/scoped_ptr_deque.h" | 15 #include "cc/scoped_ptr_deque.h" |
17 | 16 |
18 namespace cc { | 17 namespace cc { |
19 namespace internal { | 18 namespace internal { |
20 | 19 |
21 class WorkerPoolTask { | 20 class WorkerPoolTask { |
22 public: | 21 public: |
23 virtual ~WorkerPoolTask(); | 22 virtual ~WorkerPoolTask(); |
24 | 23 |
25 virtual void Run(RenderingStats* rendering_stats) = 0; | 24 virtual void Run(RenderingStats* rendering_stats) = 0; |
26 | 25 |
| 26 bool IsPending(); |
27 void Completed(); | 27 void Completed(); |
28 | 28 |
29 protected: | 29 protected: |
30 WorkerPoolTask(const base::Closure& reply); | 30 WorkerPoolTask(const base::Closure& reply); |
31 | 31 |
32 base::Closure reply_; | 32 base::Closure reply_; |
| 33 |
| 34 // Accessed from multiple threads. Set to 1 when task has completed. |
| 35 base::subtle::Atomic32 completed_; |
33 }; | 36 }; |
34 | 37 |
35 } // namespace internal | 38 } // namespace internal |
36 | 39 |
37 // A worker thread pool that runs rendering tasks and guarantees completion | 40 // A worker thread pool that runs rendering tasks and guarantees completion |
38 // of all pending tasks at shutdown. | 41 // of all pending tasks at shutdown. |
39 class WorkerPool { | 42 class WorkerPool { |
40 public: | 43 public: |
41 typedef base::Callback<void(RenderingStats*)> Callback; | 44 typedef base::Callback<void(RenderingStats*)> Callback; |
42 | 45 |
43 virtual ~WorkerPool(); | 46 virtual ~WorkerPool(); |
44 | 47 |
45 static scoped_ptr<WorkerPool> Create(size_t num_threads) { | 48 static scoped_ptr<WorkerPool> Create(size_t num_threads) { |
46 return make_scoped_ptr(new WorkerPool(num_threads)); | 49 return make_scoped_ptr(new WorkerPool(num_threads)); |
47 } | 50 } |
48 | 51 |
49 // Tells the worker pool to shutdown and returns once all pending tasks have | 52 // Tells the worker pool to shutdown and returns once all pending tasks have |
50 // completed. | 53 // completed. |
51 void Shutdown(); | 54 void Shutdown(); |
52 | 55 |
53 // Posts |task| to worker pool. On completion, |reply| | 56 // Posts |task| to worker pool. On completion, |reply| |
54 // is posted to the thread that called PostTaskAndReply(). | 57 // is posted to the thread that called PostTaskAndReply(). |
55 void PostTaskAndReply(const Callback& task, const base::Closure& reply); | 58 void PostTaskAndReply(const Callback& task, const base::Closure& reply); |
56 | 59 |
57 // Returns true when worker pool has reached its internal limit for number | 60 // Check for completed tasks and run reply callbacks. |
58 // of pending tasks. | 61 void CheckForCompletedTasks(); |
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, RenderingStats* rendering_stats); |
93 | 98 |
94 void OnTaskCompleted(); | 99 void OnTaskCompleted(); |
95 | 100 |
96 WorkerPool* worker_pool_; | 101 WorkerPool* worker_pool_; |
97 base::WeakPtrFactory<Worker> weak_ptr_factory_; | |
98 ScopedPtrDeque<internal::WorkerPoolTask> pending_tasks_; | 102 ScopedPtrDeque<internal::WorkerPoolTask> pending_tasks_; |
99 scoped_ptr<RenderingStats> rendering_stats_; | 103 scoped_ptr<RenderingStats> rendering_stats_; |
100 bool record_rendering_stats_; | 104 bool record_rendering_stats_; |
101 }; | 105 }; |
102 | 106 |
103 explicit WorkerPool(size_t num_threads); | 107 explicit WorkerPool(size_t num_threads); |
104 | 108 |
105 WorkerPool::Worker* GetWorkerForNextTask(); | 109 WorkerPool::Worker* GetWorkerForNextTask(); |
106 | 110 |
107 private: | 111 private: |
(...skipping 11 matching lines...) Expand all Loading... |
119 WorkerVector workers_; | 123 WorkerVector workers_; |
120 bool workers_need_sorting_; | 124 bool workers_need_sorting_; |
121 bool shutdown_; | 125 bool shutdown_; |
122 | 126 |
123 DISALLOW_COPY_AND_ASSIGN(WorkerPool); | 127 DISALLOW_COPY_AND_ASSIGN(WorkerPool); |
124 }; | 128 }; |
125 | 129 |
126 } // namespace cc | 130 } // namespace cc |
127 | 131 |
128 #endif // CC_WORKER_POOL_H_ | 132 #endif // CC_WORKER_POOL_H_ |
OLD | NEW |