mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-26 22:38:30 +00:00
Support process shared condition variables
This commit is contained in:
parent
3de6632be6
commit
0a9a6f86bb
14 changed files with 168 additions and 19 deletions
|
@ -16,14 +16,61 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/cp.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/thread/lock.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/thread2.h"
|
||||
#include "third_party/nsync/common.internal.h"
|
||||
#include "third_party/nsync/cv.h"
|
||||
#include "third_party/nsync/futex.internal.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
|
||||
struct PthreadWait {
|
||||
pthread_cond_t *cond;
|
||||
pthread_mutex_t *mutex;
|
||||
};
|
||||
|
||||
static void pthread_cond_leave(void *arg) {
|
||||
struct PthreadWait *wait = (struct PthreadWait *)arg;
|
||||
if (pthread_mutex_lock(wait->mutex))
|
||||
__builtin_trap();
|
||||
atomic_fetch_sub_explicit(&wait->cond->_waiters, 1, memory_order_acq_rel);
|
||||
}
|
||||
|
||||
static errno_t pthread_cond_timedwait_impl(pthread_cond_t *cond,
|
||||
pthread_mutex_t *mutex,
|
||||
const struct timespec *abstime) {
|
||||
|
||||
// this is a cancelation point
|
||||
// check the cancelation status before we begin waiting
|
||||
if (pthread_testcancel_np() == ECANCELED)
|
||||
return ECANCELED;
|
||||
|
||||
// get original monotonic sequence while lock is held
|
||||
uint32_t seq1 = atomic_load_explicit(&cond->_sequence, memory_order_relaxed);
|
||||
|
||||
// start waiting on condition variable
|
||||
atomic_fetch_add_explicit(&cond->_waiters, 1, memory_order_acq_rel);
|
||||
if (pthread_mutex_unlock(mutex))
|
||||
__builtin_trap();
|
||||
|
||||
// wait for sequence change, timeout, or cancelation
|
||||
int rc;
|
||||
struct PthreadWait waiter = {cond, mutex};
|
||||
pthread_cleanup_push(pthread_cond_leave, &waiter);
|
||||
rc = nsync_futex_wait_((atomic_int *)&cond->_sequence, seq1, cond->_pshared,
|
||||
abstime);
|
||||
pthread_cleanup_pop(true);
|
||||
if (rc == -EAGAIN)
|
||||
rc = 0;
|
||||
|
||||
// turn linux syscall status into posix errno
|
||||
return -rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for condition with optional time limit, e.g.
|
||||
*
|
||||
|
@ -49,11 +96,39 @@
|
|||
*/
|
||||
errno_t pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
||||
const struct timespec *abstime) {
|
||||
|
||||
// validate arguments
|
||||
struct PosixThread *pt;
|
||||
if (!(pt = _pthread_self()))
|
||||
return EINVAL;
|
||||
if (abstime && !(0 <= abstime->tv_nsec && abstime->tv_nsec < 1000000000))
|
||||
return EINVAL;
|
||||
if (MUTEX_TYPE(mutex->_word) != PTHREAD_MUTEX_NORMAL)
|
||||
nsync_panic_("pthread cond needs normal mutex\n");
|
||||
return nsync_cv_wait_with_deadline(
|
||||
(nsync_cv *)cond, (nsync_mu *)mutex,
|
||||
abstime ? *abstime : nsync_time_no_deadline, 0);
|
||||
|
||||
// look at the mutex argument
|
||||
uint64_t muword = atomic_load_explicit(&mutex->_word, memory_order_relaxed);
|
||||
|
||||
// check that mutex is held by caller
|
||||
if (MUTEX_TYPE(muword) == PTHREAD_MUTEX_ERRORCHECK &&
|
||||
MUTEX_OWNER(muword) != gettid())
|
||||
return EPERM;
|
||||
|
||||
// if condition variable is shared then mutex must be too
|
||||
if (cond->_pshared)
|
||||
if (MUTEX_PSHARED(muword) != PTHREAD_PROCESS_SHARED)
|
||||
return EINVAL;
|
||||
|
||||
#if PTHREAD_USE_NSYNC
|
||||
// favor *NSYNC if this is a process private condition variable
|
||||
// if using Mike Burrows' code isn't possible, use a naive impl
|
||||
if (!cond->_pshared)
|
||||
return nsync_cv_wait_with_deadline(
|
||||
(nsync_cv *)cond, (nsync_mu *)mutex,
|
||||
abstime ? *abstime : nsync_time_no_deadline, 0);
|
||||
#endif
|
||||
|
||||
errno_t err;
|
||||
BEGIN_CANCELATION_POINT;
|
||||
err = pthread_cond_timedwait_impl(cond, mutex, abstime);
|
||||
END_CANCELATION_POINT;
|
||||
return err;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue