Fix formatting
[glibc.git] / nptl / tst-cancel18.c
blob92da180fc95db8f74db63753474c4b5bd01e68c0
1 /* Copyright (C) 2003-2014 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <time.h>
24 #include <unistd.h>
27 static pthread_barrier_t b;
30 /* Cleanup handling test. */
31 static int cl_called;
33 static void
34 cl (void *arg)
36 ++cl_called;
40 static void *
41 tf (void *arg)
43 int r = pthread_barrier_wait (&b);
44 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
46 puts ("barrier_wait failed");
47 exit (1);
50 pthread_cleanup_push (cl, NULL);
52 struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
53 TEMP_FAILURE_RETRY (clock_nanosleep (CLOCK_REALTIME, 0, &ts, &ts));
55 pthread_cleanup_pop (0);
57 puts ("clock_nanosleep returned");
59 exit (1);
63 static int
64 do_test (void)
66 if (pthread_barrier_init (&b, NULL, 2) != 0)
68 puts ("barrier_init failed");
69 return 1;
72 pthread_t th;
73 if (pthread_create (&th, NULL, tf, NULL) != 0)
75 puts ("1st create failed");
76 return 1;
79 int r = pthread_barrier_wait (&b);
80 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
82 puts ("barrier_wait failed");
83 exit (1);
86 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
87 while (nanosleep (&ts, &ts) != 0)
88 continue;
90 puts ("going to cancel in-time");
91 if (pthread_cancel (th) != 0)
93 puts ("1st cancel failed");
94 return 1;
97 void *status;
98 if (pthread_join (th, &status) != 0)
100 puts ("1st join failed");
101 return 1;
103 if (status != PTHREAD_CANCELED)
105 puts ("1st thread not canceled");
106 return 1;
109 if (cl_called == 0)
111 puts ("cleanup handler not called");
112 return 1;
114 if (cl_called > 1)
116 puts ("cleanup handler called more than once");
117 return 1;
120 puts ("in-time cancellation succeeded");
123 cl_called = 0;
125 if (pthread_create (&th, NULL, tf, NULL) != 0)
127 puts ("2nd create failed");
128 return 1;
131 puts ("going to cancel early");
132 if (pthread_cancel (th) != 0)
134 puts ("2nd cancel failed");
135 return 1;
138 r = pthread_barrier_wait (&b);
139 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
141 puts ("barrier_wait failed");
142 exit (1);
145 if (pthread_join (th, &status) != 0)
147 puts ("2nd join failed");
148 return 1;
150 if (status != PTHREAD_CANCELED)
152 puts ("2nd thread not canceled");
153 return 1;
156 if (cl_called == 0)
158 printf ("cleanup handler not called\n");
159 return 1;
161 if (cl_called > 1)
163 printf ("cleanup handler called more than once\n");
164 return 1;
167 puts ("early cancellation succeeded");
169 return 0;
172 #define TEST_FUNCTION do_test ()
173 #include "../test-skeleton.c"