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_thread_pool_impl.h" | 5 #include "base/task_scheduler/scheduler_thread_pool_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/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
15 #include "base/single_thread_task_runner.h" | |
15 #include "base/task_scheduler/delayed_task_manager.h" | 16 #include "base/task_scheduler/delayed_task_manager.h" |
16 #include "base/task_scheduler/task_tracker.h" | 17 #include "base/task_scheduler/task_tracker.h" |
17 #include "base/threading/thread_local.h" | 18 #include "base/threading/thread_local.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 namespace internal { | 21 namespace internal { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 // SchedulerThreadPool that owns the current thread, if any. | 25 // SchedulerThreadPool that owns the current thread, if any. |
25 LazyInstance<ThreadLocalPointer<const SchedulerThreadPool>>::Leaky | 26 LazyInstance<ThreadLocalPointer<const SchedulerThreadPool>>::Leaky |
26 tls_current_thread_pool = LAZY_INSTANCE_INITIALIZER; | 27 tls_current_thread_pool = LAZY_INSTANCE_INITIALIZER; |
27 | 28 |
29 // SchedulerWorkerThread that owns the current thread, if any. | |
30 LazyInstance<ThreadLocalPointer<const SchedulerWorkerThread>>::Leaky | |
31 tls_current_worker_thread = LAZY_INSTANCE_INITIALIZER; | |
32 | |
28 // A task runner that runs tasks with the PARALLEL ExecutionMode. | 33 // A task runner that runs tasks with the PARALLEL ExecutionMode. |
29 class SchedulerParallelTaskRunner : public TaskRunner { | 34 class SchedulerParallelTaskRunner : public TaskRunner { |
30 public: | 35 public: |
31 // Constructs a SchedulerParallelTaskRunner which can be used to post tasks so | 36 // Constructs a SchedulerParallelTaskRunner which can be used to post tasks so |
32 // long as |thread_pool| is alive. | 37 // long as |thread_pool| is alive. |
33 // TODO(robliao): Find a concrete way to manage |thread_pool|'s memory. | 38 // TODO(robliao): Find a concrete way to manage |thread_pool|'s memory. |
34 SchedulerParallelTaskRunner(const TaskTraits& traits, | 39 SchedulerParallelTaskRunner(const TaskTraits& traits, |
35 SchedulerThreadPool* thread_pool) | 40 SchedulerThreadPool* thread_pool) |
36 : traits_(traits), thread_pool_(thread_pool) {} | 41 : traits_(traits), thread_pool_(thread_pool) {} |
37 | 42 |
38 // TaskRunner: | 43 // TaskRunner: |
39 bool PostDelayedTask(const tracked_objects::Location& from_here, | 44 bool PostDelayedTask(const tracked_objects::Location& from_here, |
40 const Closure& closure, | 45 const Closure& closure, |
41 TimeDelta delay) override { | 46 TimeDelta delay) override { |
42 // Post the task as part of a one-off single-task Sequence. | 47 // Post the task as part of a one-off single-task Sequence. |
43 return thread_pool_->PostTaskWithSequence( | 48 return thread_pool_->PostTaskWithSequence( |
44 WrapUnique(new Task(from_here, closure, traits_, delay)), | 49 WrapUnique(new Task(from_here, closure, traits_, delay)), |
45 make_scoped_refptr(new Sequence)); | 50 make_scoped_refptr(new Sequence), nullptr); |
46 } | 51 } |
47 | 52 |
48 bool RunsTasksOnCurrentThread() const override { | 53 bool RunsTasksOnCurrentThread() const override { |
49 return tls_current_thread_pool.Get().Get() == thread_pool_; | 54 return tls_current_thread_pool.Get().Get() == thread_pool_; |
50 } | 55 } |
51 | 56 |
52 private: | 57 private: |
53 ~SchedulerParallelTaskRunner() override = default; | 58 ~SchedulerParallelTaskRunner() override = default; |
54 | 59 |
55 const TaskTraits traits_; | 60 const TaskTraits traits_; |
56 SchedulerThreadPool* const thread_pool_; | 61 SchedulerThreadPool* const thread_pool_; |
57 | 62 |
58 DISALLOW_COPY_AND_ASSIGN(SchedulerParallelTaskRunner); | 63 DISALLOW_COPY_AND_ASSIGN(SchedulerParallelTaskRunner); |
59 }; | 64 }; |
60 | 65 |
61 // A task runner that runs tasks with the SEQUENCED ExecutionMode. | 66 // A task runner that runs tasks with the SEQUENCED ExecutionMode. |
62 class SchedulerSequencedTaskRunner : public SequencedTaskRunner { | 67 class SchedulerSequencedTaskRunner : public SequencedTaskRunner { |
63 public: | 68 public: |
64 // Constructs a SchedulerSequencedTaskRunner which can be used to post tasks | 69 // Constructs a SchedulerSequencedTaskRunner which can be used to post tasks |
65 // so long as |thread_pool| is alive. | 70 // so long as |thread_pool| is alive. |
66 // TODO(robliao): Find a concrete way to manage |thread_pool|'s memory. | 71 // TODO(robliao): Find a concrete way to manage |thread_pool|'s memory. |
67 SchedulerSequencedTaskRunner(const TaskTraits& traits, | 72 SchedulerSequencedTaskRunner(const TaskTraits& traits, |
68 SchedulerThreadPool* thread_pool) | 73 SchedulerThreadPool* thread_pool) |
69 : traits_(traits), thread_pool_(thread_pool) {} | 74 : traits_(traits), thread_pool_(thread_pool) {} |
70 | 75 |
71 // SequencedTaskRunner: | 76 // SequencedTaskRunner: |
72 bool PostDelayedTask(const tracked_objects::Location& from_here, | 77 bool PostDelayedTask(const tracked_objects::Location& from_here, |
73 const Closure& closure, | 78 const Closure& closure, |
74 TimeDelta delay) override { | 79 TimeDelta delay) override { |
75 // Post the task as part of |sequence|. | 80 // Post the task as part of |sequence_|. |
76 return thread_pool_->PostTaskWithSequence( | 81 return thread_pool_->PostTaskWithSequence( |
77 WrapUnique(new Task(from_here, closure, traits_, delay)), sequence_); | 82 WrapUnique(new Task(from_here, closure, traits_, delay)), sequence_, |
83 nullptr); | |
78 } | 84 } |
79 | 85 |
80 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 86 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
81 const Closure& closure, | 87 const Closure& closure, |
82 base::TimeDelta delay) override { | 88 base::TimeDelta delay) override { |
83 // Tasks are never nested within the task scheduler. | 89 // Tasks are never nested within the task scheduler. |
84 return PostDelayedTask(from_here, closure, delay); | 90 return PostDelayedTask(from_here, closure, delay); |
85 } | 91 } |
86 | 92 |
87 bool RunsTasksOnCurrentThread() const override { | 93 bool RunsTasksOnCurrentThread() const override { |
88 return tls_current_thread_pool.Get().Get() == thread_pool_; | 94 return tls_current_thread_pool.Get().Get() == thread_pool_; |
89 } | 95 } |
90 | 96 |
91 private: | 97 private: |
92 ~SchedulerSequencedTaskRunner() override = default; | 98 ~SchedulerSequencedTaskRunner() override = default; |
93 | 99 |
94 // Sequence for all Tasks posted through this TaskRunner. | 100 // Sequence for all Tasks posted through this TaskRunner. |
95 const scoped_refptr<Sequence> sequence_ = new Sequence; | 101 const scoped_refptr<Sequence> sequence_ = new Sequence; |
96 | 102 |
97 const TaskTraits traits_; | 103 const TaskTraits traits_; |
98 SchedulerThreadPool* const thread_pool_; | 104 SchedulerThreadPool* const thread_pool_; |
99 | 105 |
100 DISALLOW_COPY_AND_ASSIGN(SchedulerSequencedTaskRunner); | 106 DISALLOW_COPY_AND_ASSIGN(SchedulerSequencedTaskRunner); |
101 }; | 107 }; |
102 | 108 |
109 // A task runner that runs tasks with the SINGLE_THREADED ExecutionMode. | |
110 class SchedulerSingleThreadTaskRunner : public SingleThreadTaskRunner { | |
111 public: | |
112 // Constructs a SchedulerSingleThreadTaskRunner which can be used to post | |
113 // tasks so long as |thread_pool| and |worker_thread| are alive. | |
114 // TODO(robliao): Find a concrete way to manage the memory of |thread_pool| | |
115 // and |worker_thread|. | |
116 SchedulerSingleThreadTaskRunner(const TaskTraits& traits, | |
117 SchedulerThreadPool* thread_pool, | |
118 SchedulerWorkerThread* worker_thread) | |
119 : traits_(traits), | |
120 thread_pool_(thread_pool), | |
121 worker_thread_(worker_thread) {} | |
122 | |
123 // SingleThreadTaskRunner: | |
124 bool PostDelayedTask(const tracked_objects::Location& from_here, | |
125 const Closure& closure, | |
126 TimeDelta delay) override { | |
127 // Post the task to be executed by |worker_thread_| as part of |sequence_|. | |
128 return thread_pool_->PostTaskWithSequence( | |
129 WrapUnique(new Task(from_here, closure, traits_, delay)), sequence_, | |
130 worker_thread_); | |
131 } | |
132 | |
133 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | |
134 const Closure& closure, | |
135 base::TimeDelta delay) override { | |
136 // Tasks are never nested within the task scheduler. | |
137 return PostDelayedTask(from_here, closure, delay); | |
138 } | |
139 | |
140 bool RunsTasksOnCurrentThread() const override { | |
141 return tls_current_worker_thread.Get().Get() == worker_thread_; | |
142 } | |
143 | |
144 private: | |
145 ~SchedulerSingleThreadTaskRunner() override = default; | |
146 | |
147 // Sequence for all Tasks posted through this TaskRunner. | |
148 const scoped_refptr<Sequence> sequence_ = new Sequence; | |
149 | |
150 const TaskTraits traits_; | |
151 SchedulerThreadPool* const thread_pool_; | |
152 SchedulerWorkerThread* const worker_thread_; | |
153 | |
154 DISALLOW_COPY_AND_ASSIGN(SchedulerSingleThreadTaskRunner); | |
155 }; | |
156 | |
103 } // namespace | 157 } // namespace |
104 | 158 |
105 class SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl | 159 class SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl |
106 : public SchedulerWorkerThread::Delegate { | 160 : public SchedulerWorkerThread::Delegate { |
107 public: | 161 public: |
108 SchedulerWorkerThreadDelegateImpl( | 162 SchedulerWorkerThreadDelegateImpl( |
109 SchedulerThreadPoolImpl* outer, | 163 SchedulerThreadPoolImpl* outer, |
164 PriorityQueue* single_threaded_priority_queue, | |
110 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); | 165 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback); |
111 ~SchedulerWorkerThreadDelegateImpl() override; | 166 ~SchedulerWorkerThreadDelegateImpl() override; |
112 | 167 |
113 // SchedulerWorkerThread::Delegate: | 168 // SchedulerWorkerThread::Delegate: |
114 void OnMainEntry() override; | 169 void OnMainEntry(SchedulerWorkerThread* worker_thread) override; |
115 scoped_refptr<Sequence> GetWork( | 170 scoped_refptr<Sequence> GetWork( |
116 SchedulerWorkerThread* worker_thread) override; | 171 SchedulerWorkerThread* worker_thread) override; |
117 void ReEnqueueSequence(scoped_refptr<Sequence> sequence) override; | 172 void ReEnqueueSequence(scoped_refptr<Sequence> sequence) override; |
118 | 173 |
119 private: | 174 private: |
120 SchedulerThreadPoolImpl* outer_; | 175 SchedulerThreadPoolImpl* outer_; |
176 PriorityQueue* const single_threaded_priority_queue_; | |
121 const ReEnqueueSequenceCallback re_enqueue_sequence_callback_; | 177 const ReEnqueueSequenceCallback re_enqueue_sequence_callback_; |
122 | 178 |
179 // True if the last Sequence returned by GetWork() was extracted from | |
180 // |single_threaded_priority_queue_|. | |
181 bool last_sequence_is_single_threaded_ = false; | |
182 | |
123 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThreadDelegateImpl); | 183 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThreadDelegateImpl); |
124 }; | 184 }; |
125 | 185 |
126 SchedulerThreadPoolImpl::~SchedulerThreadPoolImpl() { | 186 SchedulerThreadPoolImpl::~SchedulerThreadPoolImpl() { |
127 // SchedulerThreadPool should never be deleted in production unless its | 187 // SchedulerThreadPool should never be deleted in production unless its |
128 // initialization failed. | 188 // initialization failed. |
129 DCHECK(join_for_testing_returned_.IsSignaled() || worker_threads_.empty()); | 189 DCHECK(join_for_testing_returned_.IsSignaled() || worker_threads_.empty()); |
130 } | 190 } |
131 | 191 |
132 std::unique_ptr<SchedulerThreadPoolImpl> SchedulerThreadPoolImpl::Create( | 192 std::unique_ptr<SchedulerThreadPoolImpl> SchedulerThreadPoolImpl::Create( |
(...skipping 28 matching lines...) Expand all Loading... | |
161 scoped_refptr<TaskRunner> SchedulerThreadPoolImpl::CreateTaskRunnerWithTraits( | 221 scoped_refptr<TaskRunner> SchedulerThreadPoolImpl::CreateTaskRunnerWithTraits( |
162 const TaskTraits& traits, | 222 const TaskTraits& traits, |
163 ExecutionMode execution_mode) { | 223 ExecutionMode execution_mode) { |
164 switch (execution_mode) { | 224 switch (execution_mode) { |
165 case ExecutionMode::PARALLEL: | 225 case ExecutionMode::PARALLEL: |
166 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this)); | 226 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this)); |
167 | 227 |
168 case ExecutionMode::SEQUENCED: | 228 case ExecutionMode::SEQUENCED: |
169 return make_scoped_refptr(new SchedulerSequencedTaskRunner(traits, this)); | 229 return make_scoped_refptr(new SchedulerSequencedTaskRunner(traits, this)); |
170 | 230 |
171 case ExecutionMode::SINGLE_THREADED: | 231 case ExecutionMode::SINGLE_THREADED: { |
172 // TODO(fdoray): Support SINGLE_THREADED TaskRunners. | 232 // TODO(fdoray): Find a better way to assign a worker thread to a |
173 NOTREACHED(); | 233 // SingleThreadTaskRunner. |
gab
2016/04/25 21:00:20
Expand this TODO to mention that this currently as
fdoray
2016/04/25 22:34:44
This code doesn't assume that all SchedulerWorkerT
gab
2016/04/26 11:46:27
This makes for a heavy WakeUp(), our design doc st
fdoray
2016/04/26 14:56:47
Added TODO requested in your initial comment. It's
| |
174 return nullptr; | 234 size_t worker_thread_index; |
235 { | |
236 AutoSchedulerLock auto_lock(next_worker_thread_index_lock_); | |
237 worker_thread_index = next_worker_thread_index_; | |
238 next_worker_thread_index_ = | |
239 (next_worker_thread_index_ + 1) % worker_threads_.size(); | |
240 } | |
241 return make_scoped_refptr(new SchedulerSingleThreadTaskRunner( | |
242 traits, this, worker_threads_[worker_thread_index].get())); | |
243 } | |
175 } | 244 } |
176 | 245 |
177 NOTREACHED(); | 246 NOTREACHED(); |
178 return nullptr; | 247 return nullptr; |
179 } | 248 } |
180 | 249 |
181 void SchedulerThreadPoolImpl::ReEnqueueSequence( | 250 void SchedulerThreadPoolImpl::ReEnqueueSequence( |
182 scoped_refptr<Sequence> sequence, | 251 scoped_refptr<Sequence> sequence, |
183 const SequenceSortKey& sequence_sort_key) { | 252 const SequenceSortKey& sequence_sort_key) { |
184 shared_priority_queue_.BeginTransaction()->Push( | 253 shared_priority_queue_.BeginTransaction()->Push( |
185 WrapUnique(new PriorityQueue::SequenceAndSortKey(std::move(sequence), | 254 WrapUnique(new PriorityQueue::SequenceAndSortKey(std::move(sequence), |
186 sequence_sort_key))); | 255 sequence_sort_key))); |
187 | 256 |
188 // The thread calling this method just ran a Task from |sequence| and will | 257 // The thread calling this method just ran a Task from |sequence| and will |
189 // soon try to get another Sequence from which to run a Task. If the thread | 258 // soon try to get another Sequence from which to run a Task. If the thread |
190 // belongs to this pool, it will get that Sequence from | 259 // belongs to this pool, it will get that Sequence from |
191 // |shared_priority_queue_|. When that's the case, there is no need to wake up | 260 // |shared_priority_queue_|. When that's the case, there is no need to wake up |
192 // another thread after |sequence| is inserted in |shared_priority_queue_|. If | 261 // another thread after |sequence| is inserted in |shared_priority_queue_|. If |
193 // we did wake up another thread, we would waste resources by having more | 262 // we did wake up another thread, we would waste resources by having more |
194 // threads trying to get a Sequence from |shared_priority_queue_| than the | 263 // threads trying to get a Sequence from |shared_priority_queue_| than the |
195 // number of Sequences in it. | 264 // number of Sequences in it. |
196 if (tls_current_thread_pool.Get().Get() != this) | 265 if (tls_current_thread_pool.Get().Get() != this) |
197 WakeUpOneThread(); | 266 WakeUpOneThread(); |
198 } | 267 } |
199 | 268 |
200 bool SchedulerThreadPoolImpl::PostTaskWithSequence( | 269 bool SchedulerThreadPoolImpl::PostTaskWithSequence( |
201 std::unique_ptr<Task> task, | 270 std::unique_ptr<Task> task, |
202 scoped_refptr<Sequence> sequence) { | 271 scoped_refptr<Sequence> sequence, |
272 SchedulerWorkerThread* worker_thread) { | |
203 DCHECK(task); | 273 DCHECK(task); |
204 DCHECK(sequence); | 274 DCHECK(sequence); |
205 | 275 |
206 if (!task_tracker_->WillPostTask(task.get())) | 276 if (!task_tracker_->WillPostTask(task.get())) |
207 return false; | 277 return false; |
208 | 278 |
209 if (task->delayed_run_time.is_null()) { | 279 if (task->delayed_run_time.is_null()) { |
210 PostTaskWithSequenceNow(std::move(task), std::move(sequence)); | 280 PostTaskWithSequenceNow(std::move(task), std::move(sequence), |
281 worker_thread); | |
211 } else { | 282 } else { |
212 delayed_task_manager_->AddDelayedTask(std::move(task), std::move(sequence), | 283 delayed_task_manager_->AddDelayedTask(std::move(task), std::move(sequence), |
213 this); | 284 worker_thread, this); |
214 } | 285 } |
215 | 286 |
216 return true; | 287 return true; |
217 } | 288 } |
218 | 289 |
219 void SchedulerThreadPoolImpl::PostTaskWithSequenceNow( | 290 void SchedulerThreadPoolImpl::PostTaskWithSequenceNow( |
220 std::unique_ptr<Task> task, | 291 std::unique_ptr<Task> task, |
221 scoped_refptr<Sequence> sequence) { | 292 scoped_refptr<Sequence> sequence, |
293 SchedulerWorkerThread* worker_thread) { | |
222 DCHECK(task); | 294 DCHECK(task); |
223 DCHECK(sequence); | 295 DCHECK(sequence); |
224 | 296 |
225 // Confirm that |task| is ready to run (its delayed run time is either null or | 297 // Confirm that |task| is ready to run (its delayed run time is either null or |
226 // in the past). | 298 // in the past). |
227 DCHECK_LE(task->delayed_run_time, delayed_task_manager_->Now()); | 299 DCHECK_LE(task->delayed_run_time, delayed_task_manager_->Now()); |
228 | 300 |
301 PriorityQueue* const priority_queue = | |
302 worker_thread ? single_threaded_priority_queues_[worker_thread].get() | |
303 : &shared_priority_queue_; | |
304 DCHECK(priority_queue); | |
305 | |
229 const bool sequence_was_empty = sequence->PushTask(std::move(task)); | 306 const bool sequence_was_empty = sequence->PushTask(std::move(task)); |
230 if (sequence_was_empty) { | 307 if (sequence_was_empty) { |
231 // Insert |sequence| in |shared_priority_queue_| if it was empty before | 308 // Insert |sequence| in |priority_queue| if it was empty before |task| was |
232 // |task| was inserted into it. Otherwise, one of these must be true: | 309 // inserted into it. Otherwise, one of these must be true: |
233 // - |sequence| is already in a PriorityQueue (not necessarily | 310 // - |sequence| is already in a PriorityQueue (not necessarily |
234 // |shared_priority_queue_|), or, | 311 // |shared_priority_queue_|), or, |
235 // - A worker thread is running a Task from |sequence|. It will insert | 312 // - A worker thread is running a Task from |sequence|. It will insert |
236 // |sequence| in a PriorityQueue once it's done running the Task. | 313 // |sequence| in a PriorityQueue once it's done running the Task. |
237 const auto sequence_sort_key = sequence->GetSortKey(); | 314 const auto sequence_sort_key = sequence->GetSortKey(); |
238 shared_priority_queue_.BeginTransaction()->Push( | 315 priority_queue->BeginTransaction()->Push( |
239 WrapUnique(new PriorityQueue::SequenceAndSortKey(std::move(sequence), | 316 WrapUnique(new PriorityQueue::SequenceAndSortKey(std::move(sequence), |
240 sequence_sort_key))); | 317 sequence_sort_key))); |
241 | 318 |
242 // Wake up a worker thread to process |sequence|. | 319 // Wake up a worker thread to process |sequence|. |
243 WakeUpOneThread(); | 320 if (worker_thread) |
321 worker_thread->WakeUp(); | |
322 else | |
323 WakeUpOneThread(); | |
244 } | 324 } |
245 } | 325 } |
246 | 326 |
247 SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: | 327 SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: |
248 SchedulerWorkerThreadDelegateImpl( | 328 SchedulerWorkerThreadDelegateImpl( |
249 SchedulerThreadPoolImpl* outer, | 329 SchedulerThreadPoolImpl* outer, |
330 PriorityQueue* single_threaded_priority_queue, | |
250 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback) | 331 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback) |
251 : outer_(outer), | 332 : outer_(outer), |
333 single_threaded_priority_queue_(single_threaded_priority_queue), | |
252 re_enqueue_sequence_callback_(re_enqueue_sequence_callback) {} | 334 re_enqueue_sequence_callback_(re_enqueue_sequence_callback) {} |
253 | 335 |
254 SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: | 336 SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: |
255 ~SchedulerWorkerThreadDelegateImpl() = default; | 337 ~SchedulerWorkerThreadDelegateImpl() = default; |
256 | 338 |
257 void SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl::OnMainEntry() { | 339 void SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl::OnMainEntry( |
340 SchedulerWorkerThread* worker_thread) { | |
341 DCHECK(!tls_current_worker_thread.Get().Get()); | |
258 DCHECK(!tls_current_thread_pool.Get().Get()); | 342 DCHECK(!tls_current_thread_pool.Get().Get()); |
343 tls_current_worker_thread.Get().Set(worker_thread); | |
259 tls_current_thread_pool.Get().Set(outer_); | 344 tls_current_thread_pool.Get().Set(outer_); |
260 } | 345 } |
261 | 346 |
262 scoped_refptr<Sequence> | 347 scoped_refptr<Sequence> |
263 SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl::GetWork( | 348 SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl::GetWork( |
264 SchedulerWorkerThread* worker_thread) { | 349 SchedulerWorkerThread* worker_thread) { |
265 std::unique_ptr<PriorityQueue::Transaction> transaction( | 350 std::unique_ptr<PriorityQueue::Transaction> shared_transaction( |
266 outer_->shared_priority_queue_.BeginTransaction()); | 351 outer_->shared_priority_queue_.BeginTransaction()); |
267 const auto& sequence_and_sort_key = transaction->Peek(); | 352 const auto& shared_sequence_and_sort_key = shared_transaction->Peek(); |
268 | 353 |
269 if (sequence_and_sort_key.is_null()) { | 354 std::unique_ptr<PriorityQueue::Transaction> single_threaded_transaction( |
270 // |transaction| is kept alive while |worker_thread| is added to | 355 single_threaded_priority_queue_->BeginTransaction()); |
356 const auto& single_threaded_sequence_and_sort_key = | |
357 single_threaded_transaction->Peek(); | |
358 | |
359 if (shared_sequence_and_sort_key.is_null() && | |
360 single_threaded_sequence_and_sort_key.is_null()) { | |
361 single_threaded_transaction.reset(); | |
362 | |
363 // |shared_transaction| is kept alive while |worker_thread| is added to | |
271 // |idle_worker_threads_stack_| to avoid this race: | 364 // |idle_worker_threads_stack_| to avoid this race: |
272 // 1. This thread creates a Transaction, finds |shared_priority_queue_| | 365 // 1. This thread creates a Transaction, finds |shared_priority_queue_| |
273 // empty and ends the Transaction. | 366 // empty and ends the Transaction. |
274 // 2. Other thread creates a Transaction, inserts a Sequence into | 367 // 2. Other thread creates a Transaction, inserts a Sequence into |
275 // |shared_priority_queue_| and ends the Transaction. This can't happen | 368 // |shared_priority_queue_| and ends the Transaction. This can't happen |
276 // if the Transaction of step 1 is still active because because there can | 369 // if the Transaction of step 1 is still active because because there can |
277 // only be one active Transaction per PriorityQueue at a time. | 370 // only be one active Transaction per PriorityQueue at a time. |
278 // 3. Other thread calls WakeUpOneThread(). No thread is woken up because | 371 // 3. Other thread calls WakeUpOneThread(). No thread is woken up because |
279 // |idle_worker_threads_stack_| is empty. | 372 // |idle_worker_threads_stack_| is empty. |
280 // 4. This thread adds itself to |idle_worker_threads_stack_| and goes to | 373 // 4. This thread adds itself to |idle_worker_threads_stack_| and goes to |
281 // sleep. No thread runs the Sequence inserted in step 2. | 374 // sleep. No thread runs the Sequence inserted in step 2. |
282 outer_->AddToIdleWorkerThreadsStack(worker_thread); | 375 outer_->AddToIdleWorkerThreadsStack(worker_thread); |
283 return nullptr; | 376 return nullptr; |
284 } | 377 } |
285 | 378 |
286 scoped_refptr<Sequence> sequence = sequence_and_sort_key.sequence; | 379 scoped_refptr<Sequence> sequence; |
287 transaction->Pop(); | 380 |
381 if (single_threaded_sequence_and_sort_key.is_null() || | |
382 (!shared_sequence_and_sort_key.is_null() && | |
383 single_threaded_sequence_and_sort_key.sort_key < | |
384 shared_sequence_and_sort_key.sort_key)) { | |
385 sequence = shared_sequence_and_sort_key.sequence; | |
386 shared_transaction->Pop(); | |
387 last_sequence_is_single_threaded_ = false; | |
388 } else { | |
389 DCHECK(!single_threaded_sequence_and_sort_key.is_null()); | |
390 sequence = single_threaded_sequence_and_sort_key.sequence; | |
391 single_threaded_transaction->Pop(); | |
392 last_sequence_is_single_threaded_ = true; | |
393 } | |
394 | |
395 shared_transaction.reset(); | |
396 single_threaded_transaction.reset(); | |
gab
2016/04/25 21:00:20
Add an inner-scope here for the above code instead
| |
397 outer_->RemoveFromIdleWorkerThreadsStack(worker_thread); | |
398 | |
288 return sequence; | 399 return sequence; |
289 } | 400 } |
290 | 401 |
291 void SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: | 402 void SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: |
292 ReEnqueueSequence(scoped_refptr<Sequence> sequence) { | 403 ReEnqueueSequence(scoped_refptr<Sequence> sequence) { |
293 re_enqueue_sequence_callback_.Run(std::move(sequence)); | 404 if (last_sequence_is_single_threaded_) { |
405 // A single-threaded Sequence is always re-enqueued in the single-threaded | |
406 // PriorityQueue from which it was extracted. | |
407 const SequenceSortKey sequence_sort_key = sequence->GetSortKey(); | |
408 single_threaded_priority_queue_->BeginTransaction()->Push( | |
409 WrapUnique(new PriorityQueue::SequenceAndSortKey(std::move(sequence), | |
410 sequence_sort_key))); | |
411 } else { | |
412 // |re_enqueue_sequence_callback_| will determine in which PriorityQueue | |
413 // |sequence| must be enqueued. | |
414 re_enqueue_sequence_callback_.Run(std::move(sequence)); | |
415 } | |
294 } | 416 } |
295 | 417 |
296 SchedulerThreadPoolImpl::SchedulerThreadPoolImpl( | 418 SchedulerThreadPoolImpl::SchedulerThreadPoolImpl( |
297 TaskTracker* task_tracker, | 419 TaskTracker* task_tracker, |
298 DelayedTaskManager* delayed_task_manager) | 420 DelayedTaskManager* delayed_task_manager) |
299 : idle_worker_threads_stack_lock_(shared_priority_queue_.container_lock()), | 421 : idle_worker_threads_stack_lock_(shared_priority_queue_.container_lock()), |
300 idle_worker_threads_stack_cv_for_testing_( | 422 idle_worker_threads_stack_cv_for_testing_( |
301 idle_worker_threads_stack_lock_.CreateConditionVariable()), | 423 idle_worker_threads_stack_lock_.CreateConditionVariable()), |
302 join_for_testing_returned_(true, false), | 424 join_for_testing_returned_(true, false), |
303 task_tracker_(task_tracker), | 425 task_tracker_(task_tracker), |
304 delayed_task_manager_(delayed_task_manager) { | 426 delayed_task_manager_(delayed_task_manager) { |
305 DCHECK(task_tracker_); | 427 DCHECK(task_tracker_); |
306 DCHECK(delayed_task_manager_); | 428 DCHECK(delayed_task_manager_); |
307 } | 429 } |
308 | 430 |
309 bool SchedulerThreadPoolImpl::Initialize( | 431 bool SchedulerThreadPoolImpl::Initialize( |
310 ThreadPriority thread_priority, | 432 ThreadPriority thread_priority, |
311 size_t max_threads, | 433 size_t max_threads, |
312 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback) { | 434 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback) { |
313 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 435 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
314 | 436 |
315 DCHECK(worker_threads_.empty()); | 437 DCHECK(worker_threads_.empty()); |
316 | 438 |
317 for (size_t i = 0; i < max_threads; ++i) { | 439 for (size_t i = 0; i < max_threads; ++i) { |
440 std::unique_ptr<PriorityQueue> single_threaded_priority_queue( | |
441 new PriorityQueue(&shared_priority_queue_)); | |
318 std::unique_ptr<SchedulerWorkerThread> worker_thread = | 442 std::unique_ptr<SchedulerWorkerThread> worker_thread = |
319 SchedulerWorkerThread::Create( | 443 SchedulerWorkerThread::Create( |
320 thread_priority, WrapUnique(new SchedulerWorkerThreadDelegateImpl( | 444 thread_priority, WrapUnique(new SchedulerWorkerThreadDelegateImpl( |
321 this, re_enqueue_sequence_callback)), | 445 this, single_threaded_priority_queue.get(), |
446 re_enqueue_sequence_callback)), | |
322 task_tracker_); | 447 task_tracker_); |
323 if (!worker_thread) | 448 if (!worker_thread) |
324 break; | 449 break; |
325 idle_worker_threads_stack_.Push(worker_thread.get()); | 450 idle_worker_threads_stack_.Push(worker_thread.get()); |
451 single_threaded_priority_queues_[worker_thread.get()] = | |
452 std::move(single_threaded_priority_queue); | |
gab
2016/04/25 21:00:20
The more I read this CL the less I like this cycli
fdoray
2016/04/25 22:34:44
Done.
| |
326 worker_threads_.push_back(std::move(worker_thread)); | 453 worker_threads_.push_back(std::move(worker_thread)); |
327 } | 454 } |
328 | 455 |
329 return !worker_threads_.empty(); | 456 return !worker_threads_.empty(); |
330 } | 457 } |
331 | 458 |
332 void SchedulerThreadPoolImpl::WakeUpOneThread() { | 459 void SchedulerThreadPoolImpl::WakeUpOneThread() { |
333 SchedulerWorkerThread* worker_thread; | 460 SchedulerWorkerThread* worker_thread; |
334 { | 461 { |
335 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 462 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
336 worker_thread = idle_worker_threads_stack_.Pop(); | 463 worker_thread = idle_worker_threads_stack_.Pop(); |
337 } | 464 } |
338 if (worker_thread) | 465 if (worker_thread) |
339 worker_thread->WakeUp(); | 466 worker_thread->WakeUp(); |
340 } | 467 } |
341 | 468 |
342 void SchedulerThreadPoolImpl::AddToIdleWorkerThreadsStack( | 469 void SchedulerThreadPoolImpl::AddToIdleWorkerThreadsStack( |
343 SchedulerWorkerThread* worker_thread) { | 470 SchedulerWorkerThread* worker_thread) { |
344 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 471 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
345 idle_worker_threads_stack_.Push(worker_thread); | 472 idle_worker_threads_stack_.Push(worker_thread); |
346 DCHECK_LE(idle_worker_threads_stack_.Size(), worker_threads_.size()); | 473 DCHECK_LE(idle_worker_threads_stack_.Size(), worker_threads_.size()); |
347 | 474 |
348 if (idle_worker_threads_stack_.Size() == worker_threads_.size()) | 475 if (idle_worker_threads_stack_.Size() == worker_threads_.size()) |
349 idle_worker_threads_stack_cv_for_testing_->Broadcast(); | 476 idle_worker_threads_stack_cv_for_testing_->Broadcast(); |
350 } | 477 } |
351 | 478 |
479 void SchedulerThreadPoolImpl::RemoveFromIdleWorkerThreadsStack( | |
480 SchedulerWorkerThread* worker_thread) { | |
481 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
482 idle_worker_threads_stack_.Remove(worker_thread); | |
483 } | |
484 | |
352 } // namespace internal | 485 } // namespace internal |
353 } // namespace base | 486 } // namespace base |
OLD | NEW |