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

61 lines
1.6 KiB
C

#include "pthread_impl.h"
/* Musl uses bit31 to mark "has waiters", bit[30:0] all 1s to indicate writer */
/* These functions have the __ prefix to help stub out thread-specific data */
int __pthread_rwlock_rdlock(pthread_rwlock_t *rw)
{
if (rw->_rw_lock == 0x7fffffff) return EDEADLK;
if (rw->_rw_lock == 0x7ffffffe) return EAGAIN;
rw->_rw_lock++;
return 0;
}
weak_alias(__pthread_rwlock_rdlock, pthread_rwlock_rdlock);
int __pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)
{
return pthread_rwlock_rdlock(rw);
}
weak_alias(__pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock);
int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rw)
{
if (rw->_rw_lock == 0x7fffffff) return EBUSY;
if (rw->_rw_lock == 0x7ffffffe) return EAGAIN;
rw->_rw_lock++;
return 0;
}
weak_alias(__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
int __pthread_rwlock_wrlock(pthread_rwlock_t *rw)
{
if (rw->_rw_lock) return EDEADLK;
rw->_rw_lock = 0x7fffffff;
return 0;
}
weak_alias(__pthread_rwlock_wrlock, pthread_rwlock_wrlock);
int __pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)
{
return pthread_rwlock_wrlock(rw);
}
weak_alias(__pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock);
int __pthread_rwlock_trywrlock(pthread_rwlock_t *rw)
{
if (rw->_rw_lock) return EBUSY;
rw->_rw_lock = 0x7fffffff;
return 0;
}
weak_alias(__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock);
int __pthread_rwlock_unlock(pthread_rwlock_t *rw)
{
if (rw->_rw_lock == 0x7fffffff)
rw->_rw_lock = 0;
else
rw->_rw_lock--;
return 0;
}
weak_alias(__pthread_rwlock_unlock, pthread_rwlock_unlock);