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