Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/task_scheduler/task_tracker.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/debug/task_annotator.h" | |
| 9 | |
| 10 namespace base { | |
| 11 namespace internal { | |
| 12 | |
| 13 namespace { | |
| 14 const char kQueueFunctionName[] = "base::PostTask"; | |
| 15 } // namespace | |
| 16 | |
| 17 TaskTracker::TaskTracker() = default; | |
| 18 TaskTracker::~TaskTracker() = default; | |
| 19 | |
| 20 void TaskTracker::Shutdown() { | |
| 21 AutoSchedulerLock auto_lock(lock_); | |
| 22 | |
| 23 DCHECK(!shutdown_completed_ && !shutdown_cv_) | |
| 24 << "TaskTracker::Shutdown() should only be invoked once."; | |
| 25 | |
| 26 shutdown_cv_ = lock_.CreateConditionVariable(); | |
| 27 | |
| 28 // Wait until the number of tasks blocking shutdown is zero. | |
| 29 while (num_tasks_blocking_shutdown_ != 0) | |
| 30 shutdown_cv_->Wait(); | |
| 31 | |
| 32 shutdown_cv_.reset(); | |
| 33 shutdown_completed_ = true; | |
| 34 } | |
| 35 | |
| 36 void TaskTracker::PostTask( | |
| 37 const Callback<void(scoped_ptr<Task>)>& post_task_callback, | |
| 38 scoped_ptr<Task> task) { | |
| 39 DCHECK(!post_task_callback.is_null()); | |
| 40 DCHECK(task); | |
| 41 | |
| 42 if (!BeforePostTask(task->traits.shutdown_behavior())) | |
| 43 return; | |
| 44 | |
| 45 debug::TaskAnnotator task_annotator; | |
| 46 task_annotator.DidQueueTask(kQueueFunctionName, *task.get()); | |
| 47 | |
| 48 post_task_callback.Run(std::move(task)); | |
| 49 } | |
| 50 | |
| 51 void TaskTracker::RunTask(const Task* task) { | |
| 52 DCHECK(task); | |
| 53 | |
| 54 const TaskShutdownBehavior shutdown_behavior = | |
| 55 task->traits.shutdown_behavior(); | |
| 56 if (!BeforeRunTask(shutdown_behavior)) | |
| 57 return; | |
| 58 | |
| 59 debug::TaskAnnotator task_annotator; | |
| 60 task_annotator.RunTask(kQueueFunctionName, *task); | |
| 61 | |
| 62 AfterRunTask(shutdown_behavior); | |
| 63 } | |
| 64 | |
| 65 bool TaskTracker::IsShuttingDownForTesting() const { | |
| 66 AutoSchedulerLock auto_lock(lock_); | |
| 67 return shutdown_cv_.get() != nullptr; | |
| 68 } | |
| 69 | |
| 70 bool TaskTracker::BeforePostTask(TaskShutdownBehavior shutdown_behavior) { | |
| 71 AutoSchedulerLock auto_lock(lock_); | |
| 72 | |
| 73 if (shutdown_completed_) | |
| 74 return false; | |
| 75 | |
| 76 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN) { | |
| 77 // BLOCK_SHUTDOWN tasks block shutdown between the moment they are posted | |
| 78 // and the moment they complete their execution. | |
| 79 ++num_tasks_blocking_shutdown_; | |
| 80 | |
| 81 // TODO(fdoray): Add a UMA histogram with the number of BLOCK_SHUTDOWN tasks | |
|
robliao
2016/03/17 23:34:21
Should we go ahead and do this now?
fdoray
2016/03/18 20:35:39
Done.
| |
| 82 // posted during shutdown. | |
| 83 | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 87 return !shutdown_cv_; | |
| 88 } | |
| 89 | |
| 90 bool TaskTracker::BeforeRunTask(TaskShutdownBehavior shutdown_behavior) { | |
| 91 AutoSchedulerLock auto_lock(lock_); | |
| 92 | |
| 93 if (shutdown_completed_) { | |
| 94 // A BLOCK_SHUTDOWN task posted after shutdown has completed is an ordering | |
| 95 // bug. This DCHECK aims to catch those early. | |
| 96 DCHECK_NE(TaskShutdownBehavior::BLOCK_SHUTDOWN, shutdown_behavior); | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 100 switch (shutdown_behavior) { | |
| 101 case TaskShutdownBehavior::BLOCK_SHUTDOWN: | |
| 102 return true; | |
| 103 | |
| 104 case TaskShutdownBehavior::SKIP_ON_SHUTDOWN: | |
| 105 if (shutdown_cv_) | |
| 106 return false; | |
| 107 | |
| 108 // SKIP_ON_SHUTDOWN tasks block shutdown while they are running. | |
| 109 ++num_tasks_blocking_shutdown_; | |
| 110 return true; | |
| 111 | |
| 112 case TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN: | |
| 113 return !shutdown_cv_; | |
| 114 } | |
| 115 | |
| 116 NOTREACHED(); | |
| 117 return false; | |
| 118 } | |
| 119 | |
| 120 void TaskTracker::AfterRunTask(TaskShutdownBehavior shutdown_behavior) { | |
| 121 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN || | |
| 122 shutdown_behavior == TaskShutdownBehavior::SKIP_ON_SHUTDOWN) { | |
| 123 AutoSchedulerLock auto_lock(lock_); | |
| 124 DCHECK_GT(num_tasks_blocking_shutdown_, 0U); | |
| 125 --num_tasks_blocking_shutdown_; | |
| 126 if (num_tasks_blocking_shutdown_ == 0 && shutdown_cv_) | |
| 127 shutdown_cv_->Signal(); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 } // namespace internal | |
| 132 } // namespace base | |
| OLD | NEW |