v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
default-platform.h
Go to the documentation of this file.
1// Copyright 2013 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_PLATFORM_H_
6#define V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
7
8#include <map>
9#include <memory>
10
14#include "include/v8-platform.h"
18
19namespace v8 {
20namespace platform {
21
22class Thread;
23class WorkerThread;
24class DefaultForegroundTaskRunner;
25class DefaultWorkerThreadsTaskRunner;
26class DefaultPageAllocator;
27
29 public:
30 explicit DefaultPlatform(
31 int thread_pool_size = 0,
32 IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
33 std::unique_ptr<v8::TracingController> tracing_controller = {},
34 PriorityMode priority_mode = PriorityMode::kDontApply);
35
36 ~DefaultPlatform() override;
37
40
41 void EnsureBackgroundTaskRunnerInitialized();
42
43 bool PumpMessageLoop(
44 v8::Isolate* isolate,
45 MessageLoopBehavior behavior = MessageLoopBehavior::kDoNotWait);
46
47 void RunIdleTasks(v8::Isolate* isolate, double idle_time_in_seconds);
48
49 void SetTracingController(
50 std::unique_ptr<v8::TracingController> tracing_controller);
51
52 using TimeFunction = double (*)();
53
54 void SetTimeFunctionForTesting(TimeFunction time_function);
55
56 // v8::Platform implementation.
57 int NumberOfWorkerThreads() override;
58 std::shared_ptr<TaskRunner> GetForegroundTaskRunner(
59 v8::Isolate* isolate, TaskPriority priority) override;
60 void PostTaskOnWorkerThreadImpl(TaskPriority priority,
61 std::unique_ptr<Task> task,
62 const SourceLocation& location) override;
63 void PostDelayedTaskOnWorkerThreadImpl(
64 TaskPriority priority, std::unique_ptr<Task> task,
65 double delay_in_seconds, const SourceLocation& location) override;
66 bool IdleTasksEnabled(Isolate* isolate) override;
67 std::unique_ptr<JobHandle> CreateJobImpl(
68 TaskPriority priority, std::unique_ptr<JobTask> job_state,
69 const SourceLocation& location) override;
70 double MonotonicallyIncreasingTime() override;
71 double CurrentClockTimeMillis() override;
72 v8::TracingController* GetTracingController() override;
73 StackTracePrinter GetStackTracePrinter() override;
74 v8::PageAllocator* GetPageAllocator() override;
75 v8::ThreadIsolatedAllocator* GetThreadIsolatedAllocator() override;
76
77 void NotifyIsolateShutdown(Isolate* isolate);
78
79 private:
81 if (priority_mode_ == PriorityMode::kDontApply) {
82 return base::Thread::Priority::kDefault;
83 }
84 switch (static_cast<TaskPriority>(i)) {
85 case TaskPriority::kUserBlocking:
86 return base::Thread::Priority::kUserBlocking;
87 case TaskPriority::kUserVisible:
88 return base::Thread::Priority::kUserVisible;
89 case TaskPriority::kBestEffort:
90 return base::Thread::Priority::kBestEffort;
91 }
92 }
93
95 if (priority_mode_ == PriorityMode::kDontApply) {
96 return 0;
97 }
98 return static_cast<int>(priority);
99 }
100
101 int num_worker_runners() const {
102 return priority_to_index(TaskPriority::kMaxPriority) + 1;
103 }
104
108 std::shared_ptr<DefaultWorkerThreadsTaskRunner> worker_threads_task_runners_
109 [static_cast<int>(TaskPriority::kMaxPriority) + 1] = {0};
110 std::map<v8::Isolate*, std::shared_ptr<DefaultForegroundTaskRunner>>
112
113 std::unique_ptr<TracingController> tracing_controller_;
114 std::unique_ptr<PageAllocator> page_allocator_;
116
118 TimeFunction time_function_for_testing_ = nullptr;
119};
120
121} // namespace platform
122} // namespace v8
123
124
125#endif // V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
DefaultThreadIsolatedAllocator thread_isolated_allocator_
DefaultPlatform(const DefaultPlatform &)=delete
std::unique_ptr< PageAllocator > page_allocator_
std::unique_ptr< TracingController > tracing_controller_
base::Thread::Priority priority_from_index(int i) const
DefaultPlatform & operator=(const DefaultPlatform &)=delete
int priority_to_index(TaskPriority priority) const
std::map< v8::Isolate *, std::shared_ptr< DefaultForegroundTaskRunner > > foreground_task_runner_map_
#define V8_PLATFORM_EXPORT
size_t priority
void RunIdleTasks(v8::Platform *platform, v8::Isolate *isolate, double idle_time_in_seconds)
void NotifyIsolateShutdown(v8::Platform *platform, Isolate *isolate)
bool PumpMessageLoop(v8::Platform *platform, v8::Isolate *isolate, MessageLoopBehavior behavior)
TaskPriority
Definition v8-platform.h:24
#define NON_EXPORTED_BASE(code)