| 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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 class SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl | 118 class SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl |
| 119 : public SchedulerWorkerThread::Delegate { | 119 : public SchedulerWorkerThread::Delegate { |
| 120 public: | 120 public: |
| 121 SchedulerWorkerThreadDelegateImpl( | 121 SchedulerWorkerThreadDelegateImpl( |
| 122 SchedulerThreadPool* outer, | 122 SchedulerThreadPool* outer, |
| 123 const EnqueueSequenceCallback& enqueue_sequence_callback); | 123 const EnqueueSequenceCallback& enqueue_sequence_callback); |
| 124 ~SchedulerWorkerThreadDelegateImpl() override; | 124 ~SchedulerWorkerThreadDelegateImpl() override; |
| 125 | 125 |
| 126 // SchedulerWorkerThread::Delegate: | 126 // SchedulerWorkerThread::Delegate: |
| 127 void OnMainEntry() override; | 127 void OnMainEntry() override; |
| 128 scoped_refptr<Sequence> GetWork( | 128 scoped_refptr<Sequence> GetWork(SchedulerWorkerThread* worker_thread, |
| 129 SchedulerWorkerThread* worker_thread) override; | 129 PriorityQueue* single_threaded_priority_queue, |
| 130 bool* is_single_threaded_sequence) override; |
| 130 void EnqueueSequence(scoped_refptr<Sequence> sequence) override; | 131 void EnqueueSequence(scoped_refptr<Sequence> sequence) override; |
| 131 | 132 |
| 132 private: | 133 private: |
| 133 SchedulerThreadPool* outer_; | 134 SchedulerThreadPool* outer_; |
| 134 const EnqueueSequenceCallback enqueue_sequence_callback_; | 135 const EnqueueSequenceCallback enqueue_sequence_callback_; |
| 135 | 136 |
| 136 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThreadDelegateImpl); | 137 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThreadDelegateImpl); |
| 137 }; | 138 }; |
| 138 | 139 |
| 139 SchedulerThreadPool::~SchedulerThreadPool() { | 140 SchedulerThreadPool::~SchedulerThreadPool() { |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl:: | 235 SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl:: |
| 235 ~SchedulerWorkerThreadDelegateImpl() = default; | 236 ~SchedulerWorkerThreadDelegateImpl() = default; |
| 236 | 237 |
| 237 void SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::OnMainEntry() { | 238 void SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::OnMainEntry() { |
| 238 DCHECK(!tls_current_thread_pool.Get().Get()); | 239 DCHECK(!tls_current_thread_pool.Get().Get()); |
| 239 tls_current_thread_pool.Get().Set(outer_); | 240 tls_current_thread_pool.Get().Set(outer_); |
| 240 } | 241 } |
| 241 | 242 |
| 242 scoped_refptr<Sequence> | 243 scoped_refptr<Sequence> |
| 243 SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::GetWork( | 244 SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::GetWork( |
| 244 SchedulerWorkerThread* worker_thread) { | 245 SchedulerWorkerThread* worker_thread, |
| 246 PriorityQueue* single_threaded_priority_queue, |
| 247 bool* is_single_threaded_sequence) { |
| 248 // TODO(fdoray): Return a Sequence from |single_threaded_priority_queue| when |
| 249 // appropriate. |
| 250 |
| 245 std::unique_ptr<PriorityQueue::Transaction> transaction( | 251 std::unique_ptr<PriorityQueue::Transaction> transaction( |
| 246 outer_->shared_priority_queue_.BeginTransaction()); | 252 outer_->shared_priority_queue_.BeginTransaction()); |
| 247 const auto sequence_and_sort_key = transaction->Peek(); | 253 const auto sequence_and_sort_key = transaction->Peek(); |
| 248 | 254 |
| 249 if (sequence_and_sort_key.is_null()) { | 255 if (sequence_and_sort_key.is_null()) { |
| 250 // |transaction| is kept alive while |worker_thread| is added to | 256 // |transaction| is kept alive while |worker_thread| is added to |
| 251 // |idle_worker_threads_stack_| to avoid this race: | 257 // |idle_worker_threads_stack_| to avoid this race: |
| 252 // 1. This thread creates a Transaction, finds |shared_priority_queue_| | 258 // 1. This thread creates a Transaction, finds |shared_priority_queue_| |
| 253 // empty and ends the Transaction. | 259 // empty and ends the Transaction. |
| 254 // 2. Other thread creates a Transaction, inserts a Sequence into | 260 // 2. Other thread creates a Transaction, inserts a Sequence into |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 | 297 |
| 292 bool SchedulerThreadPool::Initialize(ThreadPriority thread_priority, | 298 bool SchedulerThreadPool::Initialize(ThreadPriority thread_priority, |
| 293 size_t max_threads) { | 299 size_t max_threads) { |
| 294 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | 300 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); |
| 295 | 301 |
| 296 DCHECK(worker_threads_.empty()); | 302 DCHECK(worker_threads_.empty()); |
| 297 | 303 |
| 298 for (size_t i = 0; i < max_threads; ++i) { | 304 for (size_t i = 0; i < max_threads; ++i) { |
| 299 std::unique_ptr<SchedulerWorkerThread> worker_thread = | 305 std::unique_ptr<SchedulerWorkerThread> worker_thread = |
| 300 SchedulerWorkerThread::CreateSchedulerWorkerThread( | 306 SchedulerWorkerThread::CreateSchedulerWorkerThread( |
| 301 thread_priority, worker_thread_delegate_.get(), task_tracker_); | 307 thread_priority, worker_thread_delegate_.get(), task_tracker_, |
| 308 delayed_task_manager_, &shared_priority_queue_); |
| 302 if (!worker_thread) | 309 if (!worker_thread) |
| 303 break; | 310 break; |
| 304 idle_worker_threads_stack_.push(worker_thread.get()); | 311 idle_worker_threads_stack_.push(worker_thread.get()); |
| 305 worker_threads_.push_back(std::move(worker_thread)); | 312 worker_threads_.push_back(std::move(worker_thread)); |
| 306 } | 313 } |
| 307 | 314 |
| 308 return !worker_threads_.empty(); | 315 return !worker_threads_.empty(); |
| 309 } | 316 } |
| 310 | 317 |
| 311 void SchedulerThreadPool::WakeUpOneThread() { | 318 void SchedulerThreadPool::WakeUpOneThread() { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 330 if (idle_worker_threads_stack_.empty()) | 337 if (idle_worker_threads_stack_.empty()) |
| 331 return nullptr; | 338 return nullptr; |
| 332 | 339 |
| 333 auto worker_thread = idle_worker_threads_stack_.top(); | 340 auto worker_thread = idle_worker_threads_stack_.top(); |
| 334 idle_worker_threads_stack_.pop(); | 341 idle_worker_threads_stack_.pop(); |
| 335 return worker_thread; | 342 return worker_thread; |
| 336 } | 343 } |
| 337 | 344 |
| 338 } // namespace internal | 345 } // namespace internal |
| 339 } // namespace base | 346 } // namespace base |
| OLD | NEW |