]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/pthread/tst-key3.c
6efca16873ddd96406a7efcfe38ed5e866a3d6d8
[thirdparty/glibc.git] / sysdeps / pthread / tst-key3.c
1 /* Copyright (C) 2002-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <pthread.h>
20 #include <stdio.h>
21 #include <unistd.h>
22
23 #define N 2
24
25
26 static int do_test (void);
27
28 #define TEST_FUNCTION do_test ()
29 #include "../test-skeleton.c"
30
31 static int cnt0;
32 static void
33 f0 (void *p)
34 {
35 ++cnt0;
36 }
37
38
39 static int cnt1;
40 static void
41 f1 (void *p)
42 {
43 ++cnt1;
44 }
45
46
47 static void (*fcts[N]) (void *) =
48 {
49 f0,
50 f1
51 };
52
53
54 static pthread_barrier_t b;
55
56
57 static void *
58 tf (void *arg)
59 {
60 pthread_key_t *key = (pthread_key_t *) arg;
61
62 if (pthread_setspecific (*key, (void *) -1l) != 0)
63 {
64 write_message ("setspecific failed\n");
65 _exit (1);
66 }
67
68 pthread_barrier_wait (&b);
69
70 const struct timespec t = { .tv_sec = 1000, .tv_nsec = 0 };
71 while (1)
72 nanosleep (&t, NULL);
73
74 /* NOTREACHED */
75 return NULL;
76 }
77
78
79 int
80 do_test (void)
81 {
82 pthread_key_t keys[N];
83
84 int i;
85 for (i = 0; i < N; ++i)
86 if (pthread_key_create (&keys[i], fcts[i]) != 0)
87 {
88 write_message ("key_create failed\n");
89 _exit (1);
90 }
91
92 if (pthread_barrier_init (&b, NULL, 2) != 0)
93 {
94 write_message ("barrier_init failed\n");
95 _exit (1);
96 }
97
98 pthread_t th;
99 if (pthread_create (&th, NULL, tf, &keys[1]) != 0)
100 {
101 write_message ("create failed\n");
102 _exit (1);
103 }
104
105 pthread_barrier_wait (&b);
106
107 if (pthread_cancel (th) != 0)
108 {
109 write_message ("cancel failed\n");
110 _exit (1);
111 }
112
113 void *status;
114 if (pthread_join (th, &status) != 0)
115 {
116 write_message ("join failed\n");
117 _exit (1);
118 }
119
120 if (status != PTHREAD_CANCELED)
121 {
122 write_message ("thread not canceled\n");
123 _exit (1);
124 }
125
126 /* Note that the TSD destructors not necessarily have to have
127 finished by the time pthread_join returns. At least according to
128 POSIX. We implement the stronger requirement that they indeed
129 have run and therefore these tests succeed. */
130 if (cnt0 != 0)
131 {
132 write_message ("cnt0 != 0\n");
133 _exit (1);
134 }
135
136 if (cnt1 != 1)
137 {
138 write_message ("cnt1 != 1\n");
139 _exit (1);
140 }
141
142 for (i = 0; i < N; ++i)
143 if (pthread_key_delete (keys[i]) != 0)
144 {
145 write_message ("key_delete failed\n");
146 _exit (1);
147 }
148
149 if (pthread_barrier_destroy (&b) != 0)
150 {
151 write_message ("barrier_destroy failed\n");
152 _exit (1);
153 }
154
155 return 0;
156 }