1 /* Copyright (C) 2003 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 static pthread_barrier_t bar
;
41 /* Enable cancellation, but defer it. */
42 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE
, NULL
) != 0)
44 puts ("setcancelstate failed");
47 if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED
, NULL
) != 0)
49 puts ("setcanceltype failed");
53 /* Add cleanup handler. */
54 pthread_cleanup_push (cleanup
, NULL
);
56 /* Synchronize with the main thread. */
57 int r
= pthread_barrier_wait (&bar
);
58 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
60 puts ("tf: first barrier_wait failed");
64 /* And again. Once this is done the main thread should have canceled
66 r
= pthread_barrier_wait (&bar
);
67 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
69 puts ("tf: second barrier_wait failed");
73 /* Remove the cleanup handler without executing it. */
74 pthread_cleanup_pop (0);
76 /* Now react on the cancellation. */
77 pthread_testcancel ();
79 /* This call should never return. */
87 if (pthread_barrier_init (&bar
, NULL
, 2) != 0)
89 puts ("barrier_init failed");
94 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
96 puts ("pthread_create failed");
100 int r
= pthread_barrier_wait (&bar
);
101 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
103 puts ("first barrier_wait failed");
107 if (pthread_cancel (th
) != 0)
109 puts ("pthread_cancel failed");
113 r
= pthread_barrier_wait (&bar
);
114 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
116 puts ("second barrier_wait failed");
121 if (pthread_join (th
, &result
) != 0)
123 puts ("pthread_join failed");
127 if (result
!= PTHREAD_CANCELED
)
129 puts ("thread was not canceled");
135 puts ("cancellation handler has been called");
142 #define TEST_FUNCTION do_test ()
143 #include "../test-skeleton.c"