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 // andthere are live references to it, it will be enqueued when a Task is | |
gab
2016/06/10 16:15:20
"and there"
robliao
2016/06/10 18:03:41
Done.
| |
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 DCHECK(!detached_worker || !IsWakeUpPending()) << | |
79 "This thread was detached and woken up at the same time."; | |
gab
2016/06/10 16:15:20
Is this bad (i.e. someone did something wrong) or
robliao
2016/06/10 18:03:41
Yep, very bad. If the thread is signaled after the
| |
80 } | |
81 | |
82 void Join() { PlatformThread::Join(thread_handle_); } | |
83 | |
84 void WakeUp() { wake_up_event_.Signal(); } | |
85 | |
86 bool IsWakeUpPending() { return wake_up_event_.IsSignaled(); } | |
87 | |
88 private: | |
89 Worker(SchedulerWorkerThread* outer) | |
90 : outer_(outer), | |
91 wake_up_event_(WaitableEvent::ResetPolicy::MANUAL, | |
92 WaitableEvent::InitialState::NOT_SIGNALED) { | |
93 DCHECK(outer_); | |
94 } | |
95 | |
96 void Initialize() { | |
97 const size_t kDefaultStackSize = 0; | |
98 PlatformThread::CreateWithPriority(kDefaultStackSize, this, | |
99 &thread_handle_, | |
100 outer_->thread_priority_); | |
101 } | |
102 | |
103 void WaitForWork() { | |
104 DCHECK(outer_); | |
105 const TimeDelta sleep_time = outer_->delegate_->GetSleepTimeout(); | |
106 if (sleep_time.is_max()) { | |
107 // Calling TimedWait with TimeDelta::Max is not recommended per | |
108 // http://crbug.com/465948. | |
109 wake_up_event_.Wait(); | |
110 } else { | |
111 wake_up_event_.TimedWait(sleep_time); | |
112 } | |
113 wake_up_event_.Reset(); | |
gab
2016/06/10 16:15:20
Why not keep this event AUTOMATIC if you're going
robliao
2016/06/10 18:03:41
IsSignaled apparently resets the WaitableEvent! It
| |
114 } | |
115 | |
116 PlatformThreadHandle thread_handle_; | |
117 | |
118 SchedulerWorkerThread* outer_; | |
119 | |
120 // Event signaled to wake up this SchedulerWorkerThread. | |
121 WaitableEvent wake_up_event_; | |
122 | |
123 DISALLOW_COPY_AND_ASSIGN(Worker); | |
124 }; | |
125 | |
17 std::unique_ptr<SchedulerWorkerThread> SchedulerWorkerThread::Create( | 126 std::unique_ptr<SchedulerWorkerThread> SchedulerWorkerThread::Create( |
18 ThreadPriority thread_priority, | 127 ThreadPriority thread_priority, |
19 std::unique_ptr<Delegate> delegate, | 128 std::unique_ptr<Delegate> delegate, |
20 TaskTracker* task_tracker) { | 129 TaskTracker* task_tracker, |
130 InitialWorkerState worker_state) { | |
21 std::unique_ptr<SchedulerWorkerThread> worker_thread( | 131 std::unique_ptr<SchedulerWorkerThread> worker_thread( |
22 new SchedulerWorkerThread(thread_priority, std::move(delegate), | 132 new SchedulerWorkerThread(thread_priority, std::move(delegate), |
23 task_tracker)); | 133 task_tracker)); |
134 // Creation is single-threaded, so no synchronization is necessary. | |
gab
2016/06/10 16:15:20
I think what you mean is:
// Creation happens bef
robliao
2016/06/10 18:03:41
Yep, exactly what I mean. I was going for the one-
| |
135 if (worker_state == SchedulerWorkerThread::InitialWorkerState::ALIVE) { | |
136 worker_thread->CreateWorker(); | |
137 if (!worker_thread->worker_) { | |
138 return nullptr; | |
139 } | |
140 } | |
24 | 141 |
25 if (worker_thread->thread_handle_.is_null()) | |
26 return nullptr; | |
27 return worker_thread; | 142 return worker_thread; |
28 } | 143 } |
29 | 144 |
30 SchedulerWorkerThread::~SchedulerWorkerThread() { | 145 SchedulerWorkerThread::~SchedulerWorkerThread() { |
31 DCHECK(ShouldExitForTesting()); | 146 DCHECK(ShouldExitForTesting() || !worker_); |
gab
2016/06/10 16:15:20
So I thought we said the SWT object itself would a
robliao
2016/06/10 18:03:41
I don't recall a case where we would want the Sche
gab
2016/06/13 19:23:05
Feels we want to keep the ShouldExitForTesting() D
robliao
2016/06/13 23:23:28
It seemed redundant to me as the goal of ShouldExi
gab
2016/06/15 19:40:42
Wasn't it also because the rest of the scheduler a
| |
32 } | 147 } |
33 | 148 |
34 void SchedulerWorkerThread::WakeUp() { | 149 void SchedulerWorkerThread::WakeUp() { |
35 wake_up_event_.Signal(); | 150 AutoSchedulerLock auto_lock(worker_lock_); |
151 if (!worker_) | |
152 CreateWorkerAssertSynchronized(); | |
153 | |
154 if (worker_) | |
155 worker_->WakeUp(); | |
36 } | 156 } |
37 | 157 |
38 void SchedulerWorkerThread::JoinForTesting() { | 158 void SchedulerWorkerThread::JoinForTesting() { |
39 { | 159 { |
40 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); | 160 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); |
41 should_exit_for_testing_ = true; | 161 should_exit_for_testing_ = true; |
42 } | 162 } |
43 WakeUp(); | 163 WakeUp(); |
44 PlatformThread::Join(thread_handle_); | 164 |
165 // Normally holding a lock and joining is dangerous. However, since this is | |
166 // only for testing, we're okay since the only scenario that could impact this | |
167 // is a call to Detach, which is disallowed by having the delegate always | |
168 // return false for the CanDetach call. | |
169 AutoSchedulerLock auto_lock(worker_lock_); | |
170 if (worker_) | |
171 worker_->Join(); | |
172 } | |
173 | |
174 bool SchedulerWorkerThread::WorkerAliveForTesting() const { | |
175 AutoSchedulerLock auto_lock(worker_lock_); | |
176 return !!worker_; | |
45 } | 177 } |
46 | 178 |
47 SchedulerWorkerThread::SchedulerWorkerThread(ThreadPriority thread_priority, | 179 SchedulerWorkerThread::SchedulerWorkerThread(ThreadPriority thread_priority, |
48 std::unique_ptr<Delegate> delegate, | 180 std::unique_ptr<Delegate> delegate, |
49 TaskTracker* task_tracker) | 181 TaskTracker* task_tracker) |
50 : wake_up_event_(WaitableEvent::ResetPolicy::AUTOMATIC, | 182 : thread_priority_(thread_priority), |
51 WaitableEvent::InitialState::NOT_SIGNALED), | |
52 delegate_(std::move(delegate)), | 183 delegate_(std::move(delegate)), |
53 task_tracker_(task_tracker) { | 184 task_tracker_(task_tracker) { |
54 DCHECK(delegate_); | 185 DCHECK(delegate_); |
55 DCHECK(task_tracker_); | 186 DCHECK(task_tracker_); |
56 | |
57 const size_t kDefaultStackSize = 0; | |
58 PlatformThread::CreateWithPriority(kDefaultStackSize, this, &thread_handle_, | |
59 thread_priority); | |
60 } | 187 } |
61 | 188 |
62 void SchedulerWorkerThread::ThreadMain() { | 189 std::unique_ptr<SchedulerWorkerThread::Worker> SchedulerWorkerThread::Detach() { |
63 delegate_->OnMainEntry(this); | 190 DCHECK(!ShouldExitForTesting()) << "Worker was already joined"; |
191 AutoSchedulerLock auto_lock(worker_lock_); | |
192 // If a wakeup is pending, then a WakeUp came in while we were deciding to | |
193 // detach. This means we can't go away anymore since we would break the | |
194 // guarantee that we call GetWork after a successful wakeup. | |
195 return worker_->IsWakeUpPending() ? nullptr : std::move(worker_); | |
196 } | |
64 | 197 |
65 // A SchedulerWorkerThread starts out sleeping. | 198 void SchedulerWorkerThread::CreateWorker() { |
66 wake_up_event_.Wait(); | 199 worker_ = Worker::Create(this); |
200 } | |
67 | 201 |
68 while (!task_tracker_->shutdown_completed() && !ShouldExitForTesting()) { | 202 void SchedulerWorkerThread::CreateWorkerAssertSynchronized() { |
69 // Get the sequence containing the next task to execute. | 203 worker_lock_.AssertAcquired(); |
70 scoped_refptr<Sequence> sequence = delegate_->GetWork(this); | 204 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 } | 205 } |
104 | 206 |
105 bool SchedulerWorkerThread::ShouldExitForTesting() const { | 207 bool SchedulerWorkerThread::ShouldExitForTesting() const { |
106 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); | 208 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); |
107 return should_exit_for_testing_; | 209 return should_exit_for_testing_; |
108 } | 210 } |
109 | 211 |
110 } // namespace internal | 212 } // namespace internal |
111 } // namespace base | 213 } // namespace base |
OLD | NEW |