@@ -230,6 +230,11 @@ PerIsolatePlatformData::PerIsolatePlatformData(
230
230
uv_unref (reinterpret_cast <uv_handle_t *>(flush_tasks_));
231
231
}
232
232
233
+ std::shared_ptr<v8::TaskRunner>
234
+ PerIsolatePlatformData::GetForegroundTaskRunner () {
235
+ return shared_from_this ();
236
+ }
237
+
233
238
void PerIsolatePlatformData::FlushTasks (uv_async_t * handle) {
234
239
auto platform_data = static_cast <PerIsolatePlatformData*>(handle->data );
235
240
platform_data->FlushForegroundTasksInternal ();
@@ -267,7 +272,7 @@ void PerIsolatePlatformData::PostNonNestableDelayedTask(
267
272
}
268
273
269
274
PerIsolatePlatformData::~PerIsolatePlatformData () {
270
- Shutdown ( );
275
+ CHECK (!flush_tasks_ );
271
276
}
272
277
273
278
void PerIsolatePlatformData::AddShutdownCallback (void (*callback)(void *),
@@ -325,30 +330,44 @@ NodePlatform::NodePlatform(int thread_pool_size,
325
330
326
331
void NodePlatform::RegisterIsolate (Isolate* isolate, uv_loop_t * loop) {
327
332
Mutex::ScopedLock lock (per_isolate_mutex_);
328
- std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
329
- CHECK (!existing);
330
- per_isolate_[isolate] =
331
- std::make_shared<PerIsolatePlatformData>(isolate, loop);
333
+ auto delegate = std::make_shared<PerIsolatePlatformData>(isolate, loop);
334
+ IsolatePlatformDelegate* ptr = delegate.get ();
335
+ auto insertion = per_isolate_.emplace (
336
+ isolate,
337
+ std::make_pair (ptr, std::move (delegate)));
338
+ CHECK (insertion.second );
339
+ }
340
+
341
+ void NodePlatform::RegisterIsolate (Isolate* isolate,
342
+ IsolatePlatformDelegate* delegate) {
343
+ Mutex::ScopedLock lock (per_isolate_mutex_);
344
+ auto insertion = per_isolate_.emplace (
345
+ isolate,
346
+ std::make_pair (delegate, std::shared_ptr<PerIsolatePlatformData>{}));
347
+ CHECK (insertion.second );
332
348
}
333
349
334
350
void NodePlatform::UnregisterIsolate (Isolate* isolate) {
335
351
Mutex::ScopedLock lock (per_isolate_mutex_);
336
- std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
337
- CHECK (existing);
338
- existing->Shutdown ();
339
- per_isolate_.erase (isolate);
352
+ auto existing_it = per_isolate_.find (isolate);
353
+ CHECK_NE (existing_it, per_isolate_.end ());
354
+ auto & existing = existing_it->second ;
355
+ if (existing.second ) {
356
+ existing.second ->Shutdown ();
357
+ }
358
+ per_isolate_.erase (existing_it);
340
359
}
341
360
342
361
void NodePlatform::AddIsolateFinishedCallback (Isolate* isolate,
343
362
void (*cb)(void *), void* data) {
344
363
Mutex::ScopedLock lock (per_isolate_mutex_);
345
364
auto it = per_isolate_.find (isolate);
346
365
if (it == per_isolate_.end ()) {
347
- CHECK (it->second );
348
366
cb (data);
349
367
return ;
350
368
}
351
- it->second ->AddShutdownCallback (cb, data);
369
+ CHECK (it->second .second );
370
+ it->second .second ->AddShutdownCallback (cb, data);
352
371
}
353
372
354
373
void NodePlatform::Shutdown () {
@@ -394,7 +413,7 @@ void PerIsolatePlatformData::RunForegroundTask(uv_timer_t* handle) {
394
413
}
395
414
396
415
void NodePlatform::DrainTasks (Isolate* isolate) {
397
- std::shared_ptr<PerIsolatePlatformData> per_isolate = ForIsolate (isolate);
416
+ std::shared_ptr<PerIsolatePlatformData> per_isolate = ForNodeIsolate (isolate);
398
417
399
418
do {
400
419
// Worker tasks aren't associated with an Isolate.
@@ -452,23 +471,32 @@ void NodePlatform::CallDelayedOnWorkerThread(std::unique_ptr<Task> task,
452
471
}
453
472
454
473
474
+ IsolatePlatformDelegate* NodePlatform::ForIsolate (Isolate* isolate) {
475
+ Mutex::ScopedLock lock (per_isolate_mutex_);
476
+ auto data = per_isolate_[isolate];
477
+ CHECK_NOT_NULL (data.first );
478
+ return data.first ;
479
+ }
480
+
455
481
std::shared_ptr<PerIsolatePlatformData>
456
- NodePlatform::ForIsolate (Isolate* isolate) {
482
+ NodePlatform::ForNodeIsolate (Isolate* isolate) {
457
483
Mutex::ScopedLock lock (per_isolate_mutex_);
458
- std::shared_ptr<PerIsolatePlatformData> data = per_isolate_[isolate];
459
- CHECK (data);
460
- return data;
484
+ auto data = per_isolate_[isolate];
485
+ CHECK (data. second );
486
+ return data. second ;
461
487
}
462
488
463
489
bool NodePlatform::FlushForegroundTasks (Isolate* isolate) {
464
- return ForIsolate (isolate)->FlushForegroundTasksInternal ();
490
+ return ForNodeIsolate (isolate)->FlushForegroundTasksInternal ();
465
491
}
466
492
467
- bool NodePlatform::IdleTasksEnabled (Isolate* isolate) { return false ; }
493
+ bool NodePlatform::IdleTasksEnabled (Isolate* isolate) {
494
+ return ForIsolate (isolate)->IdleTasksEnabled ();
495
+ }
468
496
469
497
std::shared_ptr<v8::TaskRunner>
470
498
NodePlatform::GetForegroundTaskRunner (Isolate* isolate) {
471
- return ForIsolate (isolate);
499
+ return ForIsolate (isolate)-> GetForegroundTaskRunner () ;
472
500
}
473
501
474
502
double NodePlatform::MonotonicallyIncreasingTime () {
0 commit comments