Skip to content

Commit 1440cae

Browse files
committed
Refactor stub pthread library and enable unconditionally.
This commit changes the layout of the stub pthread library to match that of libc-top-half: splits it into (mostly) one file per function, and ensures the same API is exposed from both libraries, adding stub functions as necessary. This commit also removes `-lwasi-emulated-pthread` as well as `-D_WASI_EMULATED_PTHREAD` and enables this functionality unconditionally, seeing as this is necessary for building libc++ with threading enabled. It adds a flag `-D_WASI_STRICT_PTHREAD` that causes thread creation functions (`pthread_create`, `pthread_detach`, `pthread_join`) to be defined as a macro causing a compile error, which can be used to locate the parts of a codebase requiring changes for the single thread model. Specifically, after this commit, the two targets `wasm32-wasip1` and `wasm32-wasip1-threads` differ as follows (cleaned up for clarity): --- expected/wasm32-wasip1/defined-symbols.txt +++ expected/wasm32-wasip1-threads/defined-symbols.txt +flockfile +ftrylockfile +funlockfile +sem_destroy +sem_getvalue +sem_init +sem_post +sem_timedwait +sem_trywait +sem_wait +thrd_detach +wasi_thread_start --- expected/wasm32-wasip1/predefined-macros.txt +++ expected/wasm32-wasip1-threads/predefined-macros.txt +#define _REENTRANT 1 +#define SEM_NSEMS_MAX 256 +#define SEM_VALUE_MAX 0x7fffffff +#define __wasm_atomics__ 1 +#define __wasm_bulk_memory__ 1
1 parent 50ae119 commit 1440cae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1023
-1007
lines changed

