| 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_BASE_WORKER_POOL_H_ | 5 #ifndef CC_BASE_WORKER_POOL_H_ |
| 6 #define CC_BASE_WORKER_POOL_H_ | 6 #define CC_BASE_WORKER_POOL_H_ |
| 7 | 7 |
| 8 #include <deque> |
| 8 #include <string> | 9 #include <string> |
| 10 #include <vector> |
| 9 | 11 |
| 10 #include "base/cancelable_callback.h" | 12 #include "base/cancelable_callback.h" |
| 13 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 13 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
| 14 #include "cc/base/cc_export.h" | 17 #include "cc/base/cc_export.h" |
| 15 #include "cc/base/scoped_ptr_deque.h" | |
| 16 | 18 |
| 17 namespace cc { | 19 namespace cc { |
| 18 | 20 |
| 19 namespace internal { | 21 namespace internal { |
| 20 | 22 |
| 21 class WorkerPoolTask { | 23 class CC_EXPORT WorkerPoolTask |
| 24 : public base::RefCountedThreadSafe<WorkerPoolTask> { |
| 22 public: | 25 public: |
| 26 typedef std::vector<scoped_refptr<WorkerPoolTask> > TaskVector; |
| 27 |
| 28 virtual void RunOnThread(unsigned thread_index) = 0; |
| 29 virtual void DispatchCompletionCallback() = 0; |
| 30 |
| 31 void DidSchedule(); |
| 32 void WillRun(); |
| 33 void DidRun(); |
| 34 void DidComplete(); |
| 35 |
| 36 bool IsReadyToRun() const; |
| 37 bool HasFinishedRunning() const; |
| 38 bool HasCompleted() const; |
| 39 |
| 40 TaskVector& dependencies() { return dependencies_; } |
| 41 |
| 42 protected: |
| 43 friend class base::RefCountedThreadSafe<WorkerPoolTask>; |
| 44 |
| 45 WorkerPoolTask(); |
| 46 explicit WorkerPoolTask(TaskVector* dependencies); |
| 23 virtual ~WorkerPoolTask(); | 47 virtual ~WorkerPoolTask(); |
| 24 | 48 |
| 25 virtual void RunOnThread(unsigned thread_index) = 0; | 49 private: |
| 26 | 50 bool did_schedule_; |
| 27 void DidComplete(); | 51 bool did_run_; |
| 28 | 52 bool did_complete_; |
| 29 protected: | 53 TaskVector dependencies_; |
| 30 explicit WorkerPoolTask(const base::Closure& reply); | |
| 31 | |
| 32 const base::Closure reply_; | |
| 33 }; | 54 }; |
| 34 | 55 |
| 35 } // namespace internal | 56 } // namespace internal |
| 36 | 57 |
| 37 class CC_EXPORT WorkerPoolClient { | 58 class CC_EXPORT WorkerPoolClient { |
| 38 public: | 59 public: |
| 39 virtual void DidFinishDispatchingWorkerPoolCompletionCallbacks() = 0; | 60 virtual void DidFinishDispatchingWorkerPoolCompletionCallbacks() = 0; |
| 40 | 61 |
| 41 protected: | 62 protected: |
| 42 virtual ~WorkerPoolClient() {} | 63 virtual ~WorkerPoolClient() {} |
| 43 }; | 64 }; |
| 44 | 65 |
| 45 // A worker thread pool that runs rendering tasks and guarantees completion | 66 // A worker thread pool that runs tasks provided by task graph and |
| 46 // of all pending tasks at shutdown. | 67 // guarantees completion of all pending tasks at shutdown. |
| 47 class CC_EXPORT WorkerPool { | 68 class CC_EXPORT WorkerPool { |
| 48 public: | 69 public: |
| 49 typedef base::Callback<void()> Callback; | |
| 50 | |
| 51 virtual ~WorkerPool(); | 70 virtual ~WorkerPool(); |
| 52 | 71 |
| 53 static scoped_ptr<WorkerPool> Create( | |
| 54 size_t num_threads, | |
| 55 base::TimeDelta check_for_completed_tasks_delay, | |
| 56 const std::string& thread_name_prefix) { | |
| 57 return make_scoped_ptr(new WorkerPool(num_threads, | |
| 58 check_for_completed_tasks_delay, | |
| 59 thread_name_prefix)); | |
| 60 } | |
| 61 | |
| 62 // Tells the worker pool to shutdown and returns once all pending tasks have | 72 // Tells the worker pool to shutdown and returns once all pending tasks have |
| 63 // completed. | 73 // completed. |
| 64 void Shutdown(); | 74 virtual void Shutdown(); |
| 65 | |
| 66 // Posts |task| to worker pool. On completion, |reply| | |
| 67 // is posted to the thread that called PostTaskAndReply(). | |
| 68 void PostTaskAndReply(const Callback& task, const base::Closure& reply); | |
| 69 | 75 |
| 70 // Set a new client. | 76 // Set a new client. |
| 71 void SetClient(WorkerPoolClient* client) { | 77 void SetClient(WorkerPoolClient* client) { |
| 72 client_ = client; | 78 client_ = client; |
| 73 } | 79 } |
| 74 | 80 |
| 81 // Force a check for completed tasks. |
| 82 void CheckForCompletedTasks(); |
| 83 |
| 75 protected: | 84 protected: |
| 76 WorkerPool(size_t num_threads, | 85 WorkerPool(size_t num_threads, |
| 77 base::TimeDelta check_for_completed_tasks_delay, | 86 base::TimeDelta check_for_completed_tasks_delay, |
| 78 const std::string& thread_name_prefix); | 87 const std::string& thread_name_prefix); |
| 79 | 88 |
| 80 void PostTask(scoped_ptr<internal::WorkerPoolTask> task); | 89 void ScheduleTasks(internal::WorkerPoolTask* root); |
| 81 | 90 |
| 82 private: | 91 private: |
| 83 class Inner; | 92 class Inner; |
| 84 friend class Inner; | 93 friend class Inner; |
| 85 | 94 |
| 86 void OnTaskCompleted(); | 95 typedef std::deque<scoped_refptr<internal::WorkerPoolTask> > TaskDeque; |
| 87 void OnIdle(); | 96 |
| 97 void OnIdle(TaskDeque* completed_tasks); |
| 88 void ScheduleCheckForCompletedTasks(); | 98 void ScheduleCheckForCompletedTasks(); |
| 89 void CheckForCompletedTasks(); | 99 void DispatchCompletionCallbacks(TaskDeque* completed_tasks); |
| 90 void DispatchCompletionCallbacks(); | |
| 91 | 100 |
| 92 WorkerPoolClient* client_; | 101 WorkerPoolClient* client_; |
| 93 scoped_refptr<base::MessageLoopProxy> origin_loop_; | 102 scoped_refptr<base::MessageLoopProxy> origin_loop_; |
| 94 base::WeakPtrFactory<WorkerPool> weak_ptr_factory_; | 103 base::CancelableClosure check_for_completed_tasks_callback_; |
| 95 base::TimeDelta check_for_completed_tasks_delay_; | 104 base::TimeDelta check_for_completed_tasks_delay_; |
| 96 bool check_for_completed_tasks_pending_; | 105 bool check_for_completed_tasks_pending_; |
| 97 | 106 |
| 98 // Holds all completed tasks for which we have not yet dispatched | |
| 99 // reply callbacks. | |
| 100 ScopedPtrDeque<internal::WorkerPoolTask> completed_tasks_; | |
| 101 | |
| 102 // Hide the gory details of the worker pool in |inner_|. | 107 // Hide the gory details of the worker pool in |inner_|. |
| 103 const scoped_ptr<Inner> inner_; | 108 const scoped_ptr<Inner> inner_; |
| 104 | |
| 105 DISALLOW_COPY_AND_ASSIGN(WorkerPool); | |
| 106 }; | 109 }; |
| 107 | 110 |
| 108 } // namespace cc | 111 } // namespace cc |
| 109 | 112 |
| 110 #endif // CC_BASE_WORKER_POOL_H_ | 113 #endif // CC_BASE_WORKER_POOL_H_ |
| OLD | NEW |