Skip to content

Commit

Permalink
WIP: add support for pthread_atfork
Browse files Browse the repository at this point in the history
This change is an experiment to enable `pthread_atfork` support under
the mistaken assumption that it may have been called during thread
creation. I no longer believe it is, so this is not a good idea.

Here's how the registered handlers are executed, though:
+ prepare handlers are called in the parent before the fork with
  `__fork_handler(-1)`
+ parent handlers are called after the fork with `__fork_handler(0)`;
  fork returns the PID to the parent, which is negated (`!ret`)
+ child handlers are called after the fork with `__fork_handler(1)`;
  fork returns 0 to the child, which is negated (`!ret`)
  • Loading branch information
abrown committed Oct 16, 2023
1 parent eba961b commit e9990a2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
thread/__wait.c \
thread/__timedwait.c \
thread/default_attr.c \
thread/pthread_atfork.c \
thread/pthread_attr_destroy.c \
thread/pthread_attr_get.c \
thread/pthread_attr_init.c \
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm32-wasi-threads/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ __flbf
__floatscan
__fmodeflags
__fopen_rb_ca
__fork_handler
__fpending
__fpurge
__fputwc_unlocked
Expand Down Expand Up @@ -977,6 +978,7 @@ program_invocation_name
program_invocation_short_name
pselect
psignal
pthread_atfork
pthread_attr_destroy
pthread_attr_getdetachstate
pthread_attr_getguardsize
Expand Down
3 changes: 3 additions & 0 deletions libc-top-half/musl/src/thread/pthread_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ hidden void __wasi_thread_start_C(int tid, void *p)
// without waiting.
atomic_store((atomic_int *) &(self->tid), tid);
// Execute the user's start function.
__fork_handler(1);
__pthread_exit(args->start_func(args->start_arg));
}
#endif
Expand Down Expand Up @@ -540,7 +541,9 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
* of the current module and start executing the entry function. The
* wasi-threads specification requires the module to export a
* `wasi_thread_start` function, which is invoked with `args`. */
__fork_handler(-1);
ret = __wasi_thread_spawn((void *) args);
__fork_handler(0);
#endif

#ifdef __wasilibc_unmodified_upstream
Expand Down

0 comments on commit e9990a2

Please sign in to comment.