@@ -232,12 +232,6 @@ void TrackingTraceStateObserver::UpdateTraceCategoryState() {
232
232
.ToLocalChecked ();
233
233
}
234
234
235
- static std::atomic<uint64_t > next_thread_id{0 };
236
-
237
- uint64_t Environment::AllocateThreadId () {
238
- return next_thread_id++;
239
- }
240
-
241
235
void Environment::CreateProperties () {
242
236
HandleScope handle_scope (isolate_);
243
237
Local<Context> ctx = context ();
@@ -294,8 +288,8 @@ Environment::Environment(IsolateData* isolate_data,
294
288
Local<Context> context,
295
289
const std::vector<std::string>& args,
296
290
const std::vector<std::string>& exec_args,
297
- Flags flags,
298
- uint64_t thread_id)
291
+ EnvironmentFlags:: Flags flags,
292
+ ThreadId thread_id)
299
293
: isolate_(context->GetIsolate ()),
300
294
isolate_data_(isolate_data),
301
295
immediate_info_(context->GetIsolate ()),
@@ -307,14 +301,23 @@ Environment::Environment(IsolateData* isolate_data,
307
301
should_abort_on_uncaught_toggle_(isolate_, 1 ),
308
302
stream_base_state_(isolate_, StreamBase::kNumStreamBaseStateFields ),
309
303
flags_(flags),
310
- thread_id_(thread_id == kNoThreadId ? AllocateThreadId() : thread_id),
304
+ thread_id_(thread_id.id == static_cast <uint64_t >(-1 ) ?
305
+ AllocateEnvironmentThreadId().id : thread_id.id),
311
306
fs_stats_field_array_(isolate_, kFsStatsBufferLength ),
312
307
fs_stats_field_bigint_array_(isolate_, kFsStatsBufferLength ),
313
308
context_(context->GetIsolate (), context) {
314
309
// We'll be creating new objects so make sure we've entered the context.
315
310
HandleScope handle_scope (isolate ());
316
311
Context::Scope context_scope (context);
317
312
313
+ // Set some flags if only kDefaultFlags was passed. This can make API version
314
+ // transitions easier for embedders.
315
+ if (flags_ & EnvironmentFlags::kDefaultFlags ) {
316
+ flags_ = flags_ |
317
+ EnvironmentFlags::kOwnsProcessState |
318
+ EnvironmentFlags::kOwnsInspector ;
319
+ }
320
+
318
321
set_env_vars (per_process::system_environment);
319
322
enabled_debug_list_.Parse (this );
320
323
@@ -333,6 +336,10 @@ Environment::Environment(IsolateData* isolate_data,
333
336
334
337
AssignToContext (context, ContextInfo (" " ));
335
338
339
+ static uv_once_t init_once = UV_ONCE_INIT;
340
+ uv_once (&init_once, InitThreadLocalOnce);
341
+ uv_key_set (&thread_local_env, this );
342
+
336
343
if (tracing::AgentWriterHandle* writer = GetTracingAgentWriter ()) {
337
344
trace_state_observer_ = std::make_unique<TrackingTraceStateObserver>(this );
338
345
if (TracingController* tracing_controller = writer->GetTracingController ())
@@ -389,6 +396,9 @@ Environment::Environment(IsolateData* isolate_data,
389
396
Environment::~Environment () {
390
397
if (interrupt_data_ != nullptr ) *interrupt_data_ = nullptr ;
391
398
399
+ // FreeEnvironment() should have set this.
400
+ CHECK (is_stopping ());
401
+
392
402
isolate ()->GetHeapProfiler ()->RemoveBuildEmbedderGraphCallback (
393
403
BuildEmbedderGraph, this );
394
404
@@ -472,6 +482,15 @@ void Environment::InitializeLibuv(bool start_profiler_idle_notifier) {
472
482
uv_unref (reinterpret_cast <uv_handle_t *>(&idle_check_handle_));
473
483
uv_unref (reinterpret_cast <uv_handle_t *>(&task_queues_async_));
474
484
485
+ {
486
+ Mutex::ScopedLock lock (native_immediates_threadsafe_mutex_);
487
+ task_queues_async_initialized_ = true ;
488
+ if (native_immediates_threadsafe_.size () > 0 ||
489
+ native_immediates_interrupts_.size () > 0 ) {
490
+ uv_async_send (&task_queues_async_);
491
+ }
492
+ }
493
+
475
494
// Register clean-up cb to be called to clean up the handles
476
495
// when the environment is freed, note that they are not cleaned in
477
496
// the one environment per process setup, but will be called in
@@ -481,10 +500,6 @@ void Environment::InitializeLibuv(bool start_profiler_idle_notifier) {
481
500
if (start_profiler_idle_notifier) {
482
501
StartProfilerIdleNotifier ();
483
502
}
484
-
485
- static uv_once_t init_once = UV_ONCE_INIT;
486
- uv_once (&init_once, InitThreadLocalOnce);
487
- uv_key_set (&thread_local_env, this );
488
503
}
489
504
490
505
void Environment::ExitEnv () {
@@ -533,6 +548,11 @@ void Environment::RegisterHandleCleanups() {
533
548
}
534
549
535
550
void Environment::CleanupHandles () {
551
+ {
552
+ Mutex::ScopedLock lock (native_immediates_threadsafe_mutex_);
553
+ task_queues_async_initialized_ = false ;
554
+ }
555
+
536
556
Isolate::DisallowJavascriptExecutionScope disallow_js (isolate (),
537
557
Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
538
558
@@ -1101,6 +1121,7 @@ void Environment::CleanupFinalizationGroups() {
1101
1121
if (try_catch.HasCaught () && !try_catch.HasTerminated ())
1102
1122
errors::TriggerUncaughtException (isolate (), try_catch);
1103
1123
// Re-schedule the execution of the remainder of the queue.
1124
+ CHECK (task_queues_async_initialized_);
1104
1125
uv_async_send (&task_queues_async_);
1105
1126
return ;
1106
1127
}
0 commit comments