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