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/task_tracker.h" | 5 #include "base/task_scheduler/task_tracker.h" |
6 | 6 |
7 #include "base/atomicops.h" | |
7 #include "base/callback.h" | 8 #include "base/callback.h" |
8 #include "base/debug/task_annotator.h" | 9 #include "base/debug/task_annotator.h" |
9 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
10 #include "base/threading/sequenced_task_runner_handle.h" | 11 #include "base/threading/sequenced_task_runner_handle.h" |
11 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
13 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 namespace internal { | 17 namespace internal { |
(...skipping 12 matching lines...) Expand all Loading... | |
29 | 30 |
30 void RecordNumBlockShutdownTasksPostedDuringShutdown( | 31 void RecordNumBlockShutdownTasksPostedDuringShutdown( |
31 HistogramBase::Sample value) { | 32 HistogramBase::Sample value) { |
32 UMA_HISTOGRAM_CUSTOM_COUNTS( | 33 UMA_HISTOGRAM_CUSTOM_COUNTS( |
33 "TaskScheduler.BlockShutdownTasksPostedDuringShutdown", value, 1, | 34 "TaskScheduler.BlockShutdownTasksPostedDuringShutdown", value, 1, |
34 kMaxBlockShutdownTasksPostedDuringShutdown, 50); | 35 kMaxBlockShutdownTasksPostedDuringShutdown, 50); |
35 } | 36 } |
36 | 37 |
37 } // namespace | 38 } // namespace |
38 | 39 |
39 TaskTracker::TaskTracker() = default; | 40 class TaskTracker::State { |
41 public: | |
42 State() = default; | |
43 | |
44 // Sets a flag indicating that shutdown has started. Returns true if there are | |
45 // tasks blocking shutdown. Can only be called once. | |
46 bool StartShutdown() { | |
47 const auto new_value = | |
48 subtle::NoBarrier_AtomicIncrement(&bits_, kShutdownHasStartedMask); | |
49 | |
50 // Check that the "shutdown has started" bit isn't zero. This would happen | |
51 // if it was incremented twice. | |
52 DCHECK(new_value & kShutdownHasStartedMask); | |
53 | |
54 const auto num_tasks_blocking_shutdown = | |
55 new_value / kNumTasksBlockingShutdownIncrement; | |
56 return num_tasks_blocking_shutdown != 0; | |
57 } | |
58 | |
59 // Returns true if shutdown has started. | |
60 bool ShutdownHasStarted() const { | |
danakj
2016/06/30 22:25:36
nit: i prefer names that start with an action, in
fdoray
2016/07/04 20:53:11
Done.
| |
61 return subtle::NoBarrier_Load(&bits_) & kShutdownHasStartedMask; | |
62 } | |
63 | |
64 // Returns true if there are tasks blocking shutdown. | |
65 bool TasksAreBlockingShutdown() const { | |
66 const auto num_tasks_blocking_shutdown = | |
67 subtle::NoBarrier_Load(&bits_) / kNumTasksBlockingShutdownIncrement; | |
68 DCHECK_GE(num_tasks_blocking_shutdown, 0); | |
69 return num_tasks_blocking_shutdown != 0; | |
70 } | |
71 | |
72 // Increments the number of tasks blocking shutdown. Returns true if shutdown | |
73 // has started. | |
74 bool IncrementNumTasksBlockingShutdown() { | |
75 const auto new_bits = subtle::NoBarrier_AtomicIncrement( | |
76 &bits_, kNumTasksBlockingShutdownIncrement); | |
77 | |
78 #if DCHECK_IS_ON() | |
79 // Verify that there is no overflow. | |
danakj
2016/06/30 22:25:36
Signed integer overflow is undefined is it not? I
fdoray
2016/07/04 20:53:11
I moved the DCHECK before the increment. Note that
| |
80 const auto num_tasks_blocking_shutdown = | |
81 new_bits >> kShutdownHasStartedMask; | |
danakj
2016/06/30 22:25:36
You mean / Increment?
fdoray
2016/07/04 20:53:11
Gab had a similar comment https://codereview.chrom
danakj
2016/07/06 18:51:48
Are you sure you want to >> a signed integer? Does
| |
82 DCHECK_GE(num_tasks_blocking_shutdown, 0); | |
83 #endif | |
84 | |
85 const bool shutdown_has_started = new_bits & kShutdownHasStartedMask; | |
86 return shutdown_has_started; | |
87 } | |
88 | |
89 // Decrements the number of tasks blocking shutdown. Returns true if shutdown | |
90 // has started and the number of tasks blocking shutdown becomes zero. | |
91 bool DecrementNumTasksBlockingShutdown() { | |
92 const auto new_bits = subtle::NoBarrier_AtomicIncrement( | |
93 &bits_, -kNumTasksBlockingShutdownIncrement); | |
94 const bool shutdown_has_started = new_bits & kShutdownHasStartedMask; | |
95 const auto num_tasks_blocking_shutdown = | |
96 new_bits >> kShutdownHasStartedMask; | |
danakj
2016/06/30 22:25:36
you mean / Increment?
fdoray
2016/07/04 20:53:11
ditto
| |
97 DCHECK_GE(num_tasks_blocking_shutdown, 0); | |
98 return shutdown_has_started && num_tasks_blocking_shutdown == 0; | |
99 } | |
100 | |
101 private: | |
102 static constexpr subtle::Atomic32 kShutdownHasStartedMask = 1; | |
103 static constexpr subtle::Atomic32 kNumTasksBlockingShutdownIncrement = | |
104 1 << kShutdownHasStartedMask; | |
danakj
2016/06/30 22:25:36
I don't think this is quite right logically, if ma
fdoray
2016/07/04 20:53:11
ditto
| |
105 | |
106 // The LSB indicates whether shutdown has started. The other bits count the | |
107 // number of tasks blocking shutdown. | |
108 // | |
109 // This atomic variable is always read/written without a barrier. This is | |
110 // correct because it encapsulates all shutdown state. Atomic behavior would | |
gab
2016/06/29 21:38:47
s/Atomic behavior/Barrier semantics/ ?
fdoray
2016/07/04 20:53:11
Done.
| |
111 // need to be reassessed if another variable was added. | |
112 subtle::Atomic32 bits_ = 0; | |
113 | |
114 DISALLOW_COPY_AND_ASSIGN(State); | |
115 }; | |
116 | |
117 TaskTracker::TaskTracker() : state_(new State) {} | |
40 TaskTracker::~TaskTracker() = default; | 118 TaskTracker::~TaskTracker() = default; |
41 | 119 |
42 void TaskTracker::Shutdown() { | 120 void TaskTracker::Shutdown() { |
43 AutoSchedulerLock auto_lock(lock_); | 121 { |
122 AutoSchedulerLock auto_lock(shutdown_lock_); | |
44 | 123 |
45 // This method should only be called once. | 124 // This method can only be called once. |
46 DCHECK(!shutdown_completed_); | 125 DCHECK(!shutdown_event_); |
47 DCHECK(!shutdown_cv_); | 126 DCHECK(!num_block_shutdown_tasks_posted_during_shutdown_); |
48 | 127 |
49 shutdown_cv_ = lock_.CreateConditionVariable(); | 128 shutdown_event_.reset( |
129 new WaitableEvent(WaitableEvent::ResetPolicy::MANUAL, | |
130 WaitableEvent::InitialState::NOT_SIGNALED)); | |
50 | 131 |
51 // Wait until the number of tasks blocking shutdown is zero. | 132 const bool tasks_are_blocking_shutdown = state_->StartShutdown(); |
52 while (num_tasks_blocking_shutdown_ != 0) | |
53 shutdown_cv_->Wait(); | |
54 | 133 |
55 shutdown_cv_.reset(); | 134 // From now, if a thread causes the number of tasks blocking shutdown to |
danakj
2016/06/30 22:25:36
What happens if a BLOCK_SHUTDOWN task is posted ri
fdoray
2016/07/04 20:53:11
You mean if a BLOCK_SHUTDOWN task is posted right
danakj
2016/07/06 18:50:24
The DCHECK is that the event is not Signaled. It i
fdoray
2016/07/06 19:19:40
Thread A: state_->StartShutdown();
Thread B: Start
danakj
2016/07/07 22:59:10
Ah I see. The lock makes this work. Ok thanks for
| |
56 shutdown_completed_ = true; | 135 // become zero, it will call OnBlockingShutdownTasksComplete(). |
57 | 136 |
58 // Record the TaskScheduler.BlockShutdownTasksPostedDuringShutdown if less | 137 if (!tasks_are_blocking_shutdown) { |
59 // than |kMaxBlockShutdownTasksPostedDuringShutdown| BLOCK_SHUTDOWN tasks were | 138 shutdown_event_->Signal(); |
60 // posted during shutdown. Otherwise, the histogram has already been recorded | 139 return; |
61 // in BeforePostTask(). | 140 } |
62 if (num_block_shutdown_tasks_posted_during_shutdown_ < | 141 } |
63 kMaxBlockShutdownTasksPostedDuringShutdown) { | 142 |
64 RecordNumBlockShutdownTasksPostedDuringShutdown( | 143 // It is safe to access |shutdown_event_| without holding |lock_| because the |
65 num_block_shutdown_tasks_posted_during_shutdown_); | 144 // pointer never changes after being set above. |
145 shutdown_event_->Wait(); | |
146 | |
147 { | |
148 AutoSchedulerLock auto_lock(shutdown_lock_); | |
149 | |
150 // Record TaskScheduler.BlockShutdownTasksPostedDuringShutdown if less than | |
151 // |kMaxBlockShutdownTasksPostedDuringShutdown| BLOCK_SHUTDOWN tasks were | |
152 // posted during shutdown. Otherwise, the histogram has already been | |
153 // recorded in BeforePostTask(). | |
154 if (num_block_shutdown_tasks_posted_during_shutdown_ < | |
155 kMaxBlockShutdownTasksPostedDuringShutdown) { | |
156 RecordNumBlockShutdownTasksPostedDuringShutdown( | |
157 num_block_shutdown_tasks_posted_during_shutdown_); | |
158 } | |
66 } | 159 } |
67 } | 160 } |
68 | 161 |
69 bool TaskTracker::WillPostTask(const Task* task) { | 162 bool TaskTracker::WillPostTask(const Task* task) { |
70 DCHECK(task); | 163 DCHECK(task); |
71 | 164 |
72 if (!BeforePostTask(task->traits.shutdown_behavior())) | 165 if (!BeforePostTask(task->traits.shutdown_behavior())) |
73 return false; | 166 return false; |
74 | 167 |
75 debug::TaskAnnotator task_annotator; | 168 debug::TaskAnnotator task_annotator; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 | 202 |
110 TRACE_TASK_EXECUTION(kRunFunctionName, *task); | 203 TRACE_TASK_EXECUTION(kRunFunctionName, *task); |
111 | 204 |
112 debug::TaskAnnotator task_annotator; | 205 debug::TaskAnnotator task_annotator; |
113 task_annotator.RunTask(kQueueFunctionName, *task); | 206 task_annotator.RunTask(kQueueFunctionName, *task); |
114 } | 207 } |
115 | 208 |
116 AfterRunTask(shutdown_behavior); | 209 AfterRunTask(shutdown_behavior); |
117 } | 210 } |
118 | 211 |
212 bool TaskTracker::ShutdownCompleted() const { | |
danakj
2016/06/30 22:25:36
same naming nit: IsShutdownComplete?
fdoray
2016/07/04 20:53:11
Done.
| |
213 AutoSchedulerLock auto_lock(shutdown_lock_); | |
214 return shutdown_event_ && shutdown_event_->IsSignaled(); | |
215 } | |
216 | |
119 bool TaskTracker::IsShuttingDownForTesting() const { | 217 bool TaskTracker::IsShuttingDownForTesting() const { |
120 AutoSchedulerLock auto_lock(lock_); | 218 AutoSchedulerLock auto_lock(shutdown_lock_); |
121 return !!shutdown_cv_; | 219 return shutdown_event_ && !shutdown_event_->IsSignaled(); |
122 } | 220 } |
123 | 221 |
124 bool TaskTracker::BeforePostTask(TaskShutdownBehavior shutdown_behavior) { | 222 bool TaskTracker::BeforePostTask(TaskShutdownBehavior shutdown_behavior) { |
125 AutoSchedulerLock auto_lock(lock_); | |
126 | |
127 if (shutdown_completed_) { | |
128 // A BLOCK_SHUTDOWN task posted after shutdown has completed is an ordering | |
129 // bug. This DCHECK aims to catch those early. | |
130 DCHECK_NE(shutdown_behavior, TaskShutdownBehavior::BLOCK_SHUTDOWN); | |
131 | |
132 // No task is allowed to be posted after shutdown. | |
133 return false; | |
134 } | |
135 | |
136 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN) { | 223 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
137 // BLOCK_SHUTDOWN tasks block shutdown between the moment they are posted | 224 // BLOCK_SHUTDOWN tasks block shutdown between the moment they are posted |
138 // and the moment they complete their execution. | 225 // and the moment they complete their execution. |
139 ++num_tasks_blocking_shutdown_; | 226 const bool shutdown_started = state_->IncrementNumTasksBlockingShutdown(); |
140 | 227 |
141 if (shutdown_cv_) { | 228 if (shutdown_started) { |
danakj
2016/06/30 22:25:36
If shutdown started, it may also have finished by
fdoray
2016/07/04 20:53:11
If shutdown is complete, line 238 will DCHECK:
DCH
| |
229 AutoSchedulerLock auto_lock(shutdown_lock_); | |
230 | |
231 // A BLOCK_SHUTDOWN task posted after shutdown has completed is an | |
232 // ordering bug. This aims to catch those early. | |
233 DCHECK(shutdown_event_); | |
234 DCHECK(!shutdown_event_->IsSignaled()); | |
235 | |
142 ++num_block_shutdown_tasks_posted_during_shutdown_; | 236 ++num_block_shutdown_tasks_posted_during_shutdown_; |
143 | 237 |
144 if (num_block_shutdown_tasks_posted_during_shutdown_ == | 238 if (num_block_shutdown_tasks_posted_during_shutdown_ == |
145 kMaxBlockShutdownTasksPostedDuringShutdown) { | 239 kMaxBlockShutdownTasksPostedDuringShutdown) { |
146 // Record the TaskScheduler.BlockShutdownTasksPostedDuringShutdown | 240 // Record the TaskScheduler.BlockShutdownTasksPostedDuringShutdown |
147 // histogram as soon as its upper bound is hit. That way, a value will | 241 // histogram as soon as its upper bound is hit. That way, a value will |
148 // be recorded even if an infinite number of BLOCK_SHUTDOWN tasks are | 242 // be recorded even if an infinite number of BLOCK_SHUTDOWN tasks are |
149 // posted, preventing shutdown to complete. | 243 // posted, preventing shutdown to complete. |
150 RecordNumBlockShutdownTasksPostedDuringShutdown( | 244 RecordNumBlockShutdownTasksPostedDuringShutdown( |
151 num_block_shutdown_tasks_posted_during_shutdown_); | 245 num_block_shutdown_tasks_posted_during_shutdown_); |
152 } | 246 } |
153 } | 247 } |
154 | 248 |
155 // A BLOCK_SHUTDOWN task is allowed to be posted iff shutdown hasn't | |
156 // completed. | |
157 return true; | 249 return true; |
158 } | 250 } |
159 | 251 |
160 // A non BLOCK_SHUTDOWN task is allowed to be posted iff shutdown hasn't | 252 // A non BLOCK_SHUTDOWN task is allowed to be posted iff shutdown hasn't |
161 // started. | 253 // started. |
162 return !shutdown_cv_; | 254 return !state_->ShutdownHasStarted(); |
danakj
2016/06/30 22:25:36
What if shutdown started between this and the chec
fdoray
2016/07/04 20:53:11
Which check above? The rest of this method is for
| |
163 } | 255 } |
164 | 256 |
165 bool TaskTracker::BeforeRunTask(TaskShutdownBehavior shutdown_behavior) { | 257 bool TaskTracker::BeforeRunTask(TaskShutdownBehavior shutdown_behavior) { |
166 AutoSchedulerLock auto_lock(lock_); | 258 switch (shutdown_behavior) { |
259 case TaskShutdownBehavior::BLOCK_SHUTDOWN: { | |
260 // The number of tasks blocking shutdown has been incremented when the | |
261 // task was posted. | |
262 DCHECK(state_->TasksAreBlockingShutdown()); | |
167 | 263 |
168 if (shutdown_completed_) { | 264 // Trying to run a BLOCK_SHUTDOWN task after shutdown has completed is |
169 // Trying to run a BLOCK_SHUTDOWN task after shutdown has completed is | 265 // unexpected as it either shouldn't have been posted if shutdown |
170 // unexpected as it either shouldn't have been posted if shutdown completed | 266 // completed or should be blocking shutdown if it was posted before it |
171 // or should be blocking shutdown if it was posted before it did. | 267 // did. |
172 DCHECK_NE(shutdown_behavior, TaskShutdownBehavior::BLOCK_SHUTDOWN); | 268 DCHECK(!state_->ShutdownHasStarted() || !ShutdownCompleted()); |
173 | 269 |
174 // A worker might extract a non BLOCK_SHUTDOWN task from a PriorityQueue | 270 return true; |
175 // after shutdown. It shouldn't be allowed to run it. | 271 } |
176 return false; | |
177 } | |
178 | 272 |
179 switch (shutdown_behavior) { | 273 case TaskShutdownBehavior::SKIP_ON_SHUTDOWN: { |
180 case TaskShutdownBehavior::BLOCK_SHUTDOWN: | 274 // SKIP_ON_SHUTDOWN tasks block shutdown while they are running. |
181 DCHECK_GT(num_tasks_blocking_shutdown_, 0U); | 275 const bool shutdown_started = state_->IncrementNumTasksBlockingShutdown(); |
276 | |
277 if (shutdown_started) { | |
278 // The SKIP_ON_SHUTDOWN task isn't allowed to run during shutdown. | |
279 // Decrement the number of tasks blocking shutdown that was wrongly | |
280 // incremented. | |
281 const bool shutdown_started_and_no_tasks_block_shutdown = | |
282 state_->DecrementNumTasksBlockingShutdown(); | |
283 if (shutdown_started_and_no_tasks_block_shutdown) | |
284 OnBlockingShutdownTasksComplete(); | |
285 | |
286 return false; | |
287 } | |
288 | |
182 return true; | 289 return true; |
290 } | |
183 | 291 |
184 case TaskShutdownBehavior::SKIP_ON_SHUTDOWN: | 292 case TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN: { |
185 if (shutdown_cv_) | 293 return !state_->ShutdownHasStarted(); |
186 return false; | 294 } |
187 | |
188 // SKIP_ON_SHUTDOWN tasks block shutdown while they are running. | |
189 ++num_tasks_blocking_shutdown_; | |
190 return true; | |
191 | |
192 case TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN: | |
193 return !shutdown_cv_; | |
194 } | 295 } |
195 | 296 |
196 NOTREACHED(); | 297 NOTREACHED(); |
197 return false; | 298 return false; |
198 } | 299 } |
199 | 300 |
200 void TaskTracker::AfterRunTask(TaskShutdownBehavior shutdown_behavior) { | 301 void TaskTracker::AfterRunTask(TaskShutdownBehavior shutdown_behavior) { |
201 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN || | 302 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN || |
202 shutdown_behavior == TaskShutdownBehavior::SKIP_ON_SHUTDOWN) { | 303 shutdown_behavior == TaskShutdownBehavior::SKIP_ON_SHUTDOWN) { |
203 AutoSchedulerLock auto_lock(lock_); | 304 const bool shutdown_started_and_no_tasks_block_shutdown = |
204 DCHECK_GT(num_tasks_blocking_shutdown_, 0U); | 305 state_->DecrementNumTasksBlockingShutdown(); |
205 --num_tasks_blocking_shutdown_; | 306 if (shutdown_started_and_no_tasks_block_shutdown) |
206 if (num_tasks_blocking_shutdown_ == 0 && shutdown_cv_) | 307 OnBlockingShutdownTasksComplete(); |
207 shutdown_cv_->Signal(); | |
208 } | 308 } |
209 } | 309 } |
210 | 310 |
311 void TaskTracker::OnBlockingShutdownTasksComplete() { | |
312 AutoSchedulerLock auto_lock(shutdown_lock_); | |
313 | |
314 // This method can only be called after shutdown has started. | |
315 DCHECK(state_->ShutdownHasStarted()); | |
316 DCHECK(shutdown_event_); | |
317 | |
318 shutdown_event_->Signal(); | |
319 } | |
320 | |
211 } // namespace internal | 321 } // namespace internal |
212 } // namespace base | 322 } // namespace base |
OLD | NEW |