file tst-cputimer2.c was initially added on branch fedora-branch.
[glibc/history.git] / nptl / tst-robust1.c
blobbc48700e4a020fb0f7a2db639e6124b3907284e3
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;
101 #ifdef ENABLE_PI
102 if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
104 puts ("pthread_mutexattr_setprotocol failed");
105 return 1;
107 else
109 int e = pthread_mutex_init (&m1, &a);
110 if (e == ENOTSUP)
112 puts ("PI robust mutexes not supported");
113 return 0;
115 else if (e != 0)
117 puts ("mutex_init m1 failed");
118 return 1;
120 pthread_mutex_destroy (&m1);
122 #endif
124 #ifndef NOT_CONSISTENT
125 if (pthread_mutex_init (&m1, &a) != 0)
127 puts ("mutex_init m1 failed");
128 return 1;
131 if (pthread_mutex_init (&m2, &a) != 0)
133 puts ("mutex_init m2 failed");
134 return 1;
136 #endif
138 if (pthread_barrier_init (&b, NULL, 2) != 0)
140 puts ("barrier_init failed");
141 return 1;
144 for (long int round = 1; round < 5; ++round)
146 #ifdef NOT_CONSISTENT
147 if (pthread_mutex_init (&m1 , &a) != 0)
149 puts ("mutex_init m1 failed");
150 return 1;
152 if (pthread_mutex_init (&m2 , &a) != 0)
154 puts ("mutex_init m2 failed");
155 return 1;
157 #endif
159 pthread_t th;
160 if (pthread_create (&th, NULL, tf, (void *) round) != 0)
162 printf ("%ld: create failed\n", round);
163 return 1;
166 int e = pthread_barrier_wait (&b);
167 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
169 printf ("%ld: parent: 1st barrier_wait failed\n", round);
170 return 1;
173 if (pthread_cancel (th) != 0)
175 printf ("%ld: cancel failed\n", round);
176 return 1;
179 e = pthread_barrier_wait (&b);
180 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
182 printf ("%ld: parent: 2nd barrier_wait failed\n", round);
183 return 1;
186 #ifndef AFTER_JOIN
187 if (round & 1)
188 #endif
190 void *res;
191 if (pthread_join (th, &res) != 0)
193 printf ("%ld: join failed\n", round);
194 return 1;
196 if (res != PTHREAD_CANCELED)
198 printf ("%ld: thread not canceled\n", round);
199 return 1;
203 e = LOCK (&m1);
204 if (e == 0)
206 printf ("%ld: parent: mutex_lock m1 succeeded\n", round);
207 return 1;
209 if (e != EOWNERDEAD)
211 printf ("%ld: parent: mutex_lock m1 returned wrong code\n", round);
212 return 1;
215 e = LOCK (&m2);
216 if (e == 0)
218 printf ("%ld: parent: mutex_lock m2 succeeded\n", round);
219 return 1;
221 if (e != EOWNERDEAD)
223 printf ("%ld: parent: mutex_lock m2 returned wrong code\n", round);
224 return 1;
227 #ifndef AFTER_JOIN
228 if ((round & 1) == 0)
230 void *res;
231 if (pthread_join (th, &res) != 0)
233 printf ("%ld: join failed\n", round);
234 return 1;
236 if (res != PTHREAD_CANCELED)
238 printf ("%ld: thread not canceled\n", round);
239 return 1;
242 #endif
244 #ifndef NOT_CONSISTENT
245 e = pthread_mutex_consistent_np (&m1);
246 if (e != 0)
248 printf ("%ld: mutex_consistent m1 failed with error %d\n", round, e);
249 return 1;
252 e = pthread_mutex_consistent_np (&m2);
253 if (e != 0)
255 printf ("%ld: mutex_consistent m2 failed with error %d\n", round, e);
256 return 1;
258 #endif
260 e = pthread_mutex_unlock (&m1);
261 if (e != 0)
263 printf ("%ld: mutex_unlock m1 failed with %d\n", round, e);
264 return 1;
267 e = pthread_mutex_unlock (&m2);
268 if (e != 0)
270 printf ("%ld: mutex_unlock m2 failed with %d\n", round, e);
271 return 1;
274 #ifdef NOT_CONSISTENT
275 e = LOCK (&m1);
276 if (e == 0)
278 printf ("%ld: locking inconsistent mutex m1 succeeded\n", round);
279 return 1;
281 if (e != ENOTRECOVERABLE)
283 printf ("%ld: locking inconsistent mutex m1 failed with error %d\n",
284 round, e);
285 return 1;
288 if (pthread_mutex_destroy (&m1) != 0)
290 puts ("mutex_destroy m1 failed");
291 return 1;
294 e = LOCK (&m2);
295 if (e == 0)
297 printf ("%ld: locking inconsistent mutex m2 succeeded\n", round);
298 return 1;
300 if (e != ENOTRECOVERABLE)
302 printf ("%ld: locking inconsistent mutex m2 failed with error %d\n",
303 round, e);
304 return 1;
307 if (pthread_mutex_destroy (&m2) != 0)
309 puts ("mutex_destroy m2 failed");
310 return 1;
312 #endif
315 #ifndef NOT_CONSISTENT
316 if (pthread_mutex_destroy (&m1) != 0)
318 puts ("mutex_destroy m1 failed");
319 return 1;
322 if (pthread_mutex_destroy (&m2) != 0)
324 puts ("mutex_destroy m2 failed");
325 return 1;
327 #endif
329 if (pthread_mutexattr_destroy (&a) != 0)
331 puts ("mutexattr_destroy failed");
332 return 1;
335 return 0;
338 #define TEST_FUNCTION do_test ()
339 #include "../test-skeleton.c"