1 /* Copyright (C) 2003-2022 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/>. */
24 static pthread_barrier_t bar
;
39 /* Enable cancellation, but defer it. */
40 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE
, NULL
) != 0)
42 puts ("setcancelstate failed");
45 if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED
, NULL
) != 0)
47 puts ("setcanceltype failed");
51 /* Add cleanup handler. */
52 pthread_cleanup_push (cleanup
, NULL
);
54 /* Synchronize with the main thread. */
55 int r
= pthread_barrier_wait (&bar
);
56 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
58 puts ("tf: first barrier_wait failed");
62 /* And again. Once this is done the main thread should have canceled
64 r
= pthread_barrier_wait (&bar
);
65 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
67 puts ("tf: second barrier_wait failed");
71 /* Remove the cleanup handler without executing it. */
72 pthread_cleanup_pop (0);
74 /* Now react on the cancellation. */
75 pthread_testcancel ();
77 /* This call should never return. */
85 if (pthread_barrier_init (&bar
, NULL
, 2) != 0)
87 puts ("barrier_init failed");
92 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
94 puts ("pthread_create failed");
98 int r
= pthread_barrier_wait (&bar
);
99 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
101 puts ("first barrier_wait failed");
105 if (pthread_cancel (th
) != 0)
107 puts ("pthread_cancel failed");
111 r
= pthread_barrier_wait (&bar
);
112 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
114 puts ("second barrier_wait failed");
119 if (pthread_join (th
, &result
) != 0)
121 puts ("pthread_join failed");
125 if (result
!= PTHREAD_CANCELED
)
127 puts ("thread was not canceled");
133 puts ("cancellation handler has been called");
140 #define TEST_FUNCTION do_test ()
141 #include "../test-skeleton.c"