1 /* Copyright (C) 2005-2015 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/>. */
25 static pthread_mutex_t m1
;
26 static pthread_mutex_t m2
;
27 static pthread_barrier_t b
;
31 # define LOCK(m) pthread_mutex_lock (m)
38 long int round
= (long int) arg
;
40 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE
, NULL
) != 0)
42 printf ("%ld: setcancelstate failed\n", round
);
49 printf ("%ld: child: mutex_lock m1 failed with error %d\n", round
, e
);
56 printf ("%ld: child: mutex_lock m2 failed with error %d\n", round
, e
);
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
);
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
);
74 pthread_testcancel ();
76 printf ("%ld: testcancel returned\n", round
);
88 pthread_mutexattr_t a
;
89 if (pthread_mutexattr_init (&a
) != 0)
91 puts ("mutexattr_init failed");
94 if (pthread_mutexattr_setrobust_np (&a
, PTHREAD_MUTEX_ROBUST_NP
) != 0)
96 puts ("mutexattr_setrobust failed");
101 if (pthread_mutexattr_setprotocol (&a
, PTHREAD_PRIO_INHERIT
) != 0)
103 puts ("pthread_mutexattr_setprotocol failed");
108 int e
= pthread_mutex_init (&m1
, &a
);
111 puts ("PI robust mutexes not supported");
116 puts ("mutex_init m1 failed");
119 pthread_mutex_destroy (&m1
);
123 #ifndef NOT_CONSISTENT
124 if (pthread_mutex_init (&m1
, &a
) != 0)
126 puts ("mutex_init m1 failed");
130 if (pthread_mutex_init (&m2
, &a
) != 0)
132 puts ("mutex_init m2 failed");
137 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
139 puts ("barrier_init failed");
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");
151 if (pthread_mutex_init (&m2
, &a
) != 0)
153 puts ("mutex_init m2 failed");
159 if (pthread_create (&th
, NULL
, tf
, (void *) round
) != 0)
161 printf ("%ld: create failed\n", round
);
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
);
172 if (pthread_cancel (th
) != 0)
174 printf ("%ld: cancel failed\n", round
);
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
);
190 if (pthread_join (th
, &res
) != 0)
192 printf ("%ld: join failed\n", round
);
195 if (res
!= PTHREAD_CANCELED
)
197 printf ("%ld: thread not canceled\n", round
);
205 printf ("%ld: parent: mutex_lock m1 succeeded\n", round
);
210 printf ("%ld: parent: mutex_lock m1 returned wrong code\n", round
);
217 printf ("%ld: parent: mutex_lock m2 succeeded\n", round
);
222 printf ("%ld: parent: mutex_lock m2 returned wrong code\n", round
);
227 if ((round
& 1) == 0)
230 if (pthread_join (th
, &res
) != 0)
232 printf ("%ld: join failed\n", round
);
235 if (res
!= PTHREAD_CANCELED
)
237 printf ("%ld: thread not canceled\n", round
);
243 #ifndef NOT_CONSISTENT
244 e
= pthread_mutex_consistent_np (&m1
);
247 printf ("%ld: mutex_consistent m1 failed with error %d\n", round
, e
);
251 e
= pthread_mutex_consistent_np (&m2
);
254 printf ("%ld: mutex_consistent m2 failed with error %d\n", round
, e
);
259 e
= pthread_mutex_unlock (&m1
);
262 printf ("%ld: mutex_unlock m1 failed with %d\n", round
, e
);
266 e
= pthread_mutex_unlock (&m2
);
269 printf ("%ld: mutex_unlock m2 failed with %d\n", round
, e
);
273 #ifdef NOT_CONSISTENT
277 printf ("%ld: locking inconsistent mutex m1 succeeded\n", round
);
280 if (e
!= ENOTRECOVERABLE
)
282 printf ("%ld: locking inconsistent mutex m1 failed with error %d\n",
287 if (pthread_mutex_destroy (&m1
) != 0)
289 puts ("mutex_destroy m1 failed");
296 printf ("%ld: locking inconsistent mutex m2 succeeded\n", round
);
299 if (e
!= ENOTRECOVERABLE
)
301 printf ("%ld: locking inconsistent mutex m2 failed with error %d\n",
306 if (pthread_mutex_destroy (&m2
) != 0)
308 puts ("mutex_destroy m2 failed");
314 #ifndef NOT_CONSISTENT
315 if (pthread_mutex_destroy (&m1
) != 0)
317 puts ("mutex_destroy m1 failed");
321 if (pthread_mutex_destroy (&m2
) != 0)
323 puts ("mutex_destroy m2 failed");
328 if (pthread_mutexattr_destroy (&a
) != 0)
330 puts ("mutexattr_destroy failed");
337 #define TEST_FUNCTION do_test ()
338 #include "../test-skeleton.c"