Support C11 __STDC_SOURCE__ and _ISOC11_SOURCE
[glibc.git] / nptl / tst-cancel18.c
blob15e9ddfbbc9be9093a41bbd1445e8c538b42b9b4
1 /* Copyright (C) 2003 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 <errno.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25 #include <unistd.h>
28 static pthread_barrier_t b;
31 /* Cleanup handling test. */
32 static int cl_called;
34 static void
35 cl (void *arg)
37 ++cl_called;
41 static void *
42 tf (void *arg)
44 int r = pthread_barrier_wait (&b);
45 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
47 puts ("barrier_wait failed");
48 exit (1);
51 pthread_cleanup_push (cl, NULL);
53 struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
54 TEMP_FAILURE_RETRY (clock_nanosleep (CLOCK_REALTIME, 0, &ts, &ts));
56 pthread_cleanup_pop (0);
58 puts ("clock_nanosleep returned");
60 exit (1);
64 static int
65 do_test (void)
67 if (pthread_barrier_init (&b, NULL, 2) != 0)
69 puts ("barrier_init failed");
70 return 1;
73 pthread_t th;
74 if (pthread_create (&th, NULL, tf, NULL) != 0)
76 puts ("1st create failed");
77 return 1;
80 int r = pthread_barrier_wait (&b);
81 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
83 puts ("barrier_wait failed");
84 exit (1);
87 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
88 while (nanosleep (&ts, &ts) != 0)
89 continue;
91 puts ("going to cancel in-time");
92 if (pthread_cancel (th) != 0)
94 puts ("1st cancel failed");
95 return 1;
98 void *status;
99 if (pthread_join (th, &status) != 0)
101 puts ("1st join failed");
102 return 1;
104 if (status != PTHREAD_CANCELED)
106 puts ("1st thread not canceled");
107 return 1;
110 if (cl_called == 0)
112 puts ("cleanup handler not called");
113 return 1;
115 if (cl_called > 1)
117 puts ("cleanup handler called more than once");
118 return 1;
121 puts ("in-time cancellation succeeded");
124 cl_called = 0;
126 if (pthread_create (&th, NULL, tf, NULL) != 0)
128 puts ("2nd create failed");
129 return 1;
132 puts ("going to cancel early");
133 if (pthread_cancel (th) != 0)
135 puts ("2nd cancel failed");
136 return 1;
139 r = pthread_barrier_wait (&b);
140 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
142 puts ("barrier_wait failed");
143 exit (1);
146 if (pthread_join (th, &status) != 0)
148 puts ("2nd join failed");
149 return 1;
151 if (status != PTHREAD_CANCELED)
153 puts ("2nd thread not canceled");
154 return 1;
157 if (cl_called == 0)
159 printf ("cleanup handler not called\n");
160 return 1;
162 if (cl_called > 1)
164 printf ("cleanup handler called more than once\n");
165 return 1;
168 puts ("early cancellation succeeded");
170 return 0;
173 #define TEST_FUNCTION do_test ()
174 #include "../test-skeleton.c"