wasi-libc/stub-pthreads/stub-pthreads-emulated.c
R a05277a680
Implement a stub pthreads library for THREAD_MODEL=single (#518)
~~This patch series first starts with a number of commits stubbing out
functions in the existing `THREAD_model=posix` code. According to "The
Open Group Base Specifications Issue 7, 2018 edition", there are a
number of mandatory functions which have not been provided. There are
also some optional functions that have been partially provided in a
not-useful way (e.g. get but no set function). For these, I have chosen
to clean them up and remove the get functions for consistency.~~ EDIT:
These have been split off into separate PRs and merged.

The remainder of the patches then build up a stub implementation of
pthreads for `THREAD_MODEL=single`. I have done my best to try to make
sure that all functions are as conforming as possible (under the
assumption that another thread cannot ever be launched). This means that
objects such as mutexes and rwlocks actually do update their state and
will correctly fail when locks cannot be acquired.

When an inevitable deadlock occurs, I have chosen to return EDEADLK when
it has been explicitly listed as a permissible return value, and to
invoke `__builtin_trap` otherwise.

I have tested this by rebuilding libc++ with threads enabled and then
smoke-testing Clang/LLVM-on-WASI to make sure that it can compile a
simple program. I have not run any more-extensive conformance testing.

Fixes #501
2024-10-10 09:27:48 -07:00

41 lines
1.1 KiB
C

// This file is linked into wasi-emulated-pthread
#include "pthread_impl.h"
int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg)
{
/*
"The system lacked the necessary resources to create another thread,
or the system-imposed limit on the total number of threads in a process
{PTHREAD_THREADS_MAX} would be exceeded."
*/
return EAGAIN;
}
weak_alias(__pthread_create, pthread_create);
int __pthread_detach(pthread_t t)
{
/*
If we are the only thread, when we exit the whole process exits.
So the storage will be reclaimed no matter what.
*/
return 0;
}
weak_alias(__pthread_detach, pthread_detach);
int __pthread_join(pthread_t t, void **res)
{
/*
"The behavior is undefined if the value specified by the thread argument
to pthread_join() refers to the calling thread."
*/
return 0;
}
weak_alias(__pthread_join, pthread_join);
int pthread_tryjoin_np(pthread_t t, void **res)
{
return 0;
}
int pthread_timedjoin_np(pthread_t t, void **res, const struct timespec *at)
{
return 0;
}