Skip to content

Commit 230d4be

Browse files
authored
Improve some pthreads stub functions, batch 1 (#525)
This is the next part of breaking up #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.
1 parent 5d3c5e9 commit 230d4be

File tree

7 files changed

+41
-7
lines changed

7 files changed

+41
-7
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,15 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
292292
thread/pthread_attr_setguardsize.c \
293293
thread/pthread_attr_setstack.c \
294294
thread/pthread_attr_setstacksize.c \
295+
thread/pthread_attr_setschedparam.c \
295296
thread/pthread_barrier_destroy.c \
296297
thread/pthread_barrier_init.c \
297298
thread/pthread_barrier_wait.c \
298299
thread/pthread_barrierattr_destroy.c \
299300
thread/pthread_barrierattr_init.c \
300301
thread/pthread_barrierattr_setpshared.c \
301302
thread/pthread_cleanup_push.c \
303+
thread/pthread_cancel.c \
302304
thread/pthread_cond_broadcast.c \
303305
thread/pthread_cond_destroy.c \
304306
thread/pthread_cond_init.c \
@@ -344,6 +346,7 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
344346
thread/pthread_rwlockattr_init.c \
345347
thread/pthread_rwlockattr_setpshared.c \
346348
thread/pthread_setcancelstate.c \
349+
thread/pthread_setcanceltype.c \
347350
thread/pthread_setspecific.c \
348351
thread/pthread_self.c \
349352
thread/pthread_spin_destroy.c \

expected/wasm32-wasip1-threads/defined-symbols.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,13 +986,13 @@ psignal
986986
pthread_attr_destroy
987987
pthread_attr_getdetachstate
988988
pthread_attr_getguardsize
989-
pthread_attr_getinheritsched
990-
pthread_attr_getscope
989+
pthread_attr_getschedparam
991990
pthread_attr_getstack
992991
pthread_attr_getstacksize
993992
pthread_attr_init
994993
pthread_attr_setdetachstate
995994
pthread_attr_setguardsize
995+
pthread_attr_setschedparam
996996
pthread_attr_setstack
997997
pthread_attr_setstacksize
998998
pthread_barrier_destroy
@@ -1002,13 +1002,15 @@ pthread_barrierattr_destroy
10021002
pthread_barrierattr_getpshared
10031003
pthread_barrierattr_init
10041004
pthread_barrierattr_setpshared
1005+
pthread_cancel
10051006
pthread_cond_broadcast
10061007
pthread_cond_destroy
10071008
pthread_cond_init
10081009
pthread_cond_signal
10091010
pthread_cond_timedwait
10101011
pthread_cond_wait
10111012
pthread_condattr_destroy
1013+
pthread_condattr_getclock
10121014
pthread_condattr_getpshared
10131015
pthread_condattr_init
10141016
pthread_condattr_setclock
@@ -1055,6 +1057,7 @@ pthread_rwlockattr_init
10551057
pthread_rwlockattr_setpshared
10561058
pthread_self
10571059
pthread_setcancelstate
1060+
pthread_setcanceltype
10581061
pthread_setspecific
10591062
pthread_spin_destroy
10601063
pthread_spin_init

libc-top-half/musl/include/sched.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ extern "C" {
1616

1717
#include <bits/alltypes.h>
1818

19-
#ifdef __wasilibc_unmodified_upstream /* WASI has no CPU scheduling support. */
2019
struct sched_param {
2120
int sched_priority;
2221
int __reserved1;
@@ -31,6 +30,7 @@ struct sched_param {
3130
int __reserved3;
3231
};
3332

33+
#ifdef __wasilibc_unmodified_upstream /* WASI has no CPU scheduling support. */
3434
int sched_get_priority_max(int);
3535
int sched_get_priority_min(int);
3636
int sched_getparam(pid_t, struct sched_param *);

libc-top-half/musl/src/thread/pthread_attr_get.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include "pthread_impl.h"
22

3+
#ifndef __wasilibc_unmodified_upstream
4+
#include <common/clock.h>
5+
#endif
6+
37
int pthread_attr_getdetachstate(const pthread_attr_t *a, int *state)
48
{
59
*state = a->_a_detach;
@@ -11,13 +15,13 @@ int pthread_attr_getguardsize(const pthread_attr_t *restrict a, size_t *restrict
1115
return 0;
1216
}
1317

18+
#ifdef __wasilibc_unmodified_upstream /* WASI has no CPU scheduling support. */
1419
int pthread_attr_getinheritsched(const pthread_attr_t *restrict a, int *restrict inherit)
1520
{
1621
*inherit = a->_a_sched;
1722
return 0;
1823
}
1924

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

3437
int pthread_attr_getscope(const pthread_attr_t *restrict a, int *restrict scope)
3538
{
3639
*scope = PTHREAD_SCOPE_SYSTEM;
3740
return 0;
3841
}
42+
#else
43+
int pthread_attr_getschedparam(const pthread_attr_t *restrict a, struct sched_param *restrict param)
44+
{
45+
param->sched_priority = 0;
46+
return 0;
47+
}
48+
#endif
3949

4050
int pthread_attr_getstack(const pthread_attr_t *restrict a, void **restrict addr, size_t *restrict size)
4151
{
@@ -64,6 +74,15 @@ int pthread_condattr_getclock(const pthread_condattr_t *restrict a, clockid_t *r
6474
*clk = a->__attr & 0x7fffffff;
6575
return 0;
6676
}
77+
#else
78+
int pthread_condattr_getclock(const pthread_condattr_t *restrict a, clockid_t *restrict clk)
79+
{
80+
if (a->__attr & 0x7fffffff == __WASI_CLOCKID_REALTIME)
81+
*clk = CLOCK_REALTIME;
82+
if (a->__attr & 0x7fffffff == __WASI_CLOCKID_MONOTONIC)
83+
*clk = CLOCK_MONOTONIC;
84+
return 0;
85+
}
6786
#endif
6887

6988
int pthread_condattr_getpshared(const pthread_condattr_t *restrict a, int *restrict pshared)

libc-top-half/musl/src/thread/pthread_attr_setschedparam.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
int pthread_attr_setschedparam(pthread_attr_t *restrict a, const struct sched_param *restrict param)
44
{
5+
#ifdef __wasilibc_unmodified_upstream
56
a->_a_prio = param->sched_priority;
7+
#else
8+
if (param->sched_priority != 0) return ENOTSUP;
9+
#endif
610
return 0;
711
}

libc-top-half/musl/src/thread/pthread_cancel.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "pthread_impl.h"
44
#include "syscall.h"
55

6+
#ifdef __wasilibc_unmodified_upstream
67
hidden long __cancel(), __syscall_cp_asm(), __syscall_cp_c();
78

89
long __cancel()
@@ -99,3 +100,9 @@ int pthread_cancel(pthread_t t)
99100
}
100101
return pthread_kill(t, SIGCANCEL);
101102
}
103+
#else
104+
int pthread_cancel(pthread_t t)
105+
{
106+
return ENOTSUP;
107+
}
108+
#endif

libc-top-half/musl/src/thread/pthread_setcancelstate.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
int __pthread_setcancelstate(int new, int *old)
44
{
5-
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
65
if (new > 2U) return EINVAL;
76
struct pthread *self = __pthread_self();
87
if (old) *old = self->canceldisable;
98
self->canceldisable = new;
10-
#endif
119
return 0;
1210
}
1311

0 commit comments

Comments
 (0)