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