| 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/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 13 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 14 #include "cc/rendering_stats.h" | 15 #include "cc/rendering_stats.h" |
| 15 #include "cc/scoped_ptr_deque.h" | 16 #include "cc/scoped_ptr_deque.h" |
| 16 | 17 |
| 17 namespace cc { | 18 namespace cc { |
| 18 namespace internal { | 19 namespace internal { |
| 19 | 20 |
| 20 class WorkerPoolTask { | 21 class WorkerPoolTask { |
| 21 public: | 22 public: |
| 22 virtual ~WorkerPoolTask(); | 23 virtual ~WorkerPoolTask(); |
| 23 | 24 |
| 24 virtual void Run() = 0; | 25 virtual void Run(RenderingStats* rendering_stats) = 0; |
| 25 | 26 |
| 26 void Completed(); | 27 void Completed(); |
| 27 | 28 |
| 28 RenderingStats& rendering_stats() { return rendering_stats_; } | |
| 29 | |
| 30 protected: | 29 protected: |
| 31 WorkerPoolTask(const base::Closure& reply); | 30 WorkerPoolTask(const base::Closure& reply); |
| 32 | 31 |
| 33 base::Closure reply_; | 32 base::Closure reply_; |
| 34 RenderingStats rendering_stats_; | |
| 35 }; | 33 }; |
| 36 | 34 |
| 37 } // namespace internal | 35 } // namespace internal |
| 38 | 36 |
| 39 // A worker thread pool that runs rendering tasks and guarantees completion | 37 // A worker thread pool that runs rendering tasks and guarantees completion |
| 40 // of all pending tasks at shutdown. | 38 // of all pending tasks at shutdown. |
| 41 class WorkerPool { | 39 class WorkerPool { |
| 42 public: | 40 public: |
| 43 typedef base::Callback<void(RenderingStats*)> Callback; | 41 typedef base::Callback<void(RenderingStats*)> Callback; |
| 44 | 42 |
| 45 virtual ~WorkerPool(); | 43 virtual ~WorkerPool(); |
| 46 | 44 |
| 47 static scoped_ptr<WorkerPool> Create(size_t num_threads) { | 45 static scoped_ptr<WorkerPool> Create( |
| 48 return make_scoped_ptr(new WorkerPool(num_threads)); | 46 size_t num_threads, bool record_rendering_stats) { |
| 47 return make_scoped_ptr( |
| 48 new WorkerPool(num_threads, record_rendering_stats)); |
| 49 } | 49 } |
| 50 | 50 |
| 51 // Tells the worker pool to shutdown and returns once all pending tasks have | 51 // Tells the worker pool to shutdown and returns once all pending tasks have |
| 52 // completed. | 52 // completed. |
| 53 void Shutdown(); | 53 void Shutdown(); |
| 54 | 54 |
| 55 // Posts |task| to worker pool. On completion, |reply| | 55 // Posts |task| to worker pool. On completion, |reply| |
| 56 // is posted to the thread that called PostTaskAndReply(). | 56 // is posted to the thread that called PostTaskAndReply(). |
| 57 void PostTaskAndReply(const Callback& task, const base::Closure& reply); | 57 void PostTaskAndReply(const Callback& task, const base::Closure& reply); |
| 58 | 58 |
| 59 // Returns true when worker pool has reached its internal limit for number | 59 // Returns true when worker pool has reached its internal limit for number |
| 60 // of pending tasks. | 60 // of pending tasks. |
| 61 bool IsBusy(); | 61 bool IsBusy(); |
| 62 | 62 |
| 63 // Collect rendering stats all completed tasks. | 63 // Collect rendering stats all completed tasks. |
| 64 void GetRenderingStats(RenderingStats* stats); | 64 void GetRenderingStats(RenderingStats* stats); |
| 65 | 65 |
| 66 protected: | 66 protected: |
| 67 class Worker : public base::Thread { | 67 class Worker : public base::Thread { |
| 68 public: | 68 public: |
| 69 Worker(WorkerPool* worker_pool, const std::string name); | 69 Worker( |
| 70 WorkerPool* worker_pool, |
| 71 const std::string name, |
| 72 scoped_ptr<RenderingStats> rendering_stats); |
| 70 virtual ~Worker(); | 73 virtual ~Worker(); |
| 71 | 74 |
| 72 // This must be called before the destructor. | 75 // This must be called before the destructor. |
| 73 void StopAfterCompletingAllPendingTasks(); | 76 void StopAfterCompletingAllPendingTasks(); |
| 74 | 77 |
| 75 // Posts a task to the worker thread. | 78 // Posts a task to the worker thread. |
| 76 void PostTask(scoped_ptr<internal::WorkerPoolTask> task); | 79 void PostTask(scoped_ptr<internal::WorkerPoolTask> task); |
| 77 | 80 |
| 78 int num_pending_tasks() const { return pending_tasks_.size(); } | 81 int num_pending_tasks() const { return pending_tasks_.size(); } |
| 79 const RenderingStats& rendering_stats() const { return rendering_stats_; } | 82 const RenderingStats* rendering_stats() const { |
| 83 return rendering_stats_.get(); |
| 84 } |
| 80 | 85 |
| 81 // Overridden from base::Thread: | 86 // Overridden from base::Thread: |
| 82 virtual void Init() OVERRIDE; | 87 virtual void Init() OVERRIDE; |
| 83 | 88 |
| 84 private: | 89 private: |
| 85 static void RunTask(internal::WorkerPoolTask* task); | 90 static void RunTask( |
| 91 internal::WorkerPoolTask* task, RenderingStats* rendering_stats); |
| 86 | 92 |
| 87 void OnTaskCompleted(); | 93 void OnTaskCompleted(); |
| 88 | 94 |
| 89 WorkerPool* worker_pool_; | 95 WorkerPool* worker_pool_; |
| 90 base::WeakPtrFactory<Worker> weak_ptr_factory_; | 96 base::WeakPtrFactory<Worker> weak_ptr_factory_; |
| 91 ScopedPtrDeque<internal::WorkerPoolTask> pending_tasks_; | 97 ScopedPtrDeque<internal::WorkerPoolTask> pending_tasks_; |
| 92 RenderingStats rendering_stats_; | 98 scoped_ptr<RenderingStats> rendering_stats_; |
| 93 }; | 99 }; |
| 94 | 100 |
| 95 explicit WorkerPool(size_t num_threads); | 101 WorkerPool(size_t num_threads, bool record_rendering_stats); |
| 96 | 102 |
| 97 WorkerPool::Worker* GetWorkerForNextTask(); | 103 WorkerPool::Worker* GetWorkerForNextTask(); |
| 98 | 104 |
| 99 private: | 105 private: |
| 100 class NumPendingTasksComparator { | 106 class NumPendingTasksComparator { |
| 101 public: | 107 public: |
| 102 bool operator() (const Worker* a, const Worker* b) const { | 108 bool operator() (const Worker* a, const Worker* b) const { |
| 103 return a->num_pending_tasks() < b->num_pending_tasks(); | 109 return a->num_pending_tasks() < b->num_pending_tasks(); |
| 104 } | 110 } |
| 105 }; | 111 }; |
| 106 | 112 |
| 107 void DidNumPendingTasksChange(); | 113 void DidNumPendingTasksChange(); |
| 108 void SortWorkersIfNeeded(); | 114 void SortWorkersIfNeeded(); |
| 109 | 115 |
| 110 typedef std::vector<Worker*> WorkerVector; | 116 typedef std::vector<Worker*> WorkerVector; |
| 111 WorkerVector workers_; | 117 WorkerVector workers_; |
| 112 bool workers_need_sorting_; | 118 bool workers_need_sorting_; |
| 113 bool shutdown_; | 119 bool shutdown_; |
| 114 | 120 |
| 115 DISALLOW_COPY_AND_ASSIGN(WorkerPool); | 121 DISALLOW_COPY_AND_ASSIGN(WorkerPool); |
| 116 }; | 122 }; |
| 117 | 123 |
| 118 } // namespace cc | 124 } // namespace cc |
| 119 | 125 |
| 120 #endif // CC_WORKER_POOL_H_ | 126 #endif // CC_WORKER_POOL_H_ |
| OLD | NEW |