v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
default-foreground-task-runner.h
Go to the documentation of this file.
1// Copyright 2017 the V8 project 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#ifndef V8_LIBPLATFORM_DEFAULT_FOREGROUND_TASK_RUNNER_H_
6#define V8_LIBPLATFORM_DEFAULT_FOREGROUND_TASK_RUNNER_H_
7
8#include <memory>
9#include <queue>
10
12#include "include/v8-platform.h"
15
16namespace v8 {
17namespace platform {
18
20 : public NON_EXPORTED_BASE(TaskRunner) {
21 public:
22 using TimeFunction = double (*)();
24 public:
25 explicit RunTaskScope(
26 std::shared_ptr<DefaultForegroundTaskRunner> task_runner);
28 RunTaskScope(const RunTaskScope&) = delete;
30
31 private:
32 std::shared_ptr<DefaultForegroundTaskRunner> task_runner_;
33 };
34
36 TimeFunction time_function);
37
38 void Terminate();
39
40 std::unique_ptr<Task> PopTaskFromQueue(MessageLoopBehavior wait_for_work);
41
42 std::unique_ptr<IdleTask> PopTaskFromIdleQueue();
43
44 double MonotonicallyIncreasingTime();
45
46 // v8::TaskRunner implementation.
47 bool IdleTasksEnabled() override;
48 bool NonNestableTasksEnabled() const override;
49 bool NonNestableDelayedTasksEnabled() const override;
50
51 private:
52 // v8::TaskRunner implementation.
53 void PostTaskImpl(std::unique_ptr<Task> task,
54 const SourceLocation& location) override;
55 void PostDelayedTaskImpl(std::unique_ptr<Task> task, double delay_in_seconds,
56 const SourceLocation& location) override;
57 void PostIdleTaskImpl(std::unique_ptr<IdleTask> task,
58 const SourceLocation& location) override;
59 void PostNonNestableTaskImpl(std::unique_ptr<Task> task,
60 const SourceLocation& location) override;
61 void PostNonNestableDelayedTaskImpl(std::unique_ptr<Task> task,
62 double delay_in_seconds,
63 const SourceLocation& location) override;
64
65 enum Nestability { kNestable, kNonNestable };
66
67 void WaitForTaskLocked();
68
69 // The same as PostTask or PostNonNestableTask, but the lock is already held
70 // by the caller. If the task runner is already terminated, the task is
71 // returned (such that it can be deleted later, after releasing the lock).
72 // Otherwise, nullptr is returned.
73 std::unique_ptr<Task> PostTaskLocked(std::unique_ptr<Task> task,
74 Nestability nestability);
75
76 // The same as PostDelayedTask or PostNonNestableDelayedTask, but the lock is
77 // already held by the caller.
78 void PostDelayedTaskLocked(std::unique_ptr<Task> task,
79 double delay_in_seconds, Nestability nestability);
80
81 // A caller of this function has to hold {mutex_}.
82 std::unique_ptr<Task> PopTaskFromDelayedQueueLocked(Nestability* nestability);
83
84 // A non-nestable task is poppable only if the task runner is not nested,
85 // i.e. if a task is not being run from within a task. A nestable task is
86 // always poppable.
87 bool HasPoppableTaskInQueue() const;
88
89 // Move delayed tasks that hit their deadline to the main queue. Returns all
90 // tasks that expired but were not scheduled because the task runner was
91 // terminated.
92 std::vector<std::unique_ptr<Task>> MoveExpiredDelayedTasksLocked();
93
94 bool terminated_ = false;
97 int nesting_depth_ = 0;
98
99 using TaskQueueEntry = std::pair<Nestability, std::unique_ptr<Task>>;
100 std::deque<TaskQueueEntry> task_queue_;
101
103 std::queue<std::unique_ptr<IdleTask>> idle_task_queue_;
104
105 // Some helper constructs for the {delayed_task_queue_}.
109 std::unique_ptr<Task> task;
110 };
111
112 // Define a comparison operator for the delayed_task_queue_ to make sure
113 // that the unique_ptr in the DelayedEntry is not accessed in the priority
114 // queue. This is necessary because we have to reset the unique_ptr when we
115 // remove a DelayedEntry from the priority queue.
117 bool operator()(const DelayedEntry& left, const DelayedEntry& right) const {
118 return left.timeout_time > right.timeout_time;
119 }
120 };
121 std::priority_queue<DelayedEntry, std::vector<DelayedEntry>,
122 DelayedEntryCompare>
124
126};
127
128} // namespace platform
129} // namespace v8
130#endif // V8_LIBPLATFORM_DEFAULT_FOREGROUND_TASK_RUNNER_H_
RunTaskScope & operator=(const RunTaskScope &)=delete
std::shared_ptr< DefaultForegroundTaskRunner > task_runner_
std::queue< std::unique_ptr< IdleTask > > idle_task_queue_
std::pair< Nestability, std::unique_ptr< Task > > TaskQueueEntry
std::priority_queue< DelayedEntry, std::vector< DelayedEntry >, DelayedEntryCompare > delayed_task_queue_
#define V8_PLATFORM_EXPORT
#define NON_EXPORTED_BASE(code)
bool operator()(const DelayedEntry &left, const DelayedEntry &right) const
#define V8_NODISCARD
Definition v8config.h:693