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

Merged
merged 13 commits into from
Oct 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a stub implementation of condvars
ArcaneNibble committed Oct 10, 2024
commit 7d6fc8f43c7b0107a863e2bc7509eeafce3a30c0
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -290,6 +290,10 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
thread/pthread_barrierattr_destroy.c \
thread/pthread_barrierattr_init.c \
thread/pthread_barrierattr_setpshared.c \
thread/pthread_condattr_destroy.c \
thread/pthread_condattr_init.c \
thread/pthread_condattr_setclock.c \
thread/pthread_condattr_setpshared.c \
thread/pthread_mutex_destroy.c \
thread/pthread_mutex_init.c \
thread/pthread_mutexattr_destroy.c \
@@ -322,10 +326,6 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
thread/pthread_cond_signal.c \
thread/pthread_cond_timedwait.c \
thread/pthread_cond_wait.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 \
@@ -376,6 +376,7 @@ ifeq ($(THREAD_MODEL), single)
# pthreads stubs for single-threaded environment
LIBC_TOP_HALF_MUSL_SOURCES += \
$(STUB_PTHREADS_DIR)/barrier.c \
$(STUB_PTHREADS_DIR)/condvar.c \
$(STUB_PTHREADS_DIR)/mutex.c \
$(STUB_PTHREADS_DIR)/stub-pthreads-good.c
endif
11 changes: 11 additions & 0 deletions expected/wasm32-wasip1/defined-symbols.txt
Original file line number Diff line number Diff line change
@@ -178,6 +178,7 @@ __pow_log_data
__powf_log2_data
__progname
__progname_full
__pthread_cond_timedwait
__pthread_mutex_consistent
__pthread_mutex_lock
__pthread_mutex_timedlock
@@ -949,8 +950,18 @@ pthread_barrierattr_destroy
pthread_barrierattr_getpshared
pthread_barrierattr_init
pthread_barrierattr_setpshared
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_mutex_consistent
pthread_mutex_destroy
pthread_mutex_init
11 changes: 11 additions & 0 deletions expected/wasm32-wasip2/defined-symbols.txt
Original file line number Diff line number Diff line change
@@ -181,6 +181,7 @@ __pow_log_data
__powf_log2_data
__progname
__progname_full
__pthread_cond_timedwait
__pthread_mutex_consistent
__pthread_mutex_lock
__pthread_mutex_timedlock
@@ -1082,8 +1083,18 @@ pthread_barrierattr_destroy
pthread_barrierattr_getpshared
pthread_barrierattr_init
pthread_barrierattr_setpshared
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_mutex_consistent
pthread_mutex_destroy
pthread_mutex_init
36 changes: 36 additions & 0 deletions stub-pthreads/condvar.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "pthread_impl.h"
#include <time.h>

int pthread_cond_init(pthread_cond_t *restrict c, const pthread_condattr_t *restrict a)
{
return 0;
}
int pthread_cond_destroy(pthread_cond_t *c)
{
return 0;
}
int pthread_cond_broadcast(pthread_cond_t *c)
{
return 0;
}
int pthread_cond_signal(pthread_cond_t *c)
{
return 0;
}
int pthread_cond_wait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m)
{
/* Because there is no other thread that can signal us, this is a deadlock immediately.
The other possible choice is to return immediately (spurious wakeup), but that is likely to
just result in the program spinning forever on a condition that cannot become true. */
__builtin_trap();
}
int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts)
{
/* Error check mutexes must detect if they're not locked (UB for others) */
if (!m->_m_count) return EPERM;
int ret = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, ts, 0);
if (ret == 0) return ETIMEDOUT;
if (ret != EINTR) return ret;
return 0;
}
weak_alias(__pthread_cond_timedwait, pthread_cond_timedwait);