wasi-libc/stub-pthreads/mutex.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

68 lines
1.6 KiB
C

#include "pthread_impl.h"
/*
Musl mutex (FYI)
_m_type:
b[7] - process shared
b[3] - priority inherit
b[2] - robust
b[1:0] - type
0 - normal
1 - recursive
2 - errorcheck
_m_lock:
b[30] - owner dead, if robust
b[29:0] - tid, if not normal
b[4] - locked?, if normal
*/
int __pthread_mutex_consistent(pthread_mutex_t *m)
{
/* cannot be a robust mutex, as they're entirely unsupported in WASI */
return EINVAL;
}
weak_alias(__pthread_mutex_consistent, pthread_mutex_consistent);
int __pthread_mutex_lock(pthread_mutex_t *m)
{
if (m->_m_type&3 != PTHREAD_MUTEX_RECURSIVE) {
if (m->_m_count) return EDEADLK;
m->_m_count = 1;
} else {
if ((unsigned)m->_m_count >= INT_MAX) return EAGAIN;
m->_m_count++;
}
return 0;
}
weak_alias(__pthread_mutex_lock, pthread_mutex_lock);
int __pthread_mutex_trylock(pthread_mutex_t *m)
{
if (m->_m_type&3 != PTHREAD_MUTEX_RECURSIVE) {
if (m->_m_count) return EBUSY;
m->_m_count = 1;
} else {
if ((unsigned)m->_m_count >= INT_MAX) return EAGAIN;
m->_m_count++;
}
return 0;
}
weak_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
int __pthread_mutex_unlock(pthread_mutex_t *m)
{
if (!m->_m_count) return EPERM;
m->_m_count--;
return 0;
}
weak_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at)
{
/* "The pthread_mutex_timedlock() function may fail if: A deadlock condition was detected." */
/* This means that we don't have to wait and then return timeout, we can just detect deadlock. */
return pthread_mutex_lock(m);
}
weak_alias(__pthread_mutex_timedlock, pthread_mutex_timedlock);