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