Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(395)

Side by Side Diff: base/task_scheduler/scheduler_thread_pool_impl.cc

Issue 1903103007: TaskScheduler: Make SchedulerWorkerThread own its delegate. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sched_2b_remove_utils
Patch Set: rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_impl.h" 5 #include "base/task_scheduler/scheduler_thread_pool_impl.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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 127
128 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThreadDelegateImpl); 128 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThreadDelegateImpl);
129 }; 129 };
130 130
131 SchedulerThreadPoolImpl::~SchedulerThreadPoolImpl() { 131 SchedulerThreadPoolImpl::~SchedulerThreadPoolImpl() {
132 // SchedulerThreadPool should never be deleted in production unless its 132 // SchedulerThreadPool should never be deleted in production unless its
133 // initialization failed. 133 // initialization failed.
134 DCHECK(join_for_testing_returned_.IsSignaled() || worker_threads_.empty()); 134 DCHECK(join_for_testing_returned_.IsSignaled() || worker_threads_.empty());
135 } 135 }
136 136
137 std::unique_ptr<SchedulerThreadPoolImpl> 137 std::unique_ptr<SchedulerThreadPoolImpl> SchedulerThreadPoolImpl::Create(
138 SchedulerThreadPoolImpl::CreateThreadPool(
139 ThreadPriority thread_priority, 138 ThreadPriority thread_priority,
140 size_t max_threads, 139 size_t max_threads,
141 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, 140 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback,
142 TaskTracker* task_tracker, 141 TaskTracker* task_tracker,
143 DelayedTaskManager* delayed_task_manager) { 142 DelayedTaskManager* delayed_task_manager) {
144 std::unique_ptr<SchedulerThreadPoolImpl> thread_pool( 143 std::unique_ptr<SchedulerThreadPoolImpl> thread_pool(
145 new SchedulerThreadPoolImpl(re_enqueue_sequence_callback, task_tracker, 144 new SchedulerThreadPoolImpl(task_tracker, delayed_task_manager));
146 delayed_task_manager)); 145 if (thread_pool->Initialize(thread_priority, max_threads,
147 if (thread_pool->Initialize(thread_priority, max_threads)) 146 re_enqueue_sequence_callback)) {
148 return thread_pool; 147 return thread_pool;
148 }
149 return nullptr; 149 return nullptr;
150 } 150 }
151 151
152 void SchedulerThreadPoolImpl::WaitForAllWorkerThreadsIdleForTesting() { 152 void SchedulerThreadPoolImpl::WaitForAllWorkerThreadsIdleForTesting() {
153 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); 153 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_);
154 while (idle_worker_threads_stack_.Size() < worker_threads_.size()) 154 while (idle_worker_threads_stack_.Size() < worker_threads_.size())
155 idle_worker_threads_stack_cv_for_testing_->Wait(); 155 idle_worker_threads_stack_cv_for_testing_->Wait();
156 } 156 }
157 157
158 void SchedulerThreadPoolImpl::JoinForTesting() { 158 void SchedulerThreadPoolImpl::JoinForTesting() {
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 transaction->Pop(); 292 transaction->Pop();
293 return sequence; 293 return sequence;
294 } 294 }
295 295
296 void SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: 296 void SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl::
297 ReEnqueueSequence(scoped_refptr<Sequence> sequence) { 297 ReEnqueueSequence(scoped_refptr<Sequence> sequence) {
298 re_enqueue_sequence_callback_.Run(std::move(sequence)); 298 re_enqueue_sequence_callback_.Run(std::move(sequence));
299 } 299 }
300 300
301 SchedulerThreadPoolImpl::SchedulerThreadPoolImpl( 301 SchedulerThreadPoolImpl::SchedulerThreadPoolImpl(
302 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback,
303 TaskTracker* task_tracker, 302 TaskTracker* task_tracker,
304 DelayedTaskManager* delayed_task_manager) 303 DelayedTaskManager* delayed_task_manager)
305 : idle_worker_threads_stack_lock_(shared_priority_queue_.container_lock()), 304 : idle_worker_threads_stack_lock_(shared_priority_queue_.container_lock()),
306 idle_worker_threads_stack_cv_for_testing_( 305 idle_worker_threads_stack_cv_for_testing_(
307 idle_worker_threads_stack_lock_.CreateConditionVariable()), 306 idle_worker_threads_stack_lock_.CreateConditionVariable()),
308 join_for_testing_returned_(true, false), 307 join_for_testing_returned_(true, false),
309 worker_thread_delegate_(
310 new SchedulerWorkerThreadDelegateImpl(this,
311 re_enqueue_sequence_callback)),
312 task_tracker_(task_tracker), 308 task_tracker_(task_tracker),
313 delayed_task_manager_(delayed_task_manager) { 309 delayed_task_manager_(delayed_task_manager) {
314 DCHECK(task_tracker_); 310 DCHECK(task_tracker_);
315 DCHECK(delayed_task_manager_); 311 DCHECK(delayed_task_manager_);
316 } 312 }
317 313
318 bool SchedulerThreadPoolImpl::Initialize(ThreadPriority thread_priority, 314 bool SchedulerThreadPoolImpl::Initialize(
319 size_t max_threads) { 315 ThreadPriority thread_priority,
316 size_t max_threads,
317 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback) {
320 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); 318 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_);
321 319
322 DCHECK(worker_threads_.empty()); 320 DCHECK(worker_threads_.empty());
323 321
324 for (size_t i = 0; i < max_threads; ++i) { 322 for (size_t i = 0; i < max_threads; ++i) {
325 std::unique_ptr<SchedulerWorkerThread> worker_thread = 323 std::unique_ptr<SchedulerWorkerThread> worker_thread =
326 SchedulerWorkerThread::CreateSchedulerWorkerThread( 324 SchedulerWorkerThread::Create(
327 thread_priority, worker_thread_delegate_.get(), task_tracker_); 325 thread_priority, WrapUnique(new SchedulerWorkerThreadDelegateImpl(
326 this, re_enqueue_sequence_callback)),
327 task_tracker_);
328 if (!worker_thread) 328 if (!worker_thread)
329 break; 329 break;
330 idle_worker_threads_stack_.Push(worker_thread.get()); 330 idle_worker_threads_stack_.Push(worker_thread.get());
331 worker_threads_.push_back(std::move(worker_thread)); 331 worker_threads_.push_back(std::move(worker_thread));
332 } 332 }
333 333
334 return !worker_threads_.empty(); 334 return !worker_threads_.empty();
335 } 335 }
336 336
337 void SchedulerThreadPoolImpl::WakeUpOneThread() { 337 void SchedulerThreadPoolImpl::WakeUpOneThread() {
(...skipping 11 matching lines...) Expand all
349 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); 349 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_);
350 idle_worker_threads_stack_.Push(worker_thread); 350 idle_worker_threads_stack_.Push(worker_thread);
351 DCHECK_LE(idle_worker_threads_stack_.Size(), worker_threads_.size()); 351 DCHECK_LE(idle_worker_threads_stack_.Size(), worker_threads_.size());
352 352
353 if (idle_worker_threads_stack_.Size() == worker_threads_.size()) 353 if (idle_worker_threads_stack_.Size() == worker_threads_.size())
354 idle_worker_threads_stack_cv_for_testing_->Broadcast(); 354 idle_worker_threads_stack_cv_for_testing_->Broadcast();
355 } 355 }
356 356
357 } // namespace internal 357 } // namespace internal
358 } // namespace base 358 } // namespace base
OLDNEW
« no previous file with comments | « base/task_scheduler/scheduler_thread_pool_impl.h ('k') | base/task_scheduler/scheduler_thread_pool_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698