-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
task_queue.cc
324 lines (270 loc) · 9.95 KB
/
task_queue.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/task/sequence_manager/task_queue.h"
#include <utility>
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/task/sequence_manager/associated_thread_id.h"
#include "base/task/sequence_manager/sequence_manager_impl.h"
#include "base/task/sequence_manager/task_queue_impl.h"
#include "base/threading/thread_checker.h"
#include "base/threading/thread_checker_impl.h"
#include "base/time/time.h"
#include "base/trace_event/base_tracing.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace base {
namespace sequence_manager {
TaskQueue::QueueEnabledVoter::QueueEnabledVoter(
scoped_refptr<TaskQueue> task_queue)
: task_queue_(std::move(task_queue)), enabled_(true) {
task_queue_->AddQueueEnabledVoter(enabled_);
}
TaskQueue::QueueEnabledVoter::~QueueEnabledVoter() {
task_queue_->RemoveQueueEnabledVoter(enabled_);
}
void TaskQueue::QueueEnabledVoter::SetVoteToEnable(bool enabled) {
if (enabled == enabled_)
return;
enabled_ = enabled;
task_queue_->OnQueueEnabledVoteChanged(enabled_);
}
void TaskQueue::AddQueueEnabledVoter(bool voter_is_enabled) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
++voter_count_;
if (voter_is_enabled)
++enabled_voter_count_;
}
void TaskQueue::RemoveQueueEnabledVoter(bool voter_is_enabled) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
// Voters and task queues are often stored in pairs, and the voter is often
// destroyed after the queue is shut down.
if (!impl_) {
return;
}
bool was_enabled = AreAllQueueEnabledVotersEnabled();
if (voter_is_enabled) {
--enabled_voter_count_;
DCHECK_GE(enabled_voter_count_, 0);
}
--voter_count_;
DCHECK_GE(voter_count_, 0);
bool is_enabled = AreAllQueueEnabledVotersEnabled();
if (was_enabled != is_enabled)
impl_->SetQueueEnabled(is_enabled);
}
bool TaskQueue::AreAllQueueEnabledVotersEnabled() const {
return enabled_voter_count_ == voter_count_;
}
void TaskQueue::OnQueueEnabledVoteChanged(bool enabled) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
bool was_enabled = AreAllQueueEnabledVotersEnabled();
if (enabled) {
++enabled_voter_count_;
DCHECK_LE(enabled_voter_count_, voter_count_);
} else {
--enabled_voter_count_;
DCHECK_GE(enabled_voter_count_, 0);
}
bool is_enabled = AreAllQueueEnabledVotersEnabled();
if (was_enabled != is_enabled)
impl_->SetQueueEnabled(is_enabled);
}
TaskQueue::TaskQueue(std::unique_ptr<internal::TaskQueueImpl> impl,
const TaskQueue::Spec& spec)
: impl_(std::move(impl)),
sequence_manager_(impl_->GetSequenceManagerWeakPtr()),
associated_thread_((impl_->sequence_manager())
? impl_->sequence_manager()->associated_thread()
: MakeRefCounted<internal::AssociatedThreadId>()),
default_task_runner_(impl_->CreateTaskRunner(kTaskTypeNone)),
name_(impl_->GetProtoName()) {}
TaskQueue::~TaskQueue() {
ShutdownTaskQueueGracefully();
}
void TaskQueue::ShutdownTaskQueueGracefully() {
// scoped_refptr guarantees us that this object isn't used.
if (!impl_)
return;
if (impl_->IsUnregistered())
return;
// If we've not been unregistered then this must occur on the main thread.
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
impl_->ResetThrottler();
impl_->sequence_manager()->ShutdownTaskQueueGracefully(TakeTaskQueueImpl());
}
TaskQueue::TaskTiming::TaskTiming(bool has_wall_time, bool has_thread_time)
: has_wall_time_(has_wall_time), has_thread_time_(has_thread_time) {}
void TaskQueue::TaskTiming::RecordTaskStart(LazyNow* now) {
DCHECK_EQ(State::NotStarted, state_);
state_ = State::Running;
if (has_wall_time())
start_time_ = now->Now();
if (has_thread_time())
start_thread_time_ = base::ThreadTicks::Now();
}
void TaskQueue::TaskTiming::RecordTaskEnd(LazyNow* now) {
DCHECK(state_ == State::Running || state_ == State::Finished);
if (state_ == State::Finished)
return;
state_ = State::Finished;
if (has_wall_time())
end_time_ = now->Now();
if (has_thread_time())
end_thread_time_ = base::ThreadTicks::Now();
}
void TaskQueue::ShutdownTaskQueue() {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
// TODO(crbug.com/1413795): Fix that some task queues get shut down more than
// once.
if (!impl_) {
return;
}
if (!sequence_manager_) {
TakeTaskQueueImpl().reset();
return;
}
sequence_manager_->UnregisterTaskQueueImpl(TakeTaskQueueImpl());
}
scoped_refptr<SingleThreadTaskRunner> TaskQueue::CreateTaskRunner(
TaskType task_type) {
// We only need to lock if we're not on the main thread.
base::internal::CheckedAutoLockMaybe lock(IsOnMainThread() ? &impl_lock_
: nullptr);
DCHECK(impl_);
return impl_->CreateTaskRunner(task_type);
}
std::unique_ptr<TaskQueue::QueueEnabledVoter>
TaskQueue::CreateQueueEnabledVoter() {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
return WrapUnique(new QueueEnabledVoter(this));
}
bool TaskQueue::IsQueueEnabled() const {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
return impl_->IsQueueEnabled();
}
bool TaskQueue::IsEmpty() const {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
return impl_->IsEmpty();
}
size_t TaskQueue::GetNumberOfPendingTasks() const {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
return impl_->GetNumberOfPendingTasks();
}
bool TaskQueue::HasTaskToRunImmediatelyOrReadyDelayedTask() const {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
return impl_->HasTaskToRunImmediatelyOrReadyDelayedTask();
}
absl::optional<WakeUp> TaskQueue::GetNextDesiredWakeUp() {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
return impl_->GetNextDesiredWakeUp();
}
void TaskQueue::UpdateWakeUp(LazyNow* lazy_now) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
impl_->UpdateWakeUp(lazy_now);
}
void TaskQueue::SetQueuePriority(TaskQueue::QueuePriority priority) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
impl_->SetQueuePriority(priority);
}
TaskQueue::QueuePriority TaskQueue::GetQueuePriority() const {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
// TODO(crbug.com/1413795): change this to DCHECK(impl_) since task queues
// should not be used after shutdown.
DCHECK(impl_);
return impl_->GetQueuePriority();
}
void TaskQueue::AddTaskObserver(TaskObserver* task_observer) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
impl_->AddTaskObserver(task_observer);
}
void TaskQueue::RemoveTaskObserver(TaskObserver* task_observer) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
impl_->RemoveTaskObserver(task_observer);
}
void TaskQueue::InsertFence(InsertFencePosition position) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
impl_->InsertFence(position);
}
void TaskQueue::InsertFenceAt(TimeTicks time) {
impl_->InsertFenceAt(time);
}
void TaskQueue::RemoveFence() {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
impl_->RemoveFence();
}
bool TaskQueue::HasActiveFence() {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
return impl_->HasActiveFence();
}
bool TaskQueue::BlockedByFence() const {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
return impl_->BlockedByFence();
}
const char* TaskQueue::GetName() const {
return perfetto::protos::pbzero::SequenceManagerTask::QueueName_Name(name_);
}
void TaskQueue::WriteIntoTrace(perfetto::TracedValue context) const {
auto dict = std::move(context).WriteDictionary();
dict.Add("name", name_);
}
void TaskQueue::SetThrottler(Throttler* throttler) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
// |throttler| is guaranteed to outlive TaskQueue and TaskQueueImpl lifecycle
// is controlled by |this|.
impl_->SetThrottler(throttler);
}
void TaskQueue::ResetThrottler() {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
impl_->ResetThrottler();
}
void TaskQueue::SetShouldReportPostedTasksWhenDisabled(bool should_report) {
impl_->SetShouldReportPostedTasksWhenDisabled(should_report);
}
bool TaskQueue::IsOnMainThread() const {
return associated_thread_->IsBoundToCurrentThread();
}
std::unique_ptr<internal::TaskQueueImpl> TaskQueue::TakeTaskQueueImpl() {
base::internal::CheckedAutoLock lock(impl_lock_);
DCHECK(impl_);
return std::move(impl_);
}
void TaskQueue::SetOnTaskStartedHandler(OnTaskStartedHandler handler) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
impl_->SetOnTaskStartedHandler(std::move(handler));
}
void TaskQueue::SetOnTaskCompletedHandler(OnTaskCompletedHandler handler) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
impl_->SetOnTaskCompletedHandler(std::move(handler));
}
std::unique_ptr<TaskQueue::OnTaskPostedCallbackHandle>
TaskQueue::AddOnTaskPostedHandler(OnTaskPostedHandler handler) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
return impl_->AddOnTaskPostedHandler(std::move(handler));
}
void TaskQueue::SetTaskExecutionTraceLogger(TaskExecutionTraceLogger logger) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
DCHECK(impl_);
impl_->SetTaskExecutionTraceLogger(std::move(logger));
}
} // namespace sequence_manager
} // namespace base