Skip to content

Commit

Permalink
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.

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.  This way, fork() calls from C code no longer reset the OCaml thread machinery.  This should help with issues such as ocaml#12636.

(cherry picked from commit c6d00ee)
(and adapted to 4.14)

Co-authored-by: xavier.leroy@college-de-france.fr
  • Loading branch information
abbysmal authored and xavierleroy committed Oct 26, 2023
1 parent 5cd072a commit 2bcd1bc
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion otherlibs/systhreads/st_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#ifdef __linux__
#include <unistd.h>
#endif
#include <caml/domain.h>

typedef int st_retcode;

Expand Down Expand Up @@ -408,7 +409,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
2 changes: 2 additions & 0 deletions otherlibs/unix/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#define CAML_INTERNALS

#include <caml/domain.h>
#include <caml/mlvalues.h>
#include <caml/debugger.h>
#include <caml/eventlog.h>
Expand All @@ -27,6 +28,7 @@ CAMLprim value unix_fork(value unit)
CAML_EV_FLUSH();

ret = fork();
if (ret == 0 && caml_atfork_hook != NULL) 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 @@ -27,6 +27,8 @@ extern "C" {

void caml_init_domain(void);

CAMLextern void (*caml_atfork_hook)(void);

#endif /* CAML_INTERNALS */

#ifdef __cplusplus
Expand Down
2 changes: 2 additions & 0 deletions runtime/domain.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,5 @@ void caml_init_domain (void)
Caml_state->checking_pointer_pc = NULL;
#endif
}

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

0 comments on commit 2bcd1bc

Please sign in to comment.