Skip to content

Commit

Permalink
Properly stub out pthread cancellation
Browse files Browse the repository at this point in the history
Although it is not supported in WASI, thread cancellation is an important
feature of pthreads with wide-ranging implications.

Provide the best stubs we can within the limitations by allowing
cancellation type and state to be set, even though they will have no
effect as pthread_cancel is stubbed to always return ENOTSUP

Remove the THREAD_MODEL=single guard as it's currently not being
compiled in that case, and because we intend to provide
single-threaded stub implementations in the future.
Incidentally replace the existing pthread_setcancelstate stub
with one which actually correctly writes a value to the old parameter.
  • Loading branch information
ArcaneNibble committed Aug 6, 2024
1 parent 1b35dd3 commit 12ad798
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
thread/pthread_barrierattr_init.c \
thread/pthread_barrierattr_setpshared.c \
thread/pthread_cleanup_push.c \
thread/pthread_cancel.c \
thread/pthread_cond_broadcast.c \
thread/pthread_cond_destroy.c \
thread/pthread_cond_init.c \
Expand Down Expand Up @@ -345,6 +346,7 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
thread/pthread_rwlockattr_init.c \
thread/pthread_rwlockattr_setpshared.c \
thread/pthread_setcancelstate.c \
thread/pthread_setcanceltype.c \
thread/pthread_setspecific.c \
thread/pthread_self.c \
thread/pthread_spin_destroy.c \
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm32-wasip1-threads/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,7 @@ pthread_barrierattr_destroy
pthread_barrierattr_getpshared
pthread_barrierattr_init
pthread_barrierattr_setpshared
pthread_cancel
pthread_cond_broadcast
pthread_cond_destroy
pthread_cond_init
Expand Down Expand Up @@ -1056,6 +1057,7 @@ pthread_rwlockattr_init
pthread_rwlockattr_setpshared
pthread_self
pthread_setcancelstate
pthread_setcanceltype
pthread_setspecific
pthread_spin_destroy
pthread_spin_init
Expand Down
7 changes: 7 additions & 0 deletions libc-top-half/musl/src/thread/pthread_cancel.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "pthread_impl.h"
#include "syscall.h"

#ifdef __wasilibc_unmodified_upstream
hidden long __cancel(), __syscall_cp_asm(), __syscall_cp_c();

long __cancel()
Expand Down Expand Up @@ -99,3 +100,9 @@ int pthread_cancel(pthread_t t)
}
return pthread_kill(t, SIGCANCEL);
}
#else
int pthread_cancel(pthread_t t)
{
return ENOTSUP;
}
#endif
2 changes: 0 additions & 2 deletions libc-top-half/musl/src/thread/pthread_setcancelstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

int __pthread_setcancelstate(int new, int *old)
{
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
if (new > 2U) return EINVAL;
struct pthread *self = __pthread_self();
if (old) *old = self->canceldisable;
self->canceldisable = new;
#endif
return 0;
}

Expand Down

0 comments on commit 12ad798

Please sign in to comment.