Updated to fedora-glibc-20080524T2218
[glibc.git] / nptl / tst-cancel8.c
blobfc836627d013b17eb9d0889985ff0d794fcc3dd1
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
18 02111-1307 USA. */
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
26 static pthread_barrier_t bar;
28 static int global;
31 static void
32 cleanup (void *arg)
34 global = 1;
38 static void *
39 tf (void *arg)
41 /* Enable cancellation, but defer it. */
42 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0)
44 puts ("setcancelstate failed");
45 exit (1);
47 if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
49 puts ("setcanceltype failed");
50 exit (1);
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");
61 exit (1);
64 /* And again. Once this is done the main thread should have canceled
65 this thread. */
66 r = pthread_barrier_wait (&bar);
67 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
69 puts ("tf: second barrier_wait failed");
70 exit (1);
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. */
80 return NULL;
84 static int
85 do_test (void)
87 if (pthread_barrier_init (&bar, NULL, 2) != 0)
89 puts ("barrier_init failed");
90 exit (1);
93 pthread_t th;
94 if (pthread_create (&th, NULL, tf, NULL) != 0)
96 puts ("pthread_create failed");
97 return 1;
100 int r = pthread_barrier_wait (&bar);
101 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
103 puts ("first barrier_wait failed");
104 exit (1);
107 if (pthread_cancel (th) != 0)
109 puts ("pthread_cancel failed");
110 return 1;
113 r = pthread_barrier_wait (&bar);
114 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
116 puts ("second barrier_wait failed");
117 exit (1);
120 void *result;
121 if (pthread_join (th, &result) != 0)
123 puts ("pthread_join failed");
124 return 1;
127 if (result != PTHREAD_CANCELED)
129 puts ("thread was not canceled");
130 exit (1);
133 if (global != 0)
135 puts ("cancellation handler has been called");
136 exit (1);
139 return 0;
142 #define TEST_FUNCTION do_test ()
143 #include "../test-skeleton.c"