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

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

Issue 1876363004: TaskScheduler [11] Support ExecutionMode::SINGLE_THREADED. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@8_delayed
Patch Set: all post tasks go through SchedulerThreadPool Created 4 years, 8 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/scheduler_thread_pool.h" 5 #include "base/task_scheduler/scheduler_thread_pool.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 SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl 159 class SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl
106 : public SchedulerWorkerThread::Delegate { 160 : public SchedulerWorkerThread::Delegate {
107 public: 161 public:
108 SchedulerWorkerThreadDelegateImpl( 162 SchedulerWorkerThreadDelegateImpl(
109 SchedulerThreadPool* outer, 163 SchedulerThreadPool* outer,
164 PriorityQueue* single_threaded_priority_queue,
110 const EnqueueSequenceCallback& enqueue_sequence_callback); 165 const EnqueueSequenceCallback& 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 EnqueueSequence(scoped_refptr<Sequence> sequence) override; 172 void EnqueueSequence(scoped_refptr<Sequence> sequence) override;
118 173
119 private: 174 private:
120 SchedulerThreadPool* outer_; 175 SchedulerThreadPool* outer_;
176 PriorityQueue* const single_threaded_priority_queue_;
121 const EnqueueSequenceCallback enqueue_sequence_callback_; 177 const EnqueueSequenceCallback 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 SchedulerThreadPool::~SchedulerThreadPool() { 186 SchedulerThreadPool::~SchedulerThreadPool() {
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<SchedulerThreadPool> SchedulerThreadPool::CreateThreadPool( 192 std::unique_ptr<SchedulerThreadPool> SchedulerThreadPool::CreateThreadPool(
(...skipping 13 matching lines...) Expand all
146 scoped_refptr<TaskRunner> SchedulerThreadPool::CreateTaskRunnerWithTraits( 206 scoped_refptr<TaskRunner> SchedulerThreadPool::CreateTaskRunnerWithTraits(
147 const TaskTraits& traits, 207 const TaskTraits& traits,
148 ExecutionMode execution_mode) { 208 ExecutionMode execution_mode) {
149 switch (execution_mode) { 209 switch (execution_mode) {
150 case ExecutionMode::PARALLEL: 210 case ExecutionMode::PARALLEL:
151 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this)); 211 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this));
152 212
153 case ExecutionMode::SEQUENCED: 213 case ExecutionMode::SEQUENCED:
154 return make_scoped_refptr(new SchedulerSequencedTaskRunner(traits, this)); 214 return make_scoped_refptr(new SchedulerSequencedTaskRunner(traits, this));
155 215
156 case ExecutionMode::SINGLE_THREADED: 216 case ExecutionMode::SINGLE_THREADED: {
157 // TODO(fdoray): Support SINGLE_THREADED TaskRunners. 217 // TODO(fdoray): Find a better way to assign a worker thread to a
158 NOTREACHED(); 218 // SingleThreadTaskRunner.
159 return nullptr; 219 size_t worker_thread_index;
220 {
221 AutoSchedulerLock auto_lock(next_worker_thread_index_lock_);
222 worker_thread_index = next_worker_thread_index_;
223 next_worker_thread_index_ =
224 (next_worker_thread_index_ + 1) % worker_threads_.size();
225 }
226 return make_scoped_refptr(new SchedulerSingleThreadTaskRunner(
227 traits, this, worker_threads_[worker_thread_index].get()));
228 }
160 } 229 }
161 230
162 NOTREACHED(); 231 NOTREACHED();
163 return nullptr; 232 return nullptr;
164 } 233 }
165 234
166 void SchedulerThreadPool::EnqueueSequence( 235 void SchedulerThreadPool::EnqueueSequence(
167 scoped_refptr<Sequence> sequence, 236 scoped_refptr<Sequence> sequence,
168 const SequenceSortKey& sequence_sort_key) { 237 const SequenceSortKey& sequence_sort_key) {
169 shared_priority_queue_.BeginTransaction()->Push( 238 shared_priority_queue_.BeginTransaction()->Push(
(...skipping 21 matching lines...) Expand all
191 void SchedulerThreadPool::JoinForTesting() { 260 void SchedulerThreadPool::JoinForTesting() {
192 for (const auto& worker_thread : worker_threads_) 261 for (const auto& worker_thread : worker_threads_)
193 worker_thread->JoinForTesting(); 262 worker_thread->JoinForTesting();
194 263
195 DCHECK(!join_for_testing_returned_.IsSignaled()); 264 DCHECK(!join_for_testing_returned_.IsSignaled());
196 join_for_testing_returned_.Signal(); 265 join_for_testing_returned_.Signal();
197 } 266 }
198 267
199 bool SchedulerThreadPool::PostTaskWithSequence( 268 bool SchedulerThreadPool::PostTaskWithSequence(
200 std::unique_ptr<Task> task, 269 std::unique_ptr<Task> task,
201 scoped_refptr<Sequence> sequence) { 270 scoped_refptr<Sequence> sequence,
271 SchedulerWorkerThread* worker_thread) {
202 DCHECK(task); 272 DCHECK(task);
203 DCHECK(sequence); 273 DCHECK(sequence);
204 274
205 if (!task_tracker_->WillPostTask(task.get())) 275 if (!task_tracker_->WillPostTask(task.get()))
206 return false; 276 return false;
207 277
208 if (task->delayed_run_time.is_null()) { 278 if (task->delayed_run_time.is_null()) {
209 PostTaskWithSequenceNow(std::move(task), std::move(sequence)); 279 PostTaskWithSequenceNow(std::move(task), std::move(sequence),
280 worker_thread);
210 } else { 281 } else {
211 delayed_task_manager_->AddDelayedTask(std::move(task), std::move(sequence), 282 delayed_task_manager_->AddDelayedTask(std::move(task), std::move(sequence),
212 this); 283 worker_thread, this);
213 } 284 }
214 285
215 return true; 286 return true;
216 } 287 }
217 288
218 void SchedulerThreadPool::PostTaskWithSequenceNow( 289 void SchedulerThreadPool::PostTaskWithSequenceNow(
219 std::unique_ptr<Task> task, 290 std::unique_ptr<Task> task,
220 scoped_refptr<Sequence> sequence) { 291 scoped_refptr<Sequence> sequence,
292 SchedulerWorkerThread* worker_thread) {
221 DCHECK(task); 293 DCHECK(task);
222 DCHECK(sequence); 294 DCHECK(sequence);
223 295
224 // Confirm that |task| is ready to run (its delayed run time is either null or 296 // Confirm that |task| is ready to run (its delayed run time is either null or
225 // in the past). 297 // in the past).
226 DCHECK_LE(task->delayed_run_time, TimeTicks::Now()); 298 DCHECK_LE(task->delayed_run_time, TimeTicks::Now());
227 299
300 PriorityQueue* const priority_queue =
301 worker_thread ? single_threaded_priority_queues_[worker_thread].get()
302 : &shared_priority_queue_;
303 DCHECK(priority_queue);
304
228 const bool sequence_was_empty = sequence->PushTask(std::move(task)); 305 const bool sequence_was_empty = sequence->PushTask(std::move(task));
229 if (sequence_was_empty) { 306 if (sequence_was_empty) {
230 // Insert |sequence| in |shared_priority_queue_| if it was empty before 307 // Insert |sequence| in |priority_queue| if it was empty before |task| was
231 // |task| was inserted into it. Otherwise, one of these must be true: 308 // inserted into it. Otherwise, one of these must be true:
232 // - |sequence| is already in a PriorityQueue (not necessarily 309 // - |sequence| is already in a PriorityQueue (not necessarily
233 // |shared_priority_queue_|), or, 310 // |shared_priority_queue_|), or,
234 // - A worker thread is running a Task from |sequence|. It will insert 311 // - A worker thread is running a Task from |sequence|. It will insert
235 // |sequence| in a PriorityQueue once it's done running the Task. 312 // |sequence| in a PriorityQueue once it's done running the Task.
236 const auto sequence_sort_key = sequence->GetSortKey(); 313 const auto sequence_sort_key = sequence->GetSortKey();
237 shared_priority_queue_.BeginTransaction()->Push( 314 priority_queue->BeginTransaction()->Push(
238 WrapUnique(new PriorityQueue::SequenceAndSortKey(std::move(sequence), 315 WrapUnique(new PriorityQueue::SequenceAndSortKey(std::move(sequence),
239 sequence_sort_key))); 316 sequence_sort_key)));
240 317
241 // Wake up a worker thread to process |sequence|. 318 // Wake up a worker thread to process |sequence|.
242 WakeUpOneThread(); 319 if (worker_thread)
320 worker_thread->WakeUp();
321 else
322 WakeUpOneThread();
243 } 323 }
244 } 324 }
245 325
246 SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl:: 326 SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::
247 SchedulerWorkerThreadDelegateImpl( 327 SchedulerWorkerThreadDelegateImpl(
248 SchedulerThreadPool* outer, 328 SchedulerThreadPool* outer,
329 PriorityQueue* single_threaded_priority_queue,
249 const EnqueueSequenceCallback& enqueue_sequence_callback) 330 const EnqueueSequenceCallback& enqueue_sequence_callback)
250 : outer_(outer), enqueue_sequence_callback_(enqueue_sequence_callback) {} 331 : outer_(outer),
332 single_threaded_priority_queue_(single_threaded_priority_queue),
333 enqueue_sequence_callback_(enqueue_sequence_callback) {}
251 334
252 SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl:: 335 SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::
253 ~SchedulerWorkerThreadDelegateImpl() = default; 336 ~SchedulerWorkerThreadDelegateImpl() = default;
254 337
255 void SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::OnMainEntry() { 338 void SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::OnMainEntry(
339 SchedulerWorkerThread* worker_thread) {
340 DCHECK(!tls_current_worker_thread.Get().Get());
256 DCHECK(!tls_current_thread_pool.Get().Get()); 341 DCHECK(!tls_current_thread_pool.Get().Get());
342 tls_current_worker_thread.Get().Set(worker_thread);
257 tls_current_thread_pool.Get().Set(outer_); 343 tls_current_thread_pool.Get().Set(outer_);
258 } 344 }
259 345
260 scoped_refptr<Sequence> 346 scoped_refptr<Sequence>
261 SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::GetWork( 347 SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::GetWork(
262 SchedulerWorkerThread* worker_thread) { 348 SchedulerWorkerThread* worker_thread) {
263 std::unique_ptr<PriorityQueue::Transaction> transaction( 349 std::unique_ptr<PriorityQueue::Transaction> shared_transaction(
264 outer_->shared_priority_queue_.BeginTransaction()); 350 outer_->shared_priority_queue_.BeginTransaction());
265 const auto& sequence_and_sort_key = transaction->Peek(); 351 const auto& shared_sequence_and_sort_key = shared_transaction->Peek();
266 352
267 if (sequence_and_sort_key.is_null()) { 353 std::unique_ptr<PriorityQueue::Transaction> single_threaded_transaction(
268 // |transaction| is kept alive while |worker_thread| is added to 354 single_threaded_priority_queue_->BeginTransaction());
355 const auto& single_threaded_sequence_and_sort_key =
356 single_threaded_transaction->Peek();
357
358 if (shared_sequence_and_sort_key.is_null() &&
359 single_threaded_sequence_and_sort_key.is_null()) {
360 single_threaded_transaction.reset();
361
362 // |shared_transaction| is kept alive while |worker_thread| is added to
269 // |idle_worker_threads_stack_| to avoid this race: 363 // |idle_worker_threads_stack_| to avoid this race:
270 // 1. This thread creates a Transaction, finds |shared_priority_queue_| 364 // 1. This thread creates a Transaction, finds |shared_priority_queue_|
271 // empty and ends the Transaction. 365 // empty and ends the Transaction.
272 // 2. Other thread creates a Transaction, inserts a Sequence into 366 // 2. Other thread creates a Transaction, inserts a Sequence into
273 // |shared_priority_queue_| and ends the Transaction. This can't happen 367 // |shared_priority_queue_| and ends the Transaction. This can't happen
274 // if the Transaction of step 1 is still active because because there can 368 // if the Transaction of step 1 is still active because because there can
275 // only be one active Transaction per PriorityQueue at a time. 369 // only be one active Transaction per PriorityQueue at a time.
276 // 3. Other thread calls WakeUpOneThread(). No thread is woken up because 370 // 3. Other thread calls WakeUpOneThread(). No thread is woken up because
277 // |idle_worker_threads_stack_| is empty. 371 // |idle_worker_threads_stack_| is empty.
278 // 4. This thread adds itself to |idle_worker_threads_stack_| and goes to 372 // 4. This thread adds itself to |idle_worker_threads_stack_| and goes to
279 // sleep. No thread runs the Sequence inserted in step 2. 373 // sleep. No thread runs the Sequence inserted in step 2.
280 outer_->AddToIdleWorkerThreadsStack(worker_thread); 374 outer_->AddToIdleWorkerThreadsStack(worker_thread);
281 return nullptr; 375 return nullptr;
282 } 376 }
283 377
284 scoped_refptr<Sequence> sequence = sequence_and_sort_key.sequence; 378 scoped_refptr<Sequence> sequence;
285 transaction->Pop(); 379
380 if (single_threaded_sequence_and_sort_key.is_null() ||
381 (!shared_sequence_and_sort_key.is_null() &&
382 single_threaded_sequence_and_sort_key.sort_key <
383 shared_sequence_and_sort_key.sort_key)) {
384 sequence = shared_sequence_and_sort_key.sequence;
385 shared_transaction->Pop();
386 last_sequence_is_single_threaded_ = false;
387 } else {
388 DCHECK(!single_threaded_sequence_and_sort_key.is_null());
389 sequence = single_threaded_sequence_and_sort_key.sequence;
390 single_threaded_transaction->Pop();
391 last_sequence_is_single_threaded_ = true;
392 }
393
394 shared_transaction.reset();
395 single_threaded_transaction.reset();
396 outer_->RemoveFromIdleWorkerThreadsStack(worker_thread);
397
286 return sequence; 398 return sequence;
287 } 399 }
288 400
289 void SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::EnqueueSequence( 401 void SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::EnqueueSequence(
290 scoped_refptr<Sequence> sequence) { 402 scoped_refptr<Sequence> sequence) {
291 enqueue_sequence_callback_.Run(std::move(sequence)); 403 if (last_sequence_is_single_threaded_) {
404 // A single-threaded Sequence is always re-enqueued in the single-threaded
405 // PriorityQueue from which it was extracted.
406 const SequenceSortKey sequence_sort_key = sequence->GetSortKey();
407 single_threaded_priority_queue_->BeginTransaction()->Push(
408 WrapUnique(new PriorityQueue::SequenceAndSortKey(std::move(sequence),
409 sequence_sort_key)));
410 } else {
411 // |enqueue_sequence_callback_| will determine in which PriorityQueue
412 // |sequence| must be enqueued.
413 enqueue_sequence_callback_.Run(std::move(sequence));
414 }
292 } 415 }
293 416
294 SchedulerThreadPool::SchedulerThreadPool( 417 SchedulerThreadPool::SchedulerThreadPool(
295 TaskTracker* task_tracker, 418 TaskTracker* task_tracker,
296 DelayedTaskManager* delayed_task_manager) 419 DelayedTaskManager* delayed_task_manager)
297 : idle_worker_threads_stack_lock_(shared_priority_queue_.container_lock()), 420 : idle_worker_threads_stack_lock_(shared_priority_queue_.container_lock()),
298 idle_worker_threads_stack_cv_for_testing_( 421 idle_worker_threads_stack_cv_for_testing_(
299 idle_worker_threads_stack_lock_.CreateConditionVariable()), 422 idle_worker_threads_stack_lock_.CreateConditionVariable()),
300 join_for_testing_returned_(true, false), 423 join_for_testing_returned_(true, false),
301 task_tracker_(task_tracker), 424 task_tracker_(task_tracker),
302 delayed_task_manager_(delayed_task_manager) { 425 delayed_task_manager_(delayed_task_manager) {
303 DCHECK(task_tracker_); 426 DCHECK(task_tracker_);
304 DCHECK(delayed_task_manager_); 427 DCHECK(delayed_task_manager_);
305 } 428 }
306 429
307 bool SchedulerThreadPool::Initialize( 430 bool SchedulerThreadPool::Initialize(
308 ThreadPriority thread_priority, 431 ThreadPriority thread_priority,
309 size_t max_threads, 432 size_t max_threads,
310 const EnqueueSequenceCallback& enqueue_sequence_callback) { 433 const EnqueueSequenceCallback& enqueue_sequence_callback) {
311 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); 434 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_);
312 435
313 DCHECK(worker_threads_.empty()); 436 DCHECK(worker_threads_.empty());
314 437
315 for (size_t i = 0; i < max_threads; ++i) { 438 for (size_t i = 0; i < max_threads; ++i) {
439 std::unique_ptr<PriorityQueue> single_threaded_priority_queue(
440 new PriorityQueue(&shared_priority_queue_));
316 std::unique_ptr<SchedulerWorkerThread> worker_thread = 441 std::unique_ptr<SchedulerWorkerThread> worker_thread =
317 SchedulerWorkerThread::CreateWorkerThread( 442 SchedulerWorkerThread::CreateWorkerThread(
318 thread_priority, WrapUnique(new SchedulerWorkerThreadDelegateImpl( 443 thread_priority, WrapUnique(new SchedulerWorkerThreadDelegateImpl(
319 this, enqueue_sequence_callback)), 444 this, single_threaded_priority_queue.get(),
445 enqueue_sequence_callback)),
320 task_tracker_); 446 task_tracker_);
321 if (!worker_thread) 447 if (!worker_thread)
322 break; 448 break;
323 idle_worker_threads_stack_.Push(worker_thread.get()); 449 idle_worker_threads_stack_.Push(worker_thread.get());
450 single_threaded_priority_queues_[worker_thread.get()] =
451 std::move(single_threaded_priority_queue);
324 worker_threads_.push_back(std::move(worker_thread)); 452 worker_threads_.push_back(std::move(worker_thread));
325 } 453 }
326 454
327 return !worker_threads_.empty(); 455 return !worker_threads_.empty();
328 } 456 }
329 457
330 void SchedulerThreadPool::WakeUpOneThread() { 458 void SchedulerThreadPool::WakeUpOneThread() {
331 SchedulerWorkerThread* worker_thread = PopOneIdleWorkerThread(); 459 SchedulerWorkerThread* worker_thread = PopOneIdleWorkerThread();
332 if (worker_thread) 460 if (worker_thread)
333 worker_thread->WakeUp(); 461 worker_thread->WakeUp();
334 } 462 }
335 463
336 void SchedulerThreadPool::AddToIdleWorkerThreadsStack( 464 void SchedulerThreadPool::AddToIdleWorkerThreadsStack(
337 SchedulerWorkerThread* worker_thread) { 465 SchedulerWorkerThread* worker_thread) {
338 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); 466 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_);
339 idle_worker_threads_stack_.Push(worker_thread); 467 idle_worker_threads_stack_.Push(worker_thread);
340 DCHECK_LE(idle_worker_threads_stack_.Size(), worker_threads_.size()); 468 DCHECK_LE(idle_worker_threads_stack_.Size(), worker_threads_.size());
341 469
342 if (idle_worker_threads_stack_.Size() == worker_threads_.size()) 470 if (idle_worker_threads_stack_.Size() == worker_threads_.size())
343 idle_worker_threads_stack_cv_for_testing_->Broadcast(); 471 idle_worker_threads_stack_cv_for_testing_->Broadcast();
344 } 472 }
345 473
474 void SchedulerThreadPool::RemoveFromIdleWorkerThreadsStack(
475 SchedulerWorkerThread* worker_thread) {
476 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_);
477 idle_worker_threads_stack_.Remove(worker_thread);
478 }
479
346 SchedulerWorkerThread* SchedulerThreadPool::PopOneIdleWorkerThread() { 480 SchedulerWorkerThread* SchedulerThreadPool::PopOneIdleWorkerThread() {
347 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); 481 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_);
348 482
349 if (idle_worker_threads_stack_.Empty()) 483 if (idle_worker_threads_stack_.Empty())
350 return nullptr; 484 return nullptr;
351 485
352 return idle_worker_threads_stack_.Pop(); 486 return idle_worker_threads_stack_.Pop();
353 } 487 }
354 488
355 } // namespace internal 489 } // namespace internal
356 } // namespace base 490 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698