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_worker_pool_impl.h" | 5 #include "base/task_scheduler/scheduler_worker_pool_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 }; | 220 }; |
221 | 221 |
222 SchedulerWorkerPoolImpl::~SchedulerWorkerPoolImpl() { | 222 SchedulerWorkerPoolImpl::~SchedulerWorkerPoolImpl() { |
223 // SchedulerWorkerPool should never be deleted in production unless its | 223 // SchedulerWorkerPool should never be deleted in production unless its |
224 // initialization failed. | 224 // initialization failed. |
225 DCHECK(join_for_testing_returned_.IsSignaled() || workers_.empty()); | 225 DCHECK(join_for_testing_returned_.IsSignaled() || workers_.empty()); |
226 } | 226 } |
227 | 227 |
228 // static | 228 // static |
229 std::unique_ptr<SchedulerWorkerPoolImpl> SchedulerWorkerPoolImpl::Create( | 229 std::unique_ptr<SchedulerWorkerPoolImpl> SchedulerWorkerPoolImpl::Create( |
230 StringPiece name, | 230 const SchedulerWorkerPoolParams& params, |
231 ThreadPriority thread_priority, | |
232 size_t max_threads, | |
233 IORestriction io_restriction, | |
234 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | 231 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, |
235 TaskTracker* task_tracker, | 232 TaskTracker* task_tracker, |
236 DelayedTaskManager* delayed_task_manager) { | 233 DelayedTaskManager* delayed_task_manager) { |
237 std::unique_ptr<SchedulerWorkerPoolImpl> worker_pool( | 234 std::unique_ptr<SchedulerWorkerPoolImpl> worker_pool( |
238 new SchedulerWorkerPoolImpl(name, io_restriction, task_tracker, | 235 new SchedulerWorkerPoolImpl(params.name(), |
239 delayed_task_manager)); | 236 params.io_restriction(), |
240 if (worker_pool->Initialize(thread_priority, max_threads, | 237 task_tracker, delayed_task_manager)); |
| 238 if (worker_pool->Initialize(params.thread_priority(), |
| 239 params.max_threads(), |
241 re_enqueue_sequence_callback)) { | 240 re_enqueue_sequence_callback)) { |
242 return worker_pool; | 241 return worker_pool; |
243 } | 242 } |
244 return nullptr; | 243 return nullptr; |
245 } | 244 } |
246 | 245 |
247 void SchedulerWorkerPoolImpl::WaitForAllWorkersIdleForTesting() { | 246 void SchedulerWorkerPoolImpl::WaitForAllWorkersIdleForTesting() { |
248 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); | 247 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); |
249 while (idle_workers_stack_.Size() < workers_.size()) | 248 while (idle_workers_stack_.Size() < workers_.size()) |
250 idle_workers_stack_cv_for_testing_->Wait(); | 249 idle_workers_stack_cv_for_testing_->Wait(); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 #endif | 391 #endif |
393 | 392 |
394 PlatformThread::SetName( | 393 PlatformThread::SetName( |
395 StringPrintf("%sWorker%d", outer_->name_.c_str(), index_)); | 394 StringPrintf("%sWorker%d", outer_->name_.c_str(), index_)); |
396 | 395 |
397 DCHECK(!tls_current_worker.Get().Get()); | 396 DCHECK(!tls_current_worker.Get().Get()); |
398 DCHECK(!tls_current_worker_pool.Get().Get()); | 397 DCHECK(!tls_current_worker_pool.Get().Get()); |
399 tls_current_worker.Get().Set(worker); | 398 tls_current_worker.Get().Set(worker); |
400 tls_current_worker_pool.Get().Set(outer_); | 399 tls_current_worker_pool.Get().Set(outer_); |
401 | 400 |
402 ThreadRestrictions::SetIOAllowed(outer_->io_restriction_ == | 401 ThreadRestrictions::SetIOAllowed( |
403 IORestriction::ALLOWED); | 402 outer_->io_restriction_ == |
| 403 SchedulerWorkerPoolParams::IORestriction::ALLOWED); |
404 } | 404 } |
405 | 405 |
406 scoped_refptr<Sequence> | 406 scoped_refptr<Sequence> |
407 SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::GetWork( | 407 SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::GetWork( |
408 SchedulerWorker* worker) { | 408 SchedulerWorker* worker) { |
409 DCHECK(ContainsWorker(outer_->workers_, worker)); | 409 DCHECK(ContainsWorker(outer_->workers_, worker)); |
410 | 410 |
411 scoped_refptr<Sequence> sequence; | 411 scoped_refptr<Sequence> sequence; |
412 { | 412 { |
413 std::unique_ptr<PriorityQueue::Transaction> shared_transaction( | 413 std::unique_ptr<PriorityQueue::Transaction> shared_transaction( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 return TimeDelta::Max(); | 479 return TimeDelta::Max(); |
480 } | 480 } |
481 | 481 |
482 bool SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::CanDetach( | 482 bool SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::CanDetach( |
483 SchedulerWorker* worker) { | 483 SchedulerWorker* worker) { |
484 return false; | 484 return false; |
485 } | 485 } |
486 | 486 |
487 SchedulerWorkerPoolImpl::SchedulerWorkerPoolImpl( | 487 SchedulerWorkerPoolImpl::SchedulerWorkerPoolImpl( |
488 StringPiece name, | 488 StringPiece name, |
489 IORestriction io_restriction, | 489 SchedulerWorkerPoolParams::IORestriction io_restriction, |
490 TaskTracker* task_tracker, | 490 TaskTracker* task_tracker, |
491 DelayedTaskManager* delayed_task_manager) | 491 DelayedTaskManager* delayed_task_manager) |
492 : name_(name.as_string()), | 492 : name_(name.as_string()), |
493 io_restriction_(io_restriction), | 493 io_restriction_(io_restriction), |
494 idle_workers_stack_lock_(shared_priority_queue_.container_lock()), | 494 idle_workers_stack_lock_(shared_priority_queue_.container_lock()), |
495 idle_workers_stack_cv_for_testing_( | 495 idle_workers_stack_cv_for_testing_( |
496 idle_workers_stack_lock_.CreateConditionVariable()), | 496 idle_workers_stack_lock_.CreateConditionVariable()), |
497 join_for_testing_returned_(WaitableEvent::ResetPolicy::MANUAL, | 497 join_for_testing_returned_(WaitableEvent::ResetPolicy::MANUAL, |
498 WaitableEvent::InitialState::NOT_SIGNALED), | 498 WaitableEvent::InitialState::NOT_SIGNALED), |
499 #if DCHECK_IS_ON() | 499 #if DCHECK_IS_ON() |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 } | 556 } |
557 | 557 |
558 void SchedulerWorkerPoolImpl::RemoveFromIdleWorkersStack( | 558 void SchedulerWorkerPoolImpl::RemoveFromIdleWorkersStack( |
559 SchedulerWorker* worker) { | 559 SchedulerWorker* worker) { |
560 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); | 560 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); |
561 idle_workers_stack_.Remove(worker); | 561 idle_workers_stack_.Remove(worker); |
562 } | 562 } |
563 | 563 |
564 } // namespace internal | 564 } // namespace internal |
565 } // namespace base | 565 } // namespace base |
OLD | NEW |