Skip to content

Commit 8aee1e6

Browse files
committed
Add emulated thread-creation-related functions
These functions always fail
1 parent afe21ef commit 8aee1e6

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ LIBWASI_EMULATED_SIGNAL_SOURCES = \
143143
LIBWASI_EMULATED_SIGNAL_MUSL_SOURCES = \
144144
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/signal/psignal.c \
145145
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/strsignal.c
146+
LIBWASI_EMULATED_PTHREAD_SOURCES = \
147+
$(STUB_PTHREADS_DIR)/stub-pthreads-emulated.c
146148
LIBDL_SOURCES = $(LIBC_TOP_HALF_MUSL_SRC_DIR)/misc/dl.c
147149
LIBSETJMP_SOURCES = $(LIBC_TOP_HALF_MUSL_SRC_DIR)/setjmp/wasm32/rt.c
148150
LIBC_BOTTOM_HALF_CRT_SOURCES = $(wildcard $(LIBC_BOTTOM_HALF_DIR)/crt/*.c)
@@ -500,6 +502,7 @@ LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS = $(call objs,$(LIBWASI_EMULATED_PROCESS_CL
500502
LIBWASI_EMULATED_GETPID_OBJS = $(call objs,$(LIBWASI_EMULATED_GETPID_SOURCES))
501503
LIBWASI_EMULATED_SIGNAL_OBJS = $(call objs,$(LIBWASI_EMULATED_SIGNAL_SOURCES))
502504
LIBWASI_EMULATED_SIGNAL_MUSL_OBJS = $(call objs,$(LIBWASI_EMULATED_SIGNAL_MUSL_SOURCES))
505+
LIBWASI_EMULATED_PTHREAD_OBJS = $(call objs,$(LIBWASI_EMULATED_PTHREAD_SOURCES))
503506
LIBDL_OBJS = $(call objs,$(LIBDL_SOURCES))
504507
LIBSETJMP_OBJS = $(call objs,$(LIBSETJMP_SOURCES))
505508
LIBC_BOTTOM_HALF_CRT_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_CRT_SOURCES))
@@ -603,6 +606,7 @@ LIBWASI_EMULATED_PROCESS_CLOCKS_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULA
603606
LIBWASI_EMULATED_GETPID_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_GETPID_OBJS))
604607
LIBWASI_EMULATED_SIGNAL_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_SIGNAL_OBJS))
605608
LIBWASI_EMULATED_SIGNAL_MUSL_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS))
609+
LIBWASI_EMULATED_PTHREAD_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBWASI_EMULATED_PTHREAD_OBJS))
606610
LIBDL_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBDL_OBJS))
607611
LIBSETJMP_SO_OBJS = $(patsubst %.o,%.pic.o,$(LIBSETJMP_OBJS))
608612
BULK_MEMORY_SO_OBJS = $(patsubst %.o,%.pic.o,$(BULK_MEMORY_OBJS))
@@ -618,6 +622,7 @@ PIC_OBJS = \
618622
$(LIBWASI_EMULATED_GETPID_SO_OBJS) \
619623
$(LIBWASI_EMULATED_SIGNAL_SO_OBJS) \
620624
$(LIBWASI_EMULATED_SIGNAL_MUSL_SO_OBJS) \
625+
$(LIBWASI_EMULATED_PTHREAD_SO_OBJS) \
621626
$(LIBDL_SO_OBJS) \
622627
$(LIBSETJMP_SO_OBJS) \
623628
$(BULK_MEMORY_SO_OBJS) \
@@ -659,6 +664,8 @@ $(OBJDIR)/libwasi-emulated-getpid.so.a: $(LIBWASI_EMULATED_GETPID_SO_OBJS)
659664

660665
$(OBJDIR)/libwasi-emulated-signal.so.a: $(LIBWASI_EMULATED_SIGNAL_SO_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_SO_OBJS)
661666

667+
$(OBJDIR)/libwasi-emulated-pthread.so.a: $(LIBWASI_EMULATED_PTHREAD_SO_OBJS)
668+
662669
$(OBJDIR)/libdl.so.a: $(LIBDL_SO_OBJS)
663670

664671
$(OBJDIR)/libsetjmp.so.a: $(LIBSETJMP_SO_OBJS)
@@ -677,6 +684,8 @@ $(SYSROOT_LIB)/libwasi-emulated-getpid.a: $(LIBWASI_EMULATED_GETPID_OBJS)
677684

678685
$(SYSROOT_LIB)/libwasi-emulated-signal.a: $(LIBWASI_EMULATED_SIGNAL_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS)
679686

687+
$(SYSROOT_LIB)/libwasi-emulated-pthread.a: $(LIBWASI_EMULATED_PTHREAD_OBJS)
688+
680689
$(SYSROOT_LIB)/libdl.a: $(LIBDL_OBJS)
681690

682691
$(SYSROOT_LIB)/libsetjmp.a: $(LIBSETJMP_OBJS)
@@ -775,6 +784,11 @@ $(LIBC_TOP_HALF_ALL_OBJS) $(LIBC_TOP_HALF_ALL_SO_OBJS) $(MUSL_PRINTSCAN_LONG_DOU
775784
$(LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS) $(LIBWASI_EMULATED_PROCESS_CLOCKS_SO_OBJS): CFLAGS += \
776785
-I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
777786

787+
$(LIBWASI_EMULATED_PTHREAD_OBJS) $(LIBWASI_EMULATED_PTHREAD_SO_OBJS): CFLAGS += \
788+
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \
789+
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal \
790+
-I$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32
791+
778792
# emmalloc uses a lot of pointer type-punning, which is UB under strict aliasing,
779793
# and this was found to have real miscompilations in wasi-libc#421.
780794
$(EMMALLOC_OBJS): CFLAGS += \
@@ -824,6 +838,7 @@ LIBC_SO = \
824838
$(SYSROOT_LIB)/libwasi-emulated-process-clocks.so \
825839
$(SYSROOT_LIB)/libwasi-emulated-getpid.so \
826840
$(SYSROOT_LIB)/libwasi-emulated-signal.so \
841+
$(SYSROOT_LIB)/libwasi-emulated-pthread.so \
827842
$(SYSROOT_LIB)/libdl.so
828843
ifeq ($(BUILD_LIBSETJMP),yes)
829844
LIBC_SO += \
@@ -842,6 +857,10 @@ STATIC_LIBS = \
842857
$(SYSROOT_LIB)/libwasi-emulated-getpid.a \
843858
$(SYSROOT_LIB)/libwasi-emulated-signal.a \
844859
$(SYSROOT_LIB)/libdl.a
860+
ifneq ($(THREAD_MODEL), posix)
861+
STATIC_LIBS += \
862+
$(SYSROOT_LIB)/libwasi-emulated-pthread.a
863+
endif
845864
ifeq ($(BUILD_LIBSETJMP),yes)
846865
STATIC_LIBS += \
847866
$(SYSROOT_LIB)/libsetjmp.a

expected/wasm32-wasip1/defined-symbols.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ __powf_log2_data
181181
__progname
182182
__progname_full
183183
__pthread_cond_timedwait
184+
__pthread_create
185+
__pthread_detach
186+
__pthread_join
184187
__pthread_key_create
185188
__pthread_key_delete
186189
__pthread_mutex_consistent
@@ -980,9 +983,12 @@ pthread_condattr_getpshared
980983
pthread_condattr_init
981984
pthread_condattr_setclock
982985
pthread_condattr_setpshared
986+
pthread_create
987+
pthread_detach
983988
pthread_equal
984989
pthread_exit
985990
pthread_getspecific
991+
pthread_join
986992
pthread_key_create
987993
pthread_key_delete
988994
pthread_mutex_consistent
@@ -1026,6 +1032,8 @@ pthread_spin_lock
10261032
pthread_spin_trylock
10271033
pthread_spin_unlock
10281034
pthread_testcancel
1035+
pthread_timedjoin_np
1036+
pthread_tryjoin_np
10291037
putc
10301038
putc_unlocked
10311039
putchar

expected/wasm32-wasip2/defined-symbols.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ __powf_log2_data
184184
__progname
185185
__progname_full
186186
__pthread_cond_timedwait
187+
__pthread_create
188+
__pthread_detach
189+
__pthread_join
187190
__pthread_key_create
188191
__pthread_key_delete
189192
__pthread_mutex_consistent
@@ -1113,9 +1116,12 @@ pthread_condattr_getpshared
11131116
pthread_condattr_init
11141117
pthread_condattr_setclock
11151118
pthread_condattr_setpshared
1119+
pthread_create
1120+
pthread_detach
11161121
pthread_equal
11171122
pthread_exit
11181123
pthread_getspecific
1124+
pthread_join
11191125
pthread_key_create
11201126
pthread_key_delete
11211127
pthread_mutex_consistent
@@ -1159,6 +1165,8 @@ pthread_spin_lock
11591165
pthread_spin_trylock
11601166
pthread_spin_unlock
11611167
pthread_testcancel
1168+
pthread_timedjoin_np
1169+
pthread_tryjoin_np
11621170
putc
11631171
putc_unlocked
11641172
putchar
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// This file is linked into wasi-emulated-pthread
2+
3+
#include "pthread_impl.h"
4+
5+
int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg)
6+
{
7+
/*
8+
"The system lacked the necessary resources to create another thread,
9+
or the system-imposed limit on the total number of threads in a process
10+
{PTHREAD_THREADS_MAX} would be exceeded."
11+
*/
12+
return EAGAIN;
13+
}
14+
weak_alias(__pthread_create, pthread_create);
15+
int __pthread_detach(pthread_t t)
16+
{
17+
/*
18+
If we are the only thread, when we exit the whole process exits.
19+
So the storage will be reclaimed no matter what.
20+
*/
21+
return 0;
22+
}
23+
weak_alias(__pthread_detach, pthread_detach);
24+
int __pthread_join(pthread_t t, void **res)
25+
{
26+
/*
27+
"The behavior is undefined if the value specified by the thread argument
28+
to pthread_join() refers to the calling thread."
29+
*/
30+
return 0;
31+
}
32+
weak_alias(__pthread_join, pthread_join);
33+
int pthread_tryjoin_np(pthread_t t, void **res)
34+
{
35+
return 0;
36+
}
37+
int pthread_timedjoin_np(pthread_t t, void **res, const struct timespec *at)
38+
{
39+
return 0;
40+
}

0 commit comments

Comments
 (0)