@@ -139,21 +139,31 @@ using v8::Undefined;
139139using v8::V8;
140140using v8::Value;
141141
142+ namespace per_process {
143+ // Tells whether --prof is passed.
144+ // TODO(joyeecheung): move env->options()->prof_process to
145+ // per_process::cli_options.prof_process and use that instead.
142146static bool v8_is_profiling = false ;
143147
144- // Bit flag used to track security reverts (see node_revert.h)
145- unsigned int reverted = 0 ;
148+ // TODO(joyeecheung): these are no longer necessary. Remove them.
149+ // See: https://github.com/nodejs/node/pull/25302#discussion_r244924196
150+ // Isolate on the main thread
151+ static Mutex main_isolate_mutex;
152+ static Isolate* main_isolate;
146153
154+ // node_revert.h
155+ // Bit flag used to track security reverts.
156+ unsigned int reverted_cve = 0 ;
157+
158+ // util.h
159+ // Tells whether the per-process V8::Initialize() is called and
160+ // if it is safe to call v8::Isolate::GetCurrent().
147161bool v8_initialized = false ;
148162
163+ // node_internals.h
149164// process-relative uptime base, initialized at start-up
150165double prog_start_time;
151-
152- Mutex per_process_opts_mutex;
153- std::shared_ptr<PerProcessOptions> per_process_opts {
154- new PerProcessOptions () };
155- static Mutex node_isolate_mutex;
156- static Isolate* node_isolate;
166+ } // namespace per_process
157167
158168// Ensures that __metadata trace events are only emitted
159169// when tracing is enabled.
@@ -269,14 +279,15 @@ static struct {
269279#endif // HAVE_INSPECTOR
270280
271281 void StartTracingAgent () {
272- if (per_process_opts ->trace_event_categories .empty ()) {
282+ if (per_process::cli_options ->trace_event_categories .empty ()) {
273283 tracing_file_writer_ = tracing_agent_->DefaultHandle ();
274284 } else {
275285 tracing_file_writer_ = tracing_agent_->AddClient (
276- ParseCommaSeparatedSet (per_process_opts->trace_event_categories ),
286+ ParseCommaSeparatedSet (
287+ per_process::cli_options->trace_event_categories ),
277288 std::unique_ptr<tracing::AsyncTraceWriter>(
278289 new tracing::NodeTraceWriter (
279- per_process_opts ->trace_event_file_pattern )),
290+ per_process::cli_options ->trace_event_file_pattern )),
280291 tracing::Agent::kUseDefaultCategories );
281292 }
282293 }
@@ -500,7 +511,7 @@ const char* signo_string(int signo) {
500511}
501512
502513void * ArrayBufferAllocator::Allocate (size_t size) {
503- if (zero_fill_field_ || per_process_opts ->zero_fill_all_buffers )
514+ if (zero_fill_field_ || per_process::cli_options ->zero_fill_all_buffers )
504515 return UncheckedCalloc (size);
505516 else
506517 return UncheckedMalloc (size);
@@ -1276,12 +1287,12 @@ void ProcessArgv(std::vector<std::string>* args,
12761287 {
12771288 // TODO(addaleax): The mutex here should ideally be held during the
12781289 // entire function, but that doesn't play well with the exit() calls below.
1279- Mutex::ScopedLock lock (per_process_opts_mutex );
1290+ Mutex::ScopedLock lock (per_process::cli_options_mutex );
12801291 options_parser::PerProcessOptionsParser::instance.Parse (
12811292 args,
12821293 exec_args,
12831294 &v8_args,
1284- per_process_opts .get (),
1295+ per_process::cli_options .get (),
12851296 is_env ? kAllowedInEnvironment : kDisallowedInEnvironment ,
12861297 &errors);
12871298 }
@@ -1293,20 +1304,20 @@ void ProcessArgv(std::vector<std::string>* args,
12931304 exit (9 );
12941305 }
12951306
1296- if (per_process_opts ->print_version ) {
1307+ if (per_process::cli_options ->print_version ) {
12971308 printf (" %s\n " , NODE_VERSION);
12981309 exit (0 );
12991310 }
13001311
1301- if (per_process_opts ->print_v8_help ) {
1312+ if (per_process::cli_options ->print_v8_help ) {
13021313 V8::SetFlagsFromString (" --help" , 6 );
13031314 exit (0 );
13041315 }
13051316
1306- for (const std::string& cve : per_process_opts ->security_reverts )
1317+ for (const std::string& cve : per_process::cli_options ->security_reverts )
13071318 Revert (cve.c_str ());
13081319
1309- auto env_opts = per_process_opts ->per_isolate ->per_env ;
1320+ auto env_opts = per_process::cli_options ->per_isolate ->per_env ;
13101321 if (std::find (v8_args.begin (), v8_args.end (),
13111322 " --abort-on-uncaught-exception" ) != v8_args.end () ||
13121323 std::find (v8_args.begin (), v8_args.end (),
@@ -1319,14 +1330,14 @@ void ProcessArgv(std::vector<std::string>* args,
13191330 // behavior but it could also interfere with the user's intentions in ways
13201331 // we fail to anticipate. Dillema.
13211332 if (std::find (v8_args.begin (), v8_args.end (), " --prof" ) != v8_args.end ()) {
1322- v8_is_profiling = true ;
1333+ per_process:: v8_is_profiling = true ;
13231334 }
13241335
13251336#ifdef __POSIX__
13261337 // Block SIGPROF signals when sleeping in epoll_wait/kevent/etc. Avoids the
13271338 // performance penalty of frequent EINTR wakeups when the profiler is running.
13281339 // Only do this for v8.log profiling, as it breaks v8::CpuProfiler users.
1329- if (v8_is_profiling) {
1340+ if (per_process:: v8_is_profiling) {
13301341 uv_loop_configure (uv_default_loop (), UV_LOOP_BLOCK_SIGNAL, SIGPROF);
13311342 }
13321343#endif
@@ -1355,7 +1366,7 @@ void ProcessArgv(std::vector<std::string>* args,
13551366void Init (std::vector<std::string>* argv,
13561367 std::vector<std::string>* exec_argv) {
13571368 // Initialize prog_start_time to get relative uptime.
1358- prog_start_time = static_cast <double >(uv_now (uv_default_loop ()));
1369+ per_process:: prog_start_time = static_cast <double >(uv_now (uv_default_loop ()));
13591370
13601371 // Register built-in modules
13611372 binding::RegisterBuiltinModules ();
@@ -1371,7 +1382,7 @@ void Init(std::vector<std::string>* argv,
13711382#endif
13721383
13731384 std::shared_ptr<EnvironmentOptions> default_env_options =
1374- per_process_opts ->per_isolate ->per_env ;
1385+ per_process::cli_options ->per_isolate ->per_env ;
13751386 {
13761387 std::string text;
13771388 default_env_options->pending_deprecation =
@@ -1400,7 +1411,7 @@ void Init(std::vector<std::string>* argv,
14001411 }
14011412
14021413#if HAVE_OPENSSL
1403- std::string* openssl_config = &per_process_opts ->openssl_config ;
1414+ std::string* openssl_config = &per_process::cli_options ->openssl_config ;
14041415 if (openssl_config->empty ()) {
14051416 credentials::SafeGetenv (" OPENSSL_CONF" , openssl_config);
14061417 }
@@ -1434,16 +1445,17 @@ void Init(std::vector<std::string>* argv,
14341445 ProcessArgv (argv, exec_argv, false );
14351446
14361447 // Set the process.title immediately after processing argv if --title is set.
1437- if (!per_process_opts ->title .empty ())
1438- uv_set_process_title (per_process_opts ->title .c_str ());
1448+ if (!per_process::cli_options ->title .empty ())
1449+ uv_set_process_title (per_process::cli_options ->title .c_str ());
14391450
14401451#if defined(NODE_HAVE_I18N_SUPPORT)
14411452 // If the parameter isn't given, use the env variable.
1442- if (per_process_opts->icu_data_dir .empty ())
1443- credentials::SafeGetenv (" NODE_ICU_DATA" , &per_process_opts->icu_data_dir );
1453+ if (per_process::cli_options->icu_data_dir .empty ())
1454+ credentials::SafeGetenv (" NODE_ICU_DATA" ,
1455+ &per_process::cli_options->icu_data_dir );
14441456 // Initialize ICU.
14451457 // If icu_data_dir is empty here, it will load the 'minimal' data.
1446- if (!i18n::InitializeICUDirectory (per_process_opts ->icu_data_dir )) {
1458+ if (!i18n::InitializeICUDirectory (per_process::cli_options ->icu_data_dir )) {
14471459 fprintf (stderr,
14481460 " %s: could not initialize ICU "
14491461 " (check NODE_ICU_DATA or --icu-data-dir parameters)\n " ,
@@ -1604,7 +1616,7 @@ Environment* CreateEnvironment(IsolateData* isolate_data,
16041616 std::vector<std::string> args (argv, argv + argc);
16051617 std::vector<std::string> exec_args (exec_argv, exec_argv + exec_argc);
16061618 Environment* env = new Environment (isolate_data, context);
1607- env->Start (args, exec_args, v8_is_profiling);
1619+ env->Start (args, exec_args, per_process:: v8_is_profiling);
16081620 return env;
16091621}
16101622
@@ -1678,7 +1690,7 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
16781690 Local<Context> context = NewContext (isolate);
16791691 Context::Scope context_scope (context);
16801692 Environment env (isolate_data, context);
1681- env.Start (args, exec_args, v8_is_profiling);
1693+ env.Start (args, exec_args, per_process:: v8_is_profiling);
16821694
16831695 const char * path = args.size () > 1 ? args[1 ].c_str () : nullptr ;
16841696 StartInspector (&env, path);
@@ -1785,9 +1797,9 @@ inline int Start(uv_loop_t* event_loop,
17851797 return 12 ; // Signal internal error.
17861798
17871799 {
1788- Mutex::ScopedLock scoped_lock (node_isolate_mutex );
1789- CHECK_NULL (node_isolate );
1790- node_isolate = isolate;
1800+ Mutex::ScopedLock scoped_lock (per_process::main_isolate_mutex );
1801+ CHECK_NULL (per_process::main_isolate );
1802+ per_process::main_isolate = isolate;
17911803 }
17921804
17931805 int exit_code;
@@ -1812,9 +1824,9 @@ inline int Start(uv_loop_t* event_loop,
18121824 }
18131825
18141826 {
1815- Mutex::ScopedLock scoped_lock (node_isolate_mutex );
1816- CHECK_EQ (node_isolate , isolate);
1817- node_isolate = nullptr ;
1827+ Mutex::ScopedLock scoped_lock (per_process::main_isolate_mutex );
1828+ CHECK_EQ (per_process::main_isolate , isolate);
1829+ per_process::main_isolate = nullptr ;
18181830 }
18191831
18201832 isolate->Dispose ();
@@ -1862,14 +1874,14 @@ int Start(int argc, char** argv) {
18621874 V8::SetEntropySource (crypto::EntropySource);
18631875#endif // HAVE_OPENSSL
18641876
1865- InitializeV8Platform (per_process_opts ->v8_thread_pool_size );
1877+ InitializeV8Platform (per_process::cli_options ->v8_thread_pool_size );
18661878 V8::Initialize ();
18671879 performance::performance_v8_start = PERFORMANCE_NOW ();
1868- v8_initialized = true ;
1880+ per_process:: v8_initialized = true ;
18691881 const int exit_code =
18701882 Start (uv_default_loop (), args, exec_args);
18711883 v8_platform.StopTracingAgent ();
1872- v8_initialized = false ;
1884+ per_process:: v8_initialized = false ;
18731885 V8::Dispose ();
18741886
18751887 // uv_run cannot be called from the time before the beforeExit callback
0 commit comments