OLD | NEW |
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" |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 // another thread after |sequence| is inserted in |shared_priority_queue_|. If | 197 // another thread after |sequence| is inserted in |shared_priority_queue_|. If |
198 // we did wake up another thread, we would waste resources by having more | 198 // we did wake up another thread, we would waste resources by having more |
199 // threads trying to get a Sequence from |shared_priority_queue_| than the | 199 // threads trying to get a Sequence from |shared_priority_queue_| than the |
200 // number of Sequences in it. | 200 // number of Sequences in it. |
201 if (tls_current_thread_pool.Get().Get() != this) | 201 if (tls_current_thread_pool.Get().Get() != this) |
202 WakeUpOneThread(); | 202 WakeUpOneThread(); |
203 } | 203 } |
204 | 204 |
205 void SchedulerThreadPool::WaitForAllWorkerThreadsIdleForTesting() { | 205 void SchedulerThreadPool::WaitForAllWorkerThreadsIdleForTesting() { |
206 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 206 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
207 while (idle_worker_threads_stack_.size() < worker_threads_.size()) | 207 while (idle_worker_threads_stack_.Size() < worker_threads_.size()) |
208 idle_worker_threads_stack_cv_for_testing_->Wait(); | 208 idle_worker_threads_stack_cv_for_testing_->Wait(); |
209 } | 209 } |
210 | 210 |
211 void SchedulerThreadPool::JoinForTesting() { | 211 void SchedulerThreadPool::JoinForTesting() { |
212 for (const auto& worker_thread : worker_threads_) | 212 for (const auto& worker_thread : worker_threads_) |
213 worker_thread->JoinForTesting(); | 213 worker_thread->JoinForTesting(); |
214 | 214 |
215 DCHECK(!join_for_testing_returned_.IsSignaled()); | 215 DCHECK(!join_for_testing_returned_.IsSignaled()); |
216 join_for_testing_returned_.Signal(); | 216 join_for_testing_returned_.Signal(); |
217 } | 217 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 301 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
302 | 302 |
303 DCHECK(worker_threads_.empty()); | 303 DCHECK(worker_threads_.empty()); |
304 | 304 |
305 for (size_t i = 0; i < max_threads; ++i) { | 305 for (size_t i = 0; i < max_threads; ++i) { |
306 std::unique_ptr<SchedulerWorkerThread> worker_thread = | 306 std::unique_ptr<SchedulerWorkerThread> worker_thread = |
307 SchedulerWorkerThread::CreateSchedulerWorkerThread( | 307 SchedulerWorkerThread::CreateSchedulerWorkerThread( |
308 thread_priority, worker_thread_delegate_.get(), task_tracker_); | 308 thread_priority, worker_thread_delegate_.get(), task_tracker_); |
309 if (!worker_thread) | 309 if (!worker_thread) |
310 break; | 310 break; |
311 idle_worker_threads_stack_.push(worker_thread.get()); | 311 idle_worker_threads_stack_.Push(worker_thread.get()); |
312 worker_threads_.push_back(std::move(worker_thread)); | 312 worker_threads_.push_back(std::move(worker_thread)); |
313 } | 313 } |
314 | 314 |
315 return !worker_threads_.empty(); | 315 return !worker_threads_.empty(); |
316 } | 316 } |
317 | 317 |
318 void SchedulerThreadPool::WakeUpOneThread() { | 318 void SchedulerThreadPool::WakeUpOneThread() { |
319 SchedulerWorkerThread* worker_thread = PopOneIdleWorkerThread(); | 319 SchedulerWorkerThread* worker_thread; |
| 320 { |
| 321 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
| 322 worker_thread = idle_worker_threads_stack_.Pop(); |
| 323 } |
320 if (worker_thread) | 324 if (worker_thread) |
321 worker_thread->WakeUp(); | 325 worker_thread->WakeUp(); |
322 } | 326 } |
323 | 327 |
324 void SchedulerThreadPool::AddToIdleWorkerThreadsStack( | 328 void SchedulerThreadPool::AddToIdleWorkerThreadsStack( |
325 SchedulerWorkerThread* worker_thread) { | 329 SchedulerWorkerThread* worker_thread) { |
326 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 330 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
327 idle_worker_threads_stack_.push(worker_thread); | 331 idle_worker_threads_stack_.Push(worker_thread); |
328 DCHECK_LE(idle_worker_threads_stack_.size(), worker_threads_.size()); | 332 DCHECK_LE(idle_worker_threads_stack_.Size(), worker_threads_.size()); |
329 | 333 |
330 if (idle_worker_threads_stack_.size() == worker_threads_.size()) | 334 if (idle_worker_threads_stack_.Size() == worker_threads_.size()) |
331 idle_worker_threads_stack_cv_for_testing_->Broadcast(); | 335 idle_worker_threads_stack_cv_for_testing_->Broadcast(); |
332 } | 336 } |
333 | 337 |
334 SchedulerWorkerThread* SchedulerThreadPool::PopOneIdleWorkerThread() { | |
335 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
336 | |
337 if (idle_worker_threads_stack_.empty()) | |
338 return nullptr; | |
339 | |
340 auto worker_thread = idle_worker_threads_stack_.top(); | |
341 idle_worker_threads_stack_.pop(); | |
342 return worker_thread; | |
343 } | |
344 | |
345 } // namespace internal | 338 } // namespace internal |
346 } // namespace base | 339 } // namespace base |
OLD | NEW |