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 class SchedulerWorkerThread::Worker : public PlatformThread::Delegate { | |
18 public: | |
19 ~Worker() override = default; | |
20 | |
21 static std::unique_ptr<Worker> Create(SchedulerWorkerThread* outer) { | |
22 std::unique_ptr<Worker> worker(new Worker(outer)); | |
23 worker->Initialize(); | |
24 if (worker->thread_handle_.is_null()) | |
25 return nullptr; | |
26 return worker; | |
27 } | |
28 | |
29 // PlatformThread::Delegate. | |
30 void ThreadMain() override { | |
31 // Set if this thread was detached. | |
32 std::unique_ptr<Worker> detached_worker; | |
33 | |
34 outer_->delegate_->OnMainEntry(outer_); | |
35 | |
36 // A SchedulerWorkerThread starts out waiting for work. | |
37 WaitForWork(); | |
38 | |
39 while (!outer_->task_tracker_->shutdown_completed() && | |
40 !outer_->ShouldExitForTesting()) { | |
41 DCHECK(outer_); | |
42 // Get the sequence containing the next task to execute. | |
43 scoped_refptr<Sequence> sequence = outer_->delegate_->GetWork(outer_); | |
44 if (!sequence) { | |
45 if (outer_->delegate_->CanDetach(outer_)) { | |
46 detached_worker = outer_->Detach(); | |
47 if (detached_worker) { | |
48 DCHECK_EQ(detached_worker.get(), this); | |
49 PlatformThread::Detach(thread_handle_); | |
50 outer_ = nullptr; | |
51 break; | |
52 } | |
53 } | |
54 WaitForWork(); | |
55 continue; | |
56 } | |
57 | |
58 outer_->task_tracker_->RunTask(sequence->PeekTask()); | |
59 | |
60 const bool sequence_became_empty = sequence->PopTask(); | |
61 | |
62 // If |sequence| isn't empty immediately after the pop, re-enqueue it to | |
63 // maintain the invariant that a non-empty Sequence is always referenced | |
64 // by either a PriorityQueue or a SchedulerWorkerThread. If it is empty | |
65 // and there are live references to it, it will be enqueued when a Task is | |
66 // added to it. Otherwise, it will be destroyed at the end of this scope. | |
67 if (!sequence_became_empty) | |
68 outer_->delegate_->ReEnqueueSequence(std::move(sequence)); | |
69 | |
70 // Calling WakeUp() guarantees that this SchedulerWorkerThread will run | |
71 // Tasks from Sequences returned by the GetWork() method of |delegate_| | |
72 // until it returns nullptr. Resetting |wake_up_event_| here doesn't break | |
73 // this invariant and avoids a useless loop iteration before going to | |
74 // sleep if WakeUp() is called while this SchedulerWorkerThread is awake. | |
75 wake_up_event_.Reset(); | |
76 } | |
77 | |
78 // If a wake up is pending and we successfully detached, somehow outer_ was | |
gab
2016/06/13 19:23:05
|outer_| x2
robliao
2016/06/13 23:23:28
Done.
| |
79 // able to signal us which means it probably thinks we're still alive. This | |
80 // is bad as it will cause the WakeUp to no-op and outer_ will be stuck | |
81 // forever. | |
82 DCHECK(!detached_worker || !IsWakeUpPending()) << | |
83 "This thread was detached and woken up at the same time."; | |
84 } | |
85 | |
86 void Join() { PlatformThread::Join(thread_handle_); } | |
87 | |
88 void WakeUp() { wake_up_event_.Signal(); } | |
89 | |
90 bool IsWakeUpPending() { return wake_up_event_.IsSignaled(); } | |
91 | |
92 private: | |
93 Worker(SchedulerWorkerThread* outer) | |
94 : outer_(outer), | |
95 wake_up_event_(WaitableEvent::ResetPolicy::MANUAL, | |
96 WaitableEvent::InitialState::NOT_SIGNALED) { | |
97 DCHECK(outer_); | |
98 } | |
99 | |
100 void Initialize() { | |
101 const size_t kDefaultStackSize = 0; | |
fdoray
2016/06/13 20:31:17
constexpr
robliao
2016/06/13 23:23:28
Done.
| |
102 PlatformThread::CreateWithPriority(kDefaultStackSize, this, | |
103 &thread_handle_, | |
104 outer_->thread_priority_); | |
105 } | |
106 | |
107 void WaitForWork() { | |
108 DCHECK(outer_); | |
109 const TimeDelta sleep_time = outer_->delegate_->GetSleepTimeout(); | |
110 if (sleep_time.is_max()) { | |
111 // Calling TimedWait with TimeDelta::Max is not recommended per | |
112 // http://crbug.com/465948. | |
113 wake_up_event_.Wait(); | |
114 } else { | |
115 wake_up_event_.TimedWait(sleep_time); | |
116 } | |
117 wake_up_event_.Reset(); | |
118 } | |
119 | |
120 PlatformThreadHandle thread_handle_; | |
121 | |
122 SchedulerWorkerThread* outer_; | |
123 | |
124 // Event signaled to wake up this SchedulerWorkerThread. | |
fdoray
2016/06/13 20:31:17
this Thread (or whatever the name of this class is
robliao
2016/06/13 23:23:28
Adjusted.
| |
125 WaitableEvent wake_up_event_; | |
126 | |
127 DISALLOW_COPY_AND_ASSIGN(Worker); | |
128 }; | |
129 | |
17 std::unique_ptr<SchedulerWorkerThread> SchedulerWorkerThread::Create( | 130 std::unique_ptr<SchedulerWorkerThread> SchedulerWorkerThread::Create( |
18 ThreadPriority thread_priority, | 131 ThreadPriority thread_priority, |
19 std::unique_ptr<Delegate> delegate, | 132 std::unique_ptr<Delegate> delegate, |
20 TaskTracker* task_tracker) { | 133 TaskTracker* task_tracker, |
134 InitialState initial_state) { | |
21 std::unique_ptr<SchedulerWorkerThread> worker_thread( | 135 std::unique_ptr<SchedulerWorkerThread> worker_thread( |
22 new SchedulerWorkerThread(thread_priority, std::move(delegate), | 136 new SchedulerWorkerThread(thread_priority, std::move(delegate), |
23 task_tracker)); | 137 task_tracker)); |
138 // Creation happens before any other thread can reference this one, so no | |
139 // synchronization is necessary. | |
140 if (initial_state == SchedulerWorkerThread::InitialState::ALIVE) { | |
141 worker_thread->CreateWorker(); | |
142 if (!worker_thread->worker_) { | |
143 return nullptr; | |
144 } | |
145 } | |
24 | 146 |
25 if (worker_thread->thread_handle_.is_null()) | |
26 return nullptr; | |
27 return worker_thread; | 147 return worker_thread; |
28 } | 148 } |
29 | 149 |
30 SchedulerWorkerThread::~SchedulerWorkerThread() { | 150 SchedulerWorkerThread::~SchedulerWorkerThread() { |
31 DCHECK(ShouldExitForTesting()); | 151 // It is unexpected for worker_ to be alive and for SchedulerWorkerThread to |
gab
2016/06/13 19:23:05
|worker_|
robliao
2016/06/13 23:23:28
Done.
| |
152 // destroy since SchedulerWorkerThread owns the delegate needed by worker_. | |
153 // For testing, this generally means JoinForTesting was not called. | |
154 DCHECK(!worker_); | |
32 } | 155 } |
33 | 156 |
34 void SchedulerWorkerThread::WakeUp() { | 157 void SchedulerWorkerThread::WakeUp() { |
35 wake_up_event_.Signal(); | 158 AutoSchedulerLock auto_lock(worker_lock_); |
159 if (!worker_) | |
160 CreateWorkerAssertSynchronized(); | |
161 | |
162 if (worker_) | |
163 worker_->WakeUp(); | |
36 } | 164 } |
37 | 165 |
38 void SchedulerWorkerThread::JoinForTesting() { | 166 void SchedulerWorkerThread::JoinForTesting() { |
39 { | 167 { |
40 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); | 168 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); |
41 should_exit_for_testing_ = true; | 169 should_exit_for_testing_ = true; |
42 } | 170 } |
43 WakeUp(); | 171 WakeUp(); |
44 PlatformThread::Join(thread_handle_); | 172 |
173 // Normally holding a lock and joining is dangerous. However, since this is | |
174 // only for testing, we're okay since the only scenario that could impact this | |
175 // is a call to Detach, which is disallowed by having the delegate always | |
176 // return false for the CanDetach call. | |
177 AutoSchedulerLock auto_lock(worker_lock_); | |
178 if (worker_) | |
179 worker_->Join(); | |
180 | |
181 worker_.reset(); | |
182 } | |
183 | |
184 bool SchedulerWorkerThread::WorkerAliveForTesting() const { | |
185 AutoSchedulerLock auto_lock(worker_lock_); | |
186 return !!worker_; | |
45 } | 187 } |
46 | 188 |
47 SchedulerWorkerThread::SchedulerWorkerThread(ThreadPriority thread_priority, | 189 SchedulerWorkerThread::SchedulerWorkerThread(ThreadPriority thread_priority, |
48 std::unique_ptr<Delegate> delegate, | 190 std::unique_ptr<Delegate> delegate, |
49 TaskTracker* task_tracker) | 191 TaskTracker* task_tracker) |
50 : wake_up_event_(WaitableEvent::ResetPolicy::AUTOMATIC, | 192 : thread_priority_(thread_priority), |
51 WaitableEvent::InitialState::NOT_SIGNALED), | |
52 delegate_(std::move(delegate)), | 193 delegate_(std::move(delegate)), |
53 task_tracker_(task_tracker) { | 194 task_tracker_(task_tracker) { |
54 DCHECK(delegate_); | 195 DCHECK(delegate_); |
55 DCHECK(task_tracker_); | 196 DCHECK(task_tracker_); |
56 | |
57 const size_t kDefaultStackSize = 0; | |
58 PlatformThread::CreateWithPriority(kDefaultStackSize, this, &thread_handle_, | |
59 thread_priority); | |
60 } | 197 } |
61 | 198 |
62 void SchedulerWorkerThread::ThreadMain() { | 199 std::unique_ptr<SchedulerWorkerThread::Worker> SchedulerWorkerThread::Detach() { |
63 delegate_->OnMainEntry(this); | 200 DCHECK(!ShouldExitForTesting()) << "Worker was already joined"; |
201 AutoSchedulerLock auto_lock(worker_lock_); | |
202 // If a wakeup is pending, then a WakeUp came in while we were deciding to | |
203 // detach. This means we can't go away anymore since we would break the | |
204 // guarantee that we call GetWork after a successful wakeup. | |
205 return worker_->IsWakeUpPending() ? nullptr : std::move(worker_); | |
206 } | |
64 | 207 |
65 // A SchedulerWorkerThread starts out sleeping. | 208 void SchedulerWorkerThread::CreateWorker() { |
66 wake_up_event_.Wait(); | 209 worker_ = Worker::Create(this); |
210 } | |
67 | 211 |
68 while (!task_tracker_->shutdown_completed() && !ShouldExitForTesting()) { | 212 void SchedulerWorkerThread::CreateWorkerAssertSynchronized() { |
69 // Get the sequence containing the next task to execute. | 213 worker_lock_.AssertAcquired(); |
70 scoped_refptr<Sequence> sequence = delegate_->GetWork(this); | 214 CreateWorker(); |
71 | |
72 if (!sequence) { | |
73 TimeDelta sleep_time = delegate_->GetSleepTimeout(); | |
74 if (sleep_time.is_max()) { | |
75 // Calling TimedWait with TimeDelta::Max is not recommended per | |
76 // http://crbug.com/465948. | |
77 wake_up_event_.Wait(); | |
78 } else { | |
79 wake_up_event_.TimedWait(sleep_time); | |
80 } | |
81 continue; | |
82 } | |
83 | |
84 task_tracker_->RunTask(sequence->PeekTask()); | |
85 | |
86 const bool sequence_became_empty = sequence->PopTask(); | |
87 | |
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 | |
90 // either a PriorityQueue or a SchedulerWorkerThread. If it is empty and | |
91 // there are live references to it, it will be enqueued when a Task is added | |
92 // to it. Otherwise, it will be destroyed at the end of this scope. | |
93 if (!sequence_became_empty) | |
94 delegate_->ReEnqueueSequence(std::move(sequence)); | |
95 | |
96 // Calling WakeUp() guarantees that this SchedulerWorkerThread will run | |
97 // Tasks from Sequences returned by the GetWork() method of |delegate_| | |
98 // until it returns nullptr. Resetting |wake_up_event_| here doesn't break | |
99 // this invariant and avoids a useless loop iteration before going to sleep | |
100 // if WakeUp() is called while this SchedulerWorkerThread is awake. | |
101 wake_up_event_.Reset(); | |
102 } | |
103 } | 215 } |
104 | 216 |
105 bool SchedulerWorkerThread::ShouldExitForTesting() const { | 217 bool SchedulerWorkerThread::ShouldExitForTesting() const { |
106 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); | 218 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); |
107 return should_exit_for_testing_; | 219 return should_exit_for_testing_; |
108 } | 220 } |
109 | 221 |
110 } // namespace internal | 222 } // namespace internal |
111 } // namespace base | 223 } // namespace base |
OLD | NEW |