1 /* Copyright (C) 2003-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
26 static pthread_barrier_t b
;
29 /* Cleanup handling test. */
42 int r
= pthread_barrier_wait (&b
);
43 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
45 puts ("barrier_wait failed");
49 pthread_cleanup_push (cl
, NULL
);
51 struct timespec ts
= { .tv_sec
= arg
== NULL
? 10000000 : 0, .tv_nsec
= 0 };
52 TEMP_FAILURE_RETRY (clock_nanosleep (CLOCK_REALTIME
, 0, &ts
, &ts
));
54 pthread_cleanup_pop (0);
56 puts ("clock_nanosleep returned");
65 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
67 puts ("barrier_init failed");
72 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
74 puts ("1st create failed");
78 int r
= pthread_barrier_wait (&b
);
79 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
81 puts ("barrier_wait failed");
85 struct timespec ts
= { .tv_sec
= 0, .tv_nsec
= 100000000 };
86 while (nanosleep (&ts
, &ts
) != 0)
89 puts ("going to cancel in-time");
90 if (pthread_cancel (th
) != 0)
92 puts ("1st cancel failed");
97 if (pthread_join (th
, &status
) != 0)
99 puts ("1st join failed");
102 if (status
!= PTHREAD_CANCELED
)
104 puts ("1st thread not canceled");
110 puts ("cleanup handler not called");
115 puts ("cleanup handler called more than once");
119 puts ("in-time cancellation succeeded");
124 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
126 puts ("2nd create failed");
130 puts ("going to cancel early");
131 if (pthread_cancel (th
) != 0)
133 puts ("2nd cancel failed");
137 r
= pthread_barrier_wait (&b
);
138 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
140 puts ("barrier_wait failed");
144 if (pthread_join (th
, &status
) != 0)
146 puts ("2nd join failed");
149 if (status
!= PTHREAD_CANCELED
)
151 puts ("2nd thread not canceled");
157 printf ("cleanup handler not called\n");
162 printf ("cleanup handler called more than once\n");
166 puts ("early cancellation succeeded");
171 #define TEST_FUNCTION do_test ()
172 #include "../test-skeleton.c"