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

Implement a stub pthreads library for THREAD_MODEL=single #518

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
99 changes: 58 additions & 41 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -273,88 +273,93 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
)
endif

ifeq ($(THREAD_MODEL), posix)
# pthreads functions (possibly stub) for either thread model
LIBC_TOP_HALF_MUSL_SOURCES += \
$(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \
env/__init_tls.c \
stdio/__lockfile.c \
stdio/flockfile.c \
stdio/ftrylockfile.c \
stdio/funlockfile.c \
thread/__lock.c \
thread/__wait.c \
thread/__timedwait.c \
thread/default_attr.c \
thread/pthread_attr_destroy.c \
thread/pthread_attr_get.c \
thread/pthread_attr_init.c \
thread/pthread_attr_setdetachstate.c \
thread/pthread_attr_setguardsize.c \
thread/pthread_attr_setschedparam.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 \
thread/pthread_cond_signal.c \
thread/pthread_cond_timedwait.c \
thread/pthread_cond_wait.c \
thread/pthread_cleanup_push.c \
thread/pthread_condattr_destroy.c \
thread/pthread_condattr_init.c \
thread/pthread_condattr_setclock.c \
thread/pthread_condattr_setpshared.c \
thread/pthread_create.c \
thread/pthread_detach.c \
thread/pthread_equal.c \
thread/pthread_getattr_np.c \
thread/pthread_getspecific.c \
thread/pthread_join.c \
thread/pthread_key_create.c \
thread/pthread_mutex_consistent.c \
thread/pthread_mutex_destroy.c \
thread/pthread_mutex_init.c \
thread/pthread_mutex_getprioceiling.c \
thread/pthread_mutex_lock.c \
thread/pthread_mutex_timedlock.c \
thread/pthread_mutex_trylock.c \
thread/pthread_mutex_unlock.c \
thread/pthread_mutexattr_destroy.c \
thread/pthread_mutexattr_init.c \
thread/pthread_mutexattr_setprotocol.c \
thread/pthread_mutexattr_setpshared.c \
thread/pthread_mutexattr_setrobust.c \
thread/pthread_mutexattr_settype.c \
thread/pthread_once.c \
thread/pthread_rwlock_destroy.c \
thread/pthread_rwlock_init.c \
thread/pthread_rwlock_rdlock.c \
thread/pthread_rwlock_timedrdlock.c \
thread/pthread_rwlock_timedwrlock.c \
thread/pthread_rwlock_tryrdlock.c \
thread/pthread_rwlock_trywrlock.c \
thread/pthread_rwlock_unlock.c \
thread/pthread_rwlock_wrlock.c \
thread/pthread_rwlockattr_destroy.c \
thread/pthread_rwlockattr_init.c \
thread/pthread_rwlockattr_setpshared.c \
thread/pthread_self.c \
thread/pthread_setcancelstate.c \
thread/pthread_setcanceltype.c \
ArcaneNibble marked this conversation as resolved.
Show resolved Hide resolved
thread/pthread_setspecific.c \
thread/pthread_self.c \
thread/pthread_spin_destroy.c \
thread/pthread_spin_init.c \
thread/pthread_testcancel.c \
)
ifeq ($(THREAD_MODEL), posix)
# pthreads functions needed for actual thread support
LIBC_TOP_HALF_MUSL_SOURCES += \
$(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \
env/__init_tls.c \
stdio/__lockfile.c \
stdio/flockfile.c \
stdio/ftrylockfile.c \
stdio/funlockfile.c \
thread/__lock.c \
thread/__wait.c \
thread/__timedwait.c \
thread/pthread_barrier_destroy.c \
thread/pthread_barrier_init.c \
thread/pthread_barrier_wait.c \
thread/pthread_cond_broadcast.c \
thread/pthread_cond_destroy.c \
thread/pthread_cond_init.c \
thread/pthread_cond_signal.c \
thread/pthread_cond_timedwait.c \
thread/pthread_cond_wait.c \
thread/pthread_create.c \
thread/pthread_detach.c \
thread/pthread_getattr_np.c \
thread/pthread_join.c \
thread/pthread_mutex_consistent.c \
thread/pthread_mutex_getprioceiling.c \
thread/pthread_mutex_lock.c \
thread/pthread_mutex_timedlock.c \
thread/pthread_mutex_trylock.c \
thread/pthread_mutex_unlock.c \
thread/pthread_once.c \
thread/pthread_rwlock_rdlock.c \
thread/pthread_rwlock_timedrdlock.c \
thread/pthread_rwlock_timedwrlock.c \
thread/pthread_rwlock_tryrdlock.c \
thread/pthread_rwlock_trywrlock.c \
thread/pthread_rwlock_unlock.c \
thread/pthread_rwlock_wrlock.c \
thread/pthread_spin_lock.c \
thread/pthread_spin_trylock.c \
thread/pthread_spin_unlock.c \
thread/pthread_testcancel.c \
thread/sem_destroy.c \
thread/sem_getvalue.c \
thread/sem_init.c \
Expand All @@ -365,6 +370,18 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
thread/wasm32/wasi_thread_start.s \
)
endif
ifeq ($(THREAD_MODEL), single)
ArcaneNibble marked this conversation as resolved.
Show resolved Hide resolved
# pthreads stubs for single-threaded environment
LIBC_TOP_HALF_MUSL_SOURCES += \
$(addprefix $(LIBC_TOP_HALF_DIR)/stub-pthreads/, \
barrier.c \
condvar.c \
mutex.c \
rwlock.c \
spinlock.c \
stub-pthreads.c \
)
endif

