OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/task_scheduler/scheduler_thread_pool.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/lazy_instance.h" | |
12 #include "base/logging.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/task_scheduler/task_tracker.h" | |
15 #include "base/threading/thread_local.h" | |
16 | |
17 namespace base { | |
18 namespace internal { | |
19 | |
20 namespace { | |
21 | |
22 // Shared PriorityQueue of a thread's SchedulerThreadPool. Not set for threads | |
23 // that don't belong to a SchedulerThreadPool. | |
24 LazyInstance<ThreadLocalPointer<const PriorityQueue>>::Leaky | |
25 tls_current_shared_priority_queue = LAZY_INSTANCE_INITIALIZER; | |
26 | |
27 // A task runner that runs tasks with the PARALLEL ExecutionMode. | |
28 class SchedulerParallelTaskRunner : public TaskRunner { | |
29 public: | |
30 SchedulerParallelTaskRunner(const TaskTraits& traits, | |
31 PriorityQueue* priority_queue, | |
32 TaskTracker* task_tracker) | |
33 : traits_(traits), | |
34 priority_queue_(priority_queue), | |
35 task_tracker_(task_tracker) {} | |
36 | |
37 // TaskRunner: | |
38 bool PostDelayedTask(const tracked_objects::Location& from_here, | |
39 const Closure& closure, | |
40 TimeDelta delay) override { | |
41 // TODO(fdoray): Support delayed tasks. | |
42 DCHECK(delay.is_zero()); | |
43 | |
44 // Create a new Sequence to allow parallel execution of Tasks posted through | |
gab
2016/04/07 20:32:47
s/new Sequence/single-task Sequence/
("create" im
fdoray
2016/04/08 14:53:02
Done.
| |
45 // this TaskRunner. | |
46 scoped_refptr<Sequence> sequence(new Sequence); | |
gab
2016/04/07 20:32:47
I'd also be happy to inline this, something like:
fdoray
2016/04/08 14:53:02
Done.
| |
47 | |
48 PostTaskHelper(WrapUnique(new Task(from_here, closure, traits_)), | |
49 std::move(sequence), priority_queue_, task_tracker_); | |
50 | |
51 return true; | |
52 } | |
53 | |
54 bool RunsTasksOnCurrentThread() const override { | |
55 return tls_current_shared_priority_queue.Get().Get() == priority_queue_; | |
56 } | |
57 | |
58 private: | |
59 ~SchedulerParallelTaskRunner() override = default; | |
60 | |
61 const TaskTraits traits_; | |
62 PriorityQueue* const priority_queue_; | |
63 TaskTracker* const task_tracker_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(SchedulerParallelTaskRunner); | |
66 }; | |
67 | |
68 } // namespace | |
69 | |
70 std::unique_ptr<SchedulerThreadPool> SchedulerThreadPool::CreateThreadPool( | |
71 ThreadPriority thread_priority, | |
72 size_t max_threads, | |
73 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
74 TaskTracker* task_tracker) { | |
75 std::unique_ptr<SchedulerThreadPool> thread_pool( | |
76 new SchedulerThreadPool(ran_task_from_sequence_callback, task_tracker)); | |
77 thread_pool->Initialize(thread_priority, max_threads); | |
78 if (thread_pool->worker_threads_.empty()) | |
79 return nullptr; | |
80 return thread_pool; | |
81 } | |
82 | |
83 SchedulerThreadPool::~SchedulerThreadPool() { | |
84 AutoSchedulerLock auto_lock(join_for_testing_returned_lock_); | |
85 DCHECK(join_for_testing_returned_ || worker_threads_.empty()); | |
gab
2016/04/07 20:32:47
Wrap these two lines in #if DCHECK_IS_ON() to avoi
fdoray
2016/04/08 14:53:02
Done.
| |
86 } | |
87 | |
88 scoped_refptr<TaskRunner> SchedulerThreadPool::CreateTaskRunnerWithTraits( | |
89 const TaskTraits& traits, | |
90 ExecutionMode execution_mode) { | |
91 switch (execution_mode) { | |
92 case ExecutionMode::PARALLEL: | |
93 return make_scoped_refptr(new SchedulerParallelTaskRunner( | |
94 traits, &shared_priority_queue_, task_tracker_)); | |
95 | |
96 case ExecutionMode::SEQUENCED: | |
97 case ExecutionMode::SINGLE_THREADED: | |
98 // TODO(fdoray): Support SEQUENCED and SINGLE_THREADED TaskRunners. | |
99 NOTREACHED(); | |
100 return nullptr; | |
101 } | |
102 | |
103 NOTREACHED(); | |
104 return nullptr; | |
105 } | |
106 | |
107 void SchedulerThreadPool::InsertSequenceAfterTaskRan( | |
108 scoped_refptr<Sequence> sequence, | |
109 const SequenceSortKey& sequence_sort_key) { | |
110 auto sequence_and_sort_key = WrapUnique(new PriorityQueue::SequenceAndSortKey( | |
111 std::move(sequence), sequence_sort_key)); | |
112 auto transaction = shared_priority_queue_.BeginTransaction(); | |
113 | |
114 // The thread calling this method just ran a Task from |sequence| and will | |
115 // soon try to get another Sequence from which to run a Task. If the thread | |
116 // belongs to this pool, it will get that Sequence from | |
117 // |shared_priority_queue_|. When that's the case, there is no need to wake up | |
118 // another thread after |sequence| is inserted in |shared_priority_queue_|. If | |
119 // we did wake up another thread, we would waste resources by having more | |
120 // threads trying to get a Sequence from |shared_priority_queue_| than the | |
121 // number of Sequences in it. | |
122 if (tls_current_shared_priority_queue.Get().Get() == &shared_priority_queue_) | |
123 transaction->PushNoWakeUp(std::move(sequence_and_sort_key)); | |
124 else | |
125 transaction->Push(std::move(sequence_and_sort_key)); | |
126 } | |
127 | |
128 void SchedulerThreadPool::WaitForAllWorkerThreadsIdleForTesting() { | |
129 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
130 while (idle_worker_threads_stack_.size() < worker_threads_.size()) | |
131 idle_worker_threads_stack_cv_->Wait(); | |
132 } | |
133 | |
134 void SchedulerThreadPool::JoinForTesting() { | |
135 for (const auto& worker_thread : worker_threads_) | |
136 worker_thread->JoinForTesting(); | |
137 | |
138 AutoSchedulerLock auto_lock(join_for_testing_returned_lock_); | |
gab
2016/04/07 20:32:47
We could get away with a MemoryBarrier instead of
fdoray
2016/04/08 14:53:02
I wanted to use a MemoryBarrier but https://codere
| |
139 DCHECK(!join_for_testing_returned_); | |
140 join_for_testing_returned_ = true; | |
141 } | |
142 | |
143 SchedulerThreadPool::SchedulerThreadPool( | |
144 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
145 TaskTracker* task_tracker) | |
146 : shared_priority_queue_( | |
147 Bind(&SchedulerThreadPool::WakeUpOneThread, Unretained(this))), | |
148 idle_worker_threads_stack_lock_(shared_priority_queue_.container_lock()), | |
149 idle_worker_threads_stack_cv_( | |
150 idle_worker_threads_stack_lock_.CreateConditionVariable()), | |
151 ran_task_from_sequence_callback_(ran_task_from_sequence_callback), | |
152 task_tracker_(task_tracker) { | |
153 DCHECK(task_tracker_); | |
154 } | |
155 | |
156 void SchedulerThreadPool::Initialize(ThreadPriority thread_priority, | |
157 size_t max_threads) { | |
158 DCHECK(worker_threads_.empty()); | |
159 | |
160 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
161 | |
162 for (size_t i = 0; i < max_threads; ++i) { | |
163 std::unique_ptr<SchedulerWorkerThread> worker_thread = | |
164 SchedulerWorkerThread::CreateSchedulerWorkerThread(thread_priority, | |
165 this, task_tracker_); | |
166 if (!worker_thread) | |
167 break; | |
168 idle_worker_threads_stack_.push(worker_thread.get()); | |
169 worker_threads_.push_back(std::move(worker_thread)); | |
170 } | |
171 } | |
172 | |
173 void SchedulerThreadPool::WakeUpOneThread() { | |
174 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
175 | |
176 if (idle_worker_threads_stack_.empty()) | |
177 return; | |
178 | |
179 SchedulerWorkerThread* worker_thread = idle_worker_threads_stack_.top(); | |
180 idle_worker_threads_stack_.pop(); | |
181 worker_thread->WakeUp(); | |
gab
2016/04/07 20:32:47
Could technically release the lock before invoking
fdoray
2016/04/08 14:53:02
Done.
| |
182 } | |
183 | |
184 void SchedulerThreadPool::AddToIdleWorkerThreadsStack( | |
185 SchedulerWorkerThread* worker_thread) { | |
186 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
187 idle_worker_threads_stack_.push(worker_thread); | |
188 DCHECK_LE(idle_worker_threads_stack_.size(), worker_threads_.size()); | |
189 | |
190 if (idle_worker_threads_stack_.size() == worker_threads_.size()) | |
191 idle_worker_threads_stack_cv_->Broadcast(); | |
gab
2016/04/07 20:32:47
Would a manual-reset WaitableEvent be better here?
fdoray
2016/04/08 14:53:02
The WaitableEvent would be reset in WakeUpOneThrea
gab
2016/04/08 17:56:00
But the Broadcast's call can racily be lost, no? i
fdoray
2016/04/08 19:00:05
WaitForAllWorkerThreadsIdleForTesting will return
| |
192 } | |
193 | |
194 void SchedulerThreadPool::OnMainEntry() { | |
195 DCHECK(!tls_current_shared_priority_queue.Get().Get()); | |
196 tls_current_shared_priority_queue.Get().Set(&shared_priority_queue_); | |
197 } | |
198 | |
199 void SchedulerThreadPool::OnMainExit() { | |
200 DCHECK(tls_current_shared_priority_queue.Get().Get()); | |
201 tls_current_shared_priority_queue.Get().Set(nullptr); | |
gab
2016/04/07 20:32:47
Actually looking into this deeper turns out this i
fdoray
2016/04/08 14:53:02
Done. Interesting.
| |
202 } | |
203 | |
204 scoped_refptr<Sequence> SchedulerThreadPool::GetWork( | |
205 SchedulerWorkerThread* worker_thread) { | |
206 std::unique_ptr<PriorityQueue::Transaction> transaction( | |
207 shared_priority_queue_.BeginTransaction()); | |
208 const PriorityQueue::SequenceAndSortKey sequence_and_sort_key( | |
209 transaction->Peek()); | |
210 | |
211 if (sequence_and_sort_key.is_null()) { | |
212 // |transaction| is kept alive while |worker_thread| is added to | |
213 // |idle_worker_threads_stack_| to avoid this race: | |
214 // 1. This thread creates a Transaction, finds |shared_priority_queue_| | |
215 // empty and ends the Transaction. | |
216 // 2. Other thread creates a Transaction, inserts a Sequence into | |
217 // |shared_priority_queue_| and ends the Transaction. This can't happen | |
218 // if the Transaction of step 1 is still active because because there can | |
219 // only be one active Transaction per PriorityQueue at a time. | |
220 // 3. Other thread calls WakeUpOneThread(). No thread is woken up because | |
221 // |idle_worker_threads_stack_| is empty. | |
222 // 4. This thread adds itself to |idle_worker_threads_stack_| and goes to | |
223 // sleep. No thread runs the Sequence inserted in step 2. | |
224 AddToIdleWorkerThreadsStack(worker_thread); | |
225 return nullptr; | |
226 } | |
227 | |
228 transaction->Pop(); | |
gab
2016/04/07 20:32:47
Actually since this is all done under a single tra
fdoray
2016/04/08 14:53:02
Eventually, we'll have to:
- peek from a shared an
| |
229 return sequence_and_sort_key.sequence; | |
230 } | |
231 | |
232 void SchedulerThreadPool::RanTaskFromSequence( | |
233 scoped_refptr<Sequence> sequence) { | |
234 ran_task_from_sequence_callback_.Run(std::move(sequence)); | |
235 } | |
236 | |
237 bool PostTaskHelper(std::unique_ptr<Task> task, | |
238 scoped_refptr<Sequence> sequence, | |
239 PriorityQueue* priority_queue, | |
240 TaskTracker* task_tracker) { | |
241 DCHECK(task); | |
242 DCHECK(sequence); | |
243 DCHECK(priority_queue); | |
244 DCHECK(task_tracker); | |
245 | |
246 if (!task_tracker->WillPostTask(task.get())) | |
247 return false; | |
248 | |
249 const bool sequence_was_empty = sequence->PushTask(std::move(task)); | |
250 if (sequence_was_empty) { | |
251 // Insert |sequence| in |priority_queue| if it was empty before |task| was | |
252 // inserted into it. When that's not the case, one of these must be true: | |
253 // - |sequence| is already in a PriorityQueue, or, | |
254 // - A worker thread is running a Task from |sequence|. It will insert | |
255 // |sequence| in a PriorityQueue once it's done running the Task. | |
256 const SequenceSortKey sequence_sort_key = sequence->GetSortKey(); | |
gab
2016/04/07 20:32:47
inline this?
fdoray
2016/04/08 14:53:02
I can't. I have to make sure that sequence->GetSor
| |
257 priority_queue->BeginTransaction()->Push( | |
258 WrapUnique(new PriorityQueue::SequenceAndSortKey(std::move(sequence), | |
259 sequence_sort_key))); | |
260 } | |
261 | |
262 return true; | |
263 } | |
264 | |
265 } // namespace internal | |
266 } // namespace base | |
OLD | NEW |