hurd: Fix build with latest htl
[glibc.git] / nptl / tst-cancel4-common.c
blobc6eee735dbfd9ba0c30775904e1b0996d9e24e2b
1 /* Common file for all tst-cancel4_*
3 Copyright (C) 2016-2018 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 static int
21 do_test (void)
23 if (socketpair (AF_UNIX, SOCK_STREAM, PF_UNIX, fds) != 0)
25 perror ("socketpair");
26 exit (1);
29 set_socket_buffer (fds[1]);
31 if (mktemp (fifoname) == NULL)
33 printf ("%s: cannot generate temp file name: %m\n", __func__);
34 exit (1);
37 int result = 0;
38 size_t cnt;
39 for (cnt = 0; cnt < ntest_tf; ++cnt)
41 if (tests[cnt].only_early)
42 continue;
44 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
46 puts ("b2 init failed");
47 exit (1);
50 /* Reset the counter for the cleanup handler. */
51 cl_called = 0;
53 pthread_t th;
54 if (pthread_create (&th, NULL, tests[cnt].tf, NULL) != 0)
56 printf ("create for '%s' test failed\n", tests[cnt].name);
57 result = 1;
58 continue;
61 int r = pthread_barrier_wait (&b2);
62 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
64 printf ("%s: barrier_wait failed\n", __func__);
65 result = 1;
66 continue;
69 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
70 while (nanosleep (&ts, &ts) != 0)
71 continue;
73 if (pthread_cancel (th) != 0)
75 printf ("cancel for '%s' failed\n", tests[cnt].name);
76 result = 1;
77 continue;
80 void *status;
81 if (pthread_join (th, &status) != 0)
83 printf ("join for '%s' failed\n", tests[cnt].name);
84 result = 1;
85 continue;
87 if (status != PTHREAD_CANCELED)
89 printf ("thread for '%s' not canceled\n", tests[cnt].name);
90 result = 1;
91 continue;
94 if (pthread_barrier_destroy (&b2) != 0)
96 puts ("barrier_destroy failed");
97 result = 1;
98 continue;
101 if (cl_called == 0)
103 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
104 result = 1;
105 continue;
107 if (cl_called > 1)
109 printf ("cleanup handler called more than once for '%s'\n",
110 tests[cnt].name);
111 result = 1;
112 continue;
115 printf ("in-time cancel test of '%s' successful\n", tests[cnt].name);
117 if (tempfd != -1)
119 close (tempfd);
120 tempfd = -1;
122 if (tempfd2 != -1)
124 close (tempfd2);
125 tempfd2 = -1;
127 if (tempfname != NULL)
129 unlink (tempfname);
130 free (tempfname);
131 tempfname = NULL;
133 if (tempmsg != -1)
135 msgctl (tempmsg, IPC_RMID, NULL);
136 tempmsg = -1;
140 for (cnt = 0; cnt < ntest_tf; ++cnt)
142 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
144 puts ("b2 init failed");
145 exit (1);
148 /* Reset the counter for the cleanup handler. */
149 cl_called = 0;
151 pthread_t th;
152 if (pthread_create (&th, NULL, tests[cnt].tf, (void *) 1l) != 0)
154 printf ("create for '%s' test failed\n", tests[cnt].name);
155 result = 1;
156 continue;
159 int r = pthread_barrier_wait (&b2);
160 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
162 printf ("%s: barrier_wait failed\n", __func__);
163 result = 1;
164 continue;
167 if (pthread_cancel (th) != 0)
169 printf ("cancel for '%s' failed\n", tests[cnt].name);
170 result = 1;
171 continue;
174 r = pthread_barrier_wait (&b2);
175 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
177 printf ("%s: barrier_wait failed\n", __func__);
178 result = 1;
179 continue;
182 void *status;
183 if (pthread_join (th, &status) != 0)
185 printf ("join for '%s' failed\n", tests[cnt].name);
186 result = 1;
187 continue;
189 if (status != PTHREAD_CANCELED)
191 printf ("thread for '%s' not canceled\n", tests[cnt].name);
192 result = 1;
193 continue;
196 if (pthread_barrier_destroy (&b2) != 0)
198 puts ("barrier_destroy failed");
199 result = 1;
200 continue;
203 if (cl_called == 0)
205 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
206 result = 1;
207 continue;
209 if (cl_called > 1)
211 printf ("cleanup handler called more than once for '%s'\n",
212 tests[cnt].name);
213 result = 1;
214 continue;
217 printf ("early cancel test of '%s' successful\n", tests[cnt].name);
219 if (tempfd != -1)
221 close (tempfd);
222 tempfd = -1;
224 if (tempfd2 != -1)
226 close (tempfd2);
227 tempfd2 = -1;
229 if (tempfname != NULL)
231 unlink (tempfname);
232 free (tempfname);
233 tempfname = NULL;
235 if (tempmsg != -1)
237 msgctl (tempmsg, IPC_RMID, NULL);
238 tempmsg = -1;
242 return result;
245 #define TIMEOUT 60
246 #include <support/test-driver.c>