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.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> SchedulerWorkerThread::Create( | 17 std::unique_ptr<SchedulerWorker> SchedulerWorker::Create( |
18 ThreadPriority thread_priority, | 18 ThreadPriority thread_priority, |
19 std::unique_ptr<Delegate> delegate, | 19 std::unique_ptr<Delegate> delegate, |
20 TaskTracker* task_tracker) { | 20 TaskTracker* task_tracker) { |
21 std::unique_ptr<SchedulerWorkerThread> worker_thread( | 21 std::unique_ptr<SchedulerWorker> worker( |
22 new SchedulerWorkerThread(thread_priority, std::move(delegate), | 22 new SchedulerWorker(thread_priority, std::move(delegate), |
23 task_tracker)); | 23 task_tracker)); |
24 | 24 |
25 if (worker_thread->thread_handle_.is_null()) | 25 if (worker->thread_handle_.is_null()) |
26 return nullptr; | 26 return nullptr; |
27 return worker_thread; | 27 return worker; |
28 } | 28 } |
29 | 29 |
30 SchedulerWorkerThread::~SchedulerWorkerThread() { | 30 SchedulerWorker::~SchedulerWorker() { |
31 DCHECK(ShouldExitForTesting()); | 31 DCHECK(ShouldExitForTesting()); |
32 } | 32 } |
33 | 33 |
34 void SchedulerWorkerThread::WakeUp() { | 34 void SchedulerWorker::WakeUp() { |
35 wake_up_event_.Signal(); | 35 wake_up_event_.Signal(); |
36 } | 36 } |
37 | 37 |
38 void SchedulerWorkerThread::JoinForTesting() { | 38 void SchedulerWorker::JoinForTesting() { |
39 { | 39 { |
40 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); | 40 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); |
41 should_exit_for_testing_ = true; | 41 should_exit_for_testing_ = true; |
42 } | 42 } |
43 WakeUp(); | 43 WakeUp(); |
44 PlatformThread::Join(thread_handle_); | 44 PlatformThread::Join(thread_handle_); |
45 } | 45 } |
46 | 46 |
47 SchedulerWorkerThread::SchedulerWorkerThread(ThreadPriority thread_priority, | 47 SchedulerWorker::SchedulerWorker(ThreadPriority thread_priority, |
48 std::unique_ptr<Delegate> delegate, | 48 std::unique_ptr<Delegate> delegate, |
49 TaskTracker* task_tracker) | 49 TaskTracker* task_tracker) |
50 : wake_up_event_(WaitableEvent::ResetPolicy::AUTOMATIC, | 50 : wake_up_event_(WaitableEvent::ResetPolicy::AUTOMATIC, |
51 WaitableEvent::InitialState::NOT_SIGNALED), | 51 WaitableEvent::InitialState::NOT_SIGNALED), |
52 delegate_(std::move(delegate)), | 52 delegate_(std::move(delegate)), |
53 task_tracker_(task_tracker) { | 53 task_tracker_(task_tracker) { |
54 DCHECK(delegate_); | 54 DCHECK(delegate_); |
55 DCHECK(task_tracker_); | 55 DCHECK(task_tracker_); |
56 | 56 |
57 const size_t kDefaultStackSize = 0; | 57 const size_t kDefaultStackSize = 0; |
58 PlatformThread::CreateWithPriority(kDefaultStackSize, this, &thread_handle_, | 58 PlatformThread::CreateWithPriority(kDefaultStackSize, this, &thread_handle_, |
59 thread_priority); | 59 thread_priority); |
60 } | 60 } |
61 | 61 |
62 void SchedulerWorkerThread::ThreadMain() { | 62 void SchedulerWorker::ThreadMain() { |
63 delegate_->OnMainEntry(this); | 63 delegate_->OnMainEntry(this); |
64 | 64 |
65 // A SchedulerWorkerThread starts out sleeping. | 65 // A SchedulerWorker starts out sleeping. |
66 wake_up_event_.Wait(); | 66 wake_up_event_.Wait(); |
67 | 67 |
68 while (!task_tracker_->shutdown_completed() && !ShouldExitForTesting()) { | 68 while (!task_tracker_->shutdown_completed() && !ShouldExitForTesting()) { |
69 // Get the sequence containing the next task to execute. | 69 // Get the sequence containing the next task to execute. |
70 scoped_refptr<Sequence> sequence = delegate_->GetWork(this); | 70 scoped_refptr<Sequence> sequence = delegate_->GetWork(this); |
71 | 71 |
72 if (!sequence) { | 72 if (!sequence) { |
73 TimeDelta sleep_time = delegate_->GetSleepTimeout(); | 73 TimeDelta sleep_time = delegate_->GetSleepTimeout(); |
74 if (sleep_time.is_max()) { | 74 if (sleep_time.is_max()) { |
75 // Calling TimedWait with TimeDelta::Max is not recommended per | 75 // Calling TimedWait with TimeDelta::Max is not recommended per |
76 // http://crbug.com/465948. | 76 // http://crbug.com/465948. |
77 wake_up_event_.Wait(); | 77 wake_up_event_.Wait(); |
78 } else { | 78 } else { |
79 wake_up_event_.TimedWait(sleep_time); | 79 wake_up_event_.TimedWait(sleep_time); |
80 } | 80 } |
81 continue; | 81 continue; |
82 } | 82 } |
83 | 83 |
84 task_tracker_->RunTask(sequence->PeekTask()); | 84 task_tracker_->RunTask(sequence->PeekTask()); |
85 | 85 |
86 const bool sequence_became_empty = sequence->PopTask(); | 86 const bool sequence_became_empty = sequence->PopTask(); |
87 | 87 |
88 // If |sequence| isn't empty immediately after the pop, re-enqueue it to | 88 // If |sequence| isn't empty immediately after the pop, re-enqueue it to |
89 // maintain the invariant that a non-empty Sequence is always referenced by | 89 // maintain the invariant that a non-empty Sequence is always referenced by |
90 // either a PriorityQueue or a SchedulerWorkerThread. If it is empty and | 90 // either a PriorityQueue or a SchedulerWorker. If it is empty and there are |
91 // there are live references to it, it will be enqueued when a Task is added | 91 // live references to it, it will be enqueued when a Task is added to it. |
92 // to it. Otherwise, it will be destroyed at the end of this scope. | 92 // Otherwise, it will be destroyed at the end of this scope. |
93 if (!sequence_became_empty) | 93 if (!sequence_became_empty) |
94 delegate_->ReEnqueueSequence(std::move(sequence)); | 94 delegate_->ReEnqueueSequence(std::move(sequence)); |
95 | 95 |
96 // Calling WakeUp() guarantees that this SchedulerWorkerThread will run | 96 // Calling WakeUp() guarantees that this SchedulerWorker will run Tasks from |
97 // Tasks from Sequences returned by the GetWork() method of |delegate_| | 97 // Sequences returned by the GetWork() method of |delegate_| until it |
98 // until it returns nullptr. Resetting |wake_up_event_| here doesn't break | 98 // returns nullptr. Resetting |wake_up_event_| here doesn't break this |
99 // this invariant and avoids a useless loop iteration before going to sleep | 99 // invariant and avoids a useless loop iteration before going to sleep if |
100 // if WakeUp() is called while this SchedulerWorkerThread is awake. | 100 // WakeUp() is called while this SchedulerWorker is awake. |
101 wake_up_event_.Reset(); | 101 wake_up_event_.Reset(); |
102 } | 102 } |
103 } | 103 } |
104 | 104 |
105 bool SchedulerWorkerThread::ShouldExitForTesting() const { | 105 bool SchedulerWorker::ShouldExitForTesting() const { |
106 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); | 106 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); |
107 return should_exit_for_testing_; | 107 return should_exit_for_testing_; |
108 } | 108 } |
109 | 109 |
110 } // namespace internal | 110 } // namespace internal |
111 } // namespace base | 111 } // namespace base |
OLD | NEW |