[BZ #2509]
[glibc.git] / nptl / tst-robust1.c
blob9806ca467a6d62492de72b4789636e14e5e3e8b5
1 /* Copyright (C) 2005, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2005.
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 <errno.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
26 static pthread_mutex_t m1;
27 static pthread_mutex_t m2;
28 static pthread_barrier_t b;
31 #ifndef LOCK
32 # define LOCK(m) pthread_mutex_lock (m)
33 #endif
36 static void *
37 tf (void *arg)
39 long int round = (long int) arg;
41 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0)
43 printf ("%ld: setcancelstate failed\n", round);
44 exit (1);
47 int e = LOCK (&m1);
48 if (e != 0)
50 printf ("%ld: child: mutex_lock m1 failed with error %d\n", round, e);
51 exit (1);
54 e = LOCK (&m2);
55 if (e != 0)
57 printf ("%ld: child: mutex_lock m2 failed with error %d\n", round, e);
58 exit (1);
61 e = pthread_barrier_wait (&b);
62 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
64 printf ("%ld: child: 1st barrier_wait failed\n", round);
65 exit (1);
68 e = pthread_barrier_wait (&b);
69 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
71 printf ("%ld: child: 2nd barrier_wait failed\n", round);
72 exit (1);
75 pthread_testcancel ();
77 printf ("%ld: testcancel returned\n", round);
78 exit (1);
82 static int
83 do_test (void)
85 #ifdef PREPARE_TMO
86 PREPARE_TMO;
87 #endif
89 pthread_mutexattr_t a;
90 if (pthread_mutexattr_init (&a) != 0)
92 puts ("mutexattr_init failed");
93 return 1;
95 if (pthread_mutexattr_setrobust_np (&a, PTHREAD_MUTEX_ROBUST_NP) != 0)
97 puts ("mutexattr_setrobust failed");
98 return 1;
100 #ifndef NOT_CONSISTENT
101 if (pthread_mutex_init (&m1, &a) != 0)
103 puts ("mutex_init m1 failed");
104 return 1;
107 if (pthread_mutex_init (&m2, &a) != 0)
109 puts ("mutex_init m2 failed");
110 return 1;
112 #endif
114 if (pthread_barrier_init (&b, NULL, 2) != 0)
116 puts ("barrier_init failed");
117 return 1;
120 for (long int round = 1; round < 5; ++round)
122 #ifdef NOT_CONSISTENT
123 if (pthread_mutex_init (&m1 , &a) != 0)
125 puts ("mutex_init m1 failed");
126 return 1;
128 if (pthread_mutex_init (&m2 , &a) != 0)
130 puts ("mutex_init m2 failed");
131 return 1;
133 #endif
135 pthread_t th;
136 if (pthread_create (&th, NULL, tf, (void *) round) != 0)
138 printf ("%ld: create failed\n", round);
139 return 1;
142 int e = pthread_barrier_wait (&b);
143 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
145 printf ("%ld: parent: 1st barrier_wait failed\n", round);
146 return 1;
149 if (pthread_cancel (th) != 0)
151 printf ("%ld: cancel failed\n", round);
152 return 1;
155 e = pthread_barrier_wait (&b);
156 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
158 printf ("%ld: parent: 2nd barrier_wait failed\n", round);
159 return 1;
162 #ifndef AFTER_JOIN
163 if (round & 1)
164 #endif
166 void *res;
167 if (pthread_join (th, &res) != 0)
169 printf ("%ld: join failed\n", round);
170 return 1;
172 if (res != PTHREAD_CANCELED)
174 printf ("%ld: thread not canceled\n", round);
175 return 1;
179 e = LOCK (&m1);
180 if (e == 0)
182 printf ("%ld: parent: mutex_lock m1 succeeded\n", round);
183 return 1;
185 if (e != EOWNERDEAD)
187 printf ("%ld: parent: mutex_lock m1 returned wrong code\n", round);
188 return 1;
191 e = LOCK (&m2);
192 if (e == 0)
194 printf ("%ld: parent: mutex_lock m2 succeeded\n", round);
195 return 1;
197 if (e != EOWNERDEAD)
199 printf ("%ld: parent: mutex_lock m2 returned wrong code\n", round);
200 return 1;
203 #ifndef AFTER_JOIN
204 if ((round & 1) == 0)
206 void *res;
207 if (pthread_join (th, &res) != 0)
209 printf ("%ld: join failed\n", round);
210 return 1;
212 if (res != PTHREAD_CANCELED)
214 printf ("%ld: thread not canceled\n", round);
215 return 1;
218 #endif
220 #ifndef NOT_CONSISTENT
221 e = pthread_mutex_consistent_np (&m1);
222 if (e != 0)
224 printf ("%ld: mutex_consistent m1 failed with error %d\n", round, e);
225 return 1;
228 e = pthread_mutex_consistent_np (&m2);
229 if (e != 0)
231 printf ("%ld: mutex_consistent m2 failed with error %d\n", round, e);
232 return 1;
234 #endif
236 e = pthread_mutex_unlock (&m1);
237 if (e != 0)
239 printf ("%ld: mutex_unlock m1 failed\n", round);
240 return 1;
243 e = pthread_mutex_unlock (&m2);
244 if (e != 0)
246 printf ("%ld: mutex_unlock m2 failed\n", round);
247 return 1;
250 #ifdef NOT_CONSISTENT
251 e = LOCK (&m1);
252 if (e == 0)
254 printf ("%ld: locking inconsistent mutex m1 succeeded\n", round);
255 return 1;
257 if (e != ENOTRECOVERABLE)
259 printf ("%ld: locking inconsistent mutex m1 failed with error %d\n",
260 round, e);
261 return 1;
264 if (pthread_mutex_destroy (&m1) != 0)
266 puts ("mutex_destroy m1 failed");
267 return 1;
270 e = LOCK (&m2);
271 if (e == 0)
273 printf ("%ld: locking inconsistent mutex m2 succeeded\n", round);
274 return 1;
276 if (e != ENOTRECOVERABLE)
278 printf ("%ld: locking inconsistent mutex m2 failed with error %d\n",
279 round, e);
280 return 1;
283 if (pthread_mutex_destroy (&m2) != 0)
285 puts ("mutex_destroy m2 failed");
286 return 1;
288 #endif
291 #ifndef NOT_CONSISTENT
292 if (pthread_mutex_destroy (&m1) != 0)
294 puts ("mutex_destroy m1 failed");
295 return 1;
298 if (pthread_mutex_destroy (&m2) != 0)
300 puts ("mutex_destroy m2 failed");
301 return 1;
303 #endif
305 if (pthread_mutexattr_destroy (&a) != 0)
307 puts ("mutexattr_destroy failed");
308 return 1;
311 return 0;
314 #define TEST_FUNCTION do_test ()
315 #include "../test-skeleton.c"