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/atomicops.h" | 12 #include "base/atomicops.h" |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
18 #include "base/sequence_token.h" | 18 #include "base/sequence_token.h" |
19 #include "base/sequenced_task_runner.h" | |
20 #include "base/single_thread_task_runner.h" | |
gab
2016/10/31 19:10:11
(but here you'll want to keep them since you're us
robliao
2016/10/31 19:19:59
Not sure I follow here. The style guide permits us
gab
2016/10/31 19:28:45
Right but my other comment asks to remove from hea
robliao
2016/10/31 19:31:48
Gotcha. That other comment makes a lot more sense
fdoray
2016/10/31 19:44:51
Done.
| |
21 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
22 #include "base/task_scheduler/delayed_task_manager.h" | 20 #include "base/task_scheduler/delayed_task_manager.h" |
23 #include "base/task_scheduler/task_tracker.h" | 21 #include "base/task_scheduler/task_tracker.h" |
24 #include "base/threading/platform_thread.h" | 22 #include "base/threading/platform_thread.h" |
25 #include "base/threading/thread_local.h" | 23 #include "base/threading/thread_local.h" |
26 #include "base/threading/thread_restrictions.h" | 24 #include "base/threading/thread_restrictions.h" |
27 #include "base/time/time.h" | 25 #include "base/time/time.h" |
28 | 26 |
29 namespace base { | 27 namespace base { |
30 namespace internal { | 28 namespace internal { |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
334 | 332 |
335 DCHECK(!join_for_testing_returned_.IsSignaled()); | 333 DCHECK(!join_for_testing_returned_.IsSignaled()); |
336 join_for_testing_returned_.Signal(); | 334 join_for_testing_returned_.Signal(); |
337 } | 335 } |
338 | 336 |
339 void SchedulerWorkerPoolImpl::DisallowWorkerDetachmentForTesting() { | 337 void SchedulerWorkerPoolImpl::DisallowWorkerDetachmentForTesting() { |
340 worker_detachment_disallowed_.Set(); | 338 worker_detachment_disallowed_.Set(); |
341 } | 339 } |
342 | 340 |
343 scoped_refptr<TaskRunner> SchedulerWorkerPoolImpl::CreateTaskRunnerWithTraits( | 341 scoped_refptr<TaskRunner> SchedulerWorkerPoolImpl::CreateTaskRunnerWithTraits( |
344 const TaskTraits& traits, | 342 const TaskTraits& traits) { |
345 ExecutionMode execution_mode) { | 343 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this)); |
346 switch (execution_mode) { | 344 } |
347 case ExecutionMode::PARALLEL: | |
348 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this)); | |
349 | 345 |
350 case ExecutionMode::SEQUENCED: | 346 scoped_refptr<SequencedTaskRunner> |
351 return make_scoped_refptr(new SchedulerSequencedTaskRunner(traits, this)); | 347 SchedulerWorkerPoolImpl::CreateSequencedTaskRunnerWithTraits( |
348 const TaskTraits& traits) { | |
349 return make_scoped_refptr(new SchedulerSequencedTaskRunner(traits, this)); | |
350 } | |
352 | 351 |
353 case ExecutionMode::SINGLE_THREADED: { | 352 scoped_refptr<SingleThreadTaskRunner> |
354 // TODO(fdoray): Find a way to take load into account when assigning a | 353 SchedulerWorkerPoolImpl::CreateSingleThreadTaskRunnerWithTraits( |
355 // SchedulerWorker to a SingleThreadTaskRunner. Also, this code | 354 const TaskTraits& traits) { |
356 // assumes that all SchedulerWorkers are alive. Eventually, we might | 355 // TODO(fdoray): Find a way to take load into account when assigning a |
357 // decide to tear down threads that haven't run tasks for a long time. | 356 // SchedulerWorker to a SingleThreadTaskRunner. Also, this code assumes that |
358 size_t worker_index; | 357 // all SchedulerWorkers are alive. Eventually, we might decide to tear down |
359 { | 358 // threads that haven't run tasks for a long time. |
gab
2016/10/31 19:10:11
The second half of this comment is wrong with deta
fdoray
2016/10/31 19:44:51
Removed second part of the comment since I don't b
| |
360 AutoSchedulerLock auto_lock(next_worker_index_lock_); | 359 size_t worker_index; |
361 worker_index = next_worker_index_; | 360 { |
362 next_worker_index_ = (next_worker_index_ + 1) % workers_.size(); | 361 AutoSchedulerLock auto_lock(next_worker_index_lock_); |
363 } | 362 worker_index = next_worker_index_; |
364 return make_scoped_refptr(new SchedulerSingleThreadTaskRunner( | 363 next_worker_index_ = (next_worker_index_ + 1) % workers_.size(); |
365 traits, this, workers_[worker_index].get())); | |
366 } | |
367 } | 364 } |
368 | 365 return make_scoped_refptr(new SchedulerSingleThreadTaskRunner( |
369 NOTREACHED(); | 366 traits, this, workers_[worker_index].get())); |
370 return nullptr; | |
371 } | 367 } |
372 | 368 |
373 void SchedulerWorkerPoolImpl::ReEnqueueSequence( | 369 void SchedulerWorkerPoolImpl::ReEnqueueSequence( |
374 scoped_refptr<Sequence> sequence, | 370 scoped_refptr<Sequence> sequence, |
375 const SequenceSortKey& sequence_sort_key) { | 371 const SequenceSortKey& sequence_sort_key) { |
376 shared_priority_queue_.BeginTransaction()->Push(std::move(sequence), | 372 shared_priority_queue_.BeginTransaction()->Push(std::move(sequence), |
377 sequence_sort_key); | 373 sequence_sort_key); |
378 | 374 |
379 // The thread calling this method just ran a Task from |sequence| and will | 375 // The thread calling this method just ran a Task from |sequence| and will |
380 // soon try to get another Sequence from which to run a Task. If the thread | 376 // soon try to get another Sequence from which to run a Task. If the thread |
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
796 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); | 792 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); |
797 idle_workers_stack_.Remove(worker); | 793 idle_workers_stack_.Remove(worker); |
798 } | 794 } |
799 | 795 |
800 bool SchedulerWorkerPoolImpl::CanWorkerDetachForTesting() { | 796 bool SchedulerWorkerPoolImpl::CanWorkerDetachForTesting() { |
801 return !worker_detachment_disallowed_.IsSet(); | 797 return !worker_detachment_disallowed_.IsSet(); |
802 } | 798 } |
803 | 799 |
804 } // namespace internal | 800 } // namespace internal |
805 } // namespace base | 801 } // namespace base |
OLD | NEW |