Support process shared condition variables

This commit is contained in:
Justine Tunney 2024-07-22 16:33:23 -07:00
parent 3de6632be6
commit 0a9a6f86bb
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
14 changed files with 168 additions and 19 deletions

View file

@ -1,7 +1,7 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Copyright 2024 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
@ -16,8 +16,11 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/atomic.h"
#include "libc/limits.h"
#include "libc/thread/thread.h"
#include "third_party/nsync/cv.h"
#include "third_party/nsync/futex.internal.h"
/**
* Wakes all threads waiting on condition, e.g.
@ -37,6 +40,19 @@
* @see pthread_cond_wait
*/
errno_t pthread_cond_broadcast(pthread_cond_t *cond) {
nsync_cv_broadcast((nsync_cv *)cond);
#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) {
nsync_cv_broadcast((nsync_cv *)cond);
return 0;
}
#endif
// roll forward the monotonic sequence
atomic_fetch_add_explicit(&cond->_sequence, 1, memory_order_acq_rel);
if (atomic_load_explicit(&cond->_waiters, memory_order_acquire))
nsync_futex_wake_((atomic_int *)&cond->_sequence, INT_MAX, cond->_pshared);
return 0;
}