xref: /illumos-gate/usr/src/test/libc-tests/tests/c11_tss.c (revision 8c69cc8fbe729fa7b091e901c4b50508ccc6bb33)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2016 Joyent, Inc.
14  */
15 
16 /*
17  * Test various C11 thread-specific storage (tss(3C)) interfaces.
18  */
19 
20 #include <threads.h>
21 #include <sys/debug.h>
22 
23 #define	TSS_NTHREADS	128
24 
25 static tss_t ct_key;
26 static int ct_count;
27 static int ct_ready;
28 static mtx_t ct_mtx;
29 static cnd_t ct_cnd;
30 
31 static void
32 ct_tss_dtor(void *arg)
33 {
34 	VERIFY3S(mtx_lock(&ct_mtx), ==, thrd_success);
35 	ct_count++;
36 	VERIFY3S(mtx_unlock(&ct_mtx), ==, thrd_success);
37 }
38 
39 static int
40 ct_tss_thr(void *arg)
41 {
42 	VERIFY3P(tss_get(ct_key), ==, NULL);
43 	VERIFY3S(tss_set(ct_key, arg), ==, thrd_success);
44 
45 	VERIFY3S(mtx_lock(&ct_mtx), ==, thrd_success);
46 	ct_ready++;
47 	if (ct_ready == TSS_NTHREADS) {
48 		VERIFY3S(cnd_broadcast(&ct_cnd), ==, thrd_success);
49 	} else {
50 		while (ct_ready != TSS_NTHREADS) {
51 			VERIFY3S(cnd_wait(&ct_cnd, &ct_mtx), ==, thrd_success);
52 		}
53 	}
54 	VERIFY3S(mtx_unlock(&ct_mtx), ==, thrd_success);
55 
56 	VERIFY3P(tss_get(ct_key), ==, arg);
57 
58 	return (0);
59 }
60 
61 int
62 main(void)
63 {
64 	int i;
65 	thrd_t threads[TSS_NTHREADS];
66 
67 	VERIFY3S(tss_create(&ct_key, ct_tss_dtor), ==, thrd_success);
68 	VERIFY3S(mtx_init(&ct_mtx, mtx_plain), ==, thrd_success);
69 	VERIFY3S(cnd_init(&ct_cnd), ==, thrd_success);
70 
71 	for (i = 0; i < TSS_NTHREADS; i++) {
72 		VERIFY3S(thrd_create(&threads[i], ct_tss_thr,
73 		    (void *)(uintptr_t)(i + 100)), ==, thrd_success);
74 	}
75 
76 	for (i = 0; i < TSS_NTHREADS; i++) {
77 		VERIFY3S(thrd_join(threads[i], NULL), ==, thrd_success);
78 	}
79 
80 	VERIFY3S(ct_count, ==, TSS_NTHREADS);
81 
82 	mtx_destroy(&ct_mtx);
83 	cnd_destroy(&ct_cnd);
84 	tss_delete(ct_key);
85 
86 	return (0);
87 }
88