Makefile

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ DLMALLOC_SOURCES = $(DLMALLOC_SRC_DIR)/dlmalloc.c
7171
DLMALLOC_INC = $(DLMALLOC_DIR)/include
7272
EMMALLOC_DIR = emmalloc
7373
EMMALLOC_SOURCES = $(EMMALLOC_DIR)/emmalloc.c
74-
STUB_PTHREADS_DIR = stub-pthreads
74+
THREAD_STUB_DIR = thread-stub
7575
LIBC_BOTTOM_HALF_DIR = libc-bottom-half
7676
LIBC_BOTTOM_HALF_CLOUDLIBC_SRC = $(LIBC_BOTTOM_HALF_DIR)/cloudlibc/src
7777
LIBC_BOTTOM_HALF_CLOUDLIBC_SRC_INC = $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)/include
@@ -138,8 +138,6 @@ LIBWASI_EMULATED_SIGNAL_SOURCES = \
138138
LIBWASI_EMULATED_SIGNAL_MUSL_SOURCES = \
139139
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/signal/psignal.c \
140140
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/strsignal.c
141-
LIBWASI_EMULATED_PTHREAD_SOURCES = \
142-
$(STUB_PTHREADS_DIR)/stub-pthreads-emulated.c
143141
LIBDL_SOURCES = $(LIBC_TOP_HALF_MUSL_SRC_DIR)/misc/dl.c
144142
LIBSETJMP_SOURCES = $(LIBC_TOP_HALF_MUSL_SRC_DIR)/setjmp/wasm32/rt.c
145143
LIBC_BOTTOM_HALF_CRT_SOURCES = $(wildcard $(LIBC_BOTTOM_HALF_DIR)/crt/*.c)
@@ -373,12 +371,38 @@ endif
373371
ifeq ($(THREAD_MODEL), single)
374372
# pthreads stubs for single-threaded environment
375373
LIBC_TOP_HALF_MUSL_SOURCES += \
376-
$(STUB_PTHREADS_DIR)/barrier.c \
377-
$(STUB_PTHREADS_DIR)/condvar.c \
378-
$(STUB_PTHREADS_DIR)/mutex.c \
379-
$(STUB_PTHREADS_DIR)/rwlock.c \
380-
$(STUB_PTHREADS_DIR)/spinlock.c \
381-
$(STUB_PTHREADS_DIR)/stub-pthreads-good.c
374+
$(addprefix $(THREAD_STUB_DIR)/, \
375+
pthread_barrier_destroy.c \
376+
pthread_barrier_init.c \
377+
pthread_barrier_wait.c \
378+
pthread_cond_broadcast.c \
379+
pthread_cond_destroy.c \
380+
pthread_cond_init.c \
381+
pthread_cond_signal.c \
382+
pthread_cond_timedwait.c \
383+
pthread_cond_wait.c \
384+
pthread_create.c \
385+
pthread_detach.c \
386+
pthread_getattr_np.c \
387+
pthread_join.c \
388+
pthread_mutex_consistent.c \
389+
pthread_mutex_getprioceiling.c \
390+
pthread_mutex_lock.c \
391+
pthread_mutex_timedlock.c \
392+
pthread_mutex_trylock.c \
393+
pthread_mutex_unlock.c \
394+
pthread_once.c \
395+
pthread_rwlock_rdlock.c \
396+
pthread_rwlock_timedrdlock.c \
397+
pthread_rwlock_timedwrlock.c \
398+
pthread_rwlock_tryrdlock.c \
399+
pthread_rwlock_trywrlock.c \
400+
pthread_rwlock_unlock.c \
401+
pthread_rwlock_wrlock.c \
402+
pthread_spin_lock.c \
403+
pthread_spin_trylock.c \
404+
pthread_spin_unlock.c \
405+
)
382406
endif
383407

384408
MUSL_PRINTSCAN_SOURCES = \
@@ -505,7 +529,6 @@ LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS = $(call objs,$(LIBWASI_EMULATED_PROCESS_CL
505529
LIBWASI_EMULATED_GETPID_OBJS = $(call objs,$(LIBWASI_EMULATED_GETPID_SOURCES))
506530
LIBWASI_EMULATED_SIGNAL_OBJS = $(call objs,$(LIBWASI_EMULATED_SIGNAL_SOURCES))
507531
LIBWASI_EMULATED_SIGNAL_MUSL_OBJS = $(call objs,$(LIBWASI_EMULATED_SIGNAL_MUSL_SOURCES))
508-
LIBWASI_EMULATED_PTHREAD_OBJS = $(call objs,$(LIBWASI_EMULATED_PTHREAD_SOURCES))
509532
LIBDL_OBJS = $(call objs,$(LIBDL_SOURCES))
510533
LIBSETJMP_OBJS = $(call objs,$(LIBSETJMP_SOURCES))
511534
LIBC_BOTTOM_HALF_CRT_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_CRT_SOURCES))
@@ -529,7 +552,6 @@ LIBWASI_EMULATED_PROCESS_CLOCKS_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULA
529552
LIBWASI_EMULATED_GETPID_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_GETPID_OBJS))
530553
LIBWASI_EMULATED_SIGNAL_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_SIGNAL_OBJS))
531554
LIBWASI_EMULATED_SIGNAL_MUSL_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS))
532-
LIBWASI_EMULATED_PTHREAD_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_PTHREAD_OBJS))
533555
LIBDL_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBDL_OBJS))
534556
LIBSETJMP_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBSETJMP_OBJS))
535557
BULK_MEMORY_SO_OBJS = $(patsubst %.o,%.pic.o,$(BULK_MEMORY_OBJS))
@@ -546,7 +568,6 @@ PIC_OBJS = \
546568
$(LIBWASI_EMULATED_GETPID_SO_OBJS) \
547569
$(LIBWASI_EMULATED_SIGNAL_SO_OBJS) \
548570
$(LIBWASI_EMULATED_SIGNAL_MUSL_SO_OBJS) \
549-
$(LIBWASI_EMULATED_PTHREAD_SO_OBJS) \
550571
$(LIBDL_SO_OBJS) \
551572
$(LIBSETJMP_SO_OBJS) \
552573
$(BULK_MEMORY_SO_OBJS) \
@@ -646,8 +667,6 @@ $(OBJDIR)/libwasi-emulated-getpid.so.a: $(LIBWASI_EMULATED_GETPID_SO_OBJS)
646667

647668
$(OBJDIR)/libwasi-emulated-signal.so.a: $(LIBWASI_EMULATED_SIGNAL_SO_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_SO_OBJS)
648669

649-
$(OBJDIR)/libwasi-emulated-pthread.so.a: $(LIBWASI_EMULATED_PTHREAD_SO_OBJS)
650-
651670
$(OBJDIR)/libdl.so.a: $(LIBDL_SO_OBJS)
652671

653672
$(OBJDIR)/libsetjmp.so.a: $(LIBSETJMP_SO_OBJS)
@@ -666,8 +685,6 @@ $(SYSROOT_LIB)/libwasi-emulated-getpid.a: $(LIBWASI_EMULATED_GETPID_OBJS)
666685

667686
$(SYSROOT_LIB)/libwasi-emulated-signal.a: $(LIBWASI_EMULATED_SIGNAL_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS)
668687

669-
$(SYSROOT_LIB)/libwasi-emulated-pthread.a: $(LIBWASI_EMULATED_PTHREAD_OBJS)
670-
671688
$(SYSROOT_LIB)/libdl.a: $(LIBDL_OBJS)
672689

673690
$(SYSROOT_LIB)/libsetjmp.a: $(LIBSETJMP_OBJS)
@@ -770,12 +787,6 @@ $(FTS_OBJS) $(FTS_SO_OBJS): CFLAGS += \
770787
$(LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS) $(LIBWASI_EMULATED_PROCESS_CLOCKS_SO_OBJS): CFLAGS += \
771788
-I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
772789

773-
$(LIBWASI_EMULATED_PTHREAD_OBJS) $(LIBWASI_EMULATED_PTHREAD_SO_OBJS): CFLAGS += \
774-
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \
775-
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal \
776-
-I$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32 \
777-
-D_WASI_EMULATED_PTHREAD
778-
779790
# emmalloc uses a lot of pointer type-punning, which is UB under strict aliasing,
780791
# and this was found to have real miscompilations in wasi-libc#421.
781792
$(EMMALLOC_OBJS): CFLAGS += \
@@ -816,7 +827,6 @@ LIBC_SO = \
816827
$(SYSROOT_LIB)/libwasi-emulated-process-clocks.so \
817828
$(SYSROOT_LIB)/libwasi-emulated-getpid.so \
818829
$(SYSROOT_LIB)/libwasi-emulated-signal.so \
819-
$(SYSROOT_LIB)/libwasi-emulated-pthread.so \
820830
$(SYSROOT_LIB)/libdl.so
821831
ifeq ($(BUILD_LIBSETJMP),yes)
822832
LIBC_SO += \
@@ -835,10 +845,6 @@ STATIC_LIBS = \
835845
$(SYSROOT_LIB)/libwasi-emulated-getpid.a \
836846
$(SYSROOT_LIB)/libwasi-emulated-signal.a \
837847
$(SYSROOT_LIB)/libdl.a
838-
ifneq ($(THREAD_MODEL), posix)
839-
STATIC_LIBS += \
840-
$(SYSROOT_LIB)/libwasi-emulated-pthread.a
841-
endif
842848
ifeq ($(BUILD_LIBSETJMP),yes)
843849
STATIC_LIBS += \
844850
$(SYSROOT_LIB)/libsetjmp.a

expected/wasm32-wasip1/defined-symbols.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,14 @@ __progname
182182
__progname_full
183183
__pthread_cond_timedwait
184184
__pthread_create
185-
__pthread_detach
186185
__pthread_join
187186
__pthread_key_create
188187
__pthread_key_delete
189-
__pthread_mutex_consistent
190188
__pthread_mutex_lock
191189
__pthread_mutex_timedlock
192190
__pthread_mutex_trylock
193191
__pthread_mutex_unlock
192+
__pthread_once
194193
__pthread_rwlock_rdlock
195194
__pthread_rwlock_timedrdlock
196195
__pthread_rwlock_timedwrlock
@@ -991,13 +990,14 @@ pthread_condattr_setpshared
991990
pthread_create
992991
pthread_detach
993992
pthread_equal
994-
pthread_exit
993+
pthread_getattr_np
995994
pthread_getspecific
996995
pthread_join
997996
pthread_key_create
998997
pthread_key_delete
999998
pthread_mutex_consistent
1000999
pthread_mutex_destroy
1000+
pthread_mutex_getprioceiling
10011001
pthread_mutex_init
10021002
pthread_mutex_lock
10031003
pthread_mutex_timedlock

0 commit comments

Comments
 (0)