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 // Create a new Sequence to allow parallel execution of Tasks posted through | |
45 // this TaskRunner. | |
46 scoped_refptr<Sequence> sequence(new Sequence); | |
47 | |
48 return PostTaskHelper(WrapUnique(new Task(from_here, closure, traits_)), | |
49 std::move(sequence), priority_queue_, task_tracker_); | |
50 } | |
51 | |
52 bool RunsTasksOnCurrentThread() const override { | |
53 return tls_current_shared_priority_queue.Get().Get() == priority_queue_; | |
54 } | |
55 | |
56 private: | |
57 ~SchedulerParallelTaskRunner() override = default; | |
58 | |
59 const TaskTraits traits_; | |
60 PriorityQueue* const priority_queue_; | |
61 TaskTracker* const task_tracker_; | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(SchedulerParallelTaskRunner); | |
64 }; | |
65 | |
66 } // namespace | |
67 | |
68 std::unique_ptr<SchedulerThreadPool> SchedulerThreadPool::CreateThreadPool( | |
69 ThreadPriority thread_priority, | |
70 size_t max_threads, | |
71 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
72 TaskTracker* task_tracker) { | |
73 std::unique_ptr<SchedulerThreadPool> thread_pool( | |
74 new SchedulerThreadPool(ran_task_from_sequence_callback, task_tracker)); | |
75 thread_pool->Initialize(thread_priority, max_threads); | |
76 if (thread_pool->worker_threads_.empty()) | |
77 return nullptr; | |
78 return thread_pool; | |
79 } | |
80 | |
81 SchedulerThreadPool::~SchedulerThreadPool() { | |
82 AutoSchedulerLock auto_lock(join_for_testing_returned_lock_); | |
83 DCHECK(join_for_testing_returned_ || worker_threads_.empty()); | |
84 } | |
85 | |
86 scoped_refptr<TaskRunner> SchedulerThreadPool::CreateTaskRunnerWithTraits( | |
87 const TaskTraits& traits, | |
88 ExecutionMode execution_mode) { | |
89 switch (execution_mode) { | |
90 case ExecutionMode::PARALLEL: | |
91 return make_scoped_refptr(new SchedulerParallelTaskRunner( | |
92 traits, &shared_priority_queue_, task_tracker_)); | |
93 | |
94 case ExecutionMode::SEQUENCED: | |
95 case ExecutionMode::SINGLE_THREADED: | |
96 // TODO(fdoray): Support SEQUENCED and SINGLE_THREADED TaskRunners. | |
97 NOTREACHED(); | |
98 return nullptr; | |
99 } | |
100 | |
101 NOTREACHED(); | |
102 return nullptr; | |
103 } | |
104 | |
105 void SchedulerThreadPool::InsertSequenceAfterTaskRan( | |
106 scoped_refptr<Sequence> sequence, | |
107 const SequenceSortKey& sequence_sort_key) { | |
108 auto sequence_and_sort_key = WrapUnique(new PriorityQueue::SequenceAndSortKey( | |
109 std::move(sequence), sequence_sort_key)); | |
110 auto transaction = shared_priority_queue_.BeginTransaction(); | |
111 | |
112 // The thread calling this method just ran a Task from |sequence| and will | |
113 // soon try to get another Sequence from which to run a Task. If the thread | |
114 // belongs to this pool, it will get that Sequence from | |
115 // |shared_priority_queue_|. When that's the case, there is no need to wake up | |
116 // another thread after |sequence| is inserted in |shared_priority_queue_|. If | |
117 // we did wake up another thread, we would waste resources by having more | |
118 // threads trying to get a Sequence from |shared_priority_queue_| than the | |
119 // number of Sequences in it. | |
120 if (tls_current_shared_priority_queue.Get().Get() == &shared_priority_queue_) | |
121 transaction->PushNoWakeUp(std::move(sequence_and_sort_key)); | |
122 else | |
123 transaction->Push(std::move(sequence_and_sort_key)); | |
124 } | |
125 | |
126 void SchedulerThreadPool::WaitForAllWorkerThreadsIdleForTesting() { | |
127 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
128 while (idle_worker_threads_stack_.size() < worker_threads_.size()) | |
129 idle_worker_threads_stack_cv_->Wait(); | |
130 } | |
131 | |
132 void SchedulerThreadPool::JoinForTesting() { | |
133 for (const auto& worker_thread : worker_threads_) | |
134 worker_thread->JoinForTesting(); | |
135 | |
136 AutoSchedulerLock auto_lock(join_for_testing_returned_lock_); | |
137 DCHECK(!join_for_testing_returned_); | |
138 join_for_testing_returned_ = true; | |
139 } | |
140 | |
141 SchedulerThreadPool::SchedulerThreadPool( | |
142 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
143 TaskTracker* task_tracker) | |
144 : shared_priority_queue_( | |
145 Bind(&SchedulerThreadPool::WakeUpOneThread, Unretained(this))), | |
146 idle_worker_threads_stack_lock_(shared_priority_queue_.container_lock()), | |
147 idle_worker_threads_stack_cv_( | |
148 idle_worker_threads_stack_lock_.CreateConditionVariable()), | |
149 ran_task_from_sequence_callback_(ran_task_from_sequence_callback), | |
150 task_tracker_(task_tracker) { | |
151 DCHECK(task_tracker_); | |
152 } | |
153 | |
154 void SchedulerThreadPool::Initialize(ThreadPriority thread_priority, | |
155 size_t max_threads) { | |
156 DCHECK(worker_threads_.empty()); | |
157 | |
158 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
159 | |
160 for (size_t i = 0; i < max_threads; ++i) { | |
161 std::unique_ptr<SchedulerWorkerThread> worker_thread = | |
162 SchedulerWorkerThread::CreateSchedulerWorkerThread(thread_priority, | |
163 this, task_tracker_); | |
164 if (!worker_thread) | |
165 break; | |
166 idle_worker_threads_stack_.push(worker_thread.get()); | |
167 worker_threads_.push_back(std::move(worker_thread)); | |
168 } | |
169 } | |
170 | |
171 void SchedulerThreadPool::WakeUpOneThread() { | |
172 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
173 | |
174 if (idle_worker_threads_stack_.empty()) | |
175 return; | |
176 | |
177 SchedulerWorkerThread* worker_thread = idle_worker_threads_stack_.top(); | |
178 idle_worker_threads_stack_.pop(); | |
179 worker_thread->WakeUp(); | |
180 } | |
181 | |
182 void SchedulerThreadPool::AddToIdleWorkerThreadsStack( | |
183 SchedulerWorkerThread* worker_thread) { | |
184 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
185 idle_worker_threads_stack_.push(worker_thread); | |
186 DCHECK_LE(idle_worker_threads_stack_.size(), worker_threads_.size()); | |
187 | |
188 if (idle_worker_threads_stack_.size() == worker_threads_.size()) | |
189 idle_worker_threads_stack_cv_->Broadcast(); | |
190 } | |
191 | |
192 void SchedulerThreadPool::OnMainEntry() { | |
193 DCHECK(!tls_current_shared_priority_queue.Get().Get()); | |
194 tls_current_shared_priority_queue.Get().Set(&shared_priority_queue_); | |
195 } | |
196 | |
197 void SchedulerThreadPool::OnMainExit() { | |
198 DCHECK(tls_current_shared_priority_queue.Get().Get()); | |
199 tls_current_shared_priority_queue.Get().Set(nullptr); | |
200 } | |
201 | |
202 scoped_refptr<Sequence> SchedulerThreadPool::GetWork( | |
203 SchedulerWorkerThread* worker_thread) { | |
204 std::unique_ptr<PriorityQueue::Transaction> transaction( | |
205 shared_priority_queue_.BeginTransaction()); | |
206 const PriorityQueue::SequenceAndSortKey sequence_and_sort_key( | |
danakj
2016/04/07 23:08:44
nit: auto?
fdoray
2016/04/08 14:53:03
Done.
| |
207 transaction->Peek()); | |
208 | |
209 if (sequence_and_sort_key.is_null()) { | |
210 // |transaction| is kept alive while |worker_thread| is added to | |
211 // |idle_worker_threads_stack_| to avoid this race: | |
212 // 1. This thread creates a Transaction, finds |shared_priority_queue_| | |
213 // empty and ends the Transaction. | |
214 // 2. Other thread creates a Transaction, inserts a Sequence into | |
215 // |shared_priority_queue_| and ends the Transaction. This can't happen | |
216 // if the Transaction of step 1 is still active because because there can | |
217 // only be one active Transaction per PriorityQueue at a time. | |
218 // 3. Other thread calls WakeUpOneThread(). No thread is woken up because | |
219 // |idle_worker_threads_stack_| is empty. | |
220 // 4. This thread adds itself to |idle_worker_threads_stack_| and goes to | |
221 // sleep. No thread runs the Sequence inserted in step 2. | |
222 AddToIdleWorkerThreadsStack(worker_thread); | |
223 return nullptr; | |
224 } | |
225 | |
226 transaction->Pop(); | |
227 return sequence_and_sort_key.sequence; | |
228 } | |
229 | |
230 void SchedulerThreadPool::RanTaskFromSequence( | |
231 scoped_refptr<Sequence> sequence) { | |
232 ran_task_from_sequence_callback_.Run(std::move(sequence)); | |
233 } | |
234 | |
235 } // namespace internal | |
236 } // namespace base | |
OLD | NEW |