Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve some pthreads stub functions, batch 1 #525

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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