mirrored from https://chromium.googlesource.com/v8/v8.git
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
default-foreground-task-runner.h
129 lines (102 loc) · 4.68 KB
/
default-foreground-task-runner.h
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
// 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.
#ifndef V8_LIBPLATFORM_DEFAULT_FOREGROUND_TASK_RUNNER_H_
#define V8_LIBPLATFORM_DEFAULT_FOREGROUND_TASK_RUNNER_H_
#include <memory>
#include <queue>
#include "include/libplatform/libplatform.h"
#include "include/v8-platform.h"
#include "src/base/platform/condition-variable.h"
#include "src/base/platform/mutex.h"
namespace v8 {
namespace platform {
class V8_PLATFORM_EXPORT DefaultForegroundTaskRunner
: public NON_EXPORTED_BASE(TaskRunner) {
public:
using TimeFunction = double (*)();
class V8_NODISCARD RunTaskScope {
public:
explicit RunTaskScope(
std::shared_ptr<DefaultForegroundTaskRunner> task_runner);
~RunTaskScope();
RunTaskScope(const RunTaskScope&) = delete;
RunTaskScope& operator=(const RunTaskScope&) = delete;
private:
std::shared_ptr<DefaultForegroundTaskRunner> task_runner_;
};
DefaultForegroundTaskRunner(IdleTaskSupport idle_task_support,
TimeFunction time_function);
void Terminate();
std::unique_ptr<Task> PopTaskFromQueue(MessageLoopBehavior wait_for_work);
std::unique_ptr<IdleTask> PopTaskFromIdleQueue();
double MonotonicallyIncreasingTime();
// v8::TaskRunner implementation.
bool IdleTasksEnabled() override;
bool NonNestableTasksEnabled() const override;
private:
// v8::TaskRunner implementation.
void PostTaskImpl(std::unique_ptr<Task> task,
const SourceLocation& location) override;
void PostDelayedTaskImpl(std::unique_ptr<Task> task, double delay_in_seconds,
const SourceLocation& location) override;
void PostIdleTaskImpl(std::unique_ptr<IdleTask> task,
const SourceLocation& location) override;
void PostNonNestableTaskImpl(std::unique_ptr<Task> task,
const SourceLocation& location) override;
void PostNonNestableDelayedTaskImpl(std::unique_ptr<Task> task,
double delay_in_seconds,
const SourceLocation& location) override;
enum Nestability { kNestable, kNonNestable };
void WaitForTaskLocked();
// The same as PostTask or PostNonNestableTask, but the lock is already held
// by the caller. If the task runner is already terminated, the task is
// returned (such that it can be deleted later, after releasing the lock).
// Otherwise, nullptr is returned.
std::unique_ptr<Task> PostTaskLocked(std::unique_ptr<Task> task,
Nestability nestability);
// The same as PostDelayedTask or PostNonNestableDelayedTask, but the lock is
// already held by the caller.
void PostDelayedTaskLocked(std::unique_ptr<Task> task,
double delay_in_seconds, Nestability nestability);
// A caller of this function has to hold {mutex_}.
std::unique_ptr<Task> PopTaskFromDelayedQueueLocked(Nestability* nestability);
// A non-nestable task is poppable only if the task runner is not nested,
// i.e. if a task is not being run from within a task. A nestable task is
// always poppable.
bool HasPoppableTaskInQueue() const;
// Move delayed tasks that hit their deadline to the main queue. Returns all
// tasks that expired but were not scheduled because the task runner was
// terminated.
std::vector<std::unique_ptr<Task>> MoveExpiredDelayedTasksLocked();
bool terminated_ = false;
base::Mutex mutex_;
base::ConditionVariable event_loop_control_;
int nesting_depth_ = 0;
using TaskQueueEntry = std::pair<Nestability, std::unique_ptr<Task>>;
std::deque<TaskQueueEntry> task_queue_;
IdleTaskSupport idle_task_support_;
std::queue<std::unique_ptr<IdleTask>> idle_task_queue_;
// Some helper constructs for the {delayed_task_queue_}.
struct DelayedEntry {
double timeout_time;
Nestability nestability;
std::unique_ptr<Task> task;
};
// Define a comparison operator for the delayed_task_queue_ to make sure
// that the unique_ptr in the DelayedEntry is not accessed in the priority
// queue. This is necessary because we have to reset the unique_ptr when we
// remove a DelayedEntry from the priority queue.
struct DelayedEntryCompare {
bool operator()(const DelayedEntry& left, const DelayedEntry& right) const {
return left.timeout_time > right.timeout_time;
}
};
std::priority_queue<DelayedEntry, std::vector<DelayedEntry>,
DelayedEntryCompare>
delayed_task_queue_;
TimeFunction time_function_;
};
} // namespace platform
} // namespace v8
#endif // V8_LIBPLATFORM_DEFAULT_FOREGROUND_TASK_RUNNER_H_