MUSL_PRINTSCAN_SOURCES = \
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal/floatscan.c \
Expand Down Expand Up @@ -413,10 +430,10 @@ ifeq ($(THREAD_MODEL), posix)
# https://reviews.llvm.org/D130053).
CFLAGS += -mthread-model posix -pthread -ftls-model=local-exec
ASMFLAGS += -matomics
endif

# Include cloudlib's directory to access the structure definition of clockid_t
CFLAGS += -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
endif

ifneq ($(LTO),no)
ifeq ($(LTO),full)
Expand Down
121 changes: 121 additions & 0 deletions expected/wasm32-wasip1/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ __EINVAL
__ENOMEM
__SIG_ERR
__SIG_IGN
__acquire_ptc
__asctime_r
__assert_fail
__c_dot_utf8
Expand All @@ -34,7 +35,11 @@ __ctype_tolower_loc
__ctype_toupper_loc
__cxa_atexit
__cxa_finalize
__default_guardsize
__default_stacksize
__des_setkey
__do_cleanup_pop
__do_cleanup_push
__do_des
__duplocale
__env_rm_add
Expand Down Expand Up @@ -175,10 +180,34 @@ __pow_log_data
__powf_log2_data
__progname
__progname_full
__pthread_cond_timedwait
__pthread_create
__pthread_detach
__pthread_join
__pthread_key_create
__pthread_key_delete
__pthread_mutex_consistent
__pthread_mutex_lock
__pthread_mutex_timedlock
__pthread_mutex_trylock
__pthread_mutex_unlock
__pthread_rwlock_rdlock
__pthread_rwlock_timedrdlock
__pthread_rwlock_timedwrlock
__pthread_rwlock_tryrdlock
__pthread_rwlock_trywrlock
__pthread_rwlock_unlock
__pthread_rwlock_wrlock
__pthread_setcancelstate
__pthread_testcancel
__pthread_tsd_main
__pthread_tsd_run_dtors
__pthread_tsd_size
__putenv
__qsort_r
__rand48_step
__reallocarray
__release_ptc
__rem_pio2
__rem_pio2_large
__rem_pio2f
Expand Down Expand Up @@ -234,6 +263,9 @@ __sysv_signal
__tan
__tandf
__tanl
__testcancel
__tl_lock
__tl_unlock
__tm_to_secs
__tm_to_tzname
__tolower_l
Expand Down Expand Up @@ -330,6 +362,7 @@ __wasilibc_nocwd_symlinkat
__wasilibc_nocwd_utimensat
__wasilibc_open_nomode
__wasilibc_populate_preopens
__wasilibc_pthread_self
__wasilibc_register_preopened_fd
__wasilibc_rename_newat
__wasilibc_rename_oldat
Expand All @@ -352,6 +385,8 @@ _environ
_exit
_flushlbf
_initialize
_pthread_cleanup_pop
_pthread_cleanup_push
_start
a64l
abort
Expand Down Expand Up @@ -916,6 +951,89 @@ program_invocation_name
program_invocation_short_name
pselect
psignal
pthread_attr_destroy
pthread_attr_getdetachstate
pthread_attr_getguardsize
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
pthread_barrier_init
pthread_barrier_wait
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
pthread_condattr_setpshared
pthread_create
pthread_detach
pthread_equal
pthread_exit
pthread_getspecific
pthread_join
pthread_key_create
pthread_key_delete
pthread_mutex_consistent
pthread_mutex_destroy
pthread_mutex_init
pthread_mutex_lock
pthread_mutex_timedlock
pthread_mutex_trylock
pthread_mutex_unlock
pthread_mutexattr_destroy
pthread_mutexattr_getprotocol
pthread_mutexattr_getpshared
pthread_mutexattr_getrobust
pthread_mutexattr_gettype
pthread_mutexattr_init
pthread_mutexattr_setprotocol
pthread_mutexattr_setpshared
pthread_mutexattr_setrobust
pthread_mutexattr_settype
pthread_once
pthread_rwlock_destroy
pthread_rwlock_init
pthread_rwlock_rdlock
pthread_rwlock_timedrdlock
pthread_rwlock_timedwrlock
pthread_rwlock_tryrdlock
pthread_rwlock_trywrlock
pthread_rwlock_unlock
pthread_rwlock_wrlock
pthread_rwlockattr_destroy
pthread_rwlockattr_getpshared
pthread_rwlockattr_init
pthread_rwlockattr_setpshared
pthread_self
pthread_setcancelstate
pthread_setcanceltype
pthread_setspecific
pthread_spin_destroy
pthread_spin_init
pthread_spin_lock
pthread_spin_trylock
pthread_spin_unlock
pthread_testcancel
pthread_timedjoin_np
pthread_tryjoin_np
putc
putc_unlocked
putchar
Expand Down Expand Up @@ -1097,6 +1215,8 @@ tfind
tgamma
tgammaf
tgammal
thrd_current
thrd_equal
thrd_sleep
time
timegm
Expand All @@ -1118,6 +1238,7 @@ truncate
truncf
truncl
tsearch
tss_get
twalk
uname
ungetc
Expand Down
3 changes: 3 additions & 0 deletions expected/wasm32-wasip1/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1429,8 +1429,10 @@
#define PTHREAD_COND_INITIALIZER {{{0}}}
#define PTHREAD_CREATE_DETACHED 1
#define PTHREAD_CREATE_JOINABLE 0
#define PTHREAD_DESTRUCTOR_ITERATIONS 4
#define PTHREAD_EXPLICIT_SCHED 1
#define PTHREAD_INHERIT_SCHED 0
#define PTHREAD_KEYS_MAX 128
#define PTHREAD_MUTEX_DEFAULT 0
#define PTHREAD_MUTEX_ERRORCHECK 2
#define PTHREAD_MUTEX_INITIALIZER {{{0}}}
Expand All @@ -1448,6 +1450,7 @@
#define PTHREAD_RWLOCK_INITIALIZER {{{0}}}
#define PTHREAD_SCOPE_PROCESS 1
#define PTHREAD_SCOPE_SYSTEM 0
#define PTHREAD_STACK_MIN 2048
#define PTRBITS (sizeof(char *) * 8)
#define PTRDIFF_MAX INT32_MAX
#define PTRDIFF_MIN INT32_MIN
Expand Down
Loading