| 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 = PopOneIdleWorkerThread(); |
| 320 if (worker_thread) | 320 if (worker_thread) |
| 321 worker_thread->WakeUp(); | 321 worker_thread->WakeUp(); |
| 322 } | 322 } |
| 323 | 323 |
| 324 void SchedulerThreadPool::AddToIdleWorkerThreadsStack( | 324 void SchedulerThreadPool::AddToIdleWorkerThreadsStack( |
| 325 SchedulerWorkerThread* worker_thread) { | 325 SchedulerWorkerThread* worker_thread) { |
| 326 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 326 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
| 327 idle_worker_threads_stack_.push(worker_thread); | 327 idle_worker_threads_stack_.Push(worker_thread); |
| 328 DCHECK_LE(idle_worker_threads_stack_.size(), worker_threads_.size()); | 328 DCHECK_LE(idle_worker_threads_stack_.Size(), worker_threads_.size()); |
| 329 | 329 |
| 330 if (idle_worker_threads_stack_.size() == worker_threads_.size()) | 330 if (idle_worker_threads_stack_.Size() == worker_threads_.size()) |
| 331 idle_worker_threads_stack_cv_for_testing_->Broadcast(); | 331 idle_worker_threads_stack_cv_for_testing_->Broadcast(); |
| 332 } | 332 } |
| 333 | 333 |
| 334 SchedulerWorkerThread* SchedulerThreadPool::PopOneIdleWorkerThread() { | 334 SchedulerWorkerThread* SchedulerThreadPool::PopOneIdleWorkerThread() { |
| 335 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 335 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
| 336 | 336 |
| 337 if (idle_worker_threads_stack_.empty()) | 337 if (idle_worker_threads_stack_.Empty()) |
| 338 return nullptr; | 338 return nullptr; |
| 339 | 339 |
| 340 auto worker_thread = idle_worker_threads_stack_.top(); | 340 return idle_worker_threads_stack_.Pop(); |
| 341 idle_worker_threads_stack_.pop(); | |
| 342 return worker_thread; | |
| 343 } | 341 } |
| 344 | 342 |
| 345 } // namespace internal | 343 } // namespace internal |
| 346 } // namespace base | 344 } // namespace base |
| OLD | NEW |