Correctly handle %p in wprintf (BZ #16890)
[glibc.git] / nptl / tst-robust1.c
blob9243a79bd5aade903ed1e3f2a2265337f34500e5
1 /* Copyright (C) 2005-2014 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
25 static pthread_mutex_t m1;
26 static pthread_mutex_t m2;
27 static pthread_barrier_t b;
30 #ifndef LOCK
31 # define LOCK(m) pthread_mutex_lock (m)
32 #endif
35 static void *
36 tf (void *arg)
38 long int round = (long int) arg;
40 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0)
42 printf ("%ld: setcancelstate failed\n", round);
43 exit (1);
46 int e = LOCK (&m1);
47 if (e != 0)
49 printf ("%ld: child: mutex_lock m1 failed with error %d\n", round, e);
50 exit (1);
53 e = LOCK (&m2);
54 if (e != 0)
56 printf ("%ld: child: mutex_lock m2 failed with error %d\n", round, e);
57 exit (1);
60 e = pthread_barrier_wait (&b);
61 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
63 printf ("%ld: child: 1st barrier_wait failed\n", round);
64 exit (1);
67 e = pthread_barrier_wait (&b);
68 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
70 printf ("%ld: child: 2nd barrier_wait failed\n", round);
71 exit (1);
74 pthread_testcancel ();
76 printf ("%ld: testcancel returned\n", round);
77 exit (1);
81 static int
82 do_test (void)
84 #ifdef PREPARE_TMO
85 PREPARE_TMO;
86 #endif
88 pthread_mutexattr_t a;
89 if (pthread_mutexattr_init (&a) != 0)
91 puts ("mutexattr_init failed");
92 return 1;
94 if (pthread_mutexattr_setrobust_np (&a, PTHREAD_MUTEX_ROBUST_NP) != 0)
96 puts ("mutexattr_setrobust failed");
97 return 1;
100 #ifdef ENABLE_PI
101 if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
103 puts ("pthread_mutexattr_setprotocol failed");
104 return 1;
106 else
108 int e = pthread_mutex_init (&m1, &a);
109 if (e == ENOTSUP)
111 puts ("PI robust mutexes not supported");
112 return 0;
114 else if (e != 0)
116 puts ("mutex_init m1 failed");
117 return 1;
119 pthread_mutex_destroy (&m1);
121 #endif
123 #ifndef NOT_CONSISTENT
124 if (pthread_mutex_init (&m1, &a) != 0)
126 puts ("mutex_init m1 failed");
127 return 1;
130 if (pthread_mutex_init (&m2, &a) != 0)
132 puts ("mutex_init m2 failed");
133 return 1;
135 #endif
137 if (pthread_barrier_init (&b, NULL, 2) != 0)
139 puts ("barrier_init failed");
140 return 1;
143 for (long int round = 1; round < 5; ++round)
145 #ifdef NOT_CONSISTENT
146 if (pthread_mutex_init (&m1 , &a) != 0)
148 puts ("mutex_init m1 failed");
149 return 1;
151 if (pthread_mutex_init (&m2 , &a) != 0)
153 puts ("mutex_init m2 failed");
154 return 1;
156 #endif
158 pthread_t th;
159 if (pthread_create (&th, NULL, tf, (void *) round) != 0)
161 printf ("%ld: create failed\n", round);
162 return 1;
165 int e = pthread_barrier_wait (&b);
166 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
168 printf ("%ld: parent: 1st barrier_wait failed\n", round);
169 return 1;
172 if (pthread_cancel (th) != 0)
174 printf ("%ld: cancel failed\n", round);
175 return 1;
178 e = pthread_barrier_wait (&b);
179 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
181 printf ("%ld: parent: 2nd barrier_wait failed\n", round);
182 return 1;
185 #ifndef AFTER_JOIN
186 if (round & 1)
187 #endif
189 void *res;
190 if (pthread_join (th, &res) != 0)
192 printf ("%ld: join failed\n", round);
193 return 1;
195 if (res != PTHREAD_CANCELED)
197 printf ("%ld: thread not canceled\n", round);
198 return 1;
202 e = LOCK (&m1);
203 if (e == 0)
205 printf ("%ld: parent: mutex_lock m1 succeeded\n", round);
206 return 1;
208 if (e != EOWNERDEAD)
210 printf ("%ld: parent: mutex_lock m1 returned wrong code\n", round);
211 return 1;
214 e = LOCK (&m2);
215 if (e == 0)
217 printf ("%ld: parent: mutex_lock m2 succeeded\n", round);
218 return 1;
220 if (e != EOWNERDEAD)
222 printf ("%ld: parent: mutex_lock m2 returned wrong code\n", round);
223 return 1;
226 #ifndef AFTER_JOIN
227 if ((round & 1) == 0)
229 void *res;
230 if (pthread_join (th, &res) != 0)
232 printf ("%ld: join failed\n", round);
233 return 1;
235 if (res != PTHREAD_CANCELED)
237 printf ("%ld: thread not canceled\n", round);
238 return 1;
241 #endif
243 #ifndef NOT_CONSISTENT
244 e = pthread_mutex_consistent_np (&m1);
245 if (e != 0)
247 printf ("%ld: mutex_consistent m1 failed with error %d\n", round, e);
248 return 1;
251 e = pthread_mutex_consistent_np (&m2);
252 if (e != 0)
254 printf ("%ld: mutex_consistent m2 failed with error %d\n", round, e);
255 return 1;
257 #endif
259 e = pthread_mutex_unlock (&m1);
260 if (e != 0)
262 printf ("%ld: mutex_unlock m1 failed with %d\n", round, e);
263 return 1;
266 e = pthread_mutex_unlock (&m2);
267 if (e != 0)
269 printf ("%ld: mutex_unlock m2 failed with %d\n", round, e);
270 return 1;
273 #ifdef NOT_CONSISTENT
274 e = LOCK (&m1);
275 if (e == 0)
277 printf ("%ld: locking inconsistent mutex m1 succeeded\n", round);
278 return 1;
280 if (e != ENOTRECOVERABLE)
282 printf ("%ld: locking inconsistent mutex m1 failed with error %d\n",
283 round, e);
284 return 1;
287 if (pthread_mutex_destroy (&m1) != 0)
289 puts ("mutex_destroy m1 failed");
290 return 1;
293 e = LOCK (&m2);
294 if (e == 0)
296 printf ("%ld: locking inconsistent mutex m2 succeeded\n", round);
297 return 1;
299 if (e != ENOTRECOVERABLE)
301 printf ("%ld: locking inconsistent mutex m2 failed with error %d\n",
302 round, e);
303 return 1;
306 if (pthread_mutex_destroy (&m2) != 0)
308 puts ("mutex_destroy m2 failed");
309 return 1;
311 #endif
314 #ifndef NOT_CONSISTENT
315 if (pthread_mutex_destroy (&m1) != 0)
317 puts ("mutex_destroy m1 failed");
318 return 1;
321 if (pthread_mutex_destroy (&m2) != 0)
323 puts ("mutex_destroy m2 failed");
324 return 1;
326 #endif
328 if (pthread_mutexattr_destroy (&a) != 0)
330 puts ("mutexattr_destroy failed");
331 return 1;
334 return 0;
337 #define TEST_FUNCTION do_test ()
338 #include "../test-skeleton.c"