diff --git a/system/lib/libc/musl/src/thread/__timedwait.c b/system/lib/libc/musl/src/thread/__timedwait.c new file mode 100644 index 000000000..2057e9c54 --- /dev/null +++ b/system/lib/libc/musl/src/thread/__timedwait.c @@ -0,0 +1,57 @@ +#include +#include +#include +#ifdef __EMSCRIPTEN__ +#include +#include +#else +#include "futex.h" +#endif +#include "syscall.h" + +static int do_wait(volatile int *addr, int val, + clockid_t clk, const struct timespec *at, int priv) +{ + int r; + struct timespec to, *top=0; + + if (at) { + if (at->tv_nsec >= 1000000000UL) return EINVAL; + if (clock_gettime(clk, &to)) return EINVAL; + to.tv_sec = at->tv_sec - to.tv_sec; + if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) { + to.tv_sec--; + to.tv_nsec += 1000000000; + } + if (to.tv_sec < 0) return ETIMEDOUT; + top = &to; + } + +#ifdef __EMSCRIPTEN__ + double timeout = top->tv_sec * 1000000000.0 + top->tv_nsec; + if (timeout > 1 * 1000000000.0) timeout = 1 * 1000000000.0; + EM_ASM_INT( { Module['printErr']('Wait ' + $0 + '.') }, timeout); + r = emscripten_futex_wait((void*)addr, val, timeout); +#else + r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT, val, top); +#endif + if (r == EINTR || r == EINVAL || r == ETIMEDOUT) return r; + return 0; +} + +int __timedwait(volatile int *addr, int val, + clockid_t clk, const struct timespec *at, + void (*cleanup)(void *), void *arg, int priv) +{ + int r, cs; + + if (!cleanup) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); + pthread_cleanup_push(cleanup, arg); + + r = do_wait(addr, val, clk, at, priv); + + pthread_cleanup_pop(0); + if (!cleanup) pthread_setcancelstate(cs, 0); + + return r; +} diff --git a/system/lib/libc/musl/src/thread/pthread_cond_broadcast.c b/system/lib/libc/musl/src/thread/pthread_cond_broadcast.c new file mode 100644 index 000000000..333a4ecc1 --- /dev/null +++ b/system/lib/libc/musl/src/thread/pthread_cond_broadcast.c @@ -0,0 +1,43 @@ +#include "pthread_impl.h" + +int pthread_cond_broadcast(pthread_cond_t *c) +{ + pthread_mutex_t *m; + + if (!c->_c_waiters) return 0; + + a_inc(&c->_c_seq); + + /* If cond var is process-shared, simply wake all waiters. */ + if (c->_c_mutex == (void *)-1) { + __wake(&c->_c_seq, -1, 0); + return 0; + } + + /* Block waiters from returning so we can use the mutex. */ + while (a_swap(&c->_c_lock, 1)) + __wait(&c->_c_lock, &c->_c_lockwait, 1, 1); + if (!c->_c_waiters) + goto out; + m = c->_c_mutex; + + /* Move waiter count to the mutex */ + a_fetch_add(&m->_m_waiters, c->_c_waiters2); + c->_c_waiters2 = 0; + +#ifdef __EMSCRIPTEN__ + emscripten_futex_requeue(&c->_c_seq, !m->_m_type || (m->_m_lock&INT_MAX)!=pthread_self()->tid, &m->_m_lock, INT_MAX); +#else + /* Perform the futex requeue, waking one waiter unless we know + * that the calling thread holds the mutex. */ + __syscall(SYS_futex, &c->_c_seq, FUTEX_REQUEUE, + !m->_m_type || (m->_m_lock&INT_MAX)!=pthread_self()->tid, + INT_MAX, &m->_m_lock); +#endif + +out: + a_store(&c->_c_lock, 0); + if (c->_c_lockwait) __wake(&c->_c_lock, 1, 0); + + return 0; +} diff --git a/system/lib/libc/musl/src/thread/pthread_cond_destroy.c b/system/lib/libc/musl/src/thread/pthread_cond_destroy.c new file mode 100644 index 000000000..a096c5547 --- /dev/null +++ b/system/lib/libc/musl/src/thread/pthread_cond_destroy.c @@ -0,0 +1,13 @@ +#include "pthread_impl.h" + +int pthread_cond_destroy(pthread_cond_t *c) +{ + int priv = c->_c_mutex != (void *)-1; + int cnt; + c->_c_destroy = 1; + if (c->_c_waiters) + __wake(&c->_c_seq, -1, priv); + while ((cnt = c->_c_waiters)) + __wait(&c->_c_waiters, 0, cnt, priv); + return 0; +} diff --git a/system/lib/libc/musl/src/thread/pthread_cond_init.c b/system/lib/libc/musl/src/thread/pthread_cond_init.c new file mode 100644 index 000000000..357ecd55e --- /dev/null +++ b/system/lib/libc/musl/src/thread/pthread_cond_init.c @@ -0,0 +1,11 @@ +#include "pthread_impl.h" + +int pthread_cond_init(pthread_cond_t *restrict c, const pthread_condattr_t *restrict a) +{ + *c = (pthread_cond_t){0}; + if (a) { + c->_c_clock = a->__attr & 0x7fffffff; + if (a->__attr>>31) c->_c_mutex = (void *)-1; + } + return 0; +} diff --git a/system/lib/libc/musl/src/thread/pthread_cond_signal.c b/system/lib/libc/musl/src/thread/pthread_cond_signal.c new file mode 100644 index 000000000..71bcdcd99 --- /dev/null +++ b/system/lib/libc/musl/src/thread/pthread_cond_signal.c @@ -0,0 +1,9 @@ +#include "pthread_impl.h" + +int pthread_cond_signal(pthread_cond_t *c) +{ + if (!c->_c_waiters) return 0; + a_inc(&c->_c_seq); + if (c->_c_waiters) __wake(&c->_c_seq, 1, 0); + return 0; +} diff --git a/system/lib/libc/musl/src/thread/pthread_cond_timedwait.c b/system/lib/libc/musl/src/thread/pthread_cond_timedwait.c new file mode 100644 index 000000000..1f25c8e7b --- /dev/null +++ b/system/lib/libc/musl/src/thread/pthread_cond_timedwait.c @@ -0,0 +1,76 @@ +#include "pthread_impl.h" + +struct cm { + pthread_cond_t *c; + pthread_mutex_t *m; +}; + +static void unwait(pthread_cond_t *c, pthread_mutex_t *m) +{ + /* Removing a waiter is non-trivial if we could be using requeue + * based broadcast signals, due to mutex access issues, etc. */ + + if (c->_c_mutex == (void *)-1) { + a_dec(&c->_c_waiters); + if (c->_c_destroy) __wake(&c->_c_waiters, 1, 0); + return; + } + + while (a_swap(&c->_c_lock, 1)) + __wait(&c->_c_lock, &c->_c_lockwait, 1, 1); + + if (c->_c_waiters2) c->_c_waiters2--; + else a_dec(&m->_m_waiters); + + a_store(&c->_c_lock, 0); + if (c->_c_lockwait) __wake(&c->_c_lock, 1, 1); + + a_dec(&c->_c_waiters); + if (c->_c_destroy) __wake(&c->_c_waiters, 1, 1); +} + +static void cleanup(void *p) +{ + struct cm *cm = p; + unwait(cm->c, cm->m); + pthread_mutex_lock(cm->m); +} + +int pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts) +{ + struct cm cm = { .c=c, .m=m }; + int r, e=0, seq; + + if (m->_m_type && (m->_m_lock&INT_MAX) != pthread_self()->tid) + return EPERM; + + if (ts && ts->tv_nsec >= 1000000000UL) + return EINVAL; + + pthread_testcancel(); + + a_inc(&c->_c_waiters); + + if (c->_c_mutex != (void *)-1) { + c->_c_mutex = m; + while (a_swap(&c->_c_lock, 1)) + __wait(&c->_c_lock, &c->_c_lockwait, 1, 1); + c->_c_waiters2++; + a_store(&c->_c_lock, 0); + if (c->_c_lockwait) __wake(&c->_c_lock, 1, 1); + } + + seq = c->_c_seq; + + pthread_mutex_unlock(m); + + do e = __timedwait(&c->_c_seq, seq, c->_c_clock, ts, cleanup, &cm, 0); + while (c->_c_seq == seq && (!e || e==EINTR)); + if (e == EINTR) e = 0; + + unwait(c, m); + + if ((r=pthread_mutex_lock(m))) return r; + + return e; +} diff --git a/system/lib/libc/musl/src/thread/pthread_cond_wait.c b/system/lib/libc/musl/src/thread/pthread_cond_wait.c new file mode 100644 index 000000000..8735bf147 --- /dev/null +++ b/system/lib/libc/musl/src/thread/pthread_cond_wait.c @@ -0,0 +1,6 @@ +#include "pthread_impl.h" + +int pthread_cond_wait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m) +{ + return pthread_cond_timedwait(c, m, 0); +} diff --git a/tests/pthread/test_pthread_condition_variable.cpp b/tests/pthread/test_pthread_condition_variable.cpp new file mode 100644 index 000000000..7b6d0bad9 --- /dev/null +++ b/tests/pthread/test_pthread_condition_variable.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include + +#define NUM_THREADS 3 +#define TCOUNT 10 +#define COUNT_LIMIT 12 + +int count = 0; +int thread_ids[3] = {0,1,2}; +pthread_mutex_t count_mutex; +pthread_cond_t count_threshold_cv; + +void *inc_count(void *t) +{ + int i; + long my_id = (long)t; + + for (i=0; i