@@ -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  }
@@ -504,7 +515,7 @@ const char* signo_string(int signo) {
504515}
505516
506517void * ArrayBufferAllocator::Allocate (size_t  size) {
507-   if  (zero_fill_field_ || per_process_opts ->zero_fill_all_buffers )
518+   if  (zero_fill_field_ || per_process::cli_options ->zero_fill_all_buffers )
508519    return  UncheckedCalloc (size);
509520  else 
510521    return  UncheckedMalloc (size);
@@ -1254,12 +1265,12 @@ void ProcessArgv(std::vector<std::string>* args,
12541265  {
12551266    //  TODO(addaleax): The mutex here should ideally be held during the
12561267    //  entire function, but that doesn't play well with the exit() calls below.
1257-     Mutex::ScopedLock lock (per_process_opts_mutex );
1268+     Mutex::ScopedLock lock (per_process::cli_options_mutex );
12581269    options_parser::PerProcessOptionsParser::instance.Parse (
12591270        args,
12601271        exec_args,
12611272        &v8_args,
1262-         per_process_opts .get (),
1273+         per_process::cli_options .get (),
12631274        is_env ? kAllowedInEnvironment  : kDisallowedInEnvironment ,
12641275        &errors);
12651276  }
@@ -1271,20 +1282,20 @@ void ProcessArgv(std::vector<std::string>* args,
12711282    exit (9 );
12721283  }
12731284
1274-   if  (per_process_opts ->print_version ) {
1285+   if  (per_process::cli_options ->print_version ) {
12751286    printf (" %s\n " 
12761287    exit (0 );
12771288  }
12781289
1279-   if  (per_process_opts ->print_v8_help ) {
1290+   if  (per_process::cli_options ->print_v8_help ) {
12801291    V8::SetFlagsFromString (" --help" 6 );
12811292    exit (0 );
12821293  }
12831294
1284-   for  (const  std::string& cve : per_process_opts ->security_reverts )
1295+   for  (const  std::string& cve : per_process::cli_options ->security_reverts )
12851296    Revert (cve.c_str ());
12861297
1287-   auto  env_opts = per_process_opts ->per_isolate ->per_env ;
1298+   auto  env_opts = per_process::cli_options ->per_isolate ->per_env ;
12881299  if  (std::find (v8_args.begin (), v8_args.end (),
12891300                " --abort-on-uncaught-exception" end () ||
12901301      std::find (v8_args.begin (), v8_args.end (),
@@ -1297,14 +1308,14 @@ void ProcessArgv(std::vector<std::string>* args,
12971308  //  behavior but it could also interfere with the user's intentions in ways
12981309  //  we fail to anticipate.  Dillema.
12991310  if  (std::find (v8_args.begin (), v8_args.end (), " --prof" end ()) {
1300-     v8_is_profiling = true ;
1311+     per_process:: v8_is_profiling = true ;
13011312  }
13021313
13031314#ifdef  __POSIX__
13041315  //  Block SIGPROF signals when sleeping in epoll_wait/kevent/etc.  Avoids the
13051316  //  performance penalty of frequent EINTR wakeups when the profiler is running.
13061317  //  Only do this for v8.log profiling, as it breaks v8::CpuProfiler users.
1307-   if  (v8_is_profiling) {
1318+   if  (per_process:: v8_is_profiling) {
13081319    uv_loop_configure (uv_default_loop (), UV_LOOP_BLOCK_SIGNAL, SIGPROF);
13091320  }
13101321#endif 
@@ -1333,7 +1344,7 @@ void ProcessArgv(std::vector<std::string>* args,
13331344void  Init (std::vector<std::string>* argv,
13341345          std::vector<std::string>* exec_argv) {
13351346  //  Initialize prog_start_time to get relative uptime.
1336-   prog_start_time = static_cast <double >(uv_now (uv_default_loop ()));
1347+   per_process:: prog_start_time = static_cast <double >(uv_now (uv_default_loop ()));
13371348
13381349  //  Register built-in modules
13391350  binding::RegisterBuiltinModules ();
@@ -1349,7 +1360,7 @@ void Init(std::vector<std::string>* argv,
13491360#endif 
13501361
13511362  std::shared_ptr<EnvironmentOptions> default_env_options =
1352-       per_process_opts ->per_isolate ->per_env ;
1363+       per_process::cli_options ->per_isolate ->per_env ;
13531364  {
13541365    std::string text;
13551366    default_env_options->pending_deprecation  =
@@ -1378,7 +1389,7 @@ void Init(std::vector<std::string>* argv,
13781389  }
13791390
13801391#if  HAVE_OPENSSL
1381-   std::string* openssl_config = &per_process_opts ->openssl_config ;
1392+   std::string* openssl_config = &per_process::cli_options ->openssl_config ;
13821393  if  (openssl_config->empty ()) {
13831394    credentials::SafeGetenv (" OPENSSL_CONF" 
13841395  }
@@ -1412,16 +1423,17 @@ void Init(std::vector<std::string>* argv,
14121423  ProcessArgv (argv, exec_argv, false );
14131424
14141425  //  Set the process.title immediately after processing argv if --title is set.
1415-   if  (!per_process_opts ->title .empty ())
1416-     uv_set_process_title (per_process_opts ->title .c_str ());
1426+   if  (!per_process::cli_options ->title .empty ())
1427+     uv_set_process_title (per_process::cli_options ->title .c_str ());
14171428
14181429#if  defined(NODE_HAVE_I18N_SUPPORT)
14191430  //  If the parameter isn't given, use the env variable.
1420-   if  (per_process_opts->icu_data_dir .empty ())
1421-     credentials::SafeGetenv (" NODE_ICU_DATA" icu_data_dir );
1431+   if  (per_process::cli_options->icu_data_dir .empty ())
1432+     credentials::SafeGetenv (" NODE_ICU_DATA" 
1433+                             &per_process::cli_options->icu_data_dir );
14221434  //  Initialize ICU.
14231435  //  If icu_data_dir is empty here, it will load the 'minimal' data.
1424-   if  (!i18n::InitializeICUDirectory (per_process_opts ->icu_data_dir )) {
1436+   if  (!i18n::InitializeICUDirectory (per_process::cli_options ->icu_data_dir )) {
14251437    fprintf (stderr,
14261438            " %s: could not initialize ICU " 
14271439            " (check NODE_ICU_DATA or --icu-data-dir parameters)\n " 
@@ -1582,7 +1594,7 @@ Environment* CreateEnvironment(IsolateData* isolate_data,
15821594  std::vector<std::string> args (argv, argv + argc);
15831595  std::vector<std::string> exec_args (exec_argv, exec_argv + exec_argc);
15841596  Environment* env = new  Environment (isolate_data, context);
1585-   env->Start (args, exec_args, v8_is_profiling);
1597+   env->Start (args, exec_args, per_process:: v8_is_profiling);
15861598  return  env;
15871599}
15881600
@@ -1656,7 +1668,7 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
16561668  Local<Context> context = NewContext (isolate);
16571669  Context::Scope context_scope (context);
16581670  Environment env (isolate_data, context);
1659-   env.Start (args, exec_args, v8_is_profiling);
1671+   env.Start (args, exec_args, per_process:: v8_is_profiling);
16601672
16611673  const  char * path = args.size () > 1  ? args[1 ].c_str () : nullptr ;
16621674  StartInspector (&env, path);
@@ -1763,9 +1775,9 @@ inline int Start(uv_loop_t* event_loop,
17631775    return  12 ;  //  Signal internal error.
17641776
17651777  {
1766-     Mutex::ScopedLock scoped_lock (node_isolate_mutex );
1767-     CHECK_NULL (node_isolate );
1768-     node_isolate  = isolate;
1778+     Mutex::ScopedLock scoped_lock (per_process::main_isolate_mutex );
1779+     CHECK_NULL (per_process::main_isolate );
1780+     per_process::main_isolate  = isolate;
17691781  }
17701782
17711783  int  exit_code;
@@ -1790,9 +1802,9 @@ inline int Start(uv_loop_t* event_loop,
17901802  }
17911803
17921804  {
1793-     Mutex::ScopedLock scoped_lock (node_isolate_mutex );
1794-     CHECK_EQ (node_isolate , isolate);
1795-     node_isolate  = nullptr ;
1805+     Mutex::ScopedLock scoped_lock (per_process::main_isolate_mutex );
1806+     CHECK_EQ (per_process::main_isolate , isolate);
1807+     per_process::main_isolate  = nullptr ;
17961808  }
17971809
17981810  isolate->Dispose ();
@@ -1840,14 +1852,14 @@ int Start(int argc, char** argv) {
18401852  V8::SetEntropySource (crypto::EntropySource);
18411853#endif   //  HAVE_OPENSSL
18421854
1843-   InitializeV8Platform (per_process_opts ->v8_thread_pool_size );
1855+   InitializeV8Platform (per_process::cli_options ->v8_thread_pool_size );
18441856  V8::Initialize ();
18451857  performance::performance_v8_start = PERFORMANCE_NOW ();
1846-   v8_initialized = true ;
1858+   per_process:: v8_initialized = true ;
18471859  const  int  exit_code =
18481860      Start (uv_default_loop (), args, exec_args);
18491861  v8_platform.StopTracingAgent ();
1850-   v8_initialized = false ;
1862+   per_process:: v8_initialized = false ;
18511863  V8::Dispose ();
18521864
18531865  //  uv_run cannot be called from the time before the beforeExit callback
0 commit comments