Skip to content

Commit

Permalink
runtime: introduce an atfork hook in the runtime
Browse files Browse the repository at this point in the history
This commit introduce an atfork hook in the runtime, as a follow up to the issue encountered in MPR ocaml#455 and MPR ocaml#471.

To handle all the specific fork scenarios allowed in Multicore, a hook is required in order to let specific "alterations"
to the runtime do their job: by default, on a single domain program, only the domain lock needs to be reset.
When systhreads are in use, more cleaning up duty is required.

As a result, the usage of pthreads_atfork is also removed, opting to rely in both case on manually calling the
runtime hook when required.
  • Loading branch information
abbysmal authored and ctk21 committed Mar 15, 2021
1 parent cb4da64 commit c6d00ee
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion otherlibs/systhreads/st_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ int pthread_atfork(void (*prepare)(void), void (*parent)(void),

static int st_atfork(void (*fn)(void))
{
return pthread_atfork(NULL, NULL, fn);
caml_atfork_hook = fn;
return 0;
}

/* Signal handling */
Expand Down
1 change: 1 addition & 0 deletions otherlibs/unix/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ CAMLprim value unix_fork(value unit)
CAML_EV_FLUSH();

ret = fork();
if (ret == 0) caml_atfork_hook();
if (ret == -1) uerror("fork", Nothing);

CAML_EVENTLOG_DO({
Expand Down
2 changes: 2 additions & 0 deletions runtime/caml/domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ CAMLextern void caml_leave_blocking_section(void);
CAMLextern void (*caml_enter_blocking_section_hook)(void);
CAMLextern void (*caml_leave_blocking_section_hook)(void);

CAMLextern void (*caml_atfork_hook)(void);

CAMLextern void (*caml_domain_start_hook)(void);
CAMLextern void (*caml_domain_stop_hook)(void);

Expand Down
8 changes: 8 additions & 0 deletions runtime/domain.c
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,14 @@ CAMLexport void caml_enter_blocking_section() {
caml_enter_blocking_section_hook();
}

/* default handler for unix_fork, will be called by unix_fork. */
static void caml_atfork_default(void) {
caml_reset_domain_lock();
caml_acquire_domain_lock();
}

CAMLexport void (*caml_atfork_hook)(void) = caml_atfork_default;

void caml_print_stats () {
struct gc_stats s;
#if defined(COLLECT_STATS) && defined(NATIVE_CODE)
Expand Down

0 comments on commit c6d00ee

Please sign in to comment.