OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ |
6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ | 6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 27 matching lines...) Expand all Loading... | |
38 class DelayedTaskManager; | 38 class DelayedTaskManager; |
39 class TaskTracker; | 39 class TaskTracker; |
40 | 40 |
41 // A pool of workers that run Tasks. This class is thread-safe. | 41 // A pool of workers that run Tasks. This class is thread-safe. |
42 class BASE_EXPORT SchedulerWorkerPoolImpl : public SchedulerWorkerPool { | 42 class BASE_EXPORT SchedulerWorkerPoolImpl : public SchedulerWorkerPool { |
43 public: | 43 public: |
44 // Callback invoked when a Sequence isn't empty after a worker pops a Task | 44 // Callback invoked when a Sequence isn't empty after a worker pops a Task |
45 // from it. | 45 // from it. |
46 using ReEnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; | 46 using ReEnqueueSequenceCallback = Callback<void(scoped_refptr<Sequence>)>; |
47 | 47 |
48 // Constructs a pool without workers. Tasks can be posted to the pool, but | |
49 // they won't run until workers are created. To create workers and start | |
50 // running tasks, call Start(). | |
51 // | |
52 // |name| is used to label the pool's threads ("TaskScheduler" + |name| + | |
53 // index) and histograms ("TaskScheduler." + histogram name + "." + |name| + | |
54 // extra suffixes). |priority_hint| is the preferred thread priority; the | |
55 // actual thread priority depends on shutdown state and platform capabilities. | |
56 // |re_enqueue_sequence_callback| will be invoked after a worker of this | |
robliao
2017/04/05 23:29:23
Use the above ReEnqueueSequenceCallback comment he
fdoray
2017/04/06 15:07:12
Done.
| |
57 // worker pool tries to run a Task. |task_tracker| keeps track of tasks. | |
58 // |delayed_task_manager| handles tasks posted with a delay. | |
59 SchedulerWorkerPoolImpl( | |
60 const std::string& name, | |
61 ThreadPriority priority_hint, | |
62 ReEnqueueSequenceCallback re_enqueue_sequence_callback, | |
63 TaskTracker* task_tracker, | |
64 DelayedTaskManager* delayed_task_manager); | |
65 | |
66 // Creates workers following the |params| specification, allowing existing and | |
67 // future tasks to run. Can only be called once. CHECKs on failure. | |
68 void Start(const SchedulerWorkerPoolParams& params); | |
69 | |
48 // Destroying a SchedulerWorkerPoolImpl returned by Create() is not allowed in | 70 // Destroying a SchedulerWorkerPoolImpl returned by Create() is not allowed in |
49 // production; it is always leaked. In tests, it can only be destroyed after | 71 // production; it is always leaked. In tests, it can only be destroyed after |
50 // JoinForTesting() has returned. | 72 // JoinForTesting() has returned. |
51 ~SchedulerWorkerPoolImpl() override; | 73 ~SchedulerWorkerPoolImpl() override; |
52 | 74 |
53 // Creates a SchedulerWorkerPoolImpl following the |worker_pool_params| | |
54 // specification. |re_enqueue_sequence_callback| will be invoked after a | |
55 // worker of this worker pool tries to run a Task. |task_tracker| is used to | |
56 // handle shutdown behavior of Tasks. |delayed_task_manager| handles Tasks | |
57 // posted with a delay. Returns nullptr on failure to create a worker pool | |
58 // with at least one thread. | |
59 static std::unique_ptr<SchedulerWorkerPoolImpl> Create( | |
60 const SchedulerWorkerPoolParams& params, | |
61 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | |
62 TaskTracker* task_tracker, | |
63 DelayedTaskManager* delayed_task_manager); | |
64 | |
65 // SchedulerWorkerPool: | 75 // SchedulerWorkerPool: |
66 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( | 76 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( |
67 const TaskTraits& traits) override; | 77 const TaskTraits& traits) override; |
68 scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits( | 78 scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits( |
69 const TaskTraits& traits) override; | 79 const TaskTraits& traits) override; |
70 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, | 80 void ReEnqueueSequence(scoped_refptr<Sequence> sequence, |
71 const SequenceSortKey& sequence_sort_key) override; | 81 const SequenceSortKey& sequence_sort_key) override; |
72 bool PostTaskWithSequence(std::unique_ptr<Task> task, | 82 bool PostTaskWithSequence(std::unique_ptr<Task> task, |
73 scoped_refptr<Sequence> sequence) override; | 83 scoped_refptr<Sequence> sequence) override; |
74 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, | 84 void PostTaskWithSequenceNow(std::unique_ptr<Task> task, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 // change if workers are woken up or detached during this call. | 116 // change if workers are woken up or detached during this call. |
107 size_t NumberOfAliveWorkersForTesting(); | 117 size_t NumberOfAliveWorkersForTesting(); |
108 | 118 |
109 private: | 119 private: |
110 class SchedulerWorkerDelegateImpl; | 120 class SchedulerWorkerDelegateImpl; |
111 | 121 |
112 SchedulerWorkerPoolImpl(const SchedulerWorkerPoolParams& params, | 122 SchedulerWorkerPoolImpl(const SchedulerWorkerPoolParams& params, |
113 TaskTracker* task_tracker, | 123 TaskTracker* task_tracker, |
114 DelayedTaskManager* delayed_task_manager); | 124 DelayedTaskManager* delayed_task_manager); |
115 | 125 |
116 bool Initialize( | |
117 const SchedulerWorkerPoolParams& params, | |
118 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); | |
119 | |
120 // Wakes up the last worker from this worker pool to go idle, if any. | 126 // Wakes up the last worker from this worker pool to go idle, if any. |
121 void WakeUpOneWorker(); | 127 void WakeUpOneWorker(); |
122 | 128 |
123 // Adds |worker| to |idle_workers_stack_|. | 129 // Adds |worker| to |idle_workers_stack_|. |
124 void AddToIdleWorkersStack(SchedulerWorker* worker); | 130 void AddToIdleWorkersStack(SchedulerWorker* worker); |
125 | 131 |
126 // Peeks from |idle_workers_stack_|. | 132 // Peeks from |idle_workers_stack_|. |
127 const SchedulerWorker* PeekAtIdleWorkersStack() const; | 133 const SchedulerWorker* PeekAtIdleWorkersStack() const; |
128 | 134 |
129 // Removes |worker| from |idle_workers_stack_|. | 135 // Removes |worker| from |idle_workers_stack_|. |
130 void RemoveFromIdleWorkersStack(SchedulerWorker* worker); | 136 void RemoveFromIdleWorkersStack(SchedulerWorker* worker); |
131 | 137 |
132 // Returns true if worker thread detachment is permitted. | 138 // Returns true if worker thread detachment is permitted. |
133 bool CanWorkerDetachForTesting(); | 139 bool CanWorkerDetachForTesting(); |
134 | 140 |
135 // The name of this worker pool, used to label its worker threads. | |
136 const std::string name_; | 141 const std::string name_; |
137 | 142 const ThreadPriority priority_hint_; |
138 // All worker owned by this worker pool. Only modified during initialization | 143 const ReEnqueueSequenceCallback re_enqueue_sequence_callback_; |
139 // of the worker pool. | |
140 std::vector<scoped_refptr<SchedulerWorker>> workers_; | |
141 | 144 |
142 // PriorityQueue from which all threads of this worker pool get work. | 145 // PriorityQueue from which all threads of this worker pool get work. |
143 PriorityQueue shared_priority_queue_; | 146 PriorityQueue shared_priority_queue_; |
144 | 147 |
145 // Suggested reclaim time for workers. | 148 // All worker owned by this worker pool. Never modified after Start() (i.e. |
robliao
2017/04/05 23:29:23
s/worker/workers/
fdoray
2017/04/06 15:07:12
Done.
| |
146 const TimeDelta suggested_reclaim_time_; | 149 // can be read without synchronization once |workers_created_.IsSignaled()|). |
150 std::vector<scoped_refptr<SchedulerWorker>> workers_; | |
151 | |
152 // Suggested reclaim time for workers. Never modified after Start() (i.e. can | |
153 // be read without synchronization once |workers_created_.IsSignaled()|). | |
154 TimeDelta suggested_reclaim_time_; | |
147 | 155 |
148 // Synchronizes access to |idle_workers_stack_| and | 156 // Synchronizes access to |idle_workers_stack_| and |
149 // |idle_workers_stack_cv_for_testing_|. Has |shared_priority_queue_|'s | 157 // |idle_workers_stack_cv_for_testing_|. Has |shared_priority_queue_|'s |
150 // lock as its predecessor so that a worker can be pushed to | 158 // lock as its predecessor so that a worker can be pushed to |
151 // |idle_workers_stack_| within the scope of a Transaction (more | 159 // |idle_workers_stack_| within the scope of a Transaction (more |
152 // details in GetWork()). | 160 // details in GetWork()). |
153 mutable SchedulerLock idle_workers_stack_lock_; | 161 mutable SchedulerLock idle_workers_stack_lock_; |
154 | 162 |
155 // Stack of idle workers. Initially, all workers are on this stack. A worker | 163 // Stack of idle workers. Initially, all workers are on this stack. A worker |
156 // is removed from the stack before its WakeUp() function is called and when | 164 // is removed from the stack before its WakeUp() function is called and when |
157 // it receives work from GetWork() (a worker calls GetWork() when its sleep | 165 // it receives work from GetWork() (a worker calls GetWork() when its sleep |
158 // timeout expires, even if its WakeUp() method hasn't been called). A worker | 166 // timeout expires, even if its WakeUp() method hasn't been called). A worker |
159 // is pushed on this stack when it receives nullptr from GetWork(). | 167 // is pushed on this stack when it receives nullptr from GetWork(). |
160 SchedulerWorkerStack idle_workers_stack_; | 168 SchedulerWorkerStack idle_workers_stack_; |
161 | 169 |
162 // Signaled when all workers become idle. | 170 // Signaled when all workers become idle. |
163 std::unique_ptr<ConditionVariable> idle_workers_stack_cv_for_testing_; | 171 std::unique_ptr<ConditionVariable> idle_workers_stack_cv_for_testing_; |
164 | 172 |
165 // Signaled once JoinForTesting() has returned. | 173 // Signaled once JoinForTesting() has returned. |
166 WaitableEvent join_for_testing_returned_; | 174 WaitableEvent join_for_testing_returned_; |
167 | 175 |
168 // Indicates to the delegates that workers are not permitted to detach their | 176 // Indicates to the delegates that workers are not permitted to detach their |
169 // threads. | 177 // threads. |
170 AtomicFlag worker_detachment_disallowed_; | 178 AtomicFlag worker_detachment_disallowed_; |
171 | 179 |
172 #if DCHECK_IS_ON() | 180 #if DCHECK_IS_ON() |
173 // Signaled when all workers have been created. | 181 // Signaled when all workers have been created. |
174 WaitableEvent workers_created_; | 182 mutable WaitableEvent workers_created_; |
175 #endif | 183 #endif |
176 | 184 |
177 // TaskScheduler.DetachDuration.[worker pool name] histogram. Intentionally | 185 // TaskScheduler.DetachDuration.[worker pool name] histogram. Intentionally |
178 // leaked. | 186 // leaked. |
179 HistogramBase* const detach_duration_histogram_; | 187 HistogramBase* const detach_duration_histogram_; |
180 | 188 |
181 // TaskScheduler.NumTasksBeforeDetach.[worker pool name] histogram. | 189 // TaskScheduler.NumTasksBeforeDetach.[worker pool name] histogram. |
182 // Intentionally leaked. | 190 // Intentionally leaked. |
183 HistogramBase* const num_tasks_before_detach_histogram_; | 191 HistogramBase* const num_tasks_before_detach_histogram_; |
184 | 192 |
185 // TaskScheduler.NumTasksBetweenWaits.[worker pool name] histogram. | 193 // TaskScheduler.NumTasksBetweenWaits.[worker pool name] histogram. |
186 // Intentionally leaked. | 194 // Intentionally leaked. |
187 HistogramBase* const num_tasks_between_waits_histogram_; | 195 HistogramBase* const num_tasks_between_waits_histogram_; |
188 | 196 |
189 TaskTracker* const task_tracker_; | 197 TaskTracker* const task_tracker_; |
190 DelayedTaskManager* const delayed_task_manager_; | 198 DelayedTaskManager* const delayed_task_manager_; |
191 | 199 |
192 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); | 200 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPoolImpl); |
193 }; | 201 }; |
194 | 202 |
195 } // namespace internal | 203 } // namespace internal |
196 } // namespace base | 204 } // namespace base |
197 | 205 |
198 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ | 206 #endif // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_IMPL_H_ |
OLD | NEW |