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
26 static pthread_mutex_t m1
;
27 static pthread_mutex_t m2
;
28 static pthread_barrier_t b
;
32 # define LOCK(m) pthread_mutex_lock (m)
39 long int round
= (long int) arg
;
41 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE
, NULL
) != 0)
43 printf ("%ld: setcancelstate failed\n", round
);
50 printf ("%ld: child: mutex_lock m1 failed with error %d\n", round
, e
);
57 printf ("%ld: child: mutex_lock m2 failed with error %d\n", round
, e
);
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
);
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
);
75 pthread_testcancel ();
77 printf ("%ld: testcancel returned\n", round
);
89 pthread_mutexattr_t a
;
90 if (pthread_mutexattr_init (&a
) != 0)
92 puts ("mutexattr_init failed");
95 if (pthread_mutexattr_setrobust_np (&a
, PTHREAD_MUTEX_ROBUST_NP
) != 0)
97 puts ("mutexattr_setrobust failed");
100 #ifndef NOT_CONSISTENT
101 if (pthread_mutex_init (&m1
, &a
) != 0)
103 puts ("mutex_init m1 failed");
107 if (pthread_mutex_init (&m2
, &a
) != 0)
109 puts ("mutex_init m2 failed");
114 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
116 puts ("barrier_init failed");
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");
128 if (pthread_mutex_init (&m2
, &a
) != 0)
130 puts ("mutex_init m2 failed");
136 if (pthread_create (&th
, NULL
, tf
, (void *) round
) != 0)
138 printf ("%ld: create failed\n", round
);
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
);
149 if (pthread_cancel (th
) != 0)
151 printf ("%ld: cancel failed\n", round
);
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
);
167 if (pthread_join (th
, &res
) != 0)
169 printf ("%ld: join failed\n", round
);
172 if (res
!= PTHREAD_CANCELED
)
174 printf ("%ld: thread not canceled\n", round
);
182 printf ("%ld: parent: mutex_lock m1 succeeded\n", round
);
187 printf ("%ld: parent: mutex_lock m1 returned wrong code\n", round
);
194 printf ("%ld: parent: mutex_lock m2 succeeded\n", round
);
199 printf ("%ld: parent: mutex_lock m2 returned wrong code\n", round
);
204 if ((round
& 1) == 0)
207 if (pthread_join (th
, &res
) != 0)
209 printf ("%ld: join failed\n", round
);
212 if (res
!= PTHREAD_CANCELED
)
214 printf ("%ld: thread not canceled\n", round
);
220 #ifndef NOT_CONSISTENT
221 e
= pthread_mutex_consistent_np (&m1
);
224 printf ("%ld: mutex_consistent m1 failed with error %d\n", round
, e
);
228 e
= pthread_mutex_consistent_np (&m2
);
231 printf ("%ld: mutex_consistent m2 failed with error %d\n", round
, e
);
236 e
= pthread_mutex_unlock (&m1
);
239 printf ("%ld: mutex_unlock m1 failed\n", round
);
243 e
= pthread_mutex_unlock (&m2
);
246 printf ("%ld: mutex_unlock m2 failed\n", round
);
250 #ifdef NOT_CONSISTENT
254 printf ("%ld: locking inconsistent mutex m1 succeeded\n", round
);
257 if (e
!= ENOTRECOVERABLE
)
259 printf ("%ld: locking inconsistent mutex m1 failed with error %d\n",
264 if (pthread_mutex_destroy (&m1
) != 0)
266 puts ("mutex_destroy m1 failed");
273 printf ("%ld: locking inconsistent mutex m2 succeeded\n", round
);
276 if (e
!= ENOTRECOVERABLE
)
278 printf ("%ld: locking inconsistent mutex m2 failed with error %d\n",
283 if (pthread_mutex_destroy (&m2
) != 0)
285 puts ("mutex_destroy m2 failed");
291 #ifndef NOT_CONSISTENT
292 if (pthread_mutex_destroy (&m1
) != 0)
294 puts ("mutex_destroy m1 failed");
298 if (pthread_mutex_destroy (&m2
) != 0)
300 puts ("mutex_destroy m2 failed");
305 if (pthread_mutexattr_destroy (&a
) != 0)
307 puts ("mutexattr_destroy failed");
314 #define TEST_FUNCTION do_test ()
315 #include "../test-skeleton.c"