1 /* Copyright (C) 2003-2017 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/>. */
27 static pthread_barrier_t b
;
30 /* Cleanup handling test. */
43 int r
= pthread_barrier_wait (&b
);
44 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
46 puts ("barrier_wait failed");
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");
66 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
68 puts ("barrier_init failed");
73 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
75 puts ("1st create failed");
79 int r
= pthread_barrier_wait (&b
);
80 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
82 puts ("barrier_wait failed");
86 struct timespec ts
= { .tv_sec
= 0, .tv_nsec
= 100000000 };
87 while (nanosleep (&ts
, &ts
) != 0)
90 puts ("going to cancel in-time");
91 if (pthread_cancel (th
) != 0)
93 puts ("1st cancel failed");
98 if (pthread_join (th
, &status
) != 0)
100 puts ("1st join failed");
103 if (status
!= PTHREAD_CANCELED
)
105 puts ("1st thread not canceled");
111 puts ("cleanup handler not called");
116 puts ("cleanup handler called more than once");
120 puts ("in-time cancellation succeeded");
125 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
127 puts ("2nd create failed");
131 puts ("going to cancel early");
132 if (pthread_cancel (th
) != 0)
134 puts ("2nd cancel failed");
138 r
= pthread_barrier_wait (&b
);
139 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
141 puts ("barrier_wait failed");
145 if (pthread_join (th
, &status
) != 0)
147 puts ("2nd join failed");
150 if (status
!= PTHREAD_CANCELED
)
152 puts ("2nd thread not canceled");
158 printf ("cleanup handler not called\n");
163 printf ("cleanup handler called more than once\n");
167 puts ("early cancellation succeeded");
172 #define TEST_FUNCTION do_test ()
173 #include "../test-skeleton.c"