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