xref: /titanic_51/usr/src/test/libc-tests/tests/c11_tss.c (revision dcdfe824b3dff2df12578b936adf1daf000aa129)
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 various C11 thread-specific storage (tss(3C)) interfaces.
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	TSS_NTHREADS	128
24*dcdfe824SRobert Mustacchi 
25*dcdfe824SRobert Mustacchi static tss_t ct_key;
26*dcdfe824SRobert Mustacchi static int ct_count;
27*dcdfe824SRobert Mustacchi static int ct_ready;
28*dcdfe824SRobert Mustacchi static mtx_t ct_mtx;
29*dcdfe824SRobert Mustacchi static cnd_t ct_cnd;
30*dcdfe824SRobert Mustacchi 
31*dcdfe824SRobert Mustacchi static void
32*dcdfe824SRobert Mustacchi ct_tss_dtor(void *arg)
33*dcdfe824SRobert Mustacchi {
34*dcdfe824SRobert Mustacchi 	VERIFY3S(mtx_lock(&ct_mtx), ==, thrd_success);
35*dcdfe824SRobert Mustacchi 	ct_count++;
36*dcdfe824SRobert Mustacchi 	VERIFY3S(mtx_unlock(&ct_mtx), ==, thrd_success);
37*dcdfe824SRobert Mustacchi }
38*dcdfe824SRobert Mustacchi 
39*dcdfe824SRobert Mustacchi static int
40*dcdfe824SRobert Mustacchi ct_tss_thr(void *arg)
41*dcdfe824SRobert Mustacchi {
42*dcdfe824SRobert Mustacchi 	VERIFY3P(tss_get(ct_key), ==, NULL);
43*dcdfe824SRobert Mustacchi 	VERIFY3S(tss_set(ct_key, arg), ==, thrd_success);
44*dcdfe824SRobert Mustacchi 
45*dcdfe824SRobert Mustacchi 	VERIFY3S(mtx_lock(&ct_mtx), ==, thrd_success);
46*dcdfe824SRobert Mustacchi 	ct_ready++;
47*dcdfe824SRobert Mustacchi 	if (ct_ready == TSS_NTHREADS) {
48*dcdfe824SRobert Mustacchi 		VERIFY3S(cnd_broadcast(&ct_cnd), ==, thrd_success);
49*dcdfe824SRobert Mustacchi 	} else {
50*dcdfe824SRobert Mustacchi 		while (ct_ready != TSS_NTHREADS) {
51*dcdfe824SRobert Mustacchi 			VERIFY3S(cnd_wait(&ct_cnd, &ct_mtx), ==, thrd_success);
52*dcdfe824SRobert Mustacchi 		}
53*dcdfe824SRobert Mustacchi 	}
54*dcdfe824SRobert Mustacchi 	VERIFY3S(mtx_unlock(&ct_mtx), ==, thrd_success);
55*dcdfe824SRobert Mustacchi 
56*dcdfe824SRobert Mustacchi 	VERIFY3P(tss_get(ct_key), ==, arg);
57*dcdfe824SRobert Mustacchi 
58*dcdfe824SRobert Mustacchi 	return (0);
59*dcdfe824SRobert Mustacchi }
60*dcdfe824SRobert Mustacchi 
61*dcdfe824SRobert Mustacchi int
62*dcdfe824SRobert Mustacchi main(void)
63*dcdfe824SRobert Mustacchi {
64*dcdfe824SRobert Mustacchi 	int i;
65*dcdfe824SRobert Mustacchi 	thrd_t threads[TSS_NTHREADS];
66*dcdfe824SRobert Mustacchi 
67*dcdfe824SRobert Mustacchi 	VERIFY3S(tss_create(&ct_key, ct_tss_dtor), ==, thrd_success);
68*dcdfe824SRobert Mustacchi 	VERIFY3S(mtx_init(&ct_mtx, mtx_plain), ==, thrd_success);
69*dcdfe824SRobert Mustacchi 	VERIFY3S(cnd_init(&ct_cnd), ==, thrd_success);
70*dcdfe824SRobert Mustacchi 
71*dcdfe824SRobert Mustacchi 	for (i = 0; i < TSS_NTHREADS; i++) {
72*dcdfe824SRobert Mustacchi 		VERIFY3S(thrd_create(&threads[i], ct_tss_thr,
73*dcdfe824SRobert Mustacchi 		    (void *)(uintptr_t)(i + 100)), ==, thrd_success);
74*dcdfe824SRobert Mustacchi 	}
75*dcdfe824SRobert Mustacchi 
76*dcdfe824SRobert Mustacchi 	for (i = 0; i < TSS_NTHREADS; i++) {
77*dcdfe824SRobert Mustacchi 		VERIFY3S(thrd_join(threads[i], NULL), ==, thrd_success);
78*dcdfe824SRobert Mustacchi 	}
79*dcdfe824SRobert Mustacchi 
80*dcdfe824SRobert Mustacchi 	VERIFY3S(ct_count, ==, TSS_NTHREADS);
81*dcdfe824SRobert Mustacchi 
82*dcdfe824SRobert Mustacchi 	mtx_destroy(&ct_mtx);
83*dcdfe824SRobert Mustacchi 	cnd_destroy(&ct_cnd);
84*dcdfe824SRobert Mustacchi 	tss_delete(ct_key);
85*dcdfe824SRobert Mustacchi 
86*dcdfe824SRobert Mustacchi 	return (0);
87*dcdfe824SRobert Mustacchi }
88