8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / test / libc-tests / tests / c11_tss.c
blobb1005e701210bc80669f0540c9da749a3073a3d5
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.
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.
13 * Copyright 2016 Joyent, Inc.
17 * Test various C11 thread-specific storage (tss(3C)) interfaces.
20 #include <threads.h>
21 #include <sys/debug.h>
23 #define TSS_NTHREADS 128
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;
31 static void
32 ct_tss_dtor(void *arg)
34 VERIFY3S(mtx_lock(&ct_mtx), ==, thrd_success);
35 ct_count++;
36 VERIFY3S(mtx_unlock(&ct_mtx), ==, thrd_success);
39 static int
40 ct_tss_thr(void *arg)
42 VERIFY3P(tss_get(ct_key), ==, NULL);
43 VERIFY3S(tss_set(ct_key, arg), ==, thrd_success);
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);
54 VERIFY3S(mtx_unlock(&ct_mtx), ==, thrd_success);
56 VERIFY3P(tss_get(ct_key), ==, arg);
58 return (0);
61 int
62 main(void)
64 int i;
65 thrd_t threads[TSS_NTHREADS];
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);
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);
76 for (i = 0; i < TSS_NTHREADS; i++) {
77 VERIFY3S(thrd_join(threads[i], NULL), ==, thrd_success);
80 VERIFY3S(ct_count, ==, TSS_NTHREADS);
82 mtx_destroy(&ct_mtx);
83 cnd_destroy(&ct_cnd);
84 tss_delete(ct_key);
86 return (0);