-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathEventLoop.mm
More file actions
509 lines (465 loc) · 15.7 KB
/
Copy pathEventLoop.mm
File metadata and controls
509 lines (465 loc) · 15.7 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include "EventLoop.h"
#include <algorithm>
#include <ctime>
#include <limits>
#include "Caches.h"
#include "Helpers.h"
#include "NativeScriptException.h"
using namespace v8;
namespace {
// far enough that an "unarmed" repeating timer never fires; re-armed with
// CFRunLoopTimerSetNextFireDate when a real deadline exists
const CFTimeInterval kNeverFireInterval = 1.0e10;
// runs one unit of non-bare work without letting a C++ exception escape into
// a CFRunLoop callback frame. Deliberately no catch(...): on Darwin it would
// also swallow NSExceptions, and bare entries (which may @throw on purpose)
// never come through here anyway.
template <typename F>
void RunGuarded(F&& body) {
try {
body();
} catch (tns::NativeScriptException& ex) {
Log(@"NativeScript: uncaught NativeScriptException in event loop task: %s",
ex.getMessage().c_str());
} catch (std::exception& ex) {
Log(@"NativeScript: c++ exception in event loop task: %s", ex.what());
}
}
} // namespace
namespace tns {
double EventLoop::NowMs() {
struct timespec res;
clock_gettime(CLOCK_MONOTONIC, &res);
return 1000.0 * res.tv_sec + (double)res.tv_nsec / 1e6;
}
// converts a CLOCK_MONOTONIC due time to a CFRunLoopTimer fire date. The two
// clocks can drift (CFAbsoluteTime is wall-based), so consumers must treat a
// fire as "check what is due now", never as proof a specific item is due.
static CFAbsoluteTime FireDateFor(double dueMs, double nowMs) {
return CFAbsoluteTimeGetCurrent() + std::max(0.0, dueMs - nowMs) / 1000.0;
}
void EventLoop::BindToCurrentThread() {
std::lock_guard<std::mutex> lock(mutex_);
if (loop_ != nullptr || stopped_) {
return;
}
loop_ = CFRunLoopGetCurrent();
CFRunLoopSourceContext sourceContext = {
0, this, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, &EventLoop::InternalSourcePerform};
internalSource_ = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &sourceContext);
CFRunLoopAddSource(loop_, internalSource_, kCFRunLoopCommonModes);
CFRunLoopTimerContext timerContext = {0, this, nullptr, nullptr, nullptr};
internalTimer_ =
CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + kNeverFireInterval,
kNeverFireInterval, 0, 0, &EventLoop::InternalTimerFired, &timerContext);
CFRunLoopAddTimer(loop_, internalTimer_, kCFRunLoopCommonModes);
orderedTimer_ =
CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + kNeverFireInterval,
kNeverFireInterval, 0, 0, &EventLoop::OrderedTimerFired, &timerContext);
CFRunLoopAddTimer(loop_, orderedTimer_, kCFRunLoopCommonModes);
// flush work buffered before the home thread was known
auto now = NowMs();
if (HasDueLocked(internal_, now)) {
SignalInternalLocked();
}
ArmInternalTimerLocked(now);
// replay buffered tokens under their original keys - PostOrderedToken
// already returned those to producers as the recall handle, so the flush
// must not re-clamp them (a past key simply fires on the next pass)
auto tokens = std::move(bufferedTokens_);
bufferedTokens_.clear();
for (double due : tokens) {
pendingTokens_.insert(due);
}
ArmOrderedTimerLocked(now);
}
void EventLoop::Shutdown() {
// The dropped entries are moved out and destroyed only after the lock is
// released: an entry's destructor may post back into this very loop (a
// dropped message carrying a transferred port sentinels the port's sibling,
// and that sibling may live here), and mutex_ is not recursive.
std::deque<Entry> droppedInternal;
std::multimap<double, Entry> droppedInternalDelayed;
std::deque<Entry> droppedOrdered;
std::multimap<double, Entry> droppedOrderedDelayed;
std::lock_guard<std::mutex> lock(mutex_);
if (stopped_) {
return;
}
stopped_ = true;
droppedInternal.swap(internal_.immediate);
droppedInternalDelayed.swap(internal_.delayed);
droppedOrdered.swap(ordered_.immediate);
droppedOrderedDelayed.swap(ordered_.delayed);
pendingTokens_.clear();
bufferedTokens_.clear();
if (internalSource_ != nullptr) {
CFRunLoopSourceInvalidate(internalSource_);
CFRelease(internalSource_);
internalSource_ = nullptr;
}
if (internalTimer_ != nullptr) {
CFRunLoopTimerInvalidate(internalTimer_);
CFRelease(internalTimer_);
internalTimer_ = nullptr;
}
if (orderedTimer_ != nullptr) {
CFRunLoopTimerInvalidate(orderedTimer_);
CFRelease(orderedTimer_);
orderedTimer_ = nullptr;
}
loop_ = nullptr;
}
EventLoop::~EventLoop() {
// Normally a no-op: ~Runtime already shut the loop down on the home thread.
// A transient shared_ptr taken on a foreign posting thread can be the last
// reference only after that shutdown, when this is just member cleanup.
Shutdown();
}
void EventLoop::PostInternalLocked(Entry entry, double delayMs) {
auto now = NowMs();
if (delayMs <= 0) {
entry.time = now;
internal_.immediate.push_back(std::move(entry));
SignalInternalLocked();
} else {
auto due = now + delayMs;
entry.time = due;
internal_.delayed.emplace(due, std::move(entry));
ArmInternalTimerLocked(now);
}
}
void EventLoop::PostOrderedLocked(Entry entry, double delayMs) {
auto now = NowMs();
if (delayMs <= 0) {
entry.time = now;
ordered_.immediate.push_back(std::move(entry));
PostOrderedTokenLocked(now, now);
} else {
auto due = now + delayMs;
entry.time = due;
ordered_.delayed.emplace(due, std::move(entry));
PostOrderedTokenLocked(due, now);
}
}
double EventLoop::PostOrderedTokenLocked(double dueTimeMs, double now) {
if (loop_ == nullptr) {
bufferedTokens_.push_back(dueTimeMs);
return dueTimeMs;
}
// every token rides the timer phase, even due-now ones (a past fire date
// fires on the next runloop pass). Performed blocks would deliver sooner,
// but a self-rescheduling chain of them never lets the loop reach its
// before-waiting phase - starving CA rendering commits, the autorelease
// pool and the rejection drain - while a due timer still yields a full
// pass between fires. This also keeps due-now tokens in fire-date order
// with foreign NSTimers, like the per-timer CFRunLoopTimers this replaces.
double key = std::max(dueTimeMs, now);
pendingTokens_.insert(key);
ArmOrderedTimerLocked(now);
CFRunLoopWakeUp(loop_);
return key;
}
bool EventLoop::PostInternal(std::function<void()> fn) {
std::lock_guard<std::mutex> lock(mutex_);
if (stopped_) {
return false;
}
PostInternalLocked(Entry{nullptr, std::move(fn), true, false, 0}, 0);
return true;
}
bool EventLoop::PostInternalDelayed(std::function<void()> fn, double delayMs) {
std::lock_guard<std::mutex> lock(mutex_);
if (stopped_) {
return false;
}
PostInternalLocked(Entry{nullptr, std::move(fn), true, false, 0}, delayMs);
return true;
}
bool EventLoop::PostInternalBare(std::function<void()> fn) {
std::lock_guard<std::mutex> lock(mutex_);
if (stopped_) {
return false;
}
PostInternalLocked(Entry{nullptr, std::move(fn), true, true, 0}, 0);
return true;
}
bool EventLoop::PostOrdered(std::function<void()> fn) {
std::lock_guard<std::mutex> lock(mutex_);
if (stopped_) {
return false;
}
PostOrderedLocked(Entry{nullptr, std::move(fn), true, false, 0}, 0);
return true;
}
bool EventLoop::PostOrderedDelayed(std::function<void()> fn, double delayMs) {
std::lock_guard<std::mutex> lock(mutex_);
if (stopped_) {
return false;
}
PostOrderedLocked(Entry{nullptr, std::move(fn), true, false, 0}, delayMs);
return true;
}
double EventLoop::PostOrderedToken(double dueTimeMs) {
std::lock_guard<std::mutex> lock(mutex_);
if (stopped_) {
return dueTimeMs;
}
return PostOrderedTokenLocked(dueTimeMs, NowMs());
}
bool EventLoop::TryCancelOrderedToken(double dueTimeMs) {
std::lock_guard<std::mutex> lock(mutex_);
if (stopped_) {
return false;
}
auto bufferedIt = std::find(bufferedTokens_.begin(), bufferedTokens_.end(), dueTimeMs);
if (bufferedIt != bufferedTokens_.end()) {
bufferedTokens_.erase(bufferedIt);
return true;
}
auto pendingIt = pendingTokens_.find(dueTimeMs);
if (pendingIt == pendingTokens_.end()) {
return false;
}
pendingTokens_.erase(pendingIt);
ArmOrderedTimerLocked(NowMs());
return true;
}
void EventLoop::SetTimerSource(OrderedTaskSource* source) {
// home thread only, like every consumer of timerSource_
timerSource_ = source;
}
void EventLoop::PostV8Task(std::unique_ptr<Task> task, bool nestable, double delaySeconds) {
std::lock_guard<std::mutex> lock(mutex_);
if (stopped_) {
return;
}
PostInternalLocked(Entry{std::move(task), nullptr, nestable, false, 0}, delaySeconds * 1000.0);
}
bool EventLoop::IsStopped() {
std::lock_guard<std::mutex> lock(mutex_);
return stopped_;
}
std::unique_ptr<EventLoop::Entry> EventLoop::TakeDueLocked(Lane& lane, bool nestableOnly,
bool v8Only, double now) {
auto matches = [&](const Entry& e) {
return (!nestableOnly || e.nestable) && (!v8Only || e.task != nullptr);
};
auto imIt = lane.immediate.begin();
while (imIt != lane.immediate.end() && !matches(*imIt)) {
++imIt;
}
auto delIt = lane.delayed.begin();
while (delIt != lane.delayed.end() && !matches(delIt->second)) {
++delIt;
}
bool hasImmediate = imIt != lane.immediate.end();
bool hasDelayed = delIt != lane.delayed.end() && delIt->first <= now;
if (hasImmediate && (!hasDelayed || imIt->time <= delIt->first)) {
auto entry = std::make_unique<Entry>(std::move(*imIt));
lane.immediate.erase(imIt);
return entry;
}
if (hasDelayed) {
auto entry = std::make_unique<Entry>(std::move(delIt->second));
lane.delayed.erase(delIt);
return entry;
}
return nullptr;
}
double EventLoop::PeekDueLocked(Lane& lane, double now) {
// immediate entries are enqueued with monotonically increasing times, so
// the front is the earliest
double due = lane.immediate.empty() ? -1 : lane.immediate.front().time;
if (!lane.delayed.empty() && lane.delayed.begin()->first <= now &&
(due < 0 || lane.delayed.begin()->first < due)) {
due = lane.delayed.begin()->first;
}
return due;
}
bool EventLoop::HasDueLocked(Lane& lane, double now) {
return !lane.immediate.empty() || (!lane.delayed.empty() && lane.delayed.begin()->first <= now);
}
void EventLoop::SignalInternalLocked() {
if (internalSource_ != nullptr) {
CFRunLoopSourceSignal(internalSource_);
CFRunLoopWakeUp(loop_);
}
}
void EventLoop::ArmInternalTimerLocked(double now) {
if (internalTimer_ == nullptr) {
return;
}
// earliest not-yet-due delayed entry; already-due ones are the signal's job
double due = -1;
for (auto& pair : internal_.delayed) {
if (pair.first > now) {
due = pair.first;
break;
}
}
CFRunLoopTimerSetNextFireDate(
internalTimer_,
due >= 0 ? FireDateFor(due, now) : CFAbsoluteTimeGetCurrent() + kNeverFireInterval);
}
void EventLoop::ArmOrderedTimerLocked(double now) {
if (orderedTimer_ == nullptr) {
return;
}
CFRunLoopTimerSetNextFireDate(
orderedTimer_, !pendingTokens_.empty() ? FireDateFor(*pendingTokens_.begin(), now)
: CFAbsoluteTimeGetCurrent() + kNeverFireInterval);
}
void EventLoop::RunEntry(Entry& entry) {
if (entry.bare) {
// the fn does its own ceremony - it may lock a different isolate, or
// deliberately @throw with no V8 scopes on the stack
entry.fn();
return;
}
v8::Locker locker(isolate_);
v8::Isolate::Scope isolate_scope(isolate_);
v8::HandleScope handle_scope(isolate_);
auto run = [&]() {
if (entry.task != nullptr) {
entry.task->Run();
} else {
entry.fn();
}
// work may enqueue microtasks without entering JS (e.g. resolving the
// Atomics.waitAsync promise), which never reaches kAuto's depth-0 drain
isolate_->PerformMicrotaskCheckpoint();
};
auto cache = Caches::Get(isolate_);
if (cache != nullptr && cache->IsValid() && cache->HasContext()) {
Context::Scope context_scope(cache->GetContext());
run();
} else {
// v8 can post tasks before Runtime::Init creates the context
run();
}
}
void EventLoop::RunOneInternal() {
std::unique_ptr<Entry> entry;
{
std::lock_guard<std::mutex> lock(mutex_);
if (stopped_) {
return;
}
auto now = NowMs();
entry = TakeDueLocked(internal_, false, false, now);
// re-signal BEFORE running: one entry per runloop pass keeps the lane
// fair with other runloop work, and a bare entry may @throw and never
// return control here
if (entry != nullptr && HasDueLocked(internal_, now)) {
SignalInternalLocked();
}
}
if (entry == nullptr) {
// leftover signal: the work it announced ran early from a nested drain
return;
}
if (entry->bare) {
RunEntry(*entry);
return;
}
RunGuarded([&] { RunEntry(*entry); });
}
void EventLoop::RunNestableV8Tasks() {
// bounded to the entries present at call time so a task that reposts can't
// wedge the inspector pause loop that called us
size_t budget;
{
std::lock_guard<std::mutex> lock(mutex_);
budget = internal_.immediate.size() + internal_.delayed.size();
}
while (budget-- > 0) {
std::unique_ptr<Entry> entry;
{
std::lock_guard<std::mutex> lock(mutex_);
if (stopped_) {
return;
}
entry = TakeDueLocked(internal_, true, true, NowMs());
}
if (entry == nullptr) {
return;
}
// the pause loops call this from inside v8 inspector frames - a C++
// exception must not unwind through them
RunGuarded([&] { RunEntry(*entry); });
}
}
void EventLoop::RunOrderedTask() {
// one anonymous token = one due slot across the whole ordered domain: pick
// the earliest due item among the ordered entries and the timer source,
// whichever it is. Timers and entries only ever run on this thread, so the
// peeked winner can't be taken by anyone else before we re-lock (a
// concurrent post can only add later work).
auto now = NowMs();
double entryDue;
{
std::lock_guard<std::mutex> lock(mutex_);
if (stopped_) {
return;
}
entryDue = PeekDueLocked(ordered_, now);
}
if (timerSource_ != nullptr && timerSource_->RunIfEarliest(now, entryDue)) {
return;
}
if (entryDue < 0) {
// leftover token: nothing in the domain is due yet
return;
}
std::unique_ptr<Entry> entry;
{
std::lock_guard<std::mutex> lock(mutex_);
if (stopped_) {
return;
}
entry = TakeDueLocked(ordered_, false, false, NowMs());
}
if (entry != nullptr) {
RunGuarded([&] { RunEntry(*entry); });
}
}
void EventLoop::InternalSourcePerform(void* info) {
static_cast<EventLoop*>(info)->RunOneInternal();
}
void EventLoop::InternalTimerFired(CFRunLoopTimerRef timer, void* info) {
auto self = static_cast<EventLoop*>(info);
std::lock_guard<std::mutex> lock(self->mutex_);
if (self->stopped_) {
return;
}
auto now = NowMs();
if (HasDueLocked(self->internal_, now)) {
self->SignalInternalLocked();
}
self->ArmInternalTimerLocked(now);
}
void EventLoop::OrderedTimerFired(CFRunLoopTimerRef timer, void* info) {
auto self = static_cast<EventLoop*>(info);
bool due = false;
{
std::lock_guard<std::mutex> lock(self->mutex_);
if (self->stopped_) {
return;
}
auto now = NowMs();
if (!self->pendingTokens_.empty() && *self->pendingTokens_.begin() <= now) {
self->pendingTokens_.erase(self->pendingTokens_.begin());
due = true;
}
// one matured token per fire: re-arming with an already-past due time
// fires again on the next runloop pass, so foreign timers and blocks due
// between two matured tokens interleave instead of waiting out a batch
self->ArmOrderedTimerLocked(now);
}
if (due) {
self->RunOrderedTask();
}
}
} // namespace tns