| 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_RESOURCES_WORKER_POOL_H_ | 5 #ifndef CC_RESOURCES_WORKER_POOL_H_ |
| 6 #define CC_RESOURCES_WORKER_POOL_H_ | 6 #define CC_RESOURCES_WORKER_POOL_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 WorkerPoolTask(); | 41 WorkerPoolTask(); |
| 42 virtual ~WorkerPoolTask(); | 42 virtual ~WorkerPoolTask(); |
| 43 | 43 |
| 44 private: | 44 private: |
| 45 bool did_schedule_; | 45 bool did_schedule_; |
| 46 bool did_run_; | 46 bool did_run_; |
| 47 bool did_complete_; | 47 bool did_complete_; |
| 48 }; | 48 }; |
| 49 | 49 |
| 50 class CC_EXPORT GraphNode { |
| 51 public: |
| 52 typedef std::vector<GraphNode*> Vector; |
| 53 |
| 54 GraphNode(internal::WorkerPoolTask* task, unsigned priority); |
| 55 ~GraphNode(); |
| 56 |
| 57 WorkerPoolTask* task() { return task_; } |
| 58 |
| 59 void add_dependent(GraphNode* dependent) { |
| 60 DCHECK(dependent); |
| 61 dependents_.push_back(dependent); |
| 62 } |
| 63 const Vector& dependents() const { return dependents_; } |
| 64 |
| 65 unsigned priority() const { return priority_; } |
| 66 |
| 67 unsigned num_dependencies() const { return num_dependencies_; } |
| 68 void add_dependency() { ++num_dependencies_; } |
| 69 void remove_dependency() { |
| 70 DCHECK(num_dependencies_); |
| 71 --num_dependencies_; |
| 72 } |
| 73 |
| 74 private: |
| 75 WorkerPoolTask* task_; |
| 76 Vector dependents_; |
| 77 unsigned priority_; |
| 78 unsigned num_dependencies_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(GraphNode); |
| 81 }; |
| 82 |
| 50 } // namespace internal | 83 } // namespace internal |
| 51 } // namespace cc | 84 } // namespace cc |
| 52 | 85 |
| 53 #if defined(COMPILER_GCC) | 86 #if defined(COMPILER_GCC) |
| 54 namespace BASE_HASH_NAMESPACE { | 87 namespace BASE_HASH_NAMESPACE { |
| 55 template <> struct hash<cc::internal::WorkerPoolTask*> { | 88 template <> struct hash<cc::internal::WorkerPoolTask*> { |
| 56 size_t operator()(cc::internal::WorkerPoolTask* ptr) const { | 89 size_t operator()(cc::internal::WorkerPoolTask* ptr) const { |
| 57 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); | 90 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); |
| 58 } | 91 } |
| 59 }; | 92 }; |
| 60 } // namespace BASE_HASH_NAMESPACE | 93 } // namespace BASE_HASH_NAMESPACE |
| 61 #endif // COMPILER | 94 #endif // COMPILER |
| 62 | 95 |
| 63 namespace cc { | 96 namespace cc { |
| 64 | 97 |
| 65 // A worker thread pool that runs tasks provided by task graph and | 98 // A worker thread pool that runs tasks provided by task graph and |
| 66 // guarantees completion of all pending tasks at shutdown. | 99 // guarantees completion of all pending tasks at shutdown. |
| 67 class CC_EXPORT WorkerPool { | 100 class CC_EXPORT WorkerPool { |
| 68 public: | 101 public: |
| 69 virtual ~WorkerPool(); | 102 virtual ~WorkerPool(); |
| 70 | 103 |
| 71 // Tells the worker pool to shutdown and returns once all pending tasks have | 104 // Tells the worker pool to shutdown and returns once all pending tasks have |
| 72 // completed. | 105 // completed. |
| 73 virtual void Shutdown(); | 106 virtual void Shutdown(); |
| 74 | 107 |
| 75 // Force a check for completed tasks. | 108 // Force a check for completed tasks. |
| 76 virtual void CheckForCompletedTasks(); | 109 virtual void CheckForCompletedTasks(); |
| 77 | 110 |
| 78 protected: | 111 protected: |
| 79 class CC_EXPORT GraphNode { | |
| 80 public: | |
| 81 typedef std::vector<GraphNode*> Vector; | |
| 82 | |
| 83 GraphNode(); | |
| 84 ~GraphNode(); | |
| 85 | |
| 86 void set_task(internal::WorkerPoolTask* task) { task_ = task; } | |
| 87 internal::WorkerPoolTask* task() { return task_; } | |
| 88 | |
| 89 void add_dependent(GraphNode* dependent) { | |
| 90 DCHECK(dependent); | |
| 91 dependents_.push_back(dependent); | |
| 92 } | |
| 93 const Vector& dependents() const { | |
| 94 return dependents_; | |
| 95 } | |
| 96 | |
| 97 void set_priority(unsigned priority) { priority_ = priority; } | |
| 98 unsigned priority() const { return priority_; } | |
| 99 | |
| 100 unsigned num_dependencies() const { | |
| 101 return num_dependencies_; | |
| 102 } | |
| 103 void add_dependency() { ++num_dependencies_; } | |
| 104 void remove_dependency() { | |
| 105 DCHECK(num_dependencies_); | |
| 106 --num_dependencies_; | |
| 107 } | |
| 108 | |
| 109 private: | |
| 110 internal::WorkerPoolTask* task_; | |
| 111 Vector dependents_; | |
| 112 unsigned priority_; | |
| 113 unsigned num_dependencies_; | |
| 114 | |
| 115 DISALLOW_COPY_AND_ASSIGN(GraphNode); | |
| 116 }; | |
| 117 // A task graph contains a unique set of tasks with edges between | 112 // A task graph contains a unique set of tasks with edges between |
| 118 // dependencies pointing in the direction of the dependents. Each task | 113 // dependencies pointing in the direction of the dependents. Each task |
| 119 // need to be assigned a unique priority and a run count that matches | 114 // need to be assigned a unique priority and a run count that matches |
| 120 // the number of dependencies. | 115 // the number of dependencies. |
| 121 typedef ScopedPtrHashMap<internal::WorkerPoolTask*, GraphNode> GraphNodeMap; | 116 typedef ScopedPtrHashMap<internal::WorkerPoolTask*, internal::GraphNode> |
| 117 GraphNodeMap; |
| 122 typedef GraphNodeMap TaskGraph; | 118 typedef GraphNodeMap TaskGraph; |
| 123 | 119 |
| 124 WorkerPool(size_t num_threads, const std::string& thread_name_prefix); | 120 WorkerPool(size_t num_threads, const std::string& thread_name_prefix); |
| 125 | 121 |
| 126 // Schedule running of tasks in |graph|. Any previously scheduled tasks | 122 // Schedule running of tasks in |graph|. Any previously scheduled tasks |
| 127 // that are not already running will be canceled. Canceled tasks don't run | 123 // that are not already running will be canceled. Canceled tasks don't run |
| 128 // but completion of them is still processed. | 124 // but completion of them is still processed. |
| 129 void SetTaskGraph(TaskGraph* graph); | 125 void SetTaskGraph(TaskGraph* graph); |
| 130 | 126 |
| 131 private: | 127 private: |
| 132 class Inner; | 128 class Inner; |
| 133 friend class Inner; | 129 friend class Inner; |
| 134 | 130 |
| 135 typedef std::vector<scoped_refptr<internal::WorkerPoolTask> > TaskVector; | 131 typedef std::vector<scoped_refptr<internal::WorkerPoolTask> > TaskVector; |
| 136 | 132 |
| 137 void ProcessCompletedTasks(const TaskVector& completed_tasks); | 133 void ProcessCompletedTasks(const TaskVector& completed_tasks); |
| 138 | 134 |
| 139 bool in_dispatch_completion_callbacks_; | 135 bool in_dispatch_completion_callbacks_; |
| 140 | 136 |
| 141 // Hide the gory details of the worker pool in |inner_|. | 137 // Hide the gory details of the worker pool in |inner_|. |
| 142 const scoped_ptr<Inner> inner_; | 138 const scoped_ptr<Inner> inner_; |
| 143 }; | 139 }; |
| 144 | 140 |
| 145 } // namespace cc | 141 } // namespace cc |
| 146 | 142 |
| 147 #endif // CC_RESOURCES_WORKER_POOL_H_ | 143 #endif // CC_RESOURCES_WORKER_POOL_H_ |
| OLD | NEW |