| 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 <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/cancelable_callback.h" | 10 #include "base/cancelable_callback.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "cc/base/cc_export.h" | 14 #include "cc/base/cc_export.h" |
| 15 #include "cc/base/scoped_ptr_deque.h" | 15 #include "cc/base/scoped_ptr_deque.h" |
| 16 | 16 |
| 17 namespace cc { | 17 namespace cc { |
| 18 | 18 |
| 19 namespace internal { | 19 namespace internal { |
| 20 | 20 |
| 21 class WorkerPoolTask { | 21 class WorkerPoolTask { |
| 22 public: | 22 public: |
| 23 virtual ~WorkerPoolTask(); | 23 virtual ~WorkerPoolTask(); |
| 24 | 24 |
| 25 virtual bool IsCheap() = 0; | |
| 26 | |
| 27 virtual void Run() = 0; | |
| 28 | |
| 29 virtual void RunOnThread(unsigned thread_index) = 0; | 25 virtual void RunOnThread(unsigned thread_index) = 0; |
| 30 | 26 |
| 31 void DidComplete(); | 27 void DidComplete(); |
| 32 | 28 |
| 33 protected: | 29 protected: |
| 34 explicit WorkerPoolTask(const base::Closure& reply); | 30 explicit WorkerPoolTask(const base::Closure& reply); |
| 35 | 31 |
| 36 const base::Closure reply_; | 32 const base::Closure reply_; |
| 37 }; | 33 }; |
| 38 | 34 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 66 } | 62 } |
| 67 | 63 |
| 68 // Tells the worker pool to shutdown and returns once all pending tasks have | 64 // Tells the worker pool to shutdown and returns once all pending tasks have |
| 69 // completed. | 65 // completed. |
| 70 void Shutdown(); | 66 void Shutdown(); |
| 71 | 67 |
| 72 // Posts |task| to worker pool. On completion, |reply| | 68 // Posts |task| to worker pool. On completion, |reply| |
| 73 // is posted to the thread that called PostTaskAndReply(). | 69 // is posted to the thread that called PostTaskAndReply(). |
| 74 void PostTaskAndReply(const Callback& task, const base::Closure& reply); | 70 void PostTaskAndReply(const Callback& task, const base::Closure& reply); |
| 75 | 71 |
| 76 // Set time limit for running cheap tasks. | |
| 77 void SetRunCheapTasksTimeLimit(base::TimeTicks run_cheap_tasks_time_limit); | |
| 78 | |
| 79 protected: | 72 protected: |
| 80 WorkerPool(WorkerPoolClient* client, | 73 WorkerPool(WorkerPoolClient* client, |
| 81 size_t num_threads, | 74 size_t num_threads, |
| 82 base::TimeDelta check_for_completed_tasks_delay, | 75 base::TimeDelta check_for_completed_tasks_delay, |
| 83 const std::string& thread_name_prefix); | 76 const std::string& thread_name_prefix); |
| 84 | 77 |
| 85 void PostTask(scoped_ptr<internal::WorkerPoolTask> task); | 78 void PostTask(scoped_ptr<internal::WorkerPoolTask> task); |
| 86 | 79 |
| 87 private: | 80 private: |
| 88 class Inner; | 81 class Inner; |
| 89 friend class Inner; | 82 friend class Inner; |
| 90 | 83 |
| 91 void OnTaskCompleted(); | 84 void OnTaskCompleted(); |
| 92 void OnIdle(); | 85 void OnIdle(); |
| 93 void ScheduleCheckForCompletedTasks(); | 86 void ScheduleCheckForCompletedTasks(); |
| 94 void CheckForCompletedTasks(); | 87 void CheckForCompletedTasks(); |
| 95 void CancelCheckForCompletedTasks(); | |
| 96 void DispatchCompletionCallbacks(); | 88 void DispatchCompletionCallbacks(); |
| 97 void ScheduleRunCheapTasks(); | |
| 98 void RunCheapTasks(); | |
| 99 | 89 |
| 100 WorkerPoolClient* client_; | 90 WorkerPoolClient* client_; |
| 101 scoped_refptr<base::MessageLoopProxy> origin_loop_; | 91 scoped_refptr<base::MessageLoopProxy> origin_loop_; |
| 102 base::WeakPtrFactory<WorkerPool> weak_ptr_factory_; | 92 base::WeakPtrFactory<WorkerPool> weak_ptr_factory_; |
| 103 base::TimeTicks check_for_completed_tasks_time_; | |
| 104 base::TimeDelta check_for_completed_tasks_delay_; | 93 base::TimeDelta check_for_completed_tasks_delay_; |
| 105 base::CancelableClosure check_for_completed_tasks_callback_; | |
| 106 bool check_for_completed_tasks_pending_; | 94 bool check_for_completed_tasks_pending_; |
| 107 const base::Closure run_cheap_tasks_callback_; | |
| 108 base::TimeTicks run_cheap_tasks_time_limit_; | |
| 109 bool run_cheap_tasks_pending_; | |
| 110 | 95 |
| 111 // Holds all completed tasks for which we have not yet dispatched | 96 // Holds all completed tasks for which we have not yet dispatched |
| 112 // reply callbacks. | 97 // reply callbacks. |
| 113 ScopedPtrDeque<internal::WorkerPoolTask> completed_tasks_; | 98 ScopedPtrDeque<internal::WorkerPoolTask> completed_tasks_; |
| 114 | 99 |
| 115 // Hide the gory details of the worker pool in |inner_|. | 100 // Hide the gory details of the worker pool in |inner_|. |
| 116 const scoped_ptr<Inner> inner_; | 101 const scoped_ptr<Inner> inner_; |
| 117 | 102 |
| 118 DISALLOW_COPY_AND_ASSIGN(WorkerPool); | 103 DISALLOW_COPY_AND_ASSIGN(WorkerPool); |
| 119 }; | 104 }; |
| 120 | 105 |
| 121 } // namespace cc | 106 } // namespace cc |
| 122 | 107 |
| 123 #endif // CC_BASE_WORKER_POOL_H_ | 108 #endif // CC_BASE_WORKER_POOL_H_ |
| OLD | NEW |