1 /* Copyright (C) 2002-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18 #include <pthread.h>
19 #include <stdio.h>
20 #include <string.h>
21
22
23 static int
do_test(void)24 do_test (void)
25 {
26 pthread_key_t key1;
27 pthread_key_t key2;
28 void *value;
29 /* Addresses of val1 and val2 are used as arbitrary but valid pointers
30 in calls to pthread_setspecific to avoid GCC warnings. */
31 char val1 = 0, val2 = 0;
32 int result = 0;
33 int err;
34
35 err = pthread_key_create (&key1, NULL);
36 if (err != 0)
37 {
38 printf ("1st key_create failed: %s\n", strerror (err));
39 return 1;
40 }
41
42 /* Initial value must be NULL. */
43 value = pthread_getspecific (key1);
44 if (value != NULL)
45 {
46 puts ("1st getspecific != NULL");
47 result = 1;
48 }
49
50 err = pthread_setspecific (key1, (void *) &val1);
51 if (err != 0)
52 {
53 printf ("1st setspecific failed: %s\n", strerror (err));
54 return 1;
55 }
56
57 value = pthread_getspecific (key1);
58 if (value == NULL)
59 {
60 puts ("2nd getspecific == NULL\n");
61 result = 1;
62 }
63 else if (value != (void *) &val1)
64 {
65 puts ("2nd getspecific != &val1l\n");
66 result = 1;
67 }
68
69 err = pthread_setspecific (key1, (void *) &val2);
70 if (err != 0)
71 {
72 printf ("2nd setspecific failed: %s\n", strerror (err));
73 return 1;
74 }
75
76 value = pthread_getspecific (key1);
77 if (value == NULL)
78 {
79 puts ("3rd getspecific == NULL\n");
80 result = 1;
81 }
82 else if (value != (void *) &val2)
83 {
84 puts ("3rd getspecific != &val2\n");
85 result = 1;
86 }
87
88 err = pthread_key_delete (key1);
89 if (err != 0)
90 {
91 printf ("key_delete failed: %s\n", strerror (err));
92 result = 1;
93 }
94
95
96 err = pthread_key_create (&key2, NULL);
97 if (err != 0)
98 {
99 printf ("2nd key_create failed: %s\n", strerror (err));
100 return 1;
101 }
102
103 if (key1 != key2)
104 puts ("key1 != key2; no more tests performed");
105 else
106 {
107 value = pthread_getspecific (key2);
108 if (value != NULL)
109 {
110 puts ("4th getspecific != NULL");
111 result = 1;
112 }
113 }
114
115 return result;
116 }
117
118 #define TEST_FUNCTION do_test ()
119 #include "../test-skeleton.c"
120