mirror of
https://git.proxmox.com/git/wasi-libc
synced 2025-06-06 11:17:47 +00:00

~~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
37 lines
1.1 KiB
C
37 lines
1.1 KiB
C
#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);
|