Skip to content

Commit

Permalink
separate caml_startup_needed from caml_startup_aux
Browse files Browse the repository at this point in the history
  • Loading branch information
gasche authored and gadmm committed Mar 28, 2021
1 parent c0db4e1 commit c9ac766
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
6 changes: 4 additions & 2 deletions runtime/caml/startup_aux.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
32 changes: 19 additions & 13 deletions runtime/startup_aux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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");
Expand Down
12 changes: 7 additions & 5 deletions runtime/startup_byt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions runtime/startup_nat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit c9ac766

Please sign in to comment.