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
29 static pthread_barrier_t b;
32 /* Cleanup handling test. */
45 int r = pthread_barrier_wait (&b);
46 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
48 puts ("tf: barrier_wait failed");
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");
69 int r = pthread_barrier_wait (&b);
70 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
72 puts ("tf2: barrier_wait failed");
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");
101 struct aiocb a, a2, *ap;
103 memset (&a, '\0', sizeof (a));
104 a.aio_fildes = fds[0];
106 a.aio_nbytes = sizeof (mem);
107 if (aio_read (&a) != 0)
109 puts ("aio_read failed");
113 if (pthread_barrier_init (&b, NULL, 2) != 0)
115 puts ("barrier_init failed");
120 if (pthread_create (&th, NULL, tf, &a) != 0)
122 puts ("1st create failed");
126 int r = pthread_barrier_wait (&b);
127 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
129 puts ("barrier_wait failed");
133 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
134 while (nanosleep (&ts, &ts) != 0)
137 puts ("going to cancel tf in-time");
138 if (pthread_cancel (th) != 0)
140 puts ("1st cancel failed");
145 if (pthread_join (th, &status) != 0)
147 puts ("1st join failed");
150 if (status != PTHREAD_CANCELED)
152 puts ("1st thread not canceled");
158 puts ("tf cleanup handler not called");
163 puts ("tf cleanup handler called more than once");
169 if (pthread_create (&th, NULL, tf2, &a) != 0)
171 puts ("2nd create failed");
175 r = pthread_barrier_wait (&b);
176 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
178 puts ("2nd barrier_wait failed");
183 ts.tv_nsec = 100000000;
184 while (nanosleep (&ts, &ts) != 0)
187 puts ("going to cancel tf2 in-time");
188 if (pthread_cancel (th) != 0)
190 puts ("2nd cancel failed");
194 if (pthread_join (th, &status) != 0)
196 puts ("2nd join failed");
199 if (status != PTHREAD_CANCELED)
201 puts ("2nd thread not canceled");
207 puts ("tf2 cleanup handler not called");
212 puts ("tf2 cleanup handler called more than once");
216 puts ("in-time cancellation succeeded");
219 if (aio_cancel (fds[0], &a) != AIO_CANCELED)
221 puts ("aio_cancel failed");
222 /* If aio_cancel failed, we cannot reuse aiocb a. */
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);
235 puts ("could not allocate memory for pipe write");
239 memset (ap, '\0', sizeof (*ap));
240 ap->aio_fildes = fds[1];
242 ap->aio_nbytes = len2;
243 if (aio_write (ap) != 0)
245 puts ("aio_write failed");
249 if (pthread_create (&th, NULL, tf, ap) != 0)
251 puts ("3rd create failed");
255 puts ("going to cancel tf early");
256 if (pthread_cancel (th) != 0)
258 puts ("3rd cancel failed");
262 r = pthread_barrier_wait (&b);
263 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
265 puts ("3rd barrier_wait failed");
269 if (pthread_join (th, &status) != 0)
271 puts ("3rd join failed");
274 if (status != PTHREAD_CANCELED)
276 puts ("3rd thread not canceled");
282 puts ("tf cleanup handler not called");
287 puts ("tf cleanup handler called more than once");
293 if (pthread_create (&th, NULL, tf2, ap) != 0)
295 puts ("4th create failed");
299 puts ("going to cancel tf2 early");
300 if (pthread_cancel (th) != 0)
302 puts ("4th cancel failed");
306 r = pthread_barrier_wait (&b);
307 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
309 puts ("4th barrier_wait failed");
313 if (pthread_join (th, &status) != 0)
315 puts ("4th join failed");
318 if (status != PTHREAD_CANCELED)
320 puts ("4th thread not canceled");
326 puts ("tf2 cleanup handler not called");
331 puts ("tf2 cleanup handler called more than once");
335 puts ("early cancellation succeeded");
340 #define TEST_FUNCTION do_test ()
341 #include "../test-skeleton.c"