Skip to content

Commit

Permalink
Improve some pthreads stub functions, batch 1 (WebAssembly#525)
Browse files Browse the repository at this point in the history
This is the next part of breaking up WebAssembly#518 into smaller PRs.

This is the rest of the commits which change the existing
`THREAD_MODEL=posix` functionality. It:

* _removes_ some functions which are optional and which were already
nonfunctional.
* (Continues to) establish a precedent of trying to remain as compatible
with Open Group specifications as possible, even when there are major
differences in WASI capabilities (i.e. thread cancellation)

Compared to the RFC PR, the `pthread_atfork` stub has been dropped as it
is now officially obsolete as of the very recent Issue 8 of the
specifications.
  • Loading branch information
ArcaneNibble authored Aug 6, 2024
1 parent 5d3c5e9 commit 230d4be
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,15 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
thread/pthread_attr_setguardsize.c \
thread/pthread_attr_setstack.c \
thread/pthread_attr_setstacksize.c \
thread/pthread_attr_setschedparam.c \
thread/pthread_barrier_destroy.c \
thread/pthread_barrier_init.c \
thread/pthread_barrier_wait.c \
thread/pthread_barrierattr_destroy.c \
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 @@ -344,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
7 changes: 5 additions & 2 deletions expected/wasm32-wasip1-threads/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -986,13 +986,13 @@ psignal
pthread_attr_destroy
pthread_attr_getdetachstate
pthread_attr_getguardsize
pthread_attr_getinheritsched
pthread_attr_getscope
pthread_attr_getschedparam
pthread_attr_getstack
pthread_attr_getstacksize
pthread_attr_init
pthread_attr_setdetachstate
pthread_attr_setguardsize
pthread_attr_setschedparam
pthread_attr_setstack
pthread_attr_setstacksize
pthread_barrier_destroy
Expand All @@ -1002,13 +1002,15 @@ pthread_barrierattr_destroy
pthread_barrierattr_getpshared
pthread_barrierattr_init
pthread_barrierattr_setpshared
pthread_cancel
pthread_cond_broadcast
pthread_cond_destroy
pthread_cond_init
pthread_cond_signal
pthread_cond_timedwait
pthread_cond_wait
pthread_condattr_destroy
pthread_condattr_getclock
pthread_condattr_getpshared
pthread_condattr_init
pthread_condattr_setclock
Expand Down Expand Up @@ -1055,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
2 changes: 1 addition & 1 deletion libc-top-half/musl/include/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ extern "C" {

#include <bits/alltypes.h>

#ifdef __wasilibc_unmodified_upstream /* WASI has no CPU scheduling support. */
struct sched_param {
int sched_priority;
int __reserved1;
Expand All @@ -31,6 +30,7 @@ struct sched_param {
int __reserved3;
};

#ifdef __wasilibc_unmodified_upstream /* WASI has no CPU scheduling support. */
int sched_get_priority_max(int);
int sched_get_priority_min(int);
int sched_getparam(pid_t, struct sched_param *);
Expand Down
23 changes: 21 additions & 2 deletions libc-top-half/musl/src/thread/pthread_attr_get.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "pthread_impl.h"

#ifndef __wasilibc_unmodified_upstream
#include <common/clock.h>
#endif

int pthread_attr_getdetachstate(const pthread_attr_t *a, int *state)
{
*state = a->_a_detach;
Expand All @@ -11,13 +15,13 @@ int pthread_attr_getguardsize(const pthread_attr_t *restrict a, size_t *restrict
return 0;
}

#ifdef __wasilibc_unmodified_upstream /* WASI has no CPU scheduling support. */
int pthread_attr_getinheritsched(const pthread_attr_t *restrict a, int *restrict inherit)
{
*inherit = a->_a_sched;
return 0;
}

#ifdef __wasilibc_unmodified_upstream /* WASI has no CPU scheduling support. */
int pthread_attr_getschedparam(const pthread_attr_t *restrict a, struct sched_param *restrict param)
{
param->sched_priority = a->_a_prio;
Expand All @@ -29,13 +33,19 @@ int pthread_attr_getschedpolicy(const pthread_attr_t *restrict a, int *restrict
*policy = a->_a_policy;
return 0;
}
#endif

int pthread_attr_getscope(const pthread_attr_t *restrict a, int *restrict scope)
{
*scope = PTHREAD_SCOPE_SYSTEM;
return 0;
}
#else
int pthread_attr_getschedparam(const pthread_attr_t *restrict a, struct sched_param *restrict param)
{
param->sched_priority = 0;
return 0;
}
#endif

int pthread_attr_getstack(const pthread_attr_t *restrict a, void **restrict addr, size_t *restrict size)
{
Expand Down Expand Up @@ -64,6 +74,15 @@ int pthread_condattr_getclock(const pthread_condattr_t *restrict a, clockid_t *r
*clk = a->__attr & 0x7fffffff;
return 0;
}
#else
int pthread_condattr_getclock(const pthread_condattr_t *restrict a, clockid_t *restrict clk)
{
if (a->__attr & 0x7fffffff == __WASI_CLOCKID_REALTIME)
*clk = CLOCK_REALTIME;
if (a->__attr & 0x7fffffff == __WASI_CLOCKID_MONOTONIC)
*clk = CLOCK_MONOTONIC;
return 0;
}
#endif

int pthread_condattr_getpshared(const pthread_condattr_t *restrict a, int *restrict pshared)
Expand Down
4 changes: 4 additions & 0 deletions libc-top-half/musl/src/thread/pthread_attr_setschedparam.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

int pthread_attr_setschedparam(pthread_attr_t *restrict a, const struct sched_param *restrict param)
{
#ifdef __wasilibc_unmodified_upstream
a->_a_prio = param->sched_priority;
#else
if (param->sched_priority != 0) return ENOTSUP;
#endif
return 0;
}
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 230d4be

Please sign in to comment.