1 /* Copyright (C) 2005-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
24 static pthread_mutex_t m1
;
25 static pthread_mutex_t m2
;
26 static pthread_barrier_t b
;
30 # define LOCK(m) pthread_mutex_lock (m)
37 long int round
= (long int) arg
;
39 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE
, NULL
) != 0)
41 printf ("%ld: setcancelstate failed\n", round
);
48 printf ("%ld: child: mutex_lock m1 failed with error %d\n", round
, e
);
55 printf ("%ld: child: mutex_lock m2 failed with error %d\n", round
, e
);
59 e
= pthread_barrier_wait (&b
);
60 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
62 printf ("%ld: child: 1st barrier_wait failed\n", round
);
66 e
= pthread_barrier_wait (&b
);
67 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
69 printf ("%ld: child: 2nd barrier_wait failed\n", round
);
73 pthread_testcancel ();
75 printf ("%ld: testcancel returned\n", round
);
87 pthread_mutexattr_t a
;
88 if (pthread_mutexattr_init (&a
) != 0)
90 puts ("mutexattr_init failed");
93 if (pthread_mutexattr_setrobust (&a
, PTHREAD_MUTEX_ROBUST_NP
) != 0)
95 puts ("mutexattr_setrobust failed");
100 if (pthread_mutexattr_setprotocol (&a
, PTHREAD_PRIO_INHERIT
) != 0)
102 puts ("pthread_mutexattr_setprotocol failed");
107 int e
= pthread_mutex_init (&m1
, &a
);
110 puts ("PI robust mutexes not supported");
115 puts ("mutex_init m1 failed");
118 pthread_mutex_destroy (&m1
);
122 #ifndef NOT_CONSISTENT
123 if (pthread_mutex_init (&m1
, &a
) != 0)
125 puts ("mutex_init m1 failed");
129 if (pthread_mutex_init (&m2
, &a
) != 0)
131 puts ("mutex_init m2 failed");
136 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
138 puts ("barrier_init failed");
142 for (long int round
= 1; round
< 5; ++round
)
144 #ifdef NOT_CONSISTENT
145 if (pthread_mutex_init (&m1
, &a
) != 0)
147 puts ("mutex_init m1 failed");
150 if (pthread_mutex_init (&m2
, &a
) != 0)
152 puts ("mutex_init m2 failed");
158 if (pthread_create (&th
, NULL
, tf
, (void *) round
) != 0)
160 printf ("%ld: create failed\n", round
);
164 int e
= pthread_barrier_wait (&b
);
165 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
167 printf ("%ld: parent: 1st barrier_wait failed\n", round
);
171 if (pthread_cancel (th
) != 0)
173 printf ("%ld: cancel failed\n", round
);
177 e
= pthread_barrier_wait (&b
);
178 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
180 printf ("%ld: parent: 2nd barrier_wait failed\n", round
);
189 if (pthread_join (th
, &res
) != 0)
191 printf ("%ld: join failed\n", round
);
194 if (res
!= PTHREAD_CANCELED
)
196 printf ("%ld: thread not canceled\n", round
);
204 printf ("%ld: parent: mutex_lock m1 succeeded\n", round
);
209 printf ("%ld: parent: mutex_lock m1 returned wrong code\n", round
);
216 printf ("%ld: parent: mutex_lock m2 succeeded\n", round
);
221 printf ("%ld: parent: mutex_lock m2 returned wrong code\n", round
);
226 if ((round
& 1) == 0)
229 if (pthread_join (th
, &res
) != 0)
231 printf ("%ld: join failed\n", round
);
234 if (res
!= PTHREAD_CANCELED
)
236 printf ("%ld: thread not canceled\n", round
);
242 #ifndef NOT_CONSISTENT
243 e
= pthread_mutex_consistent (&m1
);
246 printf ("%ld: mutex_consistent m1 failed with error %d\n", round
, e
);
250 e
= pthread_mutex_consistent (&m2
);
253 printf ("%ld: mutex_consistent m2 failed with error %d\n", round
, e
);
258 e
= pthread_mutex_unlock (&m1
);
261 printf ("%ld: mutex_unlock m1 failed with %d\n", round
, e
);
265 e
= pthread_mutex_unlock (&m2
);
268 printf ("%ld: mutex_unlock m2 failed with %d\n", round
, e
);
272 #ifdef NOT_CONSISTENT
276 printf ("%ld: locking inconsistent mutex m1 succeeded\n", round
);
279 if (e
!= ENOTRECOVERABLE
)
281 printf ("%ld: locking inconsistent mutex m1 failed with error %d\n",
286 if (pthread_mutex_destroy (&m1
) != 0)
288 puts ("mutex_destroy m1 failed");
295 printf ("%ld: locking inconsistent mutex m2 succeeded\n", round
);
298 if (e
!= ENOTRECOVERABLE
)
300 printf ("%ld: locking inconsistent mutex m2 failed with error %d\n",
305 if (pthread_mutex_destroy (&m2
) != 0)
307 puts ("mutex_destroy m2 failed");
313 #ifndef NOT_CONSISTENT
314 if (pthread_mutex_destroy (&m1
) != 0)
316 puts ("mutex_destroy m1 failed");
320 if (pthread_mutex_destroy (&m2
) != 0)
322 puts ("mutex_destroy m2 failed");
327 if (pthread_mutexattr_destroy (&a
) != 0)
329 puts ("mutexattr_destroy failed");
336 #define TEST_FUNCTION do_test ()
337 #include "../test-skeleton.c"