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

Side by Side Diff: base/task_scheduler/scheduler_thread_pool.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: 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.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 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
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<SchedulerThreadPool> SchedulerThreadPool::CreateThreadPool( 137 std::unique_ptr<SchedulerThreadPool> SchedulerThreadPool::CreateThreadPool(
138 ThreadPriority thread_priority, 138 ThreadPriority thread_priority,
139 size_t max_threads, 139 size_t max_threads,
140 const EnqueueSequenceCallback& enqueue_sequence_callback, 140 const EnqueueSequenceCallback& enqueue_sequence_callback,
141 TaskTracker* task_tracker, 141 TaskTracker* task_tracker,
142 DelayedTaskManager* delayed_task_manager) { 142 DelayedTaskManager* delayed_task_manager) {
143 std::unique_ptr<SchedulerThreadPool> thread_pool(new SchedulerThreadPool( 143 std::unique_ptr<SchedulerThreadPool> thread_pool(
144 enqueue_sequence_callback, task_tracker, delayed_task_manager)); 144 new SchedulerThreadPool(task_tracker, delayed_task_manager));
145 if (thread_pool->Initialize(thread_priority, max_threads)) 145 if (thread_pool->Initialize(thread_priority, max_threads,
146 enqueue_sequence_callback))
146 return thread_pool; 147 return thread_pool;
147 return nullptr; 148 return nullptr;
148 } 149 }
149 150
150 scoped_refptr<TaskRunner> SchedulerThreadPool::CreateTaskRunnerWithTraits( 151 scoped_refptr<TaskRunner> SchedulerThreadPool::CreateTaskRunnerWithTraits(
151 const TaskTraits& traits, 152 const TaskTraits& traits,
152 ExecutionMode execution_mode) { 153 ExecutionMode execution_mode) {
153 switch (execution_mode) { 154 switch (execution_mode) {
154 case ExecutionMode::PARALLEL: 155 case ExecutionMode::PARALLEL:
155 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this)); 156 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this));
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 transaction->Pop(); 290 transaction->Pop();
290 return sequence; 291 return sequence;
291 } 292 }
292 293
293 void SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::EnqueueSequence( 294 void SchedulerThreadPool::SchedulerWorkerThreadDelegateImpl::EnqueueSequence(
294 scoped_refptr<Sequence> sequence) { 295 scoped_refptr<Sequence> sequence) {
295 enqueue_sequence_callback_.Run(std::move(sequence)); 296 enqueue_sequence_callback_.Run(std::move(sequence));
296 } 297 }
297 298
298 SchedulerThreadPool::SchedulerThreadPool( 299 SchedulerThreadPool::SchedulerThreadPool(
299 const EnqueueSequenceCallback& enqueue_sequence_callback,
300 TaskTracker* task_tracker, 300 TaskTracker* task_tracker,
301 DelayedTaskManager* delayed_task_manager) 301 DelayedTaskManager* delayed_task_manager)
302 : idle_worker_threads_stack_lock_(shared_priority_queue_.container_lock()), 302 : idle_worker_threads_stack_lock_(shared_priority_queue_.container_lock()),
303 idle_worker_threads_stack_cv_for_testing_( 303 idle_worker_threads_stack_cv_for_testing_(
304 idle_worker_threads_stack_lock_.CreateConditionVariable()), 304 idle_worker_threads_stack_lock_.CreateConditionVariable()),
305 join_for_testing_returned_(true, false), 305 join_for_testing_returned_(true, false),
306 worker_thread_delegate_(
307 new SchedulerWorkerThreadDelegateImpl(this,
308 enqueue_sequence_callback)),
309 task_tracker_(task_tracker), 306 task_tracker_(task_tracker),
310 delayed_task_manager_(delayed_task_manager) { 307 delayed_task_manager_(delayed_task_manager) {
311 DCHECK(task_tracker_); 308 DCHECK(task_tracker_);
312 DCHECK(delayed_task_manager_); 309 DCHECK(delayed_task_manager_);
313 } 310 }
314 311
315 bool SchedulerThreadPool::Initialize(ThreadPriority thread_priority, 312 bool SchedulerThreadPool::Initialize(
316 size_t max_threads) { 313 ThreadPriority thread_priority,
314 size_t max_threads,
315 const EnqueueSequenceCallback& enqueue_sequence_callback) {
317 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); 316 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_);
318 317
319 DCHECK(worker_threads_.empty()); 318 DCHECK(worker_threads_.empty());
320 319
321 for (size_t i = 0; i < max_threads; ++i) { 320 for (size_t i = 0; i < max_threads; ++i) {
322 std::unique_ptr<SchedulerWorkerThread> worker_thread = 321 std::unique_ptr<SchedulerWorkerThread> worker_thread =
323 SchedulerWorkerThread::CreateSchedulerWorkerThread( 322 SchedulerWorkerThread::CreateWorkerThread(
324 thread_priority, worker_thread_delegate_.get(), task_tracker_); 323 thread_priority, WrapUnique(new SchedulerWorkerThreadDelegateImpl(
324 this, enqueue_sequence_callback)),
325 task_tracker_);
325 if (!worker_thread) 326 if (!worker_thread)
326 break; 327 break;
327 idle_worker_threads_stack_.Push(worker_thread.get()); 328 idle_worker_threads_stack_.Push(worker_thread.get());
328 worker_threads_.push_back(std::move(worker_thread)); 329 worker_threads_.push_back(std::move(worker_thread));
329 } 330 }
330 331
331 return !worker_threads_.empty(); 332 return !worker_threads_.empty();
332 } 333 }
333 334
334 void SchedulerThreadPool::WakeUpOneThread() { 335 void SchedulerThreadPool::WakeUpOneThread() {
(...skipping 16 matching lines...) Expand all
351 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); 352 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_);
352 353
353 if (idle_worker_threads_stack_.Empty()) 354 if (idle_worker_threads_stack_.Empty())
354 return nullptr; 355 return nullptr;
355 356
356 return idle_worker_threads_stack_.Pop(); 357 return idle_worker_threads_stack_.Pop();
357 } 358 }
358 359
359 } // namespace internal 360 } // namespace internal
360 } // namespace base 361 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698