Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: base/task_scheduler/task_tracker.cc

Issue 1705943002: TaskScheduler [5/9] Task Tracker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_3_pq
Patch Set: fix PostTask comment Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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()
18 : cv_(lock_.CreateConditionVariable()),
19 num_tasks_blocking_shutdown_(0),
20 is_shutting_down_(false),
21 shutdown_completed_(false) {}
22
23 TaskTracker::~TaskTracker() = default;
24
25 void TaskTracker::Shutdown() {
26 AutoSchedulerLock auto_lock(lock_);
27
28 DCHECK(!shutdown_completed_ && !is_shutting_down_)
29 << "TaskTracker::Shutdown() should only be invoked once.";
30 is_shutting_down_ = true;
31
32 // Wait until the number of tasks blocking shutdown is zero.
33 while (num_tasks_blocking_shutdown_ != 0)
34 cv_->Wait();
35
36 is_shutting_down_ = false;
37 shutdown_completed_ = true;
38 }
39
40 void TaskTracker::PostTask(
41 const Callback<void(scoped_ptr<Task>)>& post_task_callback,
42 scoped_ptr<Task> task) {
43 DCHECK(!post_task_callback.is_null());
44 DCHECK(task.get());
45
46 if (!RequestPostTask(task->traits.shutdown_behavior()))
47 return;
48
49 debug::TaskAnnotator task_annotator;
50 task_annotator.DidQueueTask(kQueueFunctionName, *task.get());
gab 2016/03/01 22:18:47 Not your fault but noticing that "DidQueueTask" is
fdoray 2016/03/02 00:38:41 Acknowledged.
51
52 post_task_callback.Run(std::move(task));
53 }
54
55 void TaskTracker::RunTask(const Task* task) {
56 DCHECK(task);
57
58 const TaskShutdownBehavior shutdown_behavior =
59 task->traits.shutdown_behavior();
60 if (!RequestScheduleTask(shutdown_behavior))
61 return;
62
63 debug::TaskAnnotator task_annotator;
64 task_annotator.RunTask(kQueueFunctionName, *task);
65
66 DidExecuteTask(shutdown_behavior);
67 }
68
69 bool TaskTracker::RequestPostTask(TaskShutdownBehavior shutdown_behavior) {
robliao 2016/03/01 22:29:41 Now that these are private, we can give them names
fdoray 2016/03/02 00:38:41 Done.
70 AutoSchedulerLock auto_lock(lock_);
71
72 if (shutdown_completed_)
73 return false;
74
75 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN) {
76 ++num_tasks_blocking_shutdown_;
77 return true;
78 }
79
80 return !is_shutting_down_;
81 }
82
83 bool TaskTracker::RequestScheduleTask(TaskShutdownBehavior shutdown_behavior) {
gab 2016/03/01 22:18:47 "RequestRunTask" to match nomenclature in rest of
fdoray 2016/03/02 00:38:41 robliao@ suggested different method names. Reques
84 AutoSchedulerLock auto_lock(lock_);
85
86 if (shutdown_completed_) {
87 DCHECK_NE(TaskShutdownBehavior::BLOCK_SHUTDOWN, shutdown_behavior);
88 return false;
89 }
90
91 switch (shutdown_behavior) {
92 case TaskShutdownBehavior::BLOCK_SHUTDOWN:
93 return true;
94
95 case TaskShutdownBehavior::SKIP_ON_SHUTDOWN:
96 if (is_shutting_down_)
97 return false;
98 ++num_tasks_blocking_shutdown_;
robliao 2016/03/01 22:29:41 It's worth commented that we want Shutdown to bloc
fdoray 2016/03/02 00:38:40 Done.
99 return true;
100
101 case TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN:
102 return !is_shutting_down_;
103 }
104
105 return false;
robliao 2016/03/01 22:29:41 Might as well add a NOTREACHED() here.
fdoray 2016/03/02 00:38:41 Done.
106 }
107
108 void TaskTracker::DidExecuteTask(TaskShutdownBehavior shutdown_behavior) {
109 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN ||
110 shutdown_behavior == TaskShutdownBehavior::SKIP_ON_SHUTDOWN) {
111 AutoSchedulerLock auto_lock(lock_);
112 DCHECK_GT(num_tasks_blocking_shutdown_, 0U);
113 --num_tasks_blocking_shutdown_;
114 if (num_tasks_blocking_shutdown_ == 0 && is_shutting_down_)
115 cv_->Signal();
116 }
117 }
118
119 } // namespace internal
120 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698