chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / nptl / tst-cancel8.c
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.
4
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.
9
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.
14
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.  */
19
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24
25
26 static pthread_barrier_t bar;
27
28 static int global;
29
30
31 static void
32 cleanup (void *arg)
33 {
34   global = 1;
35 }
36
37
38 static void *
39 tf (void *arg)
40 {
41   /* Enable cancellation, but defer it.  */
42   if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0)
43     {
44       puts ("setcancelstate failed");
45       exit (1);
46     }
47   if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
48     {
49       puts ("setcanceltype failed");
50       exit (1);
51     }
52
53   /* Add cleanup handler.  */
54   pthread_cleanup_push (cleanup, NULL);
55
56   /* Synchronize with the main thread.  */
57   int r = pthread_barrier_wait (&bar);
58   if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
59     {
60       puts ("tf: first barrier_wait failed");
61       exit (1);
62     }
63
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)
68     {
69       puts ("tf: second barrier_wait failed");
70       exit (1);
71     }
72
73   /* Remove the cleanup handler without executing it.  */
74   pthread_cleanup_pop (0);
75
76   /* Now react on the cancellation.  */
77   pthread_testcancel ();
78
79   /* This call should never return.  */
80   return NULL;
81 }
82
83
84 static int
85 do_test (void)
86 {
87   if (pthread_barrier_init (&bar, NULL, 2) != 0)
88     {
89       puts ("barrier_init failed");
90       exit (1);
91     }
92
93   pthread_t th;
94   if (pthread_create (&th, NULL, tf, NULL) != 0)
95     {
96       puts ("pthread_create failed");
97       return 1;
98     }
99
100   int r = pthread_barrier_wait (&bar);
101   if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
102     {
103       puts ("first barrier_wait failed");
104       exit (1);
105     }
106
107   if (pthread_cancel (th) != 0)
108     {
109       puts ("pthread_cancel failed");
110       return 1;
111     }
112
113   r = pthread_barrier_wait (&bar);
114   if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
115     {
116       puts ("second barrier_wait failed");
117       exit (1);
118     }
119
120   void *result;
121   if (pthread_join (th, &result) != 0)
122     {
123       puts ("pthread_join failed");
124       return 1;
125     }
126
127   if (result != PTHREAD_CANCELED)
128     {
129       puts ("thread was not canceled");
130       exit (1);
131     }
132
133   if (global != 0)
134     {
135       puts ("cancellation handler has been called");
136       exit (1);
137     }
138
139   return 0;
140 }
141
142 #define TEST_FUNCTION do_test ()
143 #include "../test-skeleton.c"