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 #include "base/metrics/histogram_macros.h" | |
10 | |
11 namespace base { | |
12 namespace internal { | |
13 | |
14 namespace { | |
15 const char kQueueFunctionName[] = "base::PostTask"; | |
16 } // namespace | |
17 | |
18 TaskTracker::TaskTracker() = default; | |
19 TaskTracker::~TaskTracker() = default; | |
20 | |
21 void TaskTracker::Shutdown() { | |
22 size_t num_tasks_blocking_shutdown_copy; | |
23 | |
24 { | |
25 AutoSchedulerLock auto_lock(lock_); | |
26 | |
27 DCHECK(!shutdown_completed_ && !shutdown_cv_) | |
28 << "TaskTracker::Shutdown() should only be invoked once."; | |
29 | |
30 shutdown_cv_ = lock_.CreateConditionVariable(); | |
31 | |
32 // Wait until the number of tasks blocking shutdown is zero. | |
33 while (num_tasks_blocking_shutdown_ != 0) | |
34 shutdown_cv_->Wait(); | |
35 | |
36 shutdown_cv_.reset(); | |
37 shutdown_completed_ = true; | |
38 | |
39 num_tasks_blocking_shutdown_copy = num_tasks_blocking_shutdown_; | |
gab
2016/03/21 17:42:30
UMA is extremely optimized, I don't think it's wor
fdoray
2016/03/21 19:08:07
Done because it makes the code simpler.
However,
gab
2016/03/21 19:53:25
Right, I saw that, and I agree that made sense the
| |
40 } | |
41 | |
42 UMA_HISTOGRAM_COUNTS_100( | |
gab
2016/03/21 17:42:30
UMA_HISTOGRAM_COUNTS_100 sounds low given current
fdoray
2016/03/21 19:08:07
Done.
The current restriction for BlockingPool is
gab
2016/03/21 19:53:25
Ah, yes :-), then let's use that.
| |
43 "TaskScheduler.BlockShutdownTasksPostedDuringShutdown", | |
44 num_tasks_blocking_shutdown_copy); | |
45 } | |
46 | |
47 void TaskTracker::PostTask( | |
48 const Callback<void(scoped_ptr<Task>)>& post_task_callback, | |
49 scoped_ptr<Task> task) { | |
50 DCHECK(!post_task_callback.is_null()); | |
51 DCHECK(task); | |
52 | |
53 if (!BeforePostTask(task->traits.shutdown_behavior())) | |
54 return; | |
55 | |
56 debug::TaskAnnotator task_annotator; | |
57 task_annotator.DidQueueTask(kQueueFunctionName, *task); | |
58 | |
59 post_task_callback.Run(std::move(task)); | |
60 } | |
61 | |
62 void TaskTracker::RunTask(const Task* task) { | |
63 DCHECK(task); | |
64 | |
65 const TaskShutdownBehavior shutdown_behavior = | |
66 task->traits.shutdown_behavior(); | |
67 if (!BeforeRunTask(shutdown_behavior)) | |
68 return; | |
69 | |
70 debug::TaskAnnotator task_annotator; | |
71 task_annotator.RunTask(kQueueFunctionName, *task); | |
72 | |
73 AfterRunTask(shutdown_behavior); | |
74 } | |
75 | |
76 bool TaskTracker::IsShuttingDownForTesting() const { | |
77 AutoSchedulerLock auto_lock(lock_); | |
78 return !!shutdown_cv_; | |
79 } | |
80 | |
81 bool TaskTracker::BeforePostTask(TaskShutdownBehavior shutdown_behavior) { | |
82 AutoSchedulerLock auto_lock(lock_); | |
83 | |
84 if (shutdown_completed_) { | |
85 // A BLOCK_SHUTDOWN task posted after shutdown has completed is an ordering | |
86 // bug. This DCHECK aims to catch those early. | |
87 DCHECK_NE(TaskShutdownBehavior::BLOCK_SHUTDOWN, shutdown_behavior); | |
88 | |
89 // No task is allowed to be posted after shutdown. | |
90 return false; | |
91 } | |
92 | |
93 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN) { | |
94 // BLOCK_SHUTDOWN tasks block shutdown between the moment they are posted | |
95 // and the moment they complete their execution. | |
96 ++num_tasks_blocking_shutdown_; | |
97 | |
98 if (shutdown_cv_) | |
99 ++num_block_shutdown_tasks_posted_during_shutdown_; | |
100 | |
101 return true; | |
102 } | |
103 | |
104 return !shutdown_cv_; | |
105 } | |
106 | |
107 bool TaskTracker::BeforeRunTask(TaskShutdownBehavior shutdown_behavior) { | |
108 AutoSchedulerLock auto_lock(lock_); | |
109 | |
110 if (shutdown_completed_) { | |
111 // A BLOCK_SHUTDOWN task run after shutdown has completed is an ordering | |
112 // bug. This DCHECK aims to catch those early. | |
gab
2016/03/21 17:42:30
Posting is an ordering bug, running is just unexpe
fdoray
2016/03/21 19:08:07
It is not an error if a WorkerThread tries to run
gab
2016/03/21 19:53:25
Ah right, okay.
| |
113 DCHECK_NE(TaskShutdownBehavior::BLOCK_SHUTDOWN, shutdown_behavior); | |
114 return false; | |
115 } | |
116 | |
117 switch (shutdown_behavior) { | |
118 case TaskShutdownBehavior::BLOCK_SHUTDOWN: | |
119 return true; | |
120 | |
121 case TaskShutdownBehavior::SKIP_ON_SHUTDOWN: | |
122 if (shutdown_cv_) | |
123 return false; | |
124 | |
125 // SKIP_ON_SHUTDOWN tasks block shutdown while they are running. | |
126 ++num_tasks_blocking_shutdown_; | |
127 return true; | |
128 | |
129 case TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN: | |
130 return !shutdown_cv_; | |
131 } | |
132 | |
133 NOTREACHED(); | |
134 return false; | |
135 } | |
136 | |
137 void TaskTracker::AfterRunTask(TaskShutdownBehavior shutdown_behavior) { | |
138 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN || | |
139 shutdown_behavior == TaskShutdownBehavior::SKIP_ON_SHUTDOWN) { | |
140 AutoSchedulerLock auto_lock(lock_); | |
141 DCHECK_GT(num_tasks_blocking_shutdown_, 0U); | |
142 --num_tasks_blocking_shutdown_; | |
143 if (num_tasks_blocking_shutdown_ == 0 && shutdown_cv_) | |
144 shutdown_cv_->Signal(); | |
145 } | |
146 } | |
147 | |
148 } // namespace internal | |
149 } // namespace base | |
OLD | NEW |