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 #include "base/task_scheduler/scheduler_worker_pool_impl.h" | 5 #include "base/task_scheduler/scheduler_worker_pool_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "base/task_scheduler/delayed_task_manager.h" | 19 #include "base/task_scheduler/delayed_task_manager.h" |
20 #include "base/task_scheduler/task_tracker.h" | 20 #include "base/task_scheduler/task_tracker.h" |
21 #include "base/threading/platform_thread.h" | 21 #include "base/threading/platform_thread.h" |
22 #include "base/threading/thread_local.h" | 22 #include "base/threading/thread_local.h" |
23 #include "base/threading/thread_restrictions.h" | 23 #include "base/threading/thread_restrictions.h" |
24 | 24 |
25 namespace base { | 25 namespace base { |
26 namespace internal { | 26 namespace internal { |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 // SchedulerWorker that owns the current thread, if any. | |
31 LazyInstance<ThreadLocalPointer<const SchedulerWorker>>::Leaky | |
32 tls_current_worker = LAZY_INSTANCE_INITIALIZER; | |
33 | |
30 // SchedulerWorkerPool that owns the current thread, if any. | 34 // SchedulerWorkerPool that owns the current thread, if any. |
31 LazyInstance<ThreadLocalPointer<const SchedulerWorkerPool>>::Leaky | 35 LazyInstance<ThreadLocalPointer<const SchedulerWorkerPool>>::Leaky |
32 tls_current_worker_pool = LAZY_INSTANCE_INITIALIZER; | 36 tls_current_worker_pool = LAZY_INSTANCE_INITIALIZER; |
33 | 37 |
34 // SchedulerWorkerThread that owns the current thread, if any. | |
35 LazyInstance<ThreadLocalPointer<const SchedulerWorkerThread>>::Leaky | |
36 tls_current_worker_thread = LAZY_INSTANCE_INITIALIZER; | |
37 | |
38 // A task runner that runs tasks with the PARALLEL ExecutionMode. | 38 // A task runner that runs tasks with the PARALLEL ExecutionMode. |
39 class SchedulerParallelTaskRunner : public TaskRunner { | 39 class SchedulerParallelTaskRunner : public TaskRunner { |
40 public: | 40 public: |
41 // Constructs a SchedulerParallelTaskRunner which can be used to post tasks so | 41 // Constructs a SchedulerParallelTaskRunner which can be used to post tasks so |
42 // long as |worker_pool| is alive. | 42 // long as |worker_pool| is alive. |
43 // TODO(robliao): Find a concrete way to manage |worker_pool|'s memory. | 43 // TODO(robliao): Find a concrete way to manage |worker_pool|'s memory. |
44 SchedulerParallelTaskRunner(const TaskTraits& traits, | 44 SchedulerParallelTaskRunner(const TaskTraits& traits, |
45 SchedulerWorkerPool* worker_pool) | 45 SchedulerWorkerPool* worker_pool) |
46 : traits_(traits), worker_pool_(worker_pool) {} | 46 : traits_(traits), worker_pool_(worker_pool) {} |
47 | 47 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 const TaskTraits traits_; | 110 const TaskTraits traits_; |
111 SchedulerWorkerPool* const worker_pool_; | 111 SchedulerWorkerPool* const worker_pool_; |
112 | 112 |
113 DISALLOW_COPY_AND_ASSIGN(SchedulerSequencedTaskRunner); | 113 DISALLOW_COPY_AND_ASSIGN(SchedulerSequencedTaskRunner); |
114 }; | 114 }; |
115 | 115 |
116 // A task runner that runs tasks with the SINGLE_THREADED ExecutionMode. | 116 // A task runner that runs tasks with the SINGLE_THREADED ExecutionMode. |
117 class SchedulerSingleThreadTaskRunner : public SingleThreadTaskRunner { | 117 class SchedulerSingleThreadTaskRunner : public SingleThreadTaskRunner { |
118 public: | 118 public: |
119 // Constructs a SchedulerSingleThreadTaskRunner which can be used to post | 119 // Constructs a SchedulerSingleThreadTaskRunner which can be used to post |
120 // tasks so long as |worker_pool| and |worker_thread| are alive. | 120 // tasks so long as |worker_pool| and |worker| are alive. |
121 // TODO(robliao): Find a concrete way to manage the memory of |worker_pool| | 121 // TODO(robliao): Find a concrete way to manage the memory of |worker_pool| |
122 // and |worker_thread|. | 122 // and |worker|. |
123 SchedulerSingleThreadTaskRunner(const TaskTraits& traits, | 123 SchedulerSingleThreadTaskRunner(const TaskTraits& traits, |
124 SchedulerWorkerPool* worker_pool, | 124 SchedulerWorkerPool* worker_pool, |
125 SchedulerWorkerThread* worker_thread) | 125 SchedulerWorker* worker) |
126 : traits_(traits), | 126 : traits_(traits), |
127 worker_pool_(worker_pool), | 127 worker_pool_(worker_pool), |
128 worker_thread_(worker_thread) {} | 128 worker_(worker) {} |
129 | 129 |
130 // SingleThreadTaskRunner: | 130 // SingleThreadTaskRunner: |
131 bool PostDelayedTask(const tracked_objects::Location& from_here, | 131 bool PostDelayedTask(const tracked_objects::Location& from_here, |
132 const Closure& closure, | 132 const Closure& closure, |
133 TimeDelta delay) override { | 133 TimeDelta delay) override { |
134 std::unique_ptr<Task> task(new Task(from_here, closure, traits_, delay)); | 134 std::unique_ptr<Task> task(new Task(from_here, closure, traits_, delay)); |
135 task->single_thread_task_runner_ref = this; | 135 task->single_thread_task_runner_ref = this; |
136 | 136 |
137 // Post the task to be executed by |worker_thread_| as part of |sequence_|. | 137 // Post the task to be executed by |worker_| as part of |sequence_|. |
138 return worker_pool_->PostTaskWithSequence(std::move(task), sequence_, | 138 return worker_pool_->PostTaskWithSequence(std::move(task), sequence_, |
139 worker_thread_); | 139 worker_); |
140 } | 140 } |
141 | 141 |
142 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 142 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
143 const Closure& closure, | 143 const Closure& closure, |
144 base::TimeDelta delay) override { | 144 base::TimeDelta delay) override { |
145 // Tasks are never nested within the task scheduler. | 145 // Tasks are never nested within the task scheduler. |
146 return PostDelayedTask(from_here, closure, delay); | 146 return PostDelayedTask(from_here, closure, delay); |
147 } | 147 } |
148 | 148 |
149 bool RunsTasksOnCurrentThread() const override { | 149 bool RunsTasksOnCurrentThread() const override { |
150 return tls_current_worker_thread.Get().Get() == worker_thread_; | 150 return tls_current_worker.Get().Get() == worker_; |
151 } | 151 } |
152 | 152 |
153 private: | 153 private: |
154 ~SchedulerSingleThreadTaskRunner() override = default; | 154 ~SchedulerSingleThreadTaskRunner() override = default; |
155 | 155 |
156 // Sequence for all Tasks posted through this TaskRunner. | 156 // Sequence for all Tasks posted through this TaskRunner. |
157 const scoped_refptr<Sequence> sequence_ = new Sequence; | 157 const scoped_refptr<Sequence> sequence_ = new Sequence; |
158 | 158 |
159 const TaskTraits traits_; | 159 const TaskTraits traits_; |
160 SchedulerWorkerPool* const worker_pool_; | 160 SchedulerWorkerPool* const worker_pool_; |
161 SchedulerWorkerThread* const worker_thread_; | 161 SchedulerWorker* const worker_; |
162 | 162 |
163 DISALLOW_COPY_AND_ASSIGN(SchedulerSingleThreadTaskRunner); | 163 DISALLOW_COPY_AND_ASSIGN(SchedulerSingleThreadTaskRunner); |
164 }; | 164 }; |
165 | 165 |
166 // Only used in DCHECKs. | 166 // Only used in DCHECKs. |
167 bool ContainsWorkerThread( | 167 bool ContainsWorker( |
168 const std::vector<std::unique_ptr<SchedulerWorkerThread>>& worker_threads, | 168 const std::vector<std::unique_ptr<SchedulerWorker>>& workers, |
169 const SchedulerWorkerThread* worker_thread) { | 169 const SchedulerWorker* worker) { |
170 auto it = std::find_if( | 170 auto it = std::find_if(workers.begin(), workers.end(), |
171 worker_threads.begin(), worker_threads.end(), | 171 [worker](const std::unique_ptr<SchedulerWorker>& i) { |
172 [worker_thread](const std::unique_ptr<SchedulerWorkerThread>& i) { | 172 return i.get() == worker; |
173 return i.get() == worker_thread; | |
174 }); | 173 }); |
175 return it != worker_threads.end(); | 174 return it != workers.end(); |
176 } | 175 } |
177 | 176 |
178 } // namespace | 177 } // namespace |
179 | 178 |
180 class SchedulerWorkerPoolImpl::SchedulerWorkerThreadDelegateImpl | 179 class SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl |
181 : public SchedulerWorkerThread::Delegate { | 180 : public SchedulerWorker::Delegate { |
182 public: | 181 public: |
183 // |outer| owns the worker thread for which this delegate is constructed. | 182 // |outer| owns the worker for which this delegate is constructed. |
184 // |re_enqueue_sequence_callback| is invoked when ReEnqueueSequence() is | 183 // |re_enqueue_sequence_callback| is invoked when ReEnqueueSequence() is |
185 // called with a non-single-threaded Sequence. |shared_priority_queue| is a | 184 // called with a non-single-threaded Sequence. |shared_priority_queue| is a |
186 // PriorityQueue whose transactions may overlap with the worker thread's | 185 // PriorityQueue whose transactions may overlap with the worker's |
187 // single-threaded PriorityQueue's transactions. |index| will be appended to | 186 // single-threaded PriorityQueue's transactions. |index| will be appended to |
fdoray
2016/06/20 15:08:39
|index| will be appended to the pool name to label
robliao
2016/06/20 17:50:09
Took a different approach for this comment. Update
| |
188 // this thread's name to uniquely identify it. | 187 // this thread's name to uniquely identify it. |
189 SchedulerWorkerThreadDelegateImpl( | 188 SchedulerWorkerDelegateImpl( |
190 SchedulerWorkerPoolImpl* outer, | 189 SchedulerWorkerPoolImpl* outer, |
191 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | 190 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, |
192 const PriorityQueue* shared_priority_queue, | 191 const PriorityQueue* shared_priority_queue, |
193 int index); | 192 int index); |
194 ~SchedulerWorkerThreadDelegateImpl() override; | 193 ~SchedulerWorkerDelegateImpl() override; |
195 | 194 |
196 PriorityQueue* single_threaded_priority_queue() { | 195 PriorityQueue* single_threaded_priority_queue() { |
197 return &single_threaded_priority_queue_; | 196 return &single_threaded_priority_queue_; |
198 } | 197 } |
199 | 198 |
200 // SchedulerWorkerThread::Delegate: | 199 // SchedulerWorker::Delegate: |
201 void OnMainEntry(SchedulerWorkerThread* worker_thread) override; | 200 void OnMainEntry(SchedulerWorker* worker) override; |
202 scoped_refptr<Sequence> GetWork( | 201 scoped_refptr<Sequence> GetWork(SchedulerWorker* worker) override; |
203 SchedulerWorkerThread* worker_thread) override; | |
204 void ReEnqueueSequence(scoped_refptr<Sequence> sequence) override; | 202 void ReEnqueueSequence(scoped_refptr<Sequence> sequence) override; |
205 TimeDelta GetSleepTimeout() override; | 203 TimeDelta GetSleepTimeout() override; |
206 | 204 |
207 private: | 205 private: |
208 SchedulerWorkerPoolImpl* outer_; | 206 SchedulerWorkerPoolImpl* outer_; |
209 const ReEnqueueSequenceCallback re_enqueue_sequence_callback_; | 207 const ReEnqueueSequenceCallback re_enqueue_sequence_callback_; |
210 | 208 |
211 // Single-threaded PriorityQueue for the worker thread. | 209 // Single-threaded PriorityQueue for the worker. |
212 PriorityQueue single_threaded_priority_queue_; | 210 PriorityQueue single_threaded_priority_queue_; |
213 | 211 |
214 // True if the last Sequence returned by GetWork() was extracted from | 212 // True if the last Sequence returned by GetWork() was extracted from |
215 // |single_threaded_priority_queue_|. | 213 // |single_threaded_priority_queue_|. |
216 bool last_sequence_is_single_threaded_ = false; | 214 bool last_sequence_is_single_threaded_ = false; |
217 | 215 |
218 const int index_; | 216 const int index_; |
219 | 217 |
220 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThreadDelegateImpl); | 218 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerDelegateImpl); |
221 }; | 219 }; |
222 | 220 |
223 SchedulerWorkerPoolImpl::~SchedulerWorkerPoolImpl() { | 221 SchedulerWorkerPoolImpl::~SchedulerWorkerPoolImpl() { |
224 // SchedulerWorkerPool should never be deleted in production unless its | 222 // SchedulerWorkerPool should never be deleted in production unless its |
225 // initialization failed. | 223 // initialization failed. |
226 DCHECK(join_for_testing_returned_.IsSignaled() || worker_threads_.empty()); | 224 DCHECK(join_for_testing_returned_.IsSignaled() || workers_.empty()); |
227 } | 225 } |
228 | 226 |
229 // static | 227 // static |
230 std::unique_ptr<SchedulerWorkerPoolImpl> SchedulerWorkerPoolImpl::Create( | 228 std::unique_ptr<SchedulerWorkerPoolImpl> SchedulerWorkerPoolImpl::Create( |
231 StringPiece name, | 229 StringPiece name, |
232 ThreadPriority thread_priority, | 230 ThreadPriority thread_priority, |
233 size_t max_threads, | 231 size_t max_threads, |
234 IORestriction io_restriction, | 232 IORestriction io_restriction, |
235 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | 233 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, |
236 TaskTracker* task_tracker, | 234 TaskTracker* task_tracker, |
237 DelayedTaskManager* delayed_task_manager) { | 235 DelayedTaskManager* delayed_task_manager) { |
238 std::unique_ptr<SchedulerWorkerPoolImpl> worker_pool( | 236 std::unique_ptr<SchedulerWorkerPoolImpl> worker_pool( |
239 new SchedulerWorkerPoolImpl(name, io_restriction, task_tracker, | 237 new SchedulerWorkerPoolImpl(name, io_restriction, task_tracker, |
240 delayed_task_manager)); | 238 delayed_task_manager)); |
241 if (worker_pool->Initialize(thread_priority, max_threads, | 239 if (worker_pool->Initialize(thread_priority, max_threads, |
242 re_enqueue_sequence_callback)) { | 240 re_enqueue_sequence_callback)) { |
243 return worker_pool; | 241 return worker_pool; |
244 } | 242 } |
245 return nullptr; | 243 return nullptr; |
246 } | 244 } |
247 | 245 |
248 void SchedulerWorkerPoolImpl::WaitForAllWorkerWorkersIdleForTesting() { | 246 void SchedulerWorkerPoolImpl::WaitForAllWorkersIdleForTesting() { |
249 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 247 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); |
250 while (idle_worker_threads_stack_.Size() < worker_threads_.size()) | 248 while (idle_workers_stack_.Size() < workers_.size()) |
251 idle_worker_threads_stack_cv_for_testing_->Wait(); | 249 idle_workers_stack_cv_for_testing_->Wait(); |
252 } | 250 } |
253 | 251 |
254 void SchedulerWorkerPoolImpl::JoinForTesting() { | 252 void SchedulerWorkerPoolImpl::JoinForTesting() { |
255 for (const auto& worker_thread : worker_threads_) | 253 for (const auto& worker : workers_) |
256 worker_thread->JoinForTesting(); | 254 worker->JoinForTesting(); |
257 | 255 |
258 DCHECK(!join_for_testing_returned_.IsSignaled()); | 256 DCHECK(!join_for_testing_returned_.IsSignaled()); |
259 join_for_testing_returned_.Signal(); | 257 join_for_testing_returned_.Signal(); |
260 } | 258 } |
261 | 259 |
262 scoped_refptr<TaskRunner> SchedulerWorkerPoolImpl::CreateTaskRunnerWithTraits( | 260 scoped_refptr<TaskRunner> SchedulerWorkerPoolImpl::CreateTaskRunnerWithTraits( |
263 const TaskTraits& traits, | 261 const TaskTraits& traits, |
264 ExecutionMode execution_mode) { | 262 ExecutionMode execution_mode) { |
265 switch (execution_mode) { | 263 switch (execution_mode) { |
266 case ExecutionMode::PARALLEL: | 264 case ExecutionMode::PARALLEL: |
267 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this)); | 265 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this)); |
268 | 266 |
269 case ExecutionMode::SEQUENCED: | 267 case ExecutionMode::SEQUENCED: |
270 return make_scoped_refptr(new SchedulerSequencedTaskRunner(traits, this)); | 268 return make_scoped_refptr(new SchedulerSequencedTaskRunner(traits, this)); |
271 | 269 |
272 case ExecutionMode::SINGLE_THREADED: { | 270 case ExecutionMode::SINGLE_THREADED: { |
273 // TODO(fdoray): Find a way to take load into account when assigning a | 271 // TODO(fdoray): Find a way to take load into account when assigning a |
274 // SchedulerWorkerThread to a SingleThreadTaskRunner. Also, this code | 272 // SchedulerWorker to a SingleThreadTaskRunner. Also, this code |
275 // assumes that all SchedulerWorkerThreads are alive. Eventually, we might | 273 // assumes that all SchedulerWorkers are alive. Eventually, we might |
276 // decide to tear down threads that haven't run tasks for a long time. | 274 // decide to tear down threads that haven't run tasks for a long time. |
277 size_t worker_thread_index; | 275 size_t worker_index; |
278 { | 276 { |
279 AutoSchedulerLock auto_lock(next_worker_thread_index_lock_); | 277 AutoSchedulerLock auto_lock(next_worker_index_lock_); |
280 worker_thread_index = next_worker_thread_index_; | 278 worker_index = next_worker_index_; |
281 next_worker_thread_index_ = | 279 next_worker_index_ = (next_worker_index_ + 1) % workers_.size(); |
282 (next_worker_thread_index_ + 1) % worker_threads_.size(); | |
283 } | 280 } |
284 return make_scoped_refptr(new SchedulerSingleThreadTaskRunner( | 281 return make_scoped_refptr(new SchedulerSingleThreadTaskRunner( |
285 traits, this, worker_threads_[worker_thread_index].get())); | 282 traits, this, workers_[worker_index].get())); |
286 } | 283 } |
287 } | 284 } |
288 | 285 |
289 NOTREACHED(); | 286 NOTREACHED(); |
290 return nullptr; | 287 return nullptr; |
291 } | 288 } |
292 | 289 |
293 void SchedulerWorkerPoolImpl::ReEnqueueSequence( | 290 void SchedulerWorkerPoolImpl::ReEnqueueSequence( |
294 scoped_refptr<Sequence> sequence, | 291 scoped_refptr<Sequence> sequence, |
295 const SequenceSortKey& sequence_sort_key) { | 292 const SequenceSortKey& sequence_sort_key) { |
296 shared_priority_queue_.BeginTransaction()->Push(std::move(sequence), | 293 shared_priority_queue_.BeginTransaction()->Push(std::move(sequence), |
297 sequence_sort_key); | 294 sequence_sort_key); |
298 | 295 |
299 // The thread calling this method just ran a Task from |sequence| and will | 296 // The thread calling this method just ran a Task from |sequence| and will |
300 // soon try to get another Sequence from which to run a Task. If the thread | 297 // soon try to get another Sequence from which to run a Task. If the thread |
301 // belongs to this pool, it will get that Sequence from | 298 // belongs to this pool, it will get that Sequence from |
302 // |shared_priority_queue_|. When that's the case, there is no need to wake up | 299 // |shared_priority_queue_|. When that's the case, there is no need to wake up |
303 // another thread after |sequence| is inserted in |shared_priority_queue_|. If | 300 // another thread after |sequence| is inserted in |shared_priority_queue_|. If |
fdoray
2016/06/20 15:08:39
another *worker* after
robliao
2016/06/20 17:50:09
Done.
| |
304 // we did wake up another thread, we would waste resources by having more | 301 // we did wake up another thread, we would waste resources by having more |
fdoray
2016/06/20 15:08:39
we did wake up another *worker*
robliao
2016/06/20 17:50:09
Done.
| |
305 // threads trying to get a Sequence from |shared_priority_queue_| than the | 302 // threads trying to get a Sequence from |shared_priority_queue_| than the |
fdoray
2016/06/20 15:08:39
*workers* trying to get
robliao
2016/06/20 17:50:09
Done.
| |
306 // number of Sequences in it. | 303 // number of Sequences in it. |
307 if (tls_current_worker_pool.Get().Get() != this) | 304 if (tls_current_worker_pool.Get().Get() != this) |
308 WakeUpOneWorker(); | 305 WakeUpOneWorker(); |
309 } | 306 } |
310 | 307 |
311 bool SchedulerWorkerPoolImpl::PostTaskWithSequence( | 308 bool SchedulerWorkerPoolImpl::PostTaskWithSequence( |
312 std::unique_ptr<Task> task, | 309 std::unique_ptr<Task> task, |
313 scoped_refptr<Sequence> sequence, | 310 scoped_refptr<Sequence> sequence, |
314 SchedulerWorkerThread* worker_thread) { | 311 SchedulerWorker* worker) { |
315 DCHECK(task); | 312 DCHECK(task); |
316 DCHECK(sequence); | 313 DCHECK(sequence); |
317 DCHECK(!worker_thread || | 314 DCHECK(!worker || ContainsWorker(workers_, worker)); |
318 ContainsWorkerThread(worker_threads_, worker_thread)); | |
319 | 315 |
320 if (!task_tracker_->WillPostTask(task.get())) | 316 if (!task_tracker_->WillPostTask(task.get())) |
321 return false; | 317 return false; |
322 | 318 |
323 if (task->delayed_run_time.is_null()) { | 319 if (task->delayed_run_time.is_null()) { |
324 PostTaskWithSequenceNow(std::move(task), std::move(sequence), | 320 PostTaskWithSequenceNow(std::move(task), std::move(sequence), worker); |
325 worker_thread); | |
326 } else { | 321 } else { |
327 delayed_task_manager_->AddDelayedTask(std::move(task), std::move(sequence), | 322 delayed_task_manager_->AddDelayedTask(std::move(task), std::move(sequence), |
328 worker_thread, this); | 323 worker, this); |
329 } | 324 } |
330 | 325 |
331 return true; | 326 return true; |
332 } | 327 } |
333 | 328 |
334 void SchedulerWorkerPoolImpl::PostTaskWithSequenceNow( | 329 void SchedulerWorkerPoolImpl::PostTaskWithSequenceNow( |
335 std::unique_ptr<Task> task, | 330 std::unique_ptr<Task> task, |
336 scoped_refptr<Sequence> sequence, | 331 scoped_refptr<Sequence> sequence, |
337 SchedulerWorkerThread* worker_thread) { | 332 SchedulerWorker* worker) { |
338 DCHECK(task); | 333 DCHECK(task); |
339 DCHECK(sequence); | 334 DCHECK(sequence); |
340 DCHECK(!worker_thread || | 335 DCHECK(!worker || ContainsWorker(workers_, worker)); |
341 ContainsWorkerThread(worker_threads_, worker_thread)); | |
342 | 336 |
343 // Confirm that |task| is ready to run (its delayed run time is either null or | 337 // Confirm that |task| is ready to run (its delayed run time is either null or |
344 // in the past). | 338 // in the past). |
345 DCHECK_LE(task->delayed_run_time, delayed_task_manager_->Now()); | 339 DCHECK_LE(task->delayed_run_time, delayed_task_manager_->Now()); |
346 | 340 |
347 // Because |worker_thread| belongs to this worker pool, we know that the type | 341 // Because |worker| belongs to this worker pool, we know that the type |
348 // of its delegate is SchedulerWorkerThreadDelegateImpl. | 342 // of its delegate is SchedulerWorkerDelegateImpl. |
349 PriorityQueue* const priority_queue = | 343 PriorityQueue* const priority_queue = |
350 worker_thread | 344 worker |
351 ? static_cast<SchedulerWorkerThreadDelegateImpl*>( | 345 ? static_cast<SchedulerWorkerDelegateImpl*>(worker->delegate()) |
352 worker_thread->delegate()) | |
353 ->single_threaded_priority_queue() | 346 ->single_threaded_priority_queue() |
354 : &shared_priority_queue_; | 347 : &shared_priority_queue_; |
355 DCHECK(priority_queue); | 348 DCHECK(priority_queue); |
356 | 349 |
357 const bool sequence_was_empty = sequence->PushTask(std::move(task)); | 350 const bool sequence_was_empty = sequence->PushTask(std::move(task)); |
358 if (sequence_was_empty) { | 351 if (sequence_was_empty) { |
359 // Insert |sequence| in |priority_queue| if it was empty before |task| was | 352 // Insert |sequence| in |priority_queue| if it was empty before |task| was |
360 // inserted into it. Otherwise, one of these must be true: | 353 // inserted into it. Otherwise, one of these must be true: |
361 // - |sequence| is already in a PriorityQueue (not necessarily | 354 // - |sequence| is already in a PriorityQueue (not necessarily |
362 // |shared_priority_queue_|), or, | 355 // |shared_priority_queue_|), or, |
363 // - A worker thread is running a Task from |sequence|. It will insert | 356 // - A worker is running a Task from |sequence|. It will insert |sequence| |
364 // |sequence| in a PriorityQueue once it's done running the Task. | 357 // in a PriorityQueue once it's done running the Task. |
365 const auto sequence_sort_key = sequence->GetSortKey(); | 358 const auto sequence_sort_key = sequence->GetSortKey(); |
366 priority_queue->BeginTransaction()->Push(std::move(sequence), | 359 priority_queue->BeginTransaction()->Push(std::move(sequence), |
367 sequence_sort_key); | 360 sequence_sort_key); |
368 | 361 |
369 // Wake up a worker thread to process |sequence|. | 362 // Wake up a worker to process |sequence|. |
370 if (worker_thread) | 363 if (worker) |
371 worker_thread->WakeUp(); | 364 worker->WakeUp(); |
372 else | 365 else |
373 WakeUpOneWorker(); | 366 WakeUpOneWorker(); |
374 } | 367 } |
375 } | 368 } |
376 | 369 |
377 SchedulerWorkerPoolImpl::SchedulerWorkerThreadDelegateImpl:: | 370 SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl:: |
378 SchedulerWorkerThreadDelegateImpl( | 371 SchedulerWorkerDelegateImpl( |
379 SchedulerWorkerPoolImpl* outer, | 372 SchedulerWorkerPoolImpl* outer, |
380 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | 373 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, |
381 const PriorityQueue* shared_priority_queue, | 374 const PriorityQueue* shared_priority_queue, |
382 int index) | 375 int index) |
383 : outer_(outer), | 376 : outer_(outer), |
384 re_enqueue_sequence_callback_(re_enqueue_sequence_callback), | 377 re_enqueue_sequence_callback_(re_enqueue_sequence_callback), |
385 single_threaded_priority_queue_(shared_priority_queue), | 378 single_threaded_priority_queue_(shared_priority_queue), |
386 index_(index) {} | 379 index_(index) {} |
387 | 380 |
388 SchedulerWorkerPoolImpl::SchedulerWorkerThreadDelegateImpl:: | 381 SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl:: |
389 ~SchedulerWorkerThreadDelegateImpl() = default; | 382 ~SchedulerWorkerDelegateImpl() = default; |
390 | 383 |
391 void SchedulerWorkerPoolImpl::SchedulerWorkerThreadDelegateImpl::OnMainEntry( | 384 void SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::OnMainEntry( |
392 SchedulerWorkerThread* worker_thread) { | 385 SchedulerWorker* worker) { |
393 #if DCHECK_IS_ON() | 386 #if DCHECK_IS_ON() |
394 // Wait for |outer_->threads_created_| to avoid traversing | 387 // Wait for |outer_->threads_created_| to avoid traversing |
fdoray
2016/06/20 15:08:39
workers_created_ (if you change the variable's nam
robliao
2016/06/20 17:50:09
Done.
| |
395 // |outer_->worker_threads_| while it is being filled by Initialize(). | 388 // |outer_->workers_| while it is being filled by Initialize(). |
396 outer_->threads_created_.Wait(); | 389 outer_->threads_created_.Wait(); |
397 DCHECK(ContainsWorkerThread(outer_->worker_threads_, worker_thread)); | 390 DCHECK(ContainsWorker(outer_->workers_, worker)); |
398 #endif | 391 #endif |
399 | 392 |
400 PlatformThread::SetName( | 393 PlatformThread::SetName( |
401 StringPrintf("%sWorker%d", outer_->name_.c_str(), index_)); | 394 StringPrintf("%sWorker%d", outer_->name_.c_str(), index_)); |
402 | 395 |
403 DCHECK(!tls_current_worker_thread.Get().Get()); | 396 DCHECK(!tls_current_worker.Get().Get()); |
404 DCHECK(!tls_current_worker_pool.Get().Get()); | 397 DCHECK(!tls_current_worker_pool.Get().Get()); |
405 tls_current_worker_thread.Get().Set(worker_thread); | 398 tls_current_worker.Get().Set(worker); |
406 tls_current_worker_pool.Get().Set(outer_); | 399 tls_current_worker_pool.Get().Set(outer_); |
407 | 400 |
408 ThreadRestrictions::SetIOAllowed(outer_->io_restriction_ == | 401 ThreadRestrictions::SetIOAllowed(outer_->io_restriction_ == |
409 IORestriction::ALLOWED); | 402 IORestriction::ALLOWED); |
410 } | 403 } |
411 | 404 |
412 scoped_refptr<Sequence> | 405 scoped_refptr<Sequence> |
413 SchedulerWorkerPoolImpl::SchedulerWorkerThreadDelegateImpl::GetWork( | 406 SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::GetWork( |
414 SchedulerWorkerThread* worker_thread) { | 407 SchedulerWorker* worker) { |
415 DCHECK(ContainsWorkerThread(outer_->worker_threads_, worker_thread)); | 408 DCHECK(ContainsWorker(outer_->workers_, worker)); |
416 | 409 |
417 scoped_refptr<Sequence> sequence; | 410 scoped_refptr<Sequence> sequence; |
418 { | 411 { |
419 std::unique_ptr<PriorityQueue::Transaction> shared_transaction( | 412 std::unique_ptr<PriorityQueue::Transaction> shared_transaction( |
420 outer_->shared_priority_queue_.BeginTransaction()); | 413 outer_->shared_priority_queue_.BeginTransaction()); |
421 std::unique_ptr<PriorityQueue::Transaction> single_threaded_transaction( | 414 std::unique_ptr<PriorityQueue::Transaction> single_threaded_transaction( |
422 single_threaded_priority_queue_.BeginTransaction()); | 415 single_threaded_priority_queue_.BeginTransaction()); |
423 | 416 |
424 if (shared_transaction->IsEmpty() && | 417 if (shared_transaction->IsEmpty() && |
425 single_threaded_transaction->IsEmpty()) { | 418 single_threaded_transaction->IsEmpty()) { |
426 single_threaded_transaction.reset(); | 419 single_threaded_transaction.reset(); |
427 | 420 |
428 // |shared_transaction| is kept alive while |worker_thread| is added to | 421 // |shared_transaction| is kept alive while |worker| is added to |
429 // |idle_worker_threads_stack_| to avoid this race: | 422 // |idle_workers_stack_| to avoid this race: |
430 // 1. This thread creates a Transaction, finds |shared_priority_queue_| | 423 // 1. This thread creates a Transaction, finds |shared_priority_queue_| |
431 // empty and ends the Transaction. | 424 // empty and ends the Transaction. |
432 // 2. Other thread creates a Transaction, inserts a Sequence into | 425 // 2. Other thread creates a Transaction, inserts a Sequence into |
433 // |shared_priority_queue_| and ends the Transaction. This can't happen | 426 // |shared_priority_queue_| and ends the Transaction. This can't happen |
434 // if the Transaction of step 1 is still active because because there | 427 // if the Transaction of step 1 is still active because because there |
435 // can only be one active Transaction per PriorityQueue at a time. | 428 // can only be one active Transaction per PriorityQueue at a time. |
436 // 3. Other thread calls WakeUpOneWorker(). No thread is woken up because | 429 // 3. Other thread calls WakeUpOneWorker(). No thread is woken up because |
437 // |idle_worker_threads_stack_| is empty. | 430 // |idle_workers_stack_| is empty. |
438 // 4. This thread adds itself to |idle_worker_threads_stack_| and goes to | 431 // 4. This thread adds itself to |idle_workers_stack_| and goes to sleep. |
439 // sleep. No thread runs the Sequence inserted in step 2. | 432 // No thread runs the Sequence inserted in step 2. |
440 outer_->AddToIdleWorkerThreadsStack(worker_thread); | 433 outer_->AddToIdleWorkersStack(worker); |
441 return nullptr; | 434 return nullptr; |
442 } | 435 } |
443 | 436 |
444 // True if both PriorityQueues have Sequences and the Sequence at the top of | 437 // True if both PriorityQueues have Sequences and the Sequence at the top of |
445 // the shared PriorityQueue is more important. | 438 // the shared PriorityQueue is more important. |
446 const bool shared_sequence_is_more_important = | 439 const bool shared_sequence_is_more_important = |
447 !shared_transaction->IsEmpty() && | 440 !shared_transaction->IsEmpty() && |
448 !single_threaded_transaction->IsEmpty() && | 441 !single_threaded_transaction->IsEmpty() && |
449 shared_transaction->PeekSortKey() > | 442 shared_transaction->PeekSortKey() > |
450 single_threaded_transaction->PeekSortKey(); | 443 single_threaded_transaction->PeekSortKey(); |
451 | 444 |
452 if (single_threaded_transaction->IsEmpty() || | 445 if (single_threaded_transaction->IsEmpty() || |
453 shared_sequence_is_more_important) { | 446 shared_sequence_is_more_important) { |
454 sequence = shared_transaction->PopSequence(); | 447 sequence = shared_transaction->PopSequence(); |
455 last_sequence_is_single_threaded_ = false; | 448 last_sequence_is_single_threaded_ = false; |
456 } else { | 449 } else { |
457 DCHECK(!single_threaded_transaction->IsEmpty()); | 450 DCHECK(!single_threaded_transaction->IsEmpty()); |
458 sequence = single_threaded_transaction->PopSequence(); | 451 sequence = single_threaded_transaction->PopSequence(); |
459 last_sequence_is_single_threaded_ = true; | 452 last_sequence_is_single_threaded_ = true; |
460 } | 453 } |
461 } | 454 } |
462 DCHECK(sequence); | 455 DCHECK(sequence); |
463 | 456 |
464 outer_->RemoveFromIdleWorkerThreadsStack(worker_thread); | 457 outer_->RemoveFromIdleWorkersStack(worker); |
465 return sequence; | 458 return sequence; |
466 } | 459 } |
467 | 460 |
468 void SchedulerWorkerPoolImpl::SchedulerWorkerThreadDelegateImpl:: | 461 void SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl:: |
469 ReEnqueueSequence(scoped_refptr<Sequence> sequence) { | 462 ReEnqueueSequence(scoped_refptr<Sequence> sequence) { |
470 if (last_sequence_is_single_threaded_) { | 463 if (last_sequence_is_single_threaded_) { |
471 // A single-threaded Sequence is always re-enqueued in the single-threaded | 464 // A single-threaded Sequence is always re-enqueued in the single-threaded |
472 // PriorityQueue from which it was extracted. | 465 // PriorityQueue from which it was extracted. |
473 const SequenceSortKey sequence_sort_key = sequence->GetSortKey(); | 466 const SequenceSortKey sequence_sort_key = sequence->GetSortKey(); |
474 single_threaded_priority_queue_.BeginTransaction()->Push( | 467 single_threaded_priority_queue_.BeginTransaction()->Push( |
475 std::move(sequence), sequence_sort_key); | 468 std::move(sequence), sequence_sort_key); |
476 } else { | 469 } else { |
477 // |re_enqueue_sequence_callback_| will determine in which PriorityQueue | 470 // |re_enqueue_sequence_callback_| will determine in which PriorityQueue |
478 // |sequence| must be enqueued. | 471 // |sequence| must be enqueued. |
479 re_enqueue_sequence_callback_.Run(std::move(sequence)); | 472 re_enqueue_sequence_callback_.Run(std::move(sequence)); |
480 } | 473 } |
481 } | 474 } |
482 | 475 |
483 TimeDelta SchedulerWorkerPoolImpl::SchedulerWorkerThreadDelegateImpl:: | 476 TimeDelta SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl:: |
484 GetSleepTimeout() { | 477 GetSleepTimeout() { |
485 return TimeDelta::Max(); | 478 return TimeDelta::Max(); |
486 } | 479 } |
487 | 480 |
488 SchedulerWorkerPoolImpl::SchedulerWorkerPoolImpl( | 481 SchedulerWorkerPoolImpl::SchedulerWorkerPoolImpl( |
489 StringPiece name, | 482 StringPiece name, |
490 IORestriction io_restriction, | 483 IORestriction io_restriction, |
491 TaskTracker* task_tracker, | 484 TaskTracker* task_tracker, |
492 DelayedTaskManager* delayed_task_manager) | 485 DelayedTaskManager* delayed_task_manager) |
493 : name_(name.as_string()), | 486 : name_(name.as_string()), |
494 io_restriction_(io_restriction), | 487 io_restriction_(io_restriction), |
495 idle_worker_threads_stack_lock_(shared_priority_queue_.container_lock()), | 488 idle_workers_stack_lock_(shared_priority_queue_.container_lock()), |
496 idle_worker_threads_stack_cv_for_testing_( | 489 idle_workers_stack_cv_for_testing_( |
497 idle_worker_threads_stack_lock_.CreateConditionVariable()), | 490 idle_workers_stack_lock_.CreateConditionVariable()), |
498 join_for_testing_returned_(WaitableEvent::ResetPolicy::MANUAL, | 491 join_for_testing_returned_(WaitableEvent::ResetPolicy::MANUAL, |
499 WaitableEvent::InitialState::NOT_SIGNALED), | 492 WaitableEvent::InitialState::NOT_SIGNALED), |
500 #if DCHECK_IS_ON() | 493 #if DCHECK_IS_ON() |
501 threads_created_(WaitableEvent::ResetPolicy::MANUAL, | 494 threads_created_(WaitableEvent::ResetPolicy::MANUAL, |
502 WaitableEvent::InitialState::NOT_SIGNALED), | 495 WaitableEvent::InitialState::NOT_SIGNALED), |
503 #endif | 496 #endif |
504 task_tracker_(task_tracker), | 497 task_tracker_(task_tracker), |
505 delayed_task_manager_(delayed_task_manager) { | 498 delayed_task_manager_(delayed_task_manager) { |
506 DCHECK(task_tracker_); | 499 DCHECK(task_tracker_); |
507 DCHECK(delayed_task_manager_); | 500 DCHECK(delayed_task_manager_); |
508 } | 501 } |
509 | 502 |
510 bool SchedulerWorkerPoolImpl::Initialize( | 503 bool SchedulerWorkerPoolImpl::Initialize( |
511 ThreadPriority thread_priority, | 504 ThreadPriority thread_priority, |
512 size_t max_threads, | 505 size_t max_threads, |
513 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback) { | 506 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback) { |
514 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 507 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); |
515 | 508 |
516 DCHECK(worker_threads_.empty()); | 509 DCHECK(workers_.empty()); |
517 | 510 |
518 for (size_t i = 0; i < max_threads; ++i) { | 511 for (size_t i = 0; i < max_threads; ++i) { |
519 std::unique_ptr<SchedulerWorkerThread> worker_thread = | 512 std::unique_ptr<SchedulerWorker> worker = |
520 SchedulerWorkerThread::Create( | 513 SchedulerWorker::Create( |
521 thread_priority, WrapUnique(new SchedulerWorkerThreadDelegateImpl( | 514 thread_priority, WrapUnique(new SchedulerWorkerDelegateImpl( |
522 this, re_enqueue_sequence_callback, | 515 this, re_enqueue_sequence_callback, |
523 &shared_priority_queue_, static_cast<int>(i))), | 516 &shared_priority_queue_, static_cast<int>(i))), |
524 task_tracker_); | 517 task_tracker_); |
525 if (!worker_thread) | 518 if (!worker) |
526 break; | 519 break; |
527 idle_worker_threads_stack_.Push(worker_thread.get()); | 520 idle_workers_stack_.Push(worker.get()); |
528 worker_threads_.push_back(std::move(worker_thread)); | 521 workers_.push_back(std::move(worker)); |
529 } | 522 } |
530 | 523 |
531 #if DCHECK_IS_ON() | 524 #if DCHECK_IS_ON() |
532 threads_created_.Signal(); | 525 threads_created_.Signal(); |
533 #endif | 526 #endif |
534 | 527 |
535 return !worker_threads_.empty(); | 528 return !workers_.empty(); |
536 } | 529 } |
537 | 530 |
538 void SchedulerWorkerPoolImpl::WakeUpOneWorker() { | 531 void SchedulerWorkerPoolImpl::WakeUpOneWorker() { |
539 SchedulerWorkerThread* worker_thread; | 532 SchedulerWorker* worker; |
540 { | 533 { |
541 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 534 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); |
542 worker_thread = idle_worker_threads_stack_.Pop(); | 535 worker = idle_workers_stack_.Pop(); |
543 } | 536 } |
544 if (worker_thread) | 537 if (worker) |
545 worker_thread->WakeUp(); | 538 worker->WakeUp(); |
546 } | 539 } |
547 | 540 |
548 void SchedulerWorkerPoolImpl::AddToIdleWorkerThreadsStack( | 541 void SchedulerWorkerPoolImpl::AddToIdleWorkersStack( |
549 SchedulerWorkerThread* worker_thread) { | 542 SchedulerWorker* worker) { |
550 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 543 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); |
551 idle_worker_threads_stack_.Push(worker_thread); | 544 idle_workers_stack_.Push(worker); |
552 DCHECK_LE(idle_worker_threads_stack_.Size(), worker_threads_.size()); | 545 DCHECK_LE(idle_workers_stack_.Size(), workers_.size()); |
553 | 546 |
554 if (idle_worker_threads_stack_.Size() == worker_threads_.size()) | 547 if (idle_workers_stack_.Size() == workers_.size()) |
555 idle_worker_threads_stack_cv_for_testing_->Broadcast(); | 548 idle_workers_stack_cv_for_testing_->Broadcast(); |
556 } | 549 } |
557 | 550 |
558 void SchedulerWorkerPoolImpl::RemoveFromIdleWorkerThreadsStack( | 551 void SchedulerWorkerPoolImpl::RemoveFromIdleWorkersStack( |
559 SchedulerWorkerThread* worker_thread) { | 552 SchedulerWorker* worker) { |
560 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 553 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); |
561 idle_worker_threads_stack_.Remove(worker_thread); | 554 idle_workers_stack_.Remove(worker); |
562 } | 555 } |
563 | 556 |
564 } // namespace internal | 557 } // namespace internal |
565 } // namespace base | 558 } // namespace base |
OLD | NEW |