.
[glibc/history.git] / nptl / tst-cancel17.c
blob2a8c951afe9b2620442f5ed16062479af4bf08ce
1 /* Copyright (C) 2003, 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <aio.h>
21 #include <errno.h>
22 #include <pthread.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
29 static pthread_barrier_t b;
32 /* Cleanup handling test. */
33 static int cl_called;
35 static void
36 cl (void *arg)
38 ++cl_called;
42 static void *
43 tf (void *arg)
45 int r = pthread_barrier_wait (&b);
46 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
48 puts ("tf: barrier_wait failed");
49 exit (1);
52 pthread_cleanup_push (cl, NULL);
54 const struct aiocb *l[1] = { arg };
56 TEMP_FAILURE_RETRY (aio_suspend (l, 1, NULL));
58 pthread_cleanup_pop (0);
60 puts ("tf: aio_suspend returned");
62 exit (1);
66 static void *
67 tf2 (void *arg)
69 int r = pthread_barrier_wait (&b);
70 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
72 puts ("tf2: barrier_wait failed");
73 exit (1);
76 pthread_cleanup_push (cl, NULL);
78 const struct aiocb *l[1] = { arg };
79 struct timespec ts = { .tv_sec = 1000, .tv_nsec = 0 };
81 TEMP_FAILURE_RETRY (aio_suspend (l, 1, &ts));
83 pthread_cleanup_pop (0);
85 puts ("tf2: aio_suspend returned");
87 exit (1);
91 static int
92 do_test (void)
94 int fds[2];
95 if (pipe (fds) != 0)
97 puts ("pipe failed");
98 return 1;
101 struct aiocb a, a2, *ap;
102 char mem[1];
103 memset (&a, '\0', sizeof (a));
104 a.aio_fildes = fds[0];
105 a.aio_buf = mem;
106 a.aio_nbytes = sizeof (mem);
107 if (aio_read (&a) != 0)
109 puts ("aio_read failed");
110 return 1;
113 if (pthread_barrier_init (&b, NULL, 2) != 0)
115 puts ("barrier_init failed");
116 return 1;
119 pthread_t th;
120 if (pthread_create (&th, NULL, tf, &a) != 0)
122 puts ("1st create failed");
123 return 1;
126 int r = pthread_barrier_wait (&b);
127 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
129 puts ("barrier_wait failed");
130 exit (1);
133 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
134 while (nanosleep (&ts, &ts) != 0)
135 continue;
137 puts ("going to cancel tf in-time");
138 if (pthread_cancel (th) != 0)
140 puts ("1st cancel failed");
141 return 1;
144 void *status;
145 if (pthread_join (th, &status) != 0)
147 puts ("1st join failed");
148 return 1;
150 if (status != PTHREAD_CANCELED)
152 puts ("1st thread not canceled");
153 return 1;
156 if (cl_called == 0)
158 puts ("tf cleanup handler not called");
159 return 1;
161 if (cl_called > 1)
163 puts ("tf cleanup handler called more than once");
164 return 1;
167 cl_called = 0;
169 if (pthread_create (&th, NULL, tf2, &a) != 0)
171 puts ("2nd create failed");
172 return 1;
175 r = pthread_barrier_wait (&b);
176 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
178 puts ("2nd barrier_wait failed");
179 exit (1);
182 ts.tv_sec = 0;
183 ts.tv_nsec = 100000000;
184 while (nanosleep (&ts, &ts) != 0)
185 continue;
187 puts ("going to cancel tf2 in-time");
188 if (pthread_cancel (th) != 0)
190 puts ("2nd cancel failed");
191 return 1;
194 if (pthread_join (th, &status) != 0)
196 puts ("2nd join failed");
197 return 1;
199 if (status != PTHREAD_CANCELED)
201 puts ("2nd thread not canceled");
202 return 1;
205 if (cl_called == 0)
207 puts ("tf2 cleanup handler not called");
208 return 1;
210 if (cl_called > 1)
212 puts ("tf2 cleanup handler called more than once");
213 return 1;
216 puts ("in-time cancellation succeeded");
218 ap = &a;
219 if (aio_cancel (fds[0], &a) != AIO_CANCELED)
221 puts ("aio_cancel failed");
222 /* If aio_cancel failed, we cannot reuse aiocb a. */
223 ap = &a2;
227 cl_called = 0;
229 size_t len2 = fpathconf (fds[1], _PC_PIPE_BUF);
230 size_t page_size = sysconf (_SC_PAGESIZE);
231 len2 = 20 * (len2 < page_size ? page_size : len2) + sizeof (mem) + 1;
232 char *mem2 = malloc (len2);
233 if (mem2 == NULL)
235 puts ("could not allocate memory for pipe write");
236 return 1;
239 memset (ap, '\0', sizeof (*ap));
240 ap->aio_fildes = fds[1];
241 ap->aio_buf = mem2;
242 ap->aio_nbytes = len2;
243 if (aio_write (ap) != 0)
245 puts ("aio_write failed");
246 return 1;
249 if (pthread_create (&th, NULL, tf, ap) != 0)
251 puts ("3rd create failed");
252 return 1;
255 puts ("going to cancel tf early");
256 if (pthread_cancel (th) != 0)
258 puts ("3rd cancel failed");
259 return 1;
262 r = pthread_barrier_wait (&b);
263 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
265 puts ("3rd barrier_wait failed");
266 exit (1);
269 if (pthread_join (th, &status) != 0)
271 puts ("3rd join failed");
272 return 1;
274 if (status != PTHREAD_CANCELED)
276 puts ("3rd thread not canceled");
277 return 1;
280 if (cl_called == 0)
282 puts ("tf cleanup handler not called");
283 return 1;
285 if (cl_called > 1)
287 puts ("tf cleanup handler called more than once");
288 return 1;
291 cl_called = 0;
293 if (pthread_create (&th, NULL, tf2, ap) != 0)
295 puts ("4th create failed");
296 return 1;
299 puts ("going to cancel tf2 early");
300 if (pthread_cancel (th) != 0)
302 puts ("4th cancel failed");
303 return 1;
306 r = pthread_barrier_wait (&b);
307 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
309 puts ("4th barrier_wait failed");
310 exit (1);
313 if (pthread_join (th, &status) != 0)
315 puts ("4th join failed");
316 return 1;
318 if (status != PTHREAD_CANCELED)
320 puts ("4th thread not canceled");
321 return 1;
324 if (cl_called == 0)
326 puts ("tf2 cleanup handler not called");
327 return 1;
329 if (cl_called > 1)
331 puts ("tf2 cleanup handler called more than once");
332 return 1;
335 puts ("early cancellation succeeded");
337 return 0;
340 #define TEST_FUNCTION do_test ()
341 #include "../test-skeleton.c"