forked from v8/v8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault-foreground-task-runner.cc
More file actions
190 lines (159 loc) · 6.53 KB
/
default-foreground-task-runner.cc
File metadata and controls
190 lines (159 loc) · 6.53 KB
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
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/libplatform/default-foreground-task-runner.h"
#include "src/base/platform/mutex.h"
#include "src/libplatform/default-platform.h"
namespace v8 {
namespace platform {
DefaultForegroundTaskRunner::RunTaskScope::RunTaskScope(
std::shared_ptr<DefaultForegroundTaskRunner> task_runner)
: task_runner_(task_runner) {
DCHECK_GE(task_runner->nesting_depth_, 0);
task_runner->nesting_depth_++;
}
DefaultForegroundTaskRunner::RunTaskScope::~RunTaskScope() {
DCHECK_GT(task_runner_->nesting_depth_, 0);
task_runner_->nesting_depth_--;
}
DefaultForegroundTaskRunner::DefaultForegroundTaskRunner(
IdleTaskSupport idle_task_support, TimeFunction time_function)
: idle_task_support_(idle_task_support), time_function_(time_function) {}
void DefaultForegroundTaskRunner::Terminate() {
base::MutexGuard guard(&lock_);
terminated_ = true;
// Drain the task queues.
while (!task_queue_.empty()) task_queue_.pop_front();
while (!delayed_task_queue_.empty()) delayed_task_queue_.pop();
while (!idle_task_queue_.empty()) idle_task_queue_.pop();
}
void DefaultForegroundTaskRunner::PostTaskLocked(std::unique_ptr<Task> task,
Nestability nestability,
const base::MutexGuard&) {
if (terminated_) return;
task_queue_.push_back(std::make_pair(nestability, std::move(task)));
event_loop_control_.NotifyOne();
}
void DefaultForegroundTaskRunner::PostTask(std::unique_ptr<Task> task) {
base::MutexGuard guard(&lock_);
PostTaskLocked(std::move(task), kNestable, guard);
}
double DefaultForegroundTaskRunner::MonotonicallyIncreasingTime() {
return time_function_();
}
void DefaultForegroundTaskRunner::PostDelayedTaskLocked(
std::unique_ptr<Task> task, double delay_in_seconds,
Nestability nestability, const base::MutexGuard&) {
DCHECK_GE(delay_in_seconds, 0.0);
if (terminated_) return;
double deadline = MonotonicallyIncreasingTime() + delay_in_seconds;
delayed_task_queue_.push({deadline, nestability, std::move(task)});
event_loop_control_.NotifyOne();
}
void DefaultForegroundTaskRunner::PostDelayedTask(std::unique_ptr<Task> task,
double delay_in_seconds) {
base::MutexGuard guard(&lock_);
PostDelayedTaskLocked(std::move(task), delay_in_seconds, kNestable, guard);
}
void DefaultForegroundTaskRunner::PostNonNestableDelayedTask(
std::unique_ptr<Task> task, double delay_in_seconds) {
base::MutexGuard guard(&lock_);
PostDelayedTaskLocked(std::move(task), delay_in_seconds, kNonNestable, guard);
}
void DefaultForegroundTaskRunner::PostIdleTask(std::unique_ptr<IdleTask> task) {
CHECK_EQ(IdleTaskSupport::kEnabled, idle_task_support_);
base::MutexGuard guard(&lock_);
if (terminated_) return;
idle_task_queue_.push(std::move(task));
}
bool DefaultForegroundTaskRunner::IdleTasksEnabled() {
return idle_task_support_ == IdleTaskSupport::kEnabled;
}
void DefaultForegroundTaskRunner::PostNonNestableTask(
std::unique_ptr<Task> task) {
base::MutexGuard guard(&lock_);
PostTaskLocked(std::move(task), kNonNestable, guard);
}
bool DefaultForegroundTaskRunner::NonNestableTasksEnabled() const {
return true;
}
bool DefaultForegroundTaskRunner::HasPoppableTaskInQueue() const {
if (nesting_depth_ == 0) return !task_queue_.empty();
for (auto it = task_queue_.cbegin(); it != task_queue_.cend(); it++) {
if (it->first == kNestable) return true;
}
return false;
}
void DefaultForegroundTaskRunner::MoveExpiredDelayedTasks(
const base::MutexGuard& guard) {
Nestability nestability;
std::unique_ptr<Task> task =
PopTaskFromDelayedQueueLocked(guard, &nestability);
while (task) {
PostTaskLocked(std::move(task), nestability, guard);
task = PopTaskFromDelayedQueueLocked(guard, &nestability);
}
}
std::unique_ptr<Task> DefaultForegroundTaskRunner::PopTaskFromQueue(
MessageLoopBehavior wait_for_work) {
base::MutexGuard guard(&lock_);
MoveExpiredDelayedTasks(guard);
while (!HasPoppableTaskInQueue()) {
if (wait_for_work == MessageLoopBehavior::kDoNotWait) return {};
WaitForTaskLocked(guard);
MoveExpiredDelayedTasks(guard);
}
auto it = task_queue_.begin();
for (; it != task_queue_.end(); it++) {
// When the task queue is nested (i.e. popping a task from the queue from
// within a task), only nestable tasks may run. Otherwise, any task may run.
if (nesting_depth_ == 0 || it->first == kNestable) break;
}
DCHECK(it != task_queue_.end());
std::unique_ptr<Task> task = std::move(it->second);
task_queue_.erase(it);
return task;
}
std::unique_ptr<Task>
DefaultForegroundTaskRunner::PopTaskFromDelayedQueueLocked(
const base::MutexGuard&, Nestability* nestability) {
if (delayed_task_queue_.empty()) return {};
double now = MonotonicallyIncreasingTime();
const DelayedEntry& entry = delayed_task_queue_.top();
if (entry.timeout_time > now) return {};
// The const_cast here is necessary because there does not exist a clean way
// to get a unique_ptr out of the priority queue. We provide the priority
// queue with a custom comparison operator to make sure that the priority
// queue does not access the unique_ptr. Therefore it should be safe to reset
// the unique_ptr in the priority queue here. Note that the DelayedEntry is
// removed from the priority_queue immediately afterwards.
std::unique_ptr<Task> task = std::move(const_cast<DelayedEntry&>(entry).task);
*nestability = entry.nestability;
delayed_task_queue_.pop();
return task;
}
std::unique_ptr<IdleTask> DefaultForegroundTaskRunner::PopTaskFromIdleQueue() {
base::MutexGuard guard(&lock_);
if (idle_task_queue_.empty()) return {};
std::unique_ptr<IdleTask> task = std::move(idle_task_queue_.front());
idle_task_queue_.pop();
return task;
}
void DefaultForegroundTaskRunner::WaitForTaskLocked(const base::MutexGuard&) {
if (!delayed_task_queue_.empty()) {
double now = MonotonicallyIncreasingTime();
const DelayedEntry& entry = delayed_task_queue_.top();
double time_until_task = entry.timeout_time - now;
if (time_until_task > 0) {
bool woken_up = event_loop_control_.WaitFor(
&lock_,
base::TimeDelta::FromMicroseconds(
time_until_task * base::TimeConstants::kMicrosecondsPerSecond));
USE(woken_up);
}
} else {
event_loop_control_.Wait(&lock_);
}
}
} // namespace platform
} // namespace v8