Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(384)

Side by Side Diff: cc/worker_pool.h

Issue 12217105: cc: Check for completed raster tasks at interval. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix race condition Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 DidFinishDispatchingWorkerPoolCompletionCallbacks() = 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
(...skipping 12 matching lines...) Expand all
69 public: 83 public:
70 Worker(WorkerPool* worker_pool, const std::string name); 84 Worker(WorkerPool* worker_pool, const std::string name);
71 virtual ~Worker(); 85 virtual ~Worker();
72 86
73 // This must be called before the destructor. 87 // This must be called before the destructor.
74 void StopAfterCompletingAllPendingTasks(); 88 void StopAfterCompletingAllPendingTasks();
75 89
76 // Posts a task to the worker thread. 90 // Posts a task to the worker thread.
77 void PostTask(scoped_ptr<internal::WorkerPoolTask> task); 91 void PostTask(scoped_ptr<internal::WorkerPoolTask> task);
78 92
93 // Check for completed tasks and run reply callbacks.
94 void CheckForCompletedTasks();
95
79 int num_pending_tasks() const { return pending_tasks_.size(); } 96 int num_pending_tasks() const { return pending_tasks_.size(); }
80 void set_record_rendering_stats(bool record_rendering_stats) { 97 void set_record_rendering_stats(bool record_rendering_stats) {
81 record_rendering_stats_ = record_rendering_stats; 98 record_rendering_stats_ = record_rendering_stats;
82 } 99 }
83 const RenderingStats* rendering_stats() const { 100 const RenderingStats* rendering_stats() const {
84 return rendering_stats_.get(); 101 return rendering_stats_.get();
85 } 102 }
86 103
87 // Overridden from base::Thread: 104 // Overridden from base::Thread:
88 virtual void Init() OVERRIDE; 105 virtual void Init() OVERRIDE;
89 106
90 private: 107 private:
91 static void RunTask( 108 static void RunTask(
92 internal::WorkerPoolTask* task, RenderingStats* rendering_stats); 109 internal::WorkerPoolTask* task,
110 WorkerPool* worker_pool,
111 RenderingStats* rendering_stats);
93 112
94 void OnTaskCompleted(); 113 void OnTaskCompleted();
95 114
96 WorkerPool* worker_pool_; 115 WorkerPool* worker_pool_;
97 base::WeakPtrFactory<Worker> weak_ptr_factory_;
98 ScopedPtrDeque<internal::WorkerPoolTask> pending_tasks_; 116 ScopedPtrDeque<internal::WorkerPoolTask> pending_tasks_;
99 scoped_ptr<RenderingStats> rendering_stats_; 117 scoped_ptr<RenderingStats> rendering_stats_;
100 bool record_rendering_stats_; 118 bool record_rendering_stats_;
101 }; 119 };
102 120
103 explicit WorkerPool(size_t num_threads); 121 WorkerPool(WorkerPoolClient* client, size_t num_threads);
104 122
105 WorkerPool::Worker* GetWorkerForNextTask(); 123 WorkerPool::Worker* GetWorkerForNextTask();
106 124
107 private: 125 private:
108 class NumPendingTasksComparator { 126 class NumPendingTasksComparator {
109 public: 127 public:
110 bool operator() (const Worker* a, const Worker* b) const { 128 bool operator() (const Worker* a, const Worker* b) const {
111 return a->num_pending_tasks() < b->num_pending_tasks(); 129 return a->num_pending_tasks() < b->num_pending_tasks();
112 } 130 }
113 }; 131 };
114 132
115 void DidNumPendingTasksChange(); 133 // Schedule a completed tasks check if not already pending.
134 void ScheduleCheckForCompletedTasks();
135
136 // Called on origin thread before posting task to worker.
137 void WillPostTask();
138
139 // Called on worker thread after completing work.
140 void OnWorkCompletedOnWorkerThread();
141
142 // Called on origin thread after becoming idle.
143 void OnIdle();
144
145 // Check for completed tasks and run reply callbacks.
146 void CheckForCompletedTasks();
147
148 // Called when processing task completion.
149 void OnTaskCompleted();
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_
OLDNEW
« no previous file with comments | « cc/tile_manager.cc ('k') | cc/worker_pool.cc » ('j') | cc/worker_pool.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698