Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: base/task_scheduler/task_scheduler_impl.cc

Issue 2405243003: TaskScheduler: Replace the SchedulerServiceThread with a base::Thread. (Closed)
Patch Set: self-review Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/task_scheduler_impl.h" 5 #include "base/task_scheduler/task_scheduler_impl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/task_scheduler/scheduler_service_thread.h"
13 #include "base/task_scheduler/scheduler_worker_pool_params.h" 12 #include "base/task_scheduler/scheduler_worker_pool_params.h"
14 #include "base/task_scheduler/sequence_sort_key.h" 13 #include "base/task_scheduler/sequence_sort_key.h"
15 #include "base/task_scheduler/task.h" 14 #include "base/task_scheduler/task.h"
16 #include "base/time/time.h" 15 #include "base/time/time.h"
17 16
18 namespace base { 17 namespace base {
19 namespace internal { 18 namespace internal {
20 19
21 // static 20 // static
22 std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create( 21 std::unique_ptr<TaskSchedulerImpl> TaskSchedulerImpl::Create(
(...skipping 12 matching lines...) Expand all
35 #endif 34 #endif
36 } 35 }
37 36
38 void TaskSchedulerImpl::PostTaskWithTraits( 37 void TaskSchedulerImpl::PostTaskWithTraits(
39 const tracked_objects::Location& from_here, 38 const tracked_objects::Location& from_here,
40 const TaskTraits& traits, 39 const TaskTraits& traits,
41 const Closure& task) { 40 const Closure& task) {
42 // Post |task| as part of a one-off single-task Sequence. 41 // Post |task| as part of a one-off single-task Sequence.
43 GetWorkerPoolForTraits(traits)->PostTaskWithSequence( 42 GetWorkerPoolForTraits(traits)->PostTaskWithSequence(
44 MakeUnique<Task>(from_here, task, traits, TimeDelta()), 43 MakeUnique<Task>(from_here, task, traits, TimeDelta()),
45 make_scoped_refptr(new Sequence), nullptr); 44 make_scoped_refptr(new Sequence), nullptr, TimeDelta());
46 } 45 }
47 46
48 scoped_refptr<TaskRunner> TaskSchedulerImpl::CreateTaskRunnerWithTraits( 47 scoped_refptr<TaskRunner> TaskSchedulerImpl::CreateTaskRunnerWithTraits(
49 const TaskTraits& traits, 48 const TaskTraits& traits,
50 ExecutionMode execution_mode) { 49 ExecutionMode execution_mode) {
51 return GetWorkerPoolForTraits(traits)->CreateTaskRunnerWithTraits( 50 return GetWorkerPoolForTraits(traits)->CreateTaskRunnerWithTraits(
52 traits, execution_mode); 51 traits, execution_mode);
53 } 52 }
54 53
55 void TaskSchedulerImpl::Shutdown() { 54 void TaskSchedulerImpl::Shutdown() {
56 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown. 55 // TODO(fdoray): Increase the priority of BACKGROUND tasks blocking shutdown.
57 task_tracker_.Shutdown(); 56 task_tracker_.Shutdown();
58 } 57 }
59 58
60 void TaskSchedulerImpl::FlushForTesting() { 59 void TaskSchedulerImpl::FlushForTesting() {
61 task_tracker_.Flush(); 60 task_tracker_.Flush();
62 } 61 }
63 62
64 void TaskSchedulerImpl::JoinForTesting() { 63 void TaskSchedulerImpl::JoinForTesting() {
65 #if DCHECK_IS_ON() 64 #if DCHECK_IS_ON()
66 DCHECK(!join_for_testing_returned_.IsSet()); 65 DCHECK(!join_for_testing_returned_.IsSet());
67 #endif 66 #endif
67 service_thread_.Stop();
robliao 2016/10/12 20:55:45 What's the motivation for this move? I would expec
fdoray 2016/10/13 13:51:29 I didn't like the fact that the service thread cou
68 for (const auto& worker_pool : worker_pools_) 68 for (const auto& worker_pool : worker_pools_)
69 worker_pool->JoinForTesting(); 69 worker_pool->JoinForTesting();
70 service_thread_->JoinForTesting();
71 #if DCHECK_IS_ON() 70 #if DCHECK_IS_ON()
72 join_for_testing_returned_.Set(); 71 join_for_testing_returned_.Set();
73 #endif 72 #endif
74 } 73 }
75 74
76 TaskSchedulerImpl::TaskSchedulerImpl(const WorkerPoolIndexForTraitsCallback& 75 TaskSchedulerImpl::TaskSchedulerImpl(const WorkerPoolIndexForTraitsCallback&
77 worker_pool_index_for_traits_callback) 76 worker_pool_index_for_traits_callback)
78 : delayed_task_manager_( 77 : service_thread_("TaskSchedulerServiceThread"),
79 Bind(&TaskSchedulerImpl::OnDelayedRunTimeUpdated, Unretained(this))),
80 worker_pool_index_for_traits_callback_( 78 worker_pool_index_for_traits_callback_(
81 worker_pool_index_for_traits_callback) 79 worker_pool_index_for_traits_callback) {
82 {
83 DCHECK(!worker_pool_index_for_traits_callback_.is_null()); 80 DCHECK(!worker_pool_index_for_traits_callback_.is_null());
84 } 81 }
85 82
86 void TaskSchedulerImpl::Initialize( 83 void TaskSchedulerImpl::Initialize(
87 const std::vector<SchedulerWorkerPoolParams>& worker_pool_params_vector) { 84 const std::vector<SchedulerWorkerPoolParams>& worker_pool_params_vector) {
88 DCHECK(!worker_pool_params_vector.empty()); 85 DCHECK(!worker_pool_params_vector.empty());
89 86
87 // Start the service thread.
88 constexpr MessageLoop::Type kServiceThreadMessageLoopType =
89 #if defined(OS_POSIX)
90 MessageLoop::TYPE_IO;
91 #else
92 MessageLoop::TYPE_DEFAULT;
93 #endif
94 constexpr size_t kDefaultStackSize = 0;
95 const bool service_thread_start_result = service_thread_.StartWithOptions(
robliao 2016/10/12 20:55:45 Inline this call to the CHECK below.
fdoray 2016/10/13 13:51:29 Done.
96 Thread::Options(kServiceThreadMessageLoopType, kDefaultStackSize));
97 CHECK(service_thread_start_result);
98
90 const SchedulerWorkerPoolImpl::ReEnqueueSequenceCallback 99 const SchedulerWorkerPoolImpl::ReEnqueueSequenceCallback
91 re_enqueue_sequence_callback = 100 re_enqueue_sequence_callback =
92 Bind(&TaskSchedulerImpl::ReEnqueueSequenceCallback, Unretained(this)); 101 Bind(&TaskSchedulerImpl::ReEnqueueSequenceCallback, Unretained(this));
93 102
103 // Start worker pools.
94 for (const auto& worker_pool_params : worker_pool_params_vector) { 104 for (const auto& worker_pool_params : worker_pool_params_vector) {
95 // Passing pointers to objects owned by |this| to 105 // Passing pointers to objects owned by |this| to
96 // SchedulerWorkerPoolImpl::Create() is safe because a TaskSchedulerImpl 106 // SchedulerWorkerPoolImpl::Create() is safe because a TaskSchedulerImpl
97 // can't be deleted before all its worker pools have been joined. 107 // can't be deleted before all its worker pools have been joined.
98 worker_pools_.push_back(SchedulerWorkerPoolImpl::Create( 108 worker_pools_.push_back(SchedulerWorkerPoolImpl::Create(
99 worker_pool_params, re_enqueue_sequence_callback, &task_tracker_, 109 worker_pool_params, re_enqueue_sequence_callback, &task_tracker_,
100 &delayed_task_manager_)); 110 service_thread_.task_runner()));
101 CHECK(worker_pools_.back()); 111 CHECK(worker_pools_.back());
102 } 112 }
103
104 service_thread_ = SchedulerServiceThread::Create(&task_tracker_,
105 &delayed_task_manager_);
106 CHECK(service_thread_);
107 } 113 }
108 114
109 SchedulerWorkerPool* TaskSchedulerImpl::GetWorkerPoolForTraits( 115 SchedulerWorkerPool* TaskSchedulerImpl::GetWorkerPoolForTraits(
110 const TaskTraits& traits) { 116 const TaskTraits& traits) {
111 const size_t index = worker_pool_index_for_traits_callback_.Run(traits); 117 const size_t index = worker_pool_index_for_traits_callback_.Run(traits);
112 DCHECK_LT(index, worker_pools_.size()); 118 DCHECK_LT(index, worker_pools_.size());
113 return worker_pools_[index].get(); 119 return worker_pools_[index].get();
114 } 120 }
115 121
116 void TaskSchedulerImpl::ReEnqueueSequenceCallback( 122 void TaskSchedulerImpl::ReEnqueueSequenceCallback(
117 scoped_refptr<Sequence> sequence) { 123 scoped_refptr<Sequence> sequence) {
118 DCHECK(sequence); 124 DCHECK(sequence);
119 125
120 const SequenceSortKey sort_key = sequence->GetSortKey(); 126 const SequenceSortKey sort_key = sequence->GetSortKey();
121 127
122 // The next task in |sequence| should run in a worker pool suited for its 128 // The next task in |sequence| should run in a worker pool suited for its
123 // traits, except for the priority which is adjusted to the highest priority 129 // traits, except for the priority which is adjusted to the highest priority
124 // in |sequence|. 130 // in |sequence|.
125 const TaskTraits traits = 131 const TaskTraits traits =
126 sequence->PeekTaskTraits().WithPriority(sort_key.priority()); 132 sequence->PeekTaskTraits().WithPriority(sort_key.priority());
127 133
128 GetWorkerPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence), 134 GetWorkerPoolForTraits(traits)->ReEnqueueSequence(std::move(sequence),
129 sort_key); 135 sort_key);
130 } 136 }
131 137
132 void TaskSchedulerImpl::OnDelayedRunTimeUpdated() {
133 service_thread_->WakeUp();
134 }
135
136 } // namespace internal 138 } // namespace internal
137 } // namespace base 139 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698