| 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_thread.h" | 5 #include "base/task_scheduler/scheduler_worker_thread.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/task_scheduler/task_tracker.h" | 12 #include "base/task_scheduler/task_tracker.h" |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 namespace internal { | 15 namespace internal { |
| 16 | 16 |
| 17 std::unique_ptr<SchedulerWorkerThread> | 17 std::unique_ptr<SchedulerWorkerThread> |
| 18 SchedulerWorkerThread::CreateSchedulerWorkerThread( | 18 SchedulerWorkerThread::CreateSchedulerWorkerThread( |
| 19 ThreadPriority thread_priority, | 19 ThreadPriority thread_priority, |
| 20 const Closure& main_entry_callback, | 20 Delegate* delegate, |
| 21 const GetWorkCallback& get_work_callback, | |
| 22 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
| 23 TaskTracker* task_tracker) { | 21 TaskTracker* task_tracker) { |
| 24 std::unique_ptr<SchedulerWorkerThread> worker_thread( | 22 std::unique_ptr<SchedulerWorkerThread> worker_thread( |
| 25 new SchedulerWorkerThread(thread_priority, main_entry_callback, | 23 new SchedulerWorkerThread(thread_priority, delegate, task_tracker)); |
| 26 get_work_callback, | |
| 27 ran_task_from_sequence_callback, task_tracker)); | |
| 28 | 24 |
| 29 if (worker_thread->thread_handle_.is_null()) | 25 if (worker_thread->thread_handle_.is_null()) |
| 30 return nullptr; | 26 return nullptr; |
| 31 return worker_thread; | 27 return worker_thread; |
| 32 } | 28 } |
| 33 | 29 |
| 34 SchedulerWorkerThread::~SchedulerWorkerThread() { | 30 SchedulerWorkerThread::~SchedulerWorkerThread() { |
| 35 DCHECK(ShouldExitForTesting()); | 31 DCHECK(ShouldExitForTesting()); |
| 36 } | 32 } |
| 37 | 33 |
| 38 void SchedulerWorkerThread::WakeUp() { | 34 void SchedulerWorkerThread::WakeUp() { |
| 39 wake_up_event_.Signal(); | 35 wake_up_event_.Signal(); |
| 40 } | 36 } |
| 41 | 37 |
| 42 void SchedulerWorkerThread::JoinForTesting() { | 38 void SchedulerWorkerThread::JoinForTesting() { |
| 43 { | 39 { |
| 44 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); | 40 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); |
| 45 should_exit_for_testing_ = true; | 41 should_exit_for_testing_ = true; |
| 46 } | 42 } |
| 47 WakeUp(); | 43 WakeUp(); |
| 48 PlatformThread::Join(thread_handle_); | 44 PlatformThread::Join(thread_handle_); |
| 49 } | 45 } |
| 50 | 46 |
| 51 SchedulerWorkerThread::SchedulerWorkerThread( | 47 SchedulerWorkerThread::SchedulerWorkerThread(ThreadPriority thread_priority, |
| 52 ThreadPriority thread_priority, | 48 Delegate* delegate, |
| 53 const Closure& main_entry_callback, | 49 TaskTracker* task_tracker) |
| 54 const GetWorkCallback& get_work_callback, | |
| 55 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
| 56 TaskTracker* task_tracker) | |
| 57 : wake_up_event_(false, false), | 50 : wake_up_event_(false, false), |
| 58 main_entry_callback_(main_entry_callback), | 51 delegate_(delegate), |
| 59 get_work_callback_(get_work_callback), | |
| 60 ran_task_from_sequence_callback_(ran_task_from_sequence_callback), | |
| 61 task_tracker_(task_tracker) { | 52 task_tracker_(task_tracker) { |
| 62 DCHECK(!main_entry_callback_.is_null()); | 53 DCHECK(delegate_); |
| 63 DCHECK(!get_work_callback_.is_null()); | |
| 64 DCHECK(!ran_task_from_sequence_callback_.is_null()); | |
| 65 DCHECK(task_tracker_); | 54 DCHECK(task_tracker_); |
| 66 | 55 |
| 67 static const size_t kDefaultStackSize = 0; | 56 static const size_t kDefaultStackSize = 0; |
| 68 PlatformThread::CreateWithPriority(kDefaultStackSize, this, &thread_handle_, | 57 PlatformThread::CreateWithPriority(kDefaultStackSize, this, &thread_handle_, |
| 69 thread_priority); | 58 thread_priority); |
| 70 } | 59 } |
| 71 | 60 |
| 72 void SchedulerWorkerThread::ThreadMain() { | 61 void SchedulerWorkerThread::ThreadMain() { |
| 73 main_entry_callback_.Run(); | 62 delegate_->OnMainEntry(); |
| 74 | 63 |
| 75 // A SchedulerWorkerThread starts out sleeping. | 64 // A SchedulerWorkerThread starts out sleeping. |
| 76 wake_up_event_.Wait(); | 65 wake_up_event_.Wait(); |
| 77 | 66 |
| 78 while (!task_tracker_->shutdown_completed() && !ShouldExitForTesting()) { | 67 while (!task_tracker_->shutdown_completed() && !ShouldExitForTesting()) { |
| 79 // Get the sequence containing the next task to execute. | 68 // Get the sequence containing the next task to execute. |
| 80 scoped_refptr<Sequence> sequence = get_work_callback_.Run(this); | 69 scoped_refptr<Sequence> sequence = delegate_->GetWork(this); |
| 81 | 70 |
| 82 if (!sequence) { | 71 if (!sequence) { |
| 83 wake_up_event_.Wait(); | 72 wake_up_event_.Wait(); |
| 84 continue; | 73 continue; |
| 85 } | 74 } |
| 86 | 75 |
| 87 task_tracker_->RunTask(sequence->PeekTask()); | 76 task_tracker_->RunTask(sequence->PeekTask()); |
| 88 ran_task_from_sequence_callback_.Run(this, std::move(sequence)); | 77 delegate_->RanTaskFromSequence(std::move(sequence)); |
| 89 | 78 |
| 90 // Calling WakeUp() guarantees that this SchedulerWorkerThread will run | 79 // Calling WakeUp() guarantees that this SchedulerWorkerThread will run |
| 91 // Tasks from Sequences returned by |get_work_callback_| until the callback | 80 // Tasks from Sequences returned by the GetWork() method of |delegate_| |
| 92 // returns nullptr. Resetting |wake_up_event_| here doesn't break this | 81 // until it returns nullptr. Resetting |wake_up_event_| here doesn't break |
| 93 // invariant and avoids a useless loop iteration before going to sleep if | 82 // this invariant and avoids a useless loop iteration before going to sleep |
| 94 // WakeUp() is called while this SchedulerWorkerThread is awake. | 83 // if WakeUp() is called while this SchedulerWorkerThread is awake. |
| 95 wake_up_event_.Reset(); | 84 wake_up_event_.Reset(); |
| 96 } | 85 } |
| 86 |
| 87 delegate_->OnMainExit(); |
| 97 } | 88 } |
| 98 | 89 |
| 99 bool SchedulerWorkerThread::ShouldExitForTesting() const { | 90 bool SchedulerWorkerThread::ShouldExitForTesting() const { |
| 100 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); | 91 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); |
| 101 return should_exit_for_testing_; | 92 return should_exit_for_testing_; |
| 102 } | 93 } |
| 103 | 94 |
| 104 } // namespace internal | 95 } // namespace internal |
| 105 } // namespace base | 96 } // namespace base |
| OLD | NEW |