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