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 <limits> |
| 8 |
| 9 #include "base/atomicops.h" |
7 #include "base/callback.h" | 10 #include "base/callback.h" |
8 #include "base/debug/task_annotator.h" | 11 #include "base/debug/task_annotator.h" |
| 12 #include "base/logging.h" |
9 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
10 #include "base/threading/sequenced_task_runner_handle.h" | 14 #include "base/threading/sequenced_task_runner_handle.h" |
11 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
13 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
14 | 18 |
15 namespace base { | 19 namespace base { |
16 namespace internal { | 20 namespace internal { |
17 | 21 |
18 namespace { | 22 namespace { |
(...skipping 10 matching lines...) Expand all Loading... |
29 | 33 |
30 void RecordNumBlockShutdownTasksPostedDuringShutdown( | 34 void RecordNumBlockShutdownTasksPostedDuringShutdown( |
31 HistogramBase::Sample value) { | 35 HistogramBase::Sample value) { |
32 UMA_HISTOGRAM_CUSTOM_COUNTS( | 36 UMA_HISTOGRAM_CUSTOM_COUNTS( |
33 "TaskScheduler.BlockShutdownTasksPostedDuringShutdown", value, 1, | 37 "TaskScheduler.BlockShutdownTasksPostedDuringShutdown", value, 1, |
34 kMaxBlockShutdownTasksPostedDuringShutdown, 50); | 38 kMaxBlockShutdownTasksPostedDuringShutdown, 50); |
35 } | 39 } |
36 | 40 |
37 } // namespace | 41 } // namespace |
38 | 42 |
39 TaskTracker::TaskTracker() = default; | 43 class TaskTracker::State { |
| 44 public: |
| 45 State() = default; |
| 46 |
| 47 // Sets a flag indicating that shutdown has started. Returns true if there are |
| 48 // tasks blocking shutdown. Can only be called once. |
| 49 bool StartShutdown() { |
| 50 const auto new_value = |
| 51 subtle::Barrier_AtomicIncrement(&bits_, kShutdownHasStartedMask); |
| 52 |
| 53 // Check that the "shutdown has started" bit isn't zero. This would happen |
| 54 // if it was incremented twice. |
| 55 DCHECK(new_value & kShutdownHasStartedMask); |
| 56 |
| 57 const auto num_tasks_blocking_shutdown = |
| 58 new_value >> kNumTasksBlockingShutdownBitOffset; |
| 59 return num_tasks_blocking_shutdown != 0; |
| 60 } |
| 61 |
| 62 // Returns true if shutdown has started. |
| 63 bool HasShutdownStarted() const { |
| 64 return subtle::Acquire_Load(&bits_) & kShutdownHasStartedMask; |
| 65 } |
| 66 |
| 67 // Returns true if there are tasks blocking shutdown. |
| 68 bool AreTasksBlockingShutdown() const { |
| 69 const auto num_tasks_blocking_shutdown = |
| 70 subtle::Acquire_Load(&bits_) >> kNumTasksBlockingShutdownBitOffset; |
| 71 DCHECK_GE(num_tasks_blocking_shutdown, 0); |
| 72 return num_tasks_blocking_shutdown != 0; |
| 73 } |
| 74 |
| 75 // Increments the number of tasks blocking shutdown. Returns true if shutdown |
| 76 // has started. |
| 77 bool IncrementNumTasksBlockingShutdown() { |
| 78 #if DCHECK_IS_ON() |
| 79 // Verify that no overflow will occur. |
| 80 const auto num_tasks_blocking_shutdown = |
| 81 subtle::Acquire_Load(&bits_) >> kNumTasksBlockingShutdownBitOffset; |
| 82 DCHECK_LT(num_tasks_blocking_shutdown, |
| 83 std::numeric_limits<subtle::Atomic32>::max() - |
| 84 kNumTasksBlockingShutdownIncrement); |
| 85 #endif |
| 86 |
| 87 const auto new_bits = subtle::Barrier_AtomicIncrement( |
| 88 &bits_, kNumTasksBlockingShutdownIncrement); |
| 89 return new_bits & kShutdownHasStartedMask; |
| 90 } |
| 91 |
| 92 // Decrements the number of tasks blocking shutdown. Returns true if shutdown |
| 93 // has started and the number of tasks blocking shutdown becomes zero. |
| 94 bool DecrementNumTasksBlockingShutdown() { |
| 95 const auto new_bits = subtle::Barrier_AtomicIncrement( |
| 96 &bits_, -kNumTasksBlockingShutdownIncrement); |
| 97 const bool shutdown_has_started = new_bits & kShutdownHasStartedMask; |
| 98 const auto num_tasks_blocking_shutdown = |
| 99 new_bits >> kNumTasksBlockingShutdownBitOffset; |
| 100 DCHECK_GE(num_tasks_blocking_shutdown, 0); |
| 101 return shutdown_has_started && num_tasks_blocking_shutdown == 0; |
| 102 } |
| 103 |
| 104 private: |
| 105 static constexpr subtle::Atomic32 kShutdownHasStartedMask = 1; |
| 106 static constexpr subtle::Atomic32 kNumTasksBlockingShutdownBitOffset = 1; |
| 107 static constexpr subtle::Atomic32 kNumTasksBlockingShutdownIncrement = |
| 108 1 << kNumTasksBlockingShutdownBitOffset; |
| 109 |
| 110 // The LSB indicates whether shutdown has started. The other bits count the |
| 111 // number of tasks blocking shutdown. |
| 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 |
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 // If another thread posts a BLOCK_SHUTDOWN task at this moment, it will |
60 // posted during shutdown. Otherwise, the histogram has already been recorded | 139 // block until this method releases |shutdown_lock_|. Then, it will fail |
61 // in BeforePostTask(). | 140 // DCHECK(!shutdown_event_->IsSignaled()). This is the desired behavior |
62 if (num_block_shutdown_tasks_posted_during_shutdown_ < | 141 // because posting a BLOCK_SHUTDOWN task when TaskTracker::Shutdown() has |
63 kMaxBlockShutdownTasksPostedDuringShutdown) { | 142 // started and no tasks are blocking shutdown isn't allowed. |
64 RecordNumBlockShutdownTasksPostedDuringShutdown( | 143 shutdown_event_->Signal(); |
65 num_block_shutdown_tasks_posted_during_shutdown_); | 144 return; |
| 145 } |
| 146 } |
| 147 |
| 148 // It is safe to access |shutdown_event_| without holding |lock_| because the |
| 149 // pointer never changes after being set above. |
| 150 shutdown_event_->Wait(); |
| 151 |
| 152 { |
| 153 AutoSchedulerLock auto_lock(shutdown_lock_); |
| 154 |
| 155 // Record TaskScheduler.BlockShutdownTasksPostedDuringShutdown if less than |
| 156 // |kMaxBlockShutdownTasksPostedDuringShutdown| BLOCK_SHUTDOWN tasks were |
| 157 // posted during shutdown. Otherwise, the histogram has already been |
| 158 // recorded in BeforePostTask(). |
| 159 if (num_block_shutdown_tasks_posted_during_shutdown_ < |
| 160 kMaxBlockShutdownTasksPostedDuringShutdown) { |
| 161 RecordNumBlockShutdownTasksPostedDuringShutdown( |
| 162 num_block_shutdown_tasks_posted_during_shutdown_); |
| 163 } |
66 } | 164 } |
67 } | 165 } |
68 | 166 |
69 bool TaskTracker::WillPostTask(const Task* task) { | 167 bool TaskTracker::WillPostTask(const Task* task) { |
70 DCHECK(task); | 168 DCHECK(task); |
71 | 169 |
72 if (!BeforePostTask(task->traits.shutdown_behavior())) | 170 if (!BeforePostTask(task->traits.shutdown_behavior())) |
73 return false; | 171 return false; |
74 | 172 |
75 debug::TaskAnnotator task_annotator; | 173 debug::TaskAnnotator task_annotator; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 | 207 |
110 TRACE_TASK_EXECUTION(kRunFunctionName, *task); | 208 TRACE_TASK_EXECUTION(kRunFunctionName, *task); |
111 | 209 |
112 debug::TaskAnnotator task_annotator; | 210 debug::TaskAnnotator task_annotator; |
113 task_annotator.RunTask(kQueueFunctionName, *task); | 211 task_annotator.RunTask(kQueueFunctionName, *task); |
114 } | 212 } |
115 | 213 |
116 AfterRunTask(shutdown_behavior); | 214 AfterRunTask(shutdown_behavior); |
117 } | 215 } |
118 | 216 |
| 217 bool TaskTracker::IsShutdownComplete() const { |
| 218 AutoSchedulerLock auto_lock(shutdown_lock_); |
| 219 return shutdown_event_ && shutdown_event_->IsSignaled(); |
| 220 } |
| 221 |
119 bool TaskTracker::IsShuttingDownForTesting() const { | 222 bool TaskTracker::IsShuttingDownForTesting() const { |
120 AutoSchedulerLock auto_lock(lock_); | 223 AutoSchedulerLock auto_lock(shutdown_lock_); |
121 return !!shutdown_cv_; | 224 return shutdown_event_ && !shutdown_event_->IsSignaled(); |
122 } | 225 } |
123 | 226 |
124 bool TaskTracker::BeforePostTask(TaskShutdownBehavior shutdown_behavior) { | 227 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) { | 228 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
137 // BLOCK_SHUTDOWN tasks block shutdown between the moment they are posted | 229 // BLOCK_SHUTDOWN tasks block shutdown between the moment they are posted |
138 // and the moment they complete their execution. | 230 // and the moment they complete their execution. |
139 ++num_tasks_blocking_shutdown_; | 231 const bool shutdown_started = state_->IncrementNumTasksBlockingShutdown(); |
140 | 232 |
141 if (shutdown_cv_) { | 233 if (shutdown_started) { |
| 234 AutoSchedulerLock auto_lock(shutdown_lock_); |
| 235 |
| 236 // A BLOCK_SHUTDOWN task posted after shutdown has completed is an |
| 237 // ordering bug. This aims to catch those early. |
| 238 DCHECK(shutdown_event_); |
| 239 DCHECK(!shutdown_event_->IsSignaled()); |
| 240 |
142 ++num_block_shutdown_tasks_posted_during_shutdown_; | 241 ++num_block_shutdown_tasks_posted_during_shutdown_; |
143 | 242 |
144 if (num_block_shutdown_tasks_posted_during_shutdown_ == | 243 if (num_block_shutdown_tasks_posted_during_shutdown_ == |
145 kMaxBlockShutdownTasksPostedDuringShutdown) { | 244 kMaxBlockShutdownTasksPostedDuringShutdown) { |
146 // Record the TaskScheduler.BlockShutdownTasksPostedDuringShutdown | 245 // Record the TaskScheduler.BlockShutdownTasksPostedDuringShutdown |
147 // histogram as soon as its upper bound is hit. That way, a value will | 246 // 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 | 247 // be recorded even if an infinite number of BLOCK_SHUTDOWN tasks are |
149 // posted, preventing shutdown to complete. | 248 // posted, preventing shutdown to complete. |
150 RecordNumBlockShutdownTasksPostedDuringShutdown( | 249 RecordNumBlockShutdownTasksPostedDuringShutdown( |
151 num_block_shutdown_tasks_posted_during_shutdown_); | 250 num_block_shutdown_tasks_posted_during_shutdown_); |
152 } | 251 } |
153 } | 252 } |
154 | 253 |
155 // A BLOCK_SHUTDOWN task is allowed to be posted iff shutdown hasn't | |
156 // completed. | |
157 return true; | 254 return true; |
158 } | 255 } |
159 | 256 |
160 // A non BLOCK_SHUTDOWN task is allowed to be posted iff shutdown hasn't | 257 // A non BLOCK_SHUTDOWN task is allowed to be posted iff shutdown hasn't |
161 // started. | 258 // started. |
162 return !shutdown_cv_; | 259 return !state_->HasShutdownStarted(); |
163 } | 260 } |
164 | 261 |
165 bool TaskTracker::BeforeRunTask(TaskShutdownBehavior shutdown_behavior) { | 262 bool TaskTracker::BeforeRunTask(TaskShutdownBehavior shutdown_behavior) { |
166 AutoSchedulerLock auto_lock(lock_); | 263 switch (shutdown_behavior) { |
| 264 case TaskShutdownBehavior::BLOCK_SHUTDOWN: { |
| 265 // The number of tasks blocking shutdown has been incremented when the |
| 266 // task was posted. |
| 267 DCHECK(state_->AreTasksBlockingShutdown()); |
167 | 268 |
168 if (shutdown_completed_) { | 269 // 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 | 270 // unexpected as it either shouldn't have been posted if shutdown |
170 // unexpected as it either shouldn't have been posted if shutdown completed | 271 // 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. | 272 // did. |
172 DCHECK_NE(shutdown_behavior, TaskShutdownBehavior::BLOCK_SHUTDOWN); | 273 DCHECK(!state_->HasShutdownStarted() || !IsShutdownComplete()); |
173 | 274 |
174 // A worker might extract a non BLOCK_SHUTDOWN task from a PriorityQueue | 275 return true; |
175 // after shutdown. It shouldn't be allowed to run it. | 276 } |
176 return false; | |
177 } | |
178 | 277 |
179 switch (shutdown_behavior) { | 278 case TaskShutdownBehavior::SKIP_ON_SHUTDOWN: { |
180 case TaskShutdownBehavior::BLOCK_SHUTDOWN: | 279 // SKIP_ON_SHUTDOWN tasks block shutdown while they are running. |
181 DCHECK_GT(num_tasks_blocking_shutdown_, 0U); | 280 const bool shutdown_started = state_->IncrementNumTasksBlockingShutdown(); |
| 281 |
| 282 if (shutdown_started) { |
| 283 // The SKIP_ON_SHUTDOWN task isn't allowed to run during shutdown. |
| 284 // Decrement the number of tasks blocking shutdown that was wrongly |
| 285 // incremented. |
| 286 const bool shutdown_started_and_no_tasks_block_shutdown = |
| 287 state_->DecrementNumTasksBlockingShutdown(); |
| 288 if (shutdown_started_and_no_tasks_block_shutdown) |
| 289 OnBlockingShutdownTasksComplete(); |
| 290 |
| 291 return false; |
| 292 } |
| 293 |
182 return true; | 294 return true; |
| 295 } |
183 | 296 |
184 case TaskShutdownBehavior::SKIP_ON_SHUTDOWN: | 297 case TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN: { |
185 if (shutdown_cv_) | 298 return !state_->HasShutdownStarted(); |
186 return false; | 299 } |
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 } | 300 } |
195 | 301 |
196 NOTREACHED(); | 302 NOTREACHED(); |
197 return false; | 303 return false; |
198 } | 304 } |
199 | 305 |
200 void TaskTracker::AfterRunTask(TaskShutdownBehavior shutdown_behavior) { | 306 void TaskTracker::AfterRunTask(TaskShutdownBehavior shutdown_behavior) { |
201 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN || | 307 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN || |
202 shutdown_behavior == TaskShutdownBehavior::SKIP_ON_SHUTDOWN) { | 308 shutdown_behavior == TaskShutdownBehavior::SKIP_ON_SHUTDOWN) { |
203 AutoSchedulerLock auto_lock(lock_); | 309 const bool shutdown_started_and_no_tasks_block_shutdown = |
204 DCHECK_GT(num_tasks_blocking_shutdown_, 0U); | 310 state_->DecrementNumTasksBlockingShutdown(); |
205 --num_tasks_blocking_shutdown_; | 311 if (shutdown_started_and_no_tasks_block_shutdown) |
206 if (num_tasks_blocking_shutdown_ == 0 && shutdown_cv_) | 312 OnBlockingShutdownTasksComplete(); |
207 shutdown_cv_->Signal(); | |
208 } | 313 } |
209 } | 314 } |
210 | 315 |
| 316 void TaskTracker::OnBlockingShutdownTasksComplete() { |
| 317 AutoSchedulerLock auto_lock(shutdown_lock_); |
| 318 |
| 319 // This method can only be called after shutdown has started. |
| 320 DCHECK(state_->HasShutdownStarted()); |
| 321 DCHECK(shutdown_event_); |
| 322 |
| 323 shutdown_event_->Signal(); |
| 324 } |
| 325 |
211 } // namespace internal | 326 } // namespace internal |
212 } // namespace base | 327 } // namespace base |
OLD | NEW |