Skip to content

Commit 2057ce9

Browse files
authored
threads: implement support for unnamed semaphores (#316)
[POSIX semaphores] come in two forms: named and unnamed. Roughly, named semaphores use files to implement locking across processes; unnamed semaphores use a shared memory region to implement locking across threads in the same process. Since WASI currently has no process concept (and it is relatively unclear how to map the WASI files as shared memory), only the unnamed semaphores are supported by this changed. This means that `sem_open`, `sem_close`, and `sem_unlink` will not available to programs compiled with a threads-enabled `wasi-libc`. [POSIX semaphores]: https://man7.org/linux/man-pages/man7/sem_overview.7.html
1 parent 33c3753 commit 2057ce9

4 files changed

Lines changed: 26 additions & 1 deletion

File tree

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
192192
$(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \
193193
thread/__wait.c \
194194
thread/__timedwait.c \
195+
thread/pthread_cleanup_push.c \
195196
thread/pthread_mutex_consistent.c \
196197
thread/pthread_mutex_destroy.c \
197198
thread/pthread_mutex_init.c \
@@ -206,6 +207,14 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
206207
thread/pthread_mutexattr_setpshared.c \
207208
thread/pthread_mutexattr_setrobust.c \
208209
thread/pthread_mutexattr_settype.c \
210+
thread/pthread_testcancel.c \
211+
thread/sem_destroy.c \
212+
thread/sem_getvalue.c \
213+
thread/sem_init.c \
214+
thread/sem_post.c \
215+
thread/sem_timedwait.c \
216+
thread/sem_trywait.c \
217+
thread/sem_wait.c \
209218
thread/pthread_setcancelstate.c \
210219
)
211220
endif

expected/wasm32-wasi/posix/defined-symbols.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ __ctype_toupper_loc
3838
__cxa_atexit
3939
__cxa_finalize
4040
__des_setkey
41+
__do_cleanup_pop
42+
__do_cleanup_push
4143
__do_des
4244
__duplocale
4345
__env_rm_add
@@ -185,6 +187,7 @@ __pthread_mutex_trylock
185187
__pthread_mutex_trylock_owner
186188
__pthread_mutex_unlock
187189
__pthread_setcancelstate
190+
__pthread_testcancel
188191
__putenv
189192
__qsort_r
190193
__rand48_step
@@ -243,6 +246,7 @@ __sysv_signal
243246
__tan
244247
__tandf
245248
__tanl
249+
__testcancel
246250
__timedwait
247251
__timedwait_cp
248252
__tm_to_secs
@@ -365,6 +369,8 @@ _environ
365369
_exit
366370
_flushlbf
367371
_initialize
372+
_pthread_cleanup_pop
373+
_pthread_cleanup_push
368374
_start
369375
a64l
370376
abort
@@ -939,6 +945,7 @@ pthread_mutexattr_setpshared
939945
pthread_mutexattr_setrobust
940946
pthread_mutexattr_settype
941947
pthread_setcancelstate
948+
pthread_testcancel
942949
putc
943950
putc_unlocked
944951
putchar
@@ -1007,6 +1014,13 @@ sched_yield
10071014
seed48
10081015
seekdir
10091016
select
1017+
sem_destroy
1018+
sem_getvalue
1019+
sem_init
1020+
sem_post
1021+
sem_timedwait
1022+
sem_trywait
1023+
sem_wait
10101024
send
10111025
setbuf
10121026
setbuffer

expected/wasm32-wasi/posix/predefined-macros.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,8 @@
15801580
#define SEEK_SET __WASI_WHENCE_SET
15811581
#define SEGSIZE 512
15821582
#define SEM_FAILED ((sem_t *)0)
1583+
#define SEM_NSEMS_MAX 256
1584+
#define SEM_VALUE_MAX 0x7fffffff
15831585
#define SERVFAIL ns_r_servfail
15841586
#define SHORTBITS (sizeof(short) * 8)
15851587
#define SHRT_MAX 0x7fff

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
#define PTHREAD_STACK_MIN 2048
7171
#define PTHREAD_DESTRUCTOR_ITERATIONS 4
7272
#endif
73-
#ifdef __wasilibc_unmodified_upstream /* WASI has no semaphores */
73+
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
7474
#define SEM_VALUE_MAX 0x7fffffff
7575
#define SEM_NSEMS_MAX 256
7676
#endif

0 commit comments

Comments
 (0)