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> | 7 #include <limits> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 const SequenceToken& sequence_token) { | 239 const SequenceToken& sequence_token) { |
240 DCHECK(task); | 240 DCHECK(task); |
241 DCHECK(sequence_token.IsValid()); | 241 DCHECK(sequence_token.IsValid()); |
242 | 242 |
243 const TaskShutdownBehavior shutdown_behavior = | 243 const TaskShutdownBehavior shutdown_behavior = |
244 task->traits.shutdown_behavior(); | 244 task->traits.shutdown_behavior(); |
245 const bool can_run_task = BeforeRunTask(shutdown_behavior); | 245 const bool can_run_task = BeforeRunTask(shutdown_behavior); |
246 const bool is_delayed = !task->delayed_run_time.is_null(); | 246 const bool is_delayed = !task->delayed_run_time.is_null(); |
247 | 247 |
248 if (can_run_task) { | 248 if (can_run_task) { |
249 RecordTaskLatencyHistogram(task.get()); | 249 PerformRunTask(std::move(task), sequence_token); |
250 | |
251 const bool previous_singleton_allowed = | |
252 ThreadRestrictions::SetSingletonAllowed( | |
253 task->traits.shutdown_behavior() != | |
254 TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN); | |
255 const bool previous_io_allowed = | |
256 ThreadRestrictions::SetIOAllowed(task->traits.may_block()); | |
257 const bool previous_wait_allowed = ThreadRestrictions::SetWaitAllowed( | |
258 task->traits.with_base_sync_primitives()); | |
259 | |
260 { | |
261 ScopedSetSequenceTokenForCurrentThread | |
262 scoped_set_sequence_token_for_current_thread(sequence_token); | |
263 ScopedSetTaskPriorityForCurrentThread | |
264 scoped_set_task_priority_for_current_thread(task->traits.priority()); | |
265 | |
266 // Set up TaskRunnerHandle as expected for the scope of the task. | |
267 std::unique_ptr<SequencedTaskRunnerHandle> sequenced_task_runner_handle; | |
268 std::unique_ptr<ThreadTaskRunnerHandle> single_thread_task_runner_handle; | |
269 DCHECK(!task->sequenced_task_runner_ref || | |
270 !task->single_thread_task_runner_ref); | |
271 if (task->sequenced_task_runner_ref) { | |
272 sequenced_task_runner_handle.reset( | |
273 new SequencedTaskRunnerHandle(task->sequenced_task_runner_ref)); | |
274 } else if (task->single_thread_task_runner_ref) { | |
275 single_thread_task_runner_handle.reset( | |
276 new ThreadTaskRunnerHandle(task->single_thread_task_runner_ref)); | |
277 } | |
278 | |
279 TRACE_TASK_EXECUTION(kRunFunctionName, *task); | |
280 | |
281 const char* const execution_mode = | |
282 task->single_thread_task_runner_ref | |
283 ? kSingleThreadExecutionMode | |
284 : (task->sequenced_task_runner_ref ? kSequencedExecutionMode | |
285 : kParallelExecutionMode); | |
286 // TODO(gab): In a better world this would be tacked on as an extra arg | |
287 // to the trace event generated above. This is not possible however until | |
288 // http://crbug.com/652692 is resolved. | |
289 TRACE_EVENT1("task_scheduler", "TaskTracker::RunTask", "task_info", | |
290 MakeUnique<TaskTracingInfo>(task->traits, execution_mode, | |
291 sequence_token)); | |
292 | |
293 PerformRunTask(std::move(task)); | |
294 } | |
295 | |
296 ThreadRestrictions::SetWaitAllowed(previous_wait_allowed); | |
297 ThreadRestrictions::SetIOAllowed(previous_io_allowed); | |
298 ThreadRestrictions::SetSingletonAllowed(previous_singleton_allowed); | |
299 | |
300 AfterRunTask(shutdown_behavior); | 250 AfterRunTask(shutdown_behavior); |
301 } | 251 } |
302 | 252 |
303 if (!is_delayed) | 253 if (!is_delayed) |
304 DecrementNumPendingUndelayedTasks(); | 254 DecrementNumPendingUndelayedTasks(); |
305 | 255 |
306 return can_run_task; | 256 return can_run_task; |
307 } | 257 } |
308 | 258 |
309 bool TaskTracker::HasShutdownStarted() const { | 259 bool TaskTracker::HasShutdownStarted() const { |
(...skipping 10 matching lines...) Expand all Loading... |
320 | 270 |
321 // Create a dummy |shutdown_event_| to satisfy TaskTracker's expectation of | 271 // Create a dummy |shutdown_event_| to satisfy TaskTracker's expectation of |
322 // its existence during shutdown (e.g. in OnBlockingShutdownTasksComplete()). | 272 // its existence during shutdown (e.g. in OnBlockingShutdownTasksComplete()). |
323 shutdown_event_.reset( | 273 shutdown_event_.reset( |
324 new WaitableEvent(WaitableEvent::ResetPolicy::MANUAL, | 274 new WaitableEvent(WaitableEvent::ResetPolicy::MANUAL, |
325 WaitableEvent::InitialState::NOT_SIGNALED)); | 275 WaitableEvent::InitialState::NOT_SIGNALED)); |
326 | 276 |
327 state_->StartShutdown(); | 277 state_->StartShutdown(); |
328 } | 278 } |
329 | 279 |
330 void TaskTracker::PerformRunTask(std::unique_ptr<Task> task) { | 280 void TaskTracker::PerformRunTask(std::unique_ptr<Task> task, |
331 debug::TaskAnnotator().RunTask(kQueueFunctionName, task.get()); | 281 const SequenceToken& sequence_token) { |
| 282 RecordTaskLatencyHistogram(task.get()); |
| 283 |
| 284 const bool previous_singleton_allowed = |
| 285 ThreadRestrictions::SetSingletonAllowed( |
| 286 task->traits.shutdown_behavior() != |
| 287 TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN); |
| 288 const bool previous_io_allowed = |
| 289 ThreadRestrictions::SetIOAllowed(task->traits.may_block()); |
| 290 const bool previous_wait_allowed = ThreadRestrictions::SetWaitAllowed( |
| 291 task->traits.with_base_sync_primitives()); |
| 292 |
| 293 { |
| 294 ScopedSetSequenceTokenForCurrentThread |
| 295 scoped_set_sequence_token_for_current_thread(sequence_token); |
| 296 ScopedSetTaskPriorityForCurrentThread |
| 297 scoped_set_task_priority_for_current_thread(task->traits.priority()); |
| 298 |
| 299 // Set up TaskRunnerHandle as expected for the scope of the task. |
| 300 std::unique_ptr<SequencedTaskRunnerHandle> sequenced_task_runner_handle; |
| 301 std::unique_ptr<ThreadTaskRunnerHandle> single_thread_task_runner_handle; |
| 302 DCHECK(!task->sequenced_task_runner_ref || |
| 303 !task->single_thread_task_runner_ref); |
| 304 if (task->sequenced_task_runner_ref) { |
| 305 sequenced_task_runner_handle.reset( |
| 306 new SequencedTaskRunnerHandle(task->sequenced_task_runner_ref)); |
| 307 } else if (task->single_thread_task_runner_ref) { |
| 308 single_thread_task_runner_handle.reset( |
| 309 new ThreadTaskRunnerHandle(task->single_thread_task_runner_ref)); |
| 310 } |
| 311 |
| 312 TRACE_TASK_EXECUTION(kRunFunctionName, *task); |
| 313 |
| 314 const char* const execution_mode = |
| 315 task->single_thread_task_runner_ref |
| 316 ? kSingleThreadExecutionMode |
| 317 : (task->sequenced_task_runner_ref ? kSequencedExecutionMode |
| 318 : kParallelExecutionMode); |
| 319 // TODO(gab): In a better world this would be tacked on as an extra arg |
| 320 // to the trace event generated above. This is not possible however until |
| 321 // http://crbug.com/652692 is resolved. |
| 322 TRACE_EVENT1("task_scheduler", "TaskTracker::RunTask", "task_info", |
| 323 MakeUnique<TaskTracingInfo>(task->traits, execution_mode, |
| 324 sequence_token)); |
| 325 |
| 326 debug::TaskAnnotator().RunTask(kQueueFunctionName, task.get()); |
| 327 } |
| 328 |
| 329 ThreadRestrictions::SetWaitAllowed(previous_wait_allowed); |
| 330 ThreadRestrictions::SetIOAllowed(previous_io_allowed); |
| 331 ThreadRestrictions::SetSingletonAllowed(previous_singleton_allowed); |
332 } | 332 } |
333 | 333 |
334 void TaskTracker::PerformShutdown() { | 334 void TaskTracker::PerformShutdown() { |
335 { | 335 { |
336 AutoSchedulerLock auto_lock(shutdown_lock_); | 336 AutoSchedulerLock auto_lock(shutdown_lock_); |
337 | 337 |
338 // This method can only be called once. | 338 // This method can only be called once. |
339 DCHECK(!shutdown_event_); | 339 DCHECK(!shutdown_event_); |
340 DCHECK(!num_block_shutdown_tasks_posted_during_shutdown_); | 340 DCHECK(!num_block_shutdown_tasks_posted_during_shutdown_); |
341 DCHECK(!state_->HasShutdownStarted()); | 341 DCHECK(!state_->HasShutdownStarted()); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 task_latency_histograms_[static_cast<int>(task->traits.priority())] | 512 task_latency_histograms_[static_cast<int>(task->traits.priority())] |
513 [task->traits.may_block() || | 513 [task->traits.may_block() || |
514 task->traits.with_base_sync_primitives() | 514 task->traits.with_base_sync_primitives() |
515 ? 1 | 515 ? 1 |
516 : 0] | 516 : 0] |
517 ->Add(task_latency.InMicroseconds()); | 517 ->Add(task_latency.InMicroseconds()); |
518 } | 518 } |
519 | 519 |
520 } // namespace internal | 520 } // namespace internal |
521 } // namespace base | 521 } // namespace base |
OLD | NEW |