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

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: CR gab #19 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()),
gab 2016/03/17 01:42:12 Since we only need |cv_| during shutdown. How abou
fdoray 2016/03/17 20:12:16 Done.
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());
gab 2016/03/17 01:42:12 I don't think .get() is necessary here, scoped_ptr
fdoray 2016/03/17 20:12:16 Done.
45
46 if (!BeforePostTask(task->traits.shutdown_behavior()))
47 return;
48
49 debug::TaskAnnotator task_annotator;
50 task_annotator.DidQueueTask(kQueueFunctionName, *task.get());
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 (!BeforeRunTask(shutdown_behavior))
61 return;
62
63 debug::TaskAnnotator task_annotator;
64 task_annotator.RunTask(kQueueFunctionName, *task);
65
66 AfterRunTask(shutdown_behavior);
67 }
68
69 bool TaskTracker::BeforePostTask(TaskShutdownBehavior shutdown_behavior) {
70 AutoSchedulerLock auto_lock(lock_);
71
72 if (shutdown_completed_)
73 return false;
74
75 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN) {
76 // BLOCK_SHUTDOWN tasks block shutdown between the moment they are posted
77 // and the moment they complete their execution.
78 ++num_tasks_blocking_shutdown_;
79 return true;
80 }
81
82 return !is_shutting_down_;
83 }
84
85 bool TaskTracker::BeforeRunTask(TaskShutdownBehavior shutdown_behavior) {
86 AutoSchedulerLock auto_lock(lock_);
87
88 if (shutdown_completed_) {
89 DCHECK_NE(TaskShutdownBehavior::BLOCK_SHUTDOWN, shutdown_behavior);
gab 2016/03/17 01:42:12 Add a comment like: // A BLOCK_SHUTDOWN task post
fdoray 2016/03/17 20:12:16 Done.
90 return false;
91 }
92
93 switch (shutdown_behavior) {
94 case TaskShutdownBehavior::BLOCK_SHUTDOWN:
95 return true;
96
97 case TaskShutdownBehavior::SKIP_ON_SHUTDOWN:
98 if (is_shutting_down_)
99 return false;
100
101 // SKIP_ON_SHUTDOWN tasks block shutdown while they are running.
102 ++num_tasks_blocking_shutdown_;
103 return true;
104
105 case TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN:
106 return !is_shutting_down_;
107 }
108
109 NOTREACHED();
110 return false;
111 }
112
113 void TaskTracker::AfterRunTask(TaskShutdownBehavior shutdown_behavior) {
114 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN ||
115 shutdown_behavior == TaskShutdownBehavior::SKIP_ON_SHUTDOWN) {
116 AutoSchedulerLock auto_lock(lock_);
117 DCHECK_GT(num_tasks_blocking_shutdown_, 0U);
118 --num_tasks_blocking_shutdown_;
119 if (num_tasks_blocking_shutdown_ == 0 && is_shutting_down_)
120 cv_->Signal();
121 }
122 }
123
124 } // namespace internal
125 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698