@@ -139,21 +139,31 @@ using v8::Undefined; |
139 | 139 | using v8::V8; |
140 | 140 | using v8::Value; |
141 | 141 |
|
| 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. |
142 | 146 | static bool v8_is_profiling = false; |
143 | 147 |
|
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; |
146 | 153 |
|
| 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(). |
147 | 161 | bool v8_initialized = false; |
148 | 162 |
|
| 163 | +// node_internals.h |
149 | 164 | // process-relative uptime base, initialized at start-up |
150 | 165 | double 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 |
157 | 167 |
|
158 | 168 | // Ensures that __metadata trace events are only emitted |
159 | 169 | // when tracing is enabled. |
@@ -269,14 +279,15 @@ static struct { |
269 | 279 | #endif // HAVE_INSPECTOR |
270 | 280 |
|
271 | 281 | void StartTracingAgent() { |
272 | | - if (per_process_opts->trace_event_categories.empty()) { |
| 282 | + if (per_process::cli_options->trace_event_categories.empty()) { |
273 | 283 | tracing_file_writer_ = tracing_agent_->DefaultHandle(); |
274 | 284 | } else { |
275 | 285 | tracing_file_writer_ = tracing_agent_->AddClient( |
276 | | - ParseCommaSeparatedSet(per_process_opts->trace_event_categories), |
| 286 | + ParseCommaSeparatedSet( |
| 287 | + per_process::cli_options->trace_event_categories), |
277 | 288 | std::unique_ptr<tracing::AsyncTraceWriter>( |
278 | 289 | new tracing::NodeTraceWriter( |
279 | | - per_process_opts->trace_event_file_pattern)), |
| 290 | + per_process::cli_options->trace_event_file_pattern)), |
280 | 291 | tracing::Agent::kUseDefaultCategories); |
281 | 292 | } |
282 | 293 | } |
@@ -504,7 +515,7 @@ const char* signo_string(int signo) { |
504 | 515 | } |
505 | 516 |
|
506 | 517 | void* 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) |
508 | 519 | return UncheckedCalloc(size); |
509 | 520 | else |
510 | 521 | return UncheckedMalloc(size); |
@@ -1254,12 +1265,12 @@ void ProcessArgv(std::vector<std::string>* args, |
1254 | 1265 | { |
1255 | 1266 | // TODO(addaleax): The mutex here should ideally be held during the |
1256 | 1267 | // 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); |
1258 | 1269 | options_parser::PerProcessOptionsParser::instance.Parse( |
1259 | 1270 | args, |
1260 | 1271 | exec_args, |
1261 | 1272 | &v8_args, |
1262 | | - per_process_opts.get(), |
| 1273 | + per_process::cli_options.get(), |
1263 | 1274 | is_env ? kAllowedInEnvironment : kDisallowedInEnvironment, |
1264 | 1275 | &errors); |
1265 | 1276 | } |
@@ -1271,20 +1282,20 @@ void ProcessArgv(std::vector<std::string>* args, |
1271 | 1282 | exit(9); |
1272 | 1283 | } |
1273 | 1284 |
|
1274 | | - if (per_process_opts->print_version) { |
| 1285 | + if (per_process::cli_options->print_version) { |
1275 | 1286 | printf("%s\n", NODE_VERSION); |
1276 | 1287 | exit(0); |
1277 | 1288 | } |
1278 | 1289 |
|
1279 | | - if (per_process_opts->print_v8_help) { |
| 1290 | + if (per_process::cli_options->print_v8_help) { |
1280 | 1291 | V8::SetFlagsFromString("--help", 6); |
1281 | 1292 | exit(0); |
1282 | 1293 | } |
1283 | 1294 |
|
1284 | | - for (const std::string& cve : per_process_opts->security_reverts) |
| 1295 | + for (const std::string& cve : per_process::cli_options->security_reverts) |
1285 | 1296 | Revert(cve.c_str()); |
1286 | 1297 |
|
1287 | | - auto env_opts = per_process_opts->per_isolate->per_env; |
| 1298 | + auto env_opts = per_process::cli_options->per_isolate->per_env; |
1288 | 1299 | if (std::find(v8_args.begin(), v8_args.end(), |
1289 | 1300 | "--abort-on-uncaught-exception") != v8_args.end() || |
1290 | 1301 | std::find(v8_args.begin(), v8_args.end(), |
@@ -1297,14 +1308,14 @@ void ProcessArgv(std::vector<std::string>* args, |
1297 | 1308 | // behavior but it could also interfere with the user's intentions in ways |
1298 | 1309 | // we fail to anticipate. Dillema. |
1299 | 1310 | if (std::find(v8_args.begin(), v8_args.end(), "--prof") != v8_args.end()) { |
1300 | | - v8_is_profiling = true; |
| 1311 | + per_process::v8_is_profiling = true; |
1301 | 1312 | } |
1302 | 1313 |
|
1303 | 1314 | #ifdef __POSIX__ |
1304 | 1315 | // Block SIGPROF signals when sleeping in epoll_wait/kevent/etc. Avoids the |
1305 | 1316 | // performance penalty of frequent EINTR wakeups when the profiler is running. |
1306 | 1317 | // 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) { |
1308 | 1319 | uv_loop_configure(uv_default_loop(), UV_LOOP_BLOCK_SIGNAL, SIGPROF); |
1309 | 1320 | } |
1310 | 1321 | #endif |
@@ -1333,7 +1344,7 @@ void ProcessArgv(std::vector<std::string>* args, |
1333 | 1344 | void Init(std::vector<std::string>* argv, |
1334 | 1345 | std::vector<std::string>* exec_argv) { |
1335 | 1346 | // 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())); |
1337 | 1348 |
|
1338 | 1349 | // Register built-in modules |
1339 | 1350 | binding::RegisterBuiltinModules(); |
@@ -1349,7 +1360,7 @@ void Init(std::vector<std::string>* argv, |
1349 | 1360 | #endif |
1350 | 1361 |
|
1351 | 1362 | std::shared_ptr<EnvironmentOptions> default_env_options = |
1352 | | - per_process_opts->per_isolate->per_env; |
| 1363 | + per_process::cli_options->per_isolate->per_env; |
1353 | 1364 | { |
1354 | 1365 | std::string text; |
1355 | 1366 | default_env_options->pending_deprecation = |
@@ -1378,7 +1389,7 @@ void Init(std::vector<std::string>* argv, |
1378 | 1389 | } |
1379 | 1390 |
|
1380 | 1391 | #if HAVE_OPENSSL |
1381 | | - std::string* openssl_config = &per_process_opts->openssl_config; |
| 1392 | + std::string* openssl_config = &per_process::cli_options->openssl_config; |
1382 | 1393 | if (openssl_config->empty()) { |
1383 | 1394 | credentials::SafeGetenv("OPENSSL_CONF", openssl_config); |
1384 | 1395 | } |
@@ -1412,16 +1423,17 @@ void Init(std::vector<std::string>* argv, |
1412 | 1423 | ProcessArgv(argv, exec_argv, false); |
1413 | 1424 |
|
1414 | 1425 | // 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()); |
1417 | 1428 |
|
1418 | 1429 | #if defined(NODE_HAVE_I18N_SUPPORT) |
1419 | 1430 | // 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", &per_process_opts->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); |
1422 | 1434 | // Initialize ICU. |
1423 | 1435 | // 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)) { |
1425 | 1437 | fprintf(stderr, |
1426 | 1438 | "%s: could not initialize ICU " |
1427 | 1439 | "(check NODE_ICU_DATA or --icu-data-dir parameters)\n", |
@@ -1582,7 +1594,7 @@ Environment* CreateEnvironment(IsolateData* isolate_data, |
1582 | 1594 | std::vector<std::string> args(argv, argv + argc); |
1583 | 1595 | std::vector<std::string> exec_args(exec_argv, exec_argv + exec_argc); |
1584 | 1596 | 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); |
1586 | 1598 | return env; |
1587 | 1599 | } |
1588 | 1600 |
|
@@ -1656,7 +1668,7 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data, |
1656 | 1668 | Local<Context> context = NewContext(isolate); |
1657 | 1669 | Context::Scope context_scope(context); |
1658 | 1670 | 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); |
1660 | 1672 |
|
1661 | 1673 | const char* path = args.size() > 1 ? args[1].c_str() : nullptr; |
1662 | 1674 | StartInspector(&env, path); |
@@ -1763,9 +1775,9 @@ inline int Start(uv_loop_t* event_loop, |
1763 | 1775 | return 12; // Signal internal error. |
1764 | 1776 |
|
1765 | 1777 | { |
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; |
1769 | 1781 | } |
1770 | 1782 |
|
1771 | 1783 | int exit_code; |
@@ -1790,9 +1802,9 @@ inline int Start(uv_loop_t* event_loop, |
1790 | 1802 | } |
1791 | 1803 |
|
1792 | 1804 | { |
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; |
1796 | 1808 | } |
1797 | 1809 |
|
1798 | 1810 | isolate->Dispose(); |
@@ -1840,14 +1852,14 @@ int Start(int argc, char** argv) { |
1840 | 1852 | V8::SetEntropySource(crypto::EntropySource); |
1841 | 1853 | #endif // HAVE_OPENSSL |
1842 | 1854 |
|
1843 | | - InitializeV8Platform(per_process_opts->v8_thread_pool_size); |
| 1855 | + InitializeV8Platform(per_process::cli_options->v8_thread_pool_size); |
1844 | 1856 | V8::Initialize(); |
1845 | 1857 | performance::performance_v8_start = PERFORMANCE_NOW(); |
1846 | | - v8_initialized = true; |
| 1858 | + per_process::v8_initialized = true; |
1847 | 1859 | const int exit_code = |
1848 | 1860 | Start(uv_default_loop(), args, exec_args); |
1849 | 1861 | v8_platform.StopTracingAgent(); |
1850 | | - v8_initialized = false; |
| 1862 | + per_process::v8_initialized = false; |
1851 | 1863 | V8::Dispose(); |
1852 | 1864 |
|
1853 | 1865 | // uv_run cannot be called from the time before the beforeExit callback |
|