1 /* Copyright (C) 2005-2023 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/>. */
25 static pthread_barrier_t b
;
26 static pthread_cond_t c
= PTHREAD_COND_INITIALIZER
;
27 static pthread_mutex_t m
;
28 static bool first
= true;
34 long int n
= (long int) arg
;
36 if (pthread_mutex_lock (&m
) != 0)
38 printf ("thread %ld: mutex_lock failed\n", n
+ 1);
42 int e
= pthread_barrier_wait (&b
);
43 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
45 printf ("thread %ld: barrier_wait failed\n", n
+ 1);
49 e
= pthread_cond_wait (&c
, &m
);
54 printf ("thread %ld: cond_wait failed\n", n
+ 1);
63 printf ("thread %ld: cond_wait did not return EOWNERDEAD\n", n
+ 1);
68 if (pthread_cancel (pthread_self ()) != 0)
70 printf ("thread %ld: cancel failed\n", n
+ 1);
74 pthread_testcancel ();
76 printf ("thread %ld: testcancel returned\n", n
+ 1);
84 pthread_mutexattr_t a
;
85 if (pthread_mutexattr_init (&a
) != 0)
87 puts ("mutexattr_init failed");
91 if (pthread_mutexattr_setrobust (&a
, PTHREAD_MUTEX_ROBUST_NP
) != 0)
93 puts ("mutexattr_setrobust failed");
98 if (pthread_mutexattr_setprotocol (&a
, PTHREAD_PRIO_INHERIT
) != 0)
100 puts ("pthread_mutexattr_setprotocol failed");
106 e
= pthread_mutex_init (&m
, &a
);
112 puts ("PI robust mutexes not supported");
116 puts ("mutex_init failed");
120 if (pthread_mutexattr_destroy (&a
) != 0)
122 puts ("mutexattr_destroy failed");
126 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
128 puts ("barrier_init failed");
134 for (long int n
= 0; n
< N
; ++n
)
136 if (pthread_create (&th
[n
], NULL
, tf
, (void *) n
) != 0)
138 printf ("pthread_create loop %ld failed\n", n
+ 1);
142 e
= pthread_barrier_wait (&b
);
143 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
145 printf ("parent: barrier_wait failed in round %ld\n", n
+ 1);
150 if (pthread_mutex_lock (&m
) != 0)
152 puts ("parent: mutex_lock failed");
156 if (pthread_mutex_unlock (&m
) != 0)
158 puts ("parent: mutex_unlock failed");
162 if (pthread_cond_broadcast (&c
) != 0)
164 puts ("cond_broadcast failed");
168 for (int n
= 0; n
< N
; ++n
)
171 if (pthread_join (th
[n
], &res
) != 0)
173 printf ("join round %d failed\n", n
+ 1);
176 if (res
!= PTHREAD_CANCELED
)
178 printf ("thread %d not canceled\n", n
+ 1);
183 e
= pthread_mutex_lock (&m
);
186 puts ("parent: 2nd mutex_lock succeeded");
191 puts ("parent: mutex_lock did not return EOWNERDEAD");
195 if (pthread_mutex_unlock (&m
) != 0)
197 puts ("parent: 2nd mutex_unlock failed");
201 if (pthread_mutex_destroy (&m
) != 0)
203 puts ("mutex_destroy failed");
210 #define TEST_FUNCTION do_test ()
211 #include "../test-skeleton.c"