1*dcdfe824SRobert Mustacchi /*
2*dcdfe824SRobert Mustacchi * This file and its contents are supplied under the terms of the
3*dcdfe824SRobert Mustacchi * Common Development and Distribution License ("CDDL"), version 1.0.
4*dcdfe824SRobert Mustacchi * You may only use this file in accordance with the terms of version
5*dcdfe824SRobert Mustacchi * 1.0 of the CDDL.
6*dcdfe824SRobert Mustacchi *
7*dcdfe824SRobert Mustacchi * A full copy of the text of the CDDL should have accompanied this
8*dcdfe824SRobert Mustacchi * source. A copy of the CDDL is also available via the Internet at
9*dcdfe824SRobert Mustacchi * http://www.illumos.org/license/CDDL.
10*dcdfe824SRobert Mustacchi */
11*dcdfe824SRobert Mustacchi
12*dcdfe824SRobert Mustacchi /*
13*dcdfe824SRobert Mustacchi * Copyright 2016 Joyent, Inc.
14*dcdfe824SRobert Mustacchi */
15*dcdfe824SRobert Mustacchi
16*dcdfe824SRobert Mustacchi /*
17*dcdfe824SRobert Mustacchi * Test call_once(3C)
18*dcdfe824SRobert Mustacchi */
19*dcdfe824SRobert Mustacchi
20*dcdfe824SRobert Mustacchi #include <threads.h>
21*dcdfe824SRobert Mustacchi #include <sys/debug.h>
22*dcdfe824SRobert Mustacchi
23*dcdfe824SRobert Mustacchi #define CO_NTHREADS 32
24*dcdfe824SRobert Mustacchi
25*dcdfe824SRobert Mustacchi static int co_val = 41;
26*dcdfe824SRobert Mustacchi static mtx_t co_once_mtx;
27*dcdfe824SRobert Mustacchi static mtx_t co_mtx;
28*dcdfe824SRobert Mustacchi static boolean_t co_go = B_FALSE;
29*dcdfe824SRobert Mustacchi static once_flag co_once = ONCE_FLAG_INIT;
30*dcdfe824SRobert Mustacchi static cnd_t co_cnd;
31*dcdfe824SRobert Mustacchi
32*dcdfe824SRobert Mustacchi static void
co_once_func(void)33*dcdfe824SRobert Mustacchi co_once_func(void)
34*dcdfe824SRobert Mustacchi {
35*dcdfe824SRobert Mustacchi VERIFY3S(mtx_lock(&co_once_mtx), ==, thrd_success);
36*dcdfe824SRobert Mustacchi co_val++;
37*dcdfe824SRobert Mustacchi VERIFY3S(mtx_unlock(&co_once_mtx), ==, thrd_success);
38*dcdfe824SRobert Mustacchi }
39*dcdfe824SRobert Mustacchi
40*dcdfe824SRobert Mustacchi /*ARGSUSED*/
41*dcdfe824SRobert Mustacchi static int
co_thr(void * arg)42*dcdfe824SRobert Mustacchi co_thr(void *arg)
43*dcdfe824SRobert Mustacchi {
44*dcdfe824SRobert Mustacchi VERIFY3S(mtx_lock(&co_mtx), ==, thrd_success);
45*dcdfe824SRobert Mustacchi while (co_go == B_FALSE)
46*dcdfe824SRobert Mustacchi cnd_wait(&co_cnd, &co_mtx);
47*dcdfe824SRobert Mustacchi VERIFY3S(mtx_unlock(&co_mtx), ==, thrd_success);
48*dcdfe824SRobert Mustacchi call_once(&co_once, co_once_func);
49*dcdfe824SRobert Mustacchi return (0);
50*dcdfe824SRobert Mustacchi }
51*dcdfe824SRobert Mustacchi
52*dcdfe824SRobert Mustacchi int
main(void)53*dcdfe824SRobert Mustacchi main(void)
54*dcdfe824SRobert Mustacchi {
55*dcdfe824SRobert Mustacchi int i;
56*dcdfe824SRobert Mustacchi thrd_t threads[CO_NTHREADS];
57*dcdfe824SRobert Mustacchi
58*dcdfe824SRobert Mustacchi VERIFY3S(mtx_init(&co_once_mtx, mtx_plain), ==, thrd_success);
59*dcdfe824SRobert Mustacchi VERIFY3S(mtx_init(&co_mtx, mtx_plain), ==, thrd_success);
60*dcdfe824SRobert Mustacchi VERIFY3S(cnd_init(&co_cnd), ==, thrd_success);
61*dcdfe824SRobert Mustacchi
62*dcdfe824SRobert Mustacchi for (i = 0; i < CO_NTHREADS; i++) {
63*dcdfe824SRobert Mustacchi VERIFY3S(thrd_create(&threads[i], co_thr, NULL), ==,
64*dcdfe824SRobert Mustacchi thrd_success);
65*dcdfe824SRobert Mustacchi }
66*dcdfe824SRobert Mustacchi
67*dcdfe824SRobert Mustacchi VERIFY3S(mtx_lock(&co_mtx), ==, thrd_success);
68*dcdfe824SRobert Mustacchi co_go = B_TRUE;
69*dcdfe824SRobert Mustacchi VERIFY3S(mtx_unlock(&co_mtx), ==, thrd_success);
70*dcdfe824SRobert Mustacchi VERIFY3S(cnd_broadcast(&co_cnd), ==, thrd_success);
71*dcdfe824SRobert Mustacchi
72*dcdfe824SRobert Mustacchi for (i = 0; i < CO_NTHREADS; i++) {
73*dcdfe824SRobert Mustacchi VERIFY3S(thrd_join(threads[i], NULL), ==, thrd_success);
74*dcdfe824SRobert Mustacchi }
75*dcdfe824SRobert Mustacchi VERIFY3S(co_val, ==, 42);
76*dcdfe824SRobert Mustacchi
77*dcdfe824SRobert Mustacchi return (0);
78*dcdfe824SRobert Mustacchi }
79