#include <threads.h>
#include <sys/debug.h>
#define CO_NTHREADS 32
static int co_val = 41;
static mtx_t co_once_mtx;
static mtx_t co_mtx;
static boolean_t co_go = B_FALSE;
static once_flag co_once = ONCE_FLAG_INIT;
static cnd_t co_cnd;
static void
co_once_func(void)
{
VERIFY3S(mtx_lock(&co_once_mtx), ==, thrd_success);
co_val++;
VERIFY3S(mtx_unlock(&co_once_mtx), ==, thrd_success);
}
static int
co_thr(void *arg)
{
VERIFY3S(mtx_lock(&co_mtx), ==, thrd_success);
while (co_go == B_FALSE) {
(void) cnd_wait(&co_cnd, &co_mtx);
}
VERIFY3S(mtx_unlock(&co_mtx), ==, thrd_success);
call_once(&co_once, co_once_func);
return (0);
}
int
main(void)
{
int i;
thrd_t threads[CO_NTHREADS];
VERIFY3S(mtx_init(&co_once_mtx, mtx_plain), ==, thrd_success);
VERIFY3S(mtx_init(&co_mtx, mtx_plain), ==, thrd_success);
VERIFY3S(cnd_init(&co_cnd), ==, thrd_success);
for (i = 0; i < CO_NTHREADS; i++) {
VERIFY3S(thrd_create(&threads[i], co_thr, NULL), ==,
thrd_success);
}
VERIFY3S(mtx_lock(&co_mtx), ==, thrd_success);
co_go = B_TRUE;
VERIFY3S(mtx_unlock(&co_mtx), ==, thrd_success);
VERIFY3S(cnd_broadcast(&co_cnd), ==, thrd_success);
for (i = 0; i < CO_NTHREADS; i++) {
VERIFY3S(thrd_join(threads[i], NULL), ==, thrd_success);
}
VERIFY3S(co_val, ==, 42);
return (0);
}