hurd: Fix building libio/tst-memstream3.c
[glibc.git] / nptl / tst-cancel4-common.c
blob5bc7e44ae2c532264ecccef53303b786d386f9b5
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 int val;
24 socklen_t len;
26 if (socketpair (AF_UNIX, SOCK_STREAM, PF_UNIX, fds) != 0)
28 perror ("socketpair");
29 exit (1);
32 val = 1;
33 len = sizeof(val);
34 setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
35 if (getsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, &len) < 0)
37 perror ("getsockopt");
38 exit (1);
40 if (val >= WRITE_BUFFER_SIZE)
42 puts ("minimum write buffer size too large");
43 exit (1);
45 setsockopt (fds[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
47 if (mktemp (fifoname) == NULL)
49 printf ("%s: cannot generate temp file name: %m\n", __func__);
50 exit (1);
53 int result = 0;
54 size_t cnt;
55 for (cnt = 0; cnt < ntest_tf; ++cnt)
57 if (tests[cnt].only_early)
58 continue;
60 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
62 puts ("b2 init failed");
63 exit (1);
66 /* Reset the counter for the cleanup handler. */
67 cl_called = 0;
69 pthread_t th;
70 if (pthread_create (&th, NULL, tests[cnt].tf, NULL) != 0)
72 printf ("create for '%s' test failed\n", tests[cnt].name);
73 result = 1;
74 continue;
77 int r = pthread_barrier_wait (&b2);
78 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
80 printf ("%s: barrier_wait failed\n", __func__);
81 result = 1;
82 continue;
85 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
86 while (nanosleep (&ts, &ts) != 0)
87 continue;
89 if (pthread_cancel (th) != 0)
91 printf ("cancel for '%s' failed\n", tests[cnt].name);
92 result = 1;
93 continue;
96 void *status;
97 if (pthread_join (th, &status) != 0)
99 printf ("join for '%s' failed\n", tests[cnt].name);
100 result = 1;
101 continue;
103 if (status != PTHREAD_CANCELED)
105 printf ("thread for '%s' not canceled\n", tests[cnt].name);
106 result = 1;
107 continue;
110 if (pthread_barrier_destroy (&b2) != 0)
112 puts ("barrier_destroy failed");
113 result = 1;
114 continue;
117 if (cl_called == 0)
119 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
120 result = 1;
121 continue;
123 if (cl_called > 1)
125 printf ("cleanup handler called more than once for '%s'\n",
126 tests[cnt].name);
127 result = 1;
128 continue;
131 printf ("in-time cancel test of '%s' successful\n", tests[cnt].name);
133 if (tempfd != -1)
135 close (tempfd);
136 tempfd = -1;
138 if (tempfd2 != -1)
140 close (tempfd2);
141 tempfd2 = -1;
143 if (tempfname != NULL)
145 unlink (tempfname);
146 free (tempfname);
147 tempfname = NULL;
149 if (tempmsg != -1)
151 msgctl (tempmsg, IPC_RMID, NULL);
152 tempmsg = -1;
156 for (cnt = 0; cnt < ntest_tf; ++cnt)
158 if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
160 puts ("b2 init failed");
161 exit (1);
164 /* Reset the counter for the cleanup handler. */
165 cl_called = 0;
167 pthread_t th;
168 if (pthread_create (&th, NULL, tests[cnt].tf, (void *) 1l) != 0)
170 printf ("create for '%s' test failed\n", tests[cnt].name);
171 result = 1;
172 continue;
175 int r = pthread_barrier_wait (&b2);
176 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
178 printf ("%s: barrier_wait failed\n", __func__);
179 result = 1;
180 continue;
183 if (pthread_cancel (th) != 0)
185 printf ("cancel for '%s' failed\n", tests[cnt].name);
186 result = 1;
187 continue;
190 r = pthread_barrier_wait (&b2);
191 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
193 printf ("%s: barrier_wait failed\n", __func__);
194 result = 1;
195 continue;
198 void *status;
199 if (pthread_join (th, &status) != 0)
201 printf ("join for '%s' failed\n", tests[cnt].name);
202 result = 1;
203 continue;
205 if (status != PTHREAD_CANCELED)
207 printf ("thread for '%s' not canceled\n", tests[cnt].name);
208 result = 1;
209 continue;
212 if (pthread_barrier_destroy (&b2) != 0)
214 puts ("barrier_destroy failed");
215 result = 1;
216 continue;
219 if (cl_called == 0)
221 printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
222 result = 1;
223 continue;
225 if (cl_called > 1)
227 printf ("cleanup handler called more than once for '%s'\n",
228 tests[cnt].name);
229 result = 1;
230 continue;
233 printf ("early cancel test of '%s' successful\n", tests[cnt].name);
235 if (tempfd != -1)
237 close (tempfd);
238 tempfd = -1;
240 if (tempfd2 != -1)
242 close (tempfd2);
243 tempfd2 = -1;
245 if (tempfname != NULL)
247 unlink (tempfname);
248 free (tempfname);
249 tempfname = NULL;
251 if (tempmsg != -1)
253 msgctl (tempmsg, IPC_RMID, NULL);
254 tempmsg = -1;
258 return result;
261 #define TIMEOUT 60
262 #include <support/test-driver.c>