1 /* Copyright (C) 2003-2016 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/>. */
25 static pthread_barrier_t bar
;
40 /* Enable cancellation, but defer it. */
41 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE
, NULL
) != 0)
43 puts ("setcancelstate failed");
46 if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED
, NULL
) != 0)
48 puts ("setcanceltype failed");
52 /* Add cleanup handler. */
53 pthread_cleanup_push (cleanup
, NULL
);
55 /* Synchronize with the main thread. */
56 int r
= pthread_barrier_wait (&bar
);
57 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
59 puts ("tf: first barrier_wait failed");
63 /* And again. Once this is done the main thread should have canceled
65 r
= pthread_barrier_wait (&bar
);
66 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
68 puts ("tf: second barrier_wait failed");
72 /* Remove the cleanup handler without executing it. */
73 pthread_cleanup_pop (0);
75 /* Now react on the cancellation. */
76 pthread_testcancel ();
78 /* This call should never return. */
86 if (pthread_barrier_init (&bar
, NULL
, 2) != 0)
88 puts ("barrier_init failed");
93 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
95 puts ("pthread_create failed");
99 int r
= pthread_barrier_wait (&bar
);
100 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
102 puts ("first barrier_wait failed");
106 if (pthread_cancel (th
) != 0)
108 puts ("pthread_cancel failed");
112 r
= pthread_barrier_wait (&bar
);
113 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
115 puts ("second barrier_wait failed");
120 if (pthread_join (th
, &result
) != 0)
122 puts ("pthread_join failed");
126 if (result
!= PTHREAD_CANCELED
)
128 puts ("thread was not canceled");
134 puts ("cancellation handler has been called");
141 #define TEST_FUNCTION do_test ()
142 #include "../test-skeleton.c"