-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathNativeScriptPlatform.mm
More file actions
184 lines (142 loc) · 6.74 KB
/
Copy pathNativeScriptPlatform.mm
File metadata and controls
184 lines (142 loc) · 6.74 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
#include "NativeScriptPlatform.h"
using namespace v8;
namespace tns {
NativeScriptPlatform* NativeScriptPlatform::instance_ = nullptr;
namespace {
/**
* The v8::TaskRunner handed to v8 for one isolate. Stateless beyond the
* isolate pointer: every post resolves the current EventLoop through the
* platform registry, so a stale loop replaced by RefreshEventLoop is
* redirected transparently, and posts for a disposed isolate (registry entry
* gone) drop instead of reviving a dead pointer's entry.
*/
class V8TaskRunnerAdapter : public v8::TaskRunner {
public:
explicit V8TaskRunnerAdapter(Isolate* isolate) : isolate_(isolate) {}
bool IdleTasksEnabled() override { return false; }
bool NonNestableTasksEnabled() const override { return true; }
bool NonNestableDelayedTasksEnabled() const override { return true; }
protected:
void PostTaskImpl(std::unique_ptr<Task> task, const SourceLocation& location) override {
Post(std::move(task), true, 0);
}
void PostNonNestableTaskImpl(std::unique_ptr<Task> task,
const SourceLocation& location) override {
Post(std::move(task), false, 0);
}
void PostDelayedTaskImpl(std::unique_ptr<Task> task, double delay_in_seconds,
const SourceLocation& location) override {
Post(std::move(task), true, delay_in_seconds);
}
void PostNonNestableDelayedTaskImpl(std::unique_ptr<Task> task, double delay_in_seconds,
const SourceLocation& location) override {
Post(std::move(task), false, delay_in_seconds);
}
private:
void Post(std::unique_ptr<Task> task, bool nestable, double delaySeconds) {
auto loop = NativeScriptPlatform::Instance()->LookupEventLoop(isolate_);
if (loop != nullptr) {
loop->PostV8Task(std::move(task), nestable, delaySeconds);
}
}
Isolate* isolate_;
};
} // namespace
NativeScriptPlatform::NativeScriptPlatform(std::unique_ptr<Platform> defaultPlatform)
: default_(std::move(defaultPlatform)) {
instance_ = this;
}
NativeScriptPlatform::IsolateEntry& NativeScriptPlatform::GetEntryLocked(Isolate* isolate) {
auto it = loops_.find(isolate);
if (it != loops_.end()) {
return it->second;
}
auto emplaced =
loops_.emplace(isolate, IsolateEntry{std::make_shared<EventLoop>(isolate),
std::make_shared<V8TaskRunnerAdapter>(isolate)});
return emplaced.first->second;
}
std::shared_ptr<EventLoop> NativeScriptPlatform::GetEventLoop(Isolate* isolate) {
std::lock_guard<std::mutex> lock(loopsMutex_);
return GetEntryLocked(isolate).loop;
}
std::shared_ptr<EventLoop> NativeScriptPlatform::RefreshEventLoop(Isolate* isolate) {
std::lock_guard<std::mutex> lock(loopsMutex_);
auto& entry = GetEntryLocked(isolate);
if (entry.loop->IsStopped()) {
entry.loop = std::make_shared<EventLoop>(isolate);
}
return entry.loop;
}
std::shared_ptr<EventLoop> NativeScriptPlatform::LookupEventLoop(Isolate* isolate) {
std::lock_guard<std::mutex> lock(loopsMutex_);
auto it = loops_.find(isolate);
return it != loops_.end() ? it->second.loop : nullptr;
}
void NativeScriptPlatform::IsolateDisposed(Isolate* isolate,
const std::shared_ptr<EventLoop>& loop) {
std::lock_guard<std::mutex> lock(loopsMutex_);
auto it = loops_.find(isolate);
if (it != loops_.end() && it->second.loop == loop) {
loops_.erase(it);
}
}
PageAllocator* NativeScriptPlatform::GetPageAllocator() { return default_->GetPageAllocator(); }
ThreadIsolatedAllocator* NativeScriptPlatform::GetThreadIsolatedAllocator() {
return default_->GetThreadIsolatedAllocator();
}
size_t NativeScriptPlatform::GetZeroSegmentSize() { return default_->GetZeroSegmentSize(); }
void NativeScriptPlatform::OnCriticalMemoryPressure() { default_->OnCriticalMemoryPressure(); }
int NativeScriptPlatform::NumberOfWorkerThreads() { return default_->NumberOfWorkerThreads(); }
std::shared_ptr<TaskRunner> NativeScriptPlatform::GetForegroundTaskRunner(Isolate* isolate,
TaskPriority priority) {
// one runner regardless of priority: the home runloop's FIFO order is the
// priority model of the runtime thread
std::lock_guard<std::mutex> lock(loopsMutex_);
return GetEntryLocked(isolate).runner;
}
bool NativeScriptPlatform::IdleTasksEnabled(Isolate* isolate) { return false; }
std::unique_ptr<ScopedBoostablePriority> NativeScriptPlatform::CreateBoostablePriorityScope() {
return default_->CreateBoostablePriorityScope();
}
std::unique_ptr<ScopedBlockingCall> NativeScriptPlatform::CreateBlockingScope(
BlockingType blocking_type) {
return default_->CreateBlockingScope(blocking_type);
}
double NativeScriptPlatform::MonotonicallyIncreasingTime() {
return default_->MonotonicallyIncreasingTime();
}
int64_t NativeScriptPlatform::CurrentClockTimeMilliseconds() {
return default_->CurrentClockTimeMilliseconds();
}
double NativeScriptPlatform::CurrentClockTimeMillis() { return default_->CurrentClockTimeMillis(); }
double NativeScriptPlatform::CurrentClockTimeMillisecondsHighResolution() {
return default_->CurrentClockTimeMillisecondsHighResolution();
}
Platform::StackTracePrinter NativeScriptPlatform::GetStackTracePrinter() {
return default_->GetStackTracePrinter();
}
TracingController* NativeScriptPlatform::GetTracingController() {
return default_->GetTracingController();
}
void NativeScriptPlatform::DumpWithoutCrashing() { default_->DumpWithoutCrashing(); }
HighAllocationThroughputObserver* NativeScriptPlatform::GetHighAllocationThroughputObserver() {
return default_->GetHighAllocationThroughputObserver();
}
std::unique_ptr<JobHandle> NativeScriptPlatform::CreateJobImpl(TaskPriority priority,
std::unique_ptr<JobTask> job_task,
const SourceLocation& location) {
return default_->CreateJob(priority, std::move(job_task), location);
}
void NativeScriptPlatform::PostTaskOnWorkerThreadImpl(TaskPriority priority,
std::unique_ptr<Task> task,
const SourceLocation& location) {
default_->PostTaskOnWorkerThread(priority, std::move(task), location);
}
void NativeScriptPlatform::PostDelayedTaskOnWorkerThreadImpl(TaskPriority priority,
std::unique_ptr<Task> task,
double delay_in_seconds,
const SourceLocation& location) {
default_->PostDelayedTaskOnWorkerThread(priority, std::move(task), delay_in_seconds, location);
}
} // namespace tns