Add more tests of csqrt.
[glibc.git] / nptl / tst-cancel8.c
blob02ea5945345a77f6cdb26d2328ce2281a126734b
1 /* Copyright (C) 2003-2015 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/>. */
19 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
25 static pthread_barrier_t bar;
27 static int global;
30 static void
31 cleanup (void *arg)
33 global = 1;
37 static void *
38 tf (void *arg)
40 /* Enable cancellation, but defer it. */
41 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0)
43 puts ("setcancelstate failed");
44 exit (1);
46 if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
48 puts ("setcanceltype failed");
49 exit (1);
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");
60 exit (1);
63 /* And again. Once this is done the main thread should have canceled
64 this thread. */
65 r = pthread_barrier_wait (&bar);
66 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
68 puts ("tf: second barrier_wait failed");
69 exit (1);
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. */
79 return NULL;
83 static int
84 do_test (void)
86 if (pthread_barrier_init (&bar, NULL, 2) != 0)
88 puts ("barrier_init failed");
89 exit (1);
92 pthread_t th;
93 if (pthread_create (&th, NULL, tf, NULL) != 0)
95 puts ("pthread_create failed");
96 return 1;
99 int r = pthread_barrier_wait (&bar);
100 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
102 puts ("first barrier_wait failed");
103 exit (1);
106 if (pthread_cancel (th) != 0)
108 puts ("pthread_cancel failed");
109 return 1;
112 r = pthread_barrier_wait (&bar);
113 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
115 puts ("second barrier_wait failed");
116 exit (1);
119 void *result;
120 if (pthread_join (th, &result) != 0)
122 puts ("pthread_join failed");
123 return 1;
126 if (result != PTHREAD_CANCELED)
128 puts ("thread was not canceled");
129 exit (1);
132 if (global != 0)
134 puts ("cancellation handler has been called");
135 exit (1);
138 return 0;
141 #define TEST_FUNCTION do_test ()
142 #include "../test-skeleton.c"