1 /* C11 threads specific storage tests.
2 Copyright (C) 2018-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <threads.h>
20 #include <stdio.h>
21 #include <unistd.h>
22
23 #include <support/check.h>
24
25 /* Thread specific storage. */
26 static tss_t key;
27
28 #define TSS_VALUE (void*) 0xFF
29
30 static int
tss_thrd(void * arg)31 tss_thrd (void *arg)
32 {
33 if (tss_create (&key, NULL) != thrd_success)
34 FAIL_EXIT1 ("tss_create failed");
35
36 if (tss_set (key, TSS_VALUE) != thrd_success)
37 FAIL_EXIT1 ("tss_set failed");
38
39 void *value = tss_get (key);
40 if (value == 0)
41 FAIL_EXIT1 ("tss_get failed");
42 if (value != TSS_VALUE)
43 FAIL_EXIT1 ("tss_get returned %p, expected %p", value, TSS_VALUE);
44
45 thrd_exit (thrd_success);
46 }
47
48 static int
do_test(void)49 do_test (void)
50 {
51 /* Setting an invalid key should return an error. */
52 if (tss_set (key, TSS_VALUE) == thrd_success)
53 FAIL_EXIT1 ("tss_set succeed where it should have failed");
54
55 if (tss_create (&key, NULL) != thrd_success)
56 FAIL_EXIT1 ("tss_create failed");
57
58 thrd_t id;
59 if (thrd_create (&id, tss_thrd, NULL) != thrd_success)
60 FAIL_EXIT1 ("thrd_create failed");
61
62 if (thrd_join (id, NULL) != thrd_success)
63 FAIL_EXIT1 ("thrd failed");
64
65 /* The value set in tss_thrd should not be visible here. */
66 void *value = tss_get (key);
67 if (value != 0)
68 FAIL_EXIT1 ("tss_get succeed where it should have failed");
69
70 tss_delete (key);
71
72 return 0;
73 }
74
75 #include <support/test-driver.c>
76