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/utils.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 // Post the task as part of a one-off single-task Sequence. | |
45 return PostTaskHelper(WrapUnique(new Task(from_here, closure, traits_)), | |
46 make_scoped_refptr(new Sequence), priority_queue_, | |
47 task_tracker_); | |
48 } | |
49 | |
50 bool RunsTasksOnCurrentThread() const override { | |
51 return tls_current_shared_priority_queue.Get().Get() == priority_queue_; | |
52 } | |
53 | |
54 private: | |
55 ~SchedulerParallelTaskRunner() override = default; | |
56 | |
57 const TaskTraits traits_; | |
58 PriorityQueue* const priority_queue_; | |
59 TaskTracker* const task_tracker_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(SchedulerParallelTaskRunner); | |
62 }; | |
63 | |
64 } // namespace | |
65 | |
66 std::unique_ptr<SchedulerThreadPool> SchedulerThreadPool::CreateThreadPool( | |
67 ThreadPriority thread_priority, | |
68 size_t max_threads, | |
69 const EnqueueSequenceCallback& enqueue_sequence_callback, | |
70 TaskTracker* task_tracker) { | |
71 std::unique_ptr<SchedulerThreadPool> thread_pool( | |
72 new SchedulerThreadPool(enqueue_sequence_callback, task_tracker)); | |
73 if (thread_pool->Initialize(thread_priority, max_threads)) | |
74 return thread_pool; | |
75 return nullptr; | |
76 } | |
77 | |
78 SchedulerThreadPool::~SchedulerThreadPool() { | |
79 #if DCHECK_IS_ON() | |
80 // SchedulerThreadPool should never be deleted in production unless its | |
81 // initialization failed. | |
82 AutoSchedulerLock auto_lock(worker_threads_lock_); | |
83 DCHECK(join_for_testing_returned_.IsSignaled() || worker_threads_.empty()); | |
84 #endif // DCHECK_IS_ON() | |
85 } | |
86 | |
87 scoped_refptr<TaskRunner> SchedulerThreadPool::CreateTaskRunnerWithTraits( | |
88 const TaskTraits& traits, | |
89 ExecutionMode execution_mode) { | |
90 switch (execution_mode) { | |
91 case ExecutionMode::PARALLEL: | |
92 return make_scoped_refptr(new SchedulerParallelTaskRunner( | |
93 traits, &shared_priority_queue_, task_tracker_)); | |
94 | |
95 case ExecutionMode::SEQUENCED: | |
96 case ExecutionMode::SINGLE_THREADED: | |
97 // TODO(fdoray): Support SEQUENCED and SINGLE_THREADED TaskRunners. | |
98 NOTREACHED(); | |
99 return nullptr; | |
100 } | |
101 | |
102 NOTREACHED(); | |
103 return nullptr; | |
104 } | |
105 | |
106 void SchedulerThreadPool::EnqueueSequence( | |
107 scoped_refptr<Sequence> sequence, | |
108 const SequenceSortKey& sequence_sort_key) { | |
109 auto sequence_and_sort_key = WrapUnique(new PriorityQueue::SequenceAndSortKey( | |
110 std::move(sequence), sequence_sort_key)); | |
111 auto transaction = shared_priority_queue_.BeginTransaction(); | |
112 | |
113 // The thread calling this method just ran a Task from |sequence| and will | |
114 // soon try to get another Sequence from which to run a Task. If the thread | |
115 // belongs to this pool, it will get that Sequence from | |
116 // |shared_priority_queue_|. When that's the case, there is no need to wake up | |
117 // another thread after |sequence| is inserted in |shared_priority_queue_|. If | |
118 // we did wake up another thread, we would waste resources by having more | |
119 // threads trying to get a Sequence from |shared_priority_queue_| than the | |
120 // number of Sequences in it. | |
121 if (tls_current_shared_priority_queue.Get().Get() == &shared_priority_queue_) | |
122 transaction->PushNoWakeUp(std::move(sequence_and_sort_key)); | |
123 else | |
124 transaction->Push(std::move(sequence_and_sort_key)); | |
125 } | |
126 | |
127 void SchedulerThreadPool::WaitForAllWorkerThreadsIdleForTesting() { | |
128 AutoSchedulerLock auto_lock(worker_threads_lock_); | |
129 while (idle_worker_threads_stack_.size() < worker_threads_.size()) | |
130 idle_worker_threads_stack_cv_for_testing_->Wait(); | |
131 } | |
132 | |
133 void SchedulerThreadPool::JoinForTesting() { | |
134 std::vector<SchedulerWorkerThread*> worker_threads_copy; | |
135 { | |
136 AutoSchedulerLock auto_lock(worker_threads_lock_); | |
137 for (const auto& worker_thread : worker_threads_) | |
138 worker_threads_copy.push_back(worker_thread.get()); | |
139 } | |
140 | |
141 // Join threads without holding |worker_threads_lock_|. Otherwise, a deadlock | |
142 // could occur when they call GetWork(). | |
143 for (const auto& worker_thread : worker_threads_copy) | |
144 worker_thread->JoinForTesting(); | |
145 | |
gab
2016/04/08 17:56:00
Do we need to verify that worker_threads_copy is s
fdoray
2016/04/08 19:00:05
I would like to put back the comment that says tha
gab
2016/04/08 21:10:50
Okay, feel free to put it back for now and we'll s
| |
146 DCHECK(!join_for_testing_returned_.IsSignaled()); | |
147 join_for_testing_returned_.Signal(); | |
148 } | |
149 | |
150 SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl:: | |
151 SchedulerWorkerThreadDelegateImpl( | |
152 SchedulerThreadPool* outer, | |
153 const EnqueueSequenceCallback enqueue_sequence_callback) | |
154 : outer_(outer), enqueue_sequence_callback_(enqueue_sequence_callback) {} | |
155 | |
156 SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl:: | |
157 ~SchedulerWorkerThreadDelegateImpl() = default; | |
158 | |
159 void SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::OnMainEntry() { | |
160 DCHECK(!tls_current_shared_priority_queue.Get().Get()); | |
161 tls_current_shared_priority_queue.Get().Set(&outer_->shared_priority_queue_); | |
162 } | |
163 | |
164 scoped_refptr<Sequence> | |
165 SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::GetWork( | |
166 SchedulerWorkerThread* worker_thread) { | |
167 std::unique_ptr<PriorityQueue::Transaction> transaction( | |
168 outer_->shared_priority_queue_.BeginTransaction()); | |
169 const auto sequence_and_sort_key = transaction->Peek(); | |
170 | |
171 if (sequence_and_sort_key.is_null()) { | |
172 // |transaction| is kept alive while |worker_thread| is added to | |
173 // |idle_worker_threads_stack_| to avoid this race: | |
174 // 1. This thread creates a Transaction, finds |shared_priority_queue_| | |
175 // empty and ends the Transaction. | |
176 // 2. Other thread creates a Transaction, inserts a Sequence into | |
177 // |shared_priority_queue_| and ends the Transaction. This can't happen | |
178 // if the Transaction of step 1 is still active because because there can | |
179 // only be one active Transaction per PriorityQueue at a time. | |
180 // 3. Other thread calls WakeUpOneThread(). No thread is woken up because | |
181 // |idle_worker_threads_stack_| is empty. | |
182 // 4. This thread adds itself to |idle_worker_threads_stack_| and goes to | |
183 // sleep. No thread runs the Sequence inserted in step 2. | |
184 outer_->AddToIdleWorkerThreadsStack(worker_thread); | |
185 return nullptr; | |
186 } | |
187 | |
188 transaction->Pop(); | |
189 return sequence_and_sort_key.sequence; | |
190 } | |
191 | |
192 void SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::EnqueueSequence( | |
193 scoped_refptr<Sequence> sequence) { | |
194 enqueue_sequence_callback_.Run(std::move(sequence)); | |
195 } | |
196 | |
197 SchedulerThreadPool::SchedulerThreadPool( | |
198 const EnqueueSequenceCallback& enqueue_sequence_callback, | |
199 TaskTracker* task_tracker) | |
200 : shared_priority_queue_( | |
201 Bind(&SchedulerThreadPool::WakeUpOneThread, Unretained(this))), | |
202 worker_threads_lock_(shared_priority_queue_.container_lock()), | |
203 idle_worker_threads_stack_cv_for_testing_( | |
204 worker_threads_lock_.CreateConditionVariable()), | |
205 join_for_testing_returned_(true, false), | |
206 worker_thread_delegate_(this, enqueue_sequence_callback), | |
207 task_tracker_(task_tracker) { | |
208 DCHECK(task_tracker_); | |
209 } | |
210 | |
211 bool SchedulerThreadPool::Initialize(ThreadPriority thread_priority, | |
212 size_t max_threads) { | |
213 AutoSchedulerLock auto_lock(worker_threads_lock_); | |
214 | |
215 DCHECK(worker_threads_.empty()); | |
216 | |
217 for (size_t i = 0; i < max_threads; ++i) { | |
218 std::unique_ptr<SchedulerWorkerThread> worker_thread = | |
219 SchedulerWorkerThread::CreateSchedulerWorkerThread( | |
220 thread_priority, &worker_thread_delegate_, task_tracker_); | |
221 if (!worker_thread) | |
222 break; | |
223 idle_worker_threads_stack_.push(worker_thread.get()); | |
224 worker_threads_.push_back(std::move(worker_thread)); | |
225 } | |
226 | |
227 return !worker_threads_.empty(); | |
228 } | |
229 | |
230 void SchedulerThreadPool::WakeUpOneThread() { | |
231 SchedulerWorkerThread* worker_thread; | |
232 { | |
233 AutoSchedulerLock auto_lock(worker_threads_lock_); | |
234 | |
235 if (idle_worker_threads_stack_.empty()) | |
236 return; | |
237 | |
238 worker_thread = idle_worker_threads_stack_.top(); | |
239 idle_worker_threads_stack_.pop(); | |
240 } | |
241 worker_thread->WakeUp(); | |
242 } | |
243 | |
244 void SchedulerThreadPool::AddToIdleWorkerThreadsStack( | |
245 SchedulerWorkerThread* worker_thread) { | |
246 AutoSchedulerLock auto_lock(worker_threads_lock_); | |
247 idle_worker_threads_stack_.push(worker_thread); | |
248 DCHECK_LE(idle_worker_threads_stack_.size(), worker_threads_.size()); | |
249 | |
250 if (idle_worker_threads_stack_.size() == worker_threads_.size()) | |
251 idle_worker_threads_stack_cv_for_testing_->Broadcast(); | |
252 } | |
253 | |
254 } // namespace internal | |
255 } // namespace base | |
OLD | NEW |