From 12ad798a8a33a61c18818d71b177ed2553262974 Mon Sep 17 00:00:00 2001 From: R Date: Wed, 17 Jul 2024 23:18:56 +0100 Subject: [PATCH] Properly stub out pthread cancellation 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. --- Makefile | 2 ++ expected/wasm32-wasip1-threads/defined-symbols.txt | 2 ++ libc-top-half/musl/src/thread/pthread_cancel.c | 7 +++++++ libc-top-half/musl/src/thread/pthread_setcancelstate.c | 2 -- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 3f2515f53..39a16b141 100644 --- a/Makefile +++ b/Makefile @@ -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 \ @@ -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 \ diff --git a/expected/wasm32-wasip1-threads/defined-symbols.txt b/expected/wasm32-wasip1-threads/defined-symbols.txt index 7427fc3c1..2bb7584bf 100644 --- a/expected/wasm32-wasip1-threads/defined-symbols.txt +++ b/expected/wasm32-wasip1-threads/defined-symbols.txt @@ -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 @@ -1056,6 +1057,7 @@ pthread_rwlockattr_init pthread_rwlockattr_setpshared pthread_self pthread_setcancelstate +pthread_setcanceltype pthread_setspecific pthread_spin_destroy pthread_spin_init diff --git a/libc-top-half/musl/src/thread/pthread_cancel.c b/libc-top-half/musl/src/thread/pthread_cancel.c index 2f9d5e975..ae0c2d84d 100644 --- a/libc-top-half/musl/src/thread/pthread_cancel.c +++ b/libc-top-half/musl/src/thread/pthread_cancel.c @@ -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() @@ -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 diff --git a/libc-top-half/musl/src/thread/pthread_setcancelstate.c b/libc-top-half/musl/src/thread/pthread_setcancelstate.c index 4f7a00e58..5ab8c338f 100644 --- a/libc-top-half/musl/src/thread/pthread_setcancelstate.c +++ b/libc-top-half/musl/src/thread/pthread_setcancelstate.c @@ -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; }