diff --git a/runtime/caml/startup_aux.h b/runtime/caml/startup_aux.h index 6739a2bee164..c4c89ff7af17 100644 --- a/runtime/caml/startup_aux.h +++ b/runtime/caml/startup_aux.h @@ -27,10 +27,12 @@ extern uintnat caml_init_max_stack_wsz; extern uintnat caml_trace_level; extern int caml_cleanup_on_exit; +/* Returns 0 if the runtime is already initialized. */ +extern int caml_startup_needed(); + /* Common entry point to caml_startup. - Returns 0 if the runtime is already initialized. If [pooling] is 1, [caml_stat_*] functions will be backed by a pool. */ -extern int caml_startup_aux (int pooling); +extern void caml_startup_aux (int pooling); #endif /* CAML_INTERNALS */ diff --git a/runtime/startup_aux.c b/runtime/startup_aux.c index c120ea3c79ad..1bcdd1ead736 100644 --- a/runtime/startup_aux.c +++ b/runtime/startup_aux.c @@ -150,18 +150,32 @@ static int startup_count = 0; /* Has the runtime been shut down already? */ static int shutdown_happened = 0; -int caml_startup_aux(int pooling) +int caml_startup_needed() { if (shutdown_happened == 1) caml_fatal_error("caml_startup was called after the runtime " "was shut down with caml_shutdown"); - /* Second and subsequent calls are ignored, + /* Second and subsequent startups are ignored, since the runtime has already started */ startup_count++; - if (startup_count > 1) - return 0; + return (startup_count == 1); +} + + +int caml_shutdown_needed() +{ + if (startup_count <= 0) + caml_fatal_error("a call to caml_shutdown has no " + "corresponding call to caml_startup"); + /* Do nothing unless it's the last call remaining */ + startup_count--; + return (startup_count == 0); +} + +void caml_startup_aux(int pooling) +{ /* Determine options */ #ifdef DEBUG caml_verb_gc = 0x3F; @@ -196,8 +210,6 @@ int caml_startup_aux(int pooling) caml_init_backtrace(); caml_record_backtrace(Val_int(record_backtrace)); init_atom_table(); - - return 1; } static void call_registered_value(char* name) @@ -209,13 +221,7 @@ static void call_registered_value(char* name) CAMLexport void caml_shutdown(void) { - if (startup_count <= 0) - caml_fatal_error("a call to caml_shutdown has no " - "corresponding call to caml_startup"); - - /* Do nothing unless it's the last call remaining */ - startup_count--; - if (startup_count > 0) + if (!caml_shutdown_needed()) return; call_registered_value("Pervasives.do_at_exit"); diff --git a/runtime/startup_byt.c b/runtime/startup_byt.c index 4ea771a4bd47..f66b43b35ee2 100644 --- a/runtime/startup_byt.c +++ b/runtime/startup_byt.c @@ -459,11 +459,12 @@ CAMLexport void caml_main(char_os **argv) char_os * shared_lib_path, * shared_libs; char_os * exe_name, * proc_self_exe; - if (!caml_startup_aux(0)) { - /* startup was already done once */ - return; + if (!caml_startup_needed()) { + return; } + caml_startup_aux(0); + caml_ext_table_init(&caml_shared_libs_path, 8); /* Determine position of bytecode file */ @@ -582,11 +583,12 @@ CAMLexport value caml_startup_code_exn( char_os * cds_file; char_os * exe_name; - if (!caml_startup_aux(pooling)) { - /* startup was already done once */ + if (!caml_startup_needed()) { return Val_unit; } + caml_startup_aux(pooling); + cds_file = caml_secure_getenv(T("CAML_DEBUG_FILE")); if (cds_file != NULL) { caml_cds_file = caml_stat_strdup_os(cds_file); diff --git a/runtime/startup_nat.c b/runtime/startup_nat.c index ed15f88662b9..13672f057f93 100644 --- a/runtime/startup_nat.c +++ b/runtime/startup_nat.c @@ -103,11 +103,12 @@ value caml_startup_common(char_os **argv, int pooling) char_os * exe_name, * proc_self_exe; char tos; - if (!caml_startup_aux(pooling)) { - /* startup was already done once */ + if (!caml_startup_needed()) { return Val_unit; } + caml_startup_aux(pooling); + caml_init_frame_descriptors(); Caml_state->top_of_stack = &tos; init_static();