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

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

Issue 1851403003: TaskScheduler [8] SEQUENCED TaskRunners in SchedulerThreadPool. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@threadpool
Patch Set: 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/task_scheduler/task_tracker.h" 15 #include "base/task_scheduler/task_tracker.h"
15 16
16 namespace base { 17 namespace base {
17 namespace internal { 18 namespace internal {
18 19
19 namespace { 20 namespace {
20 21
21 // Shared PriorityQueue of a thread's SchedulerThreadPool. Not set for threads 22 // Shared PriorityQueue of a thread's SchedulerThreadPool. Not set for threads
22 // that don't belong to a SchedulerThreadPool. 23 // that don't belong to a SchedulerThreadPool.
23 LazyInstance<ThreadLocalPointer<const PriorityQueue>>::Leaky 24 LazyInstance<ThreadLocalPointer<const PriorityQueue>>::Leaky
(...skipping 28 matching lines...) Expand all
52 private: 53 private:
53 ~SchedulerParallelTaskRunner() override = default; 54 ~SchedulerParallelTaskRunner() override = default;
54 55
55 const TaskTraits traits_; 56 const TaskTraits traits_;
56 PriorityQueue* const priority_queue_; 57 PriorityQueue* const priority_queue_;
57 TaskTracker* const task_tracker_; 58 TaskTracker* const task_tracker_;
58 59
59 DISALLOW_COPY_AND_ASSIGN(SchedulerParallelTaskRunner); 60 DISALLOW_COPY_AND_ASSIGN(SchedulerParallelTaskRunner);
60 }; 61 };
61 62
63 // A task runner that runs tasks with the SEQUENCED ExecutionMode.
64 class SchedulerSequencedTaskRunner : public SequencedTaskRunner {
65 public:
66 SchedulerSequencedTaskRunner(const TaskTraits& traits,
67 PriorityQueue* priority_queue,
68 TaskTracker* task_tracker)
69 : traits_(traits),
70 priority_queue_(priority_queue),
71 task_tracker_(task_tracker) {}
72
73 // SequencedTaskRunner:
74 bool PostDelayedTask(const tracked_objects::Location& from_here,
75 const Closure& closure,
76 TimeDelta delay) override {
77 // TODO(fdoray): Support delayed tasks.
78 DCHECK(delay.is_zero());
79 PostTaskHelper(WrapUnique(new Task(from_here, closure, traits_)), sequence_,
80 priority_queue_, task_tracker_);
81 return true;
82 }
83
84 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
85 const Closure& closure,
86 base::TimeDelta delay) override {
87 // Tasks are never nested within the task scheduler.
88 return PostDelayedTask(from_here, closure, delay);
89 }
90
91 bool RunsTasksOnCurrentThread() const override {
92 return g_current_shared_priority_queue.Get().Get() == priority_queue_;
93 }
94
95 private:
96 ~SchedulerSequencedTaskRunner() override = default;
97
98 const TaskTraits traits_;
99 const scoped_refptr<Sequence> sequence_ = new Sequence;
100 PriorityQueue* const priority_queue_;
101 TaskTracker* const task_tracker_;
102
103 DISALLOW_COPY_AND_ASSIGN(SchedulerSequencedTaskRunner);
104 };
105
62 void PostTaskCallback(scoped_refptr<Sequence> sequence, 106 void PostTaskCallback(scoped_refptr<Sequence> sequence,
63 PriorityQueue* priority_queue, 107 PriorityQueue* priority_queue,
64 std::unique_ptr<Task> task) { 108 std::unique_ptr<Task> task) {
65 DCHECK(sequence); 109 DCHECK(sequence);
66 DCHECK(priority_queue); 110 DCHECK(priority_queue);
67 DCHECK(task); 111 DCHECK(task);
68 112
69 if (sequence->PushTask(std::move(task))) { 113 if (sequence->PushTask(std::move(task))) {
70 // |sequence| must be inserted into |priority_queue| because it was empty 114 // |sequence| must be inserted into |priority_queue| because it was empty
71 // before |task| was inserted into it. 115 // before |task| was inserted into it.
(...skipping 28 matching lines...) Expand all
100 144
101 scoped_refptr<TaskRunner> SchedulerThreadPool::CreateTaskRunnerWithTraits( 145 scoped_refptr<TaskRunner> SchedulerThreadPool::CreateTaskRunnerWithTraits(
102 const TaskTraits& traits, 146 const TaskTraits& traits,
103 ExecutionMode execution_mode) { 147 ExecutionMode execution_mode) {
104 switch (execution_mode) { 148 switch (execution_mode) {
105 case ExecutionMode::PARALLEL: 149 case ExecutionMode::PARALLEL:
106 return make_scoped_refptr(new SchedulerParallelTaskRunner( 150 return make_scoped_refptr(new SchedulerParallelTaskRunner(
107 traits, &shared_priority_queue_, task_tracker_)); 151 traits, &shared_priority_queue_, task_tracker_));
108 152
109 case ExecutionMode::SEQUENCED: 153 case ExecutionMode::SEQUENCED:
154 return make_scoped_refptr(new SchedulerSequencedTaskRunner(
155 traits, &shared_priority_queue_, task_tracker_));
156
110 case ExecutionMode::SINGLE_THREADED: 157 case ExecutionMode::SINGLE_THREADED:
111 NOTIMPLEMENTED(); 158 NOTIMPLEMENTED();
112 return nullptr; 159 return nullptr;
113 } 160 }
114 161
115 NOTREACHED(); 162 NOTREACHED();
116 return nullptr; 163 return nullptr;
117 } 164 }
118 165
119 void SchedulerThreadPool::ReinsertSequence( 166 void SchedulerThreadPool::ReinsertSequence(
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 DCHECK(priority_queue); 302 DCHECK(priority_queue);
256 DCHECK(task_tracker); 303 DCHECK(task_tracker);
257 304
258 task_tracker->PostTask( 305 task_tracker->PostTask(
259 Bind(&PostTaskCallback, std::move(sequence), priority_queue), 306 Bind(&PostTaskCallback, std::move(sequence), priority_queue),
260 std::move(task)); 307 std::move(task));
261 } 308 }
262 309
263 } // namespace internal 310 } // namespace internal
264 } // namespace base 311 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698