1 /* Copyright (C) 2005-2016 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/>. */
26 static pthread_barrier_t b
;
27 static pthread_cond_t c
= PTHREAD_COND_INITIALIZER
;
28 static pthread_mutex_t m
;
29 static bool first
= true;
35 long int n
= (long int) arg
;
37 if (pthread_mutex_lock (&m
) != 0)
39 printf ("thread %ld: mutex_lock failed\n", n
+ 1);
43 int e
= pthread_barrier_wait (&b
);
44 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
46 printf ("thread %ld: barrier_wait failed\n", n
+ 1);
50 e
= pthread_cond_wait (&c
, &m
);
55 printf ("thread %ld: cond_wait failed\n", n
+ 1);
64 printf ("thread %ld: cond_wait did not return EOWNERDEAD\n", n
+ 1);
69 if (pthread_cancel (pthread_self ()) != 0)
71 printf ("thread %ld: cancel failed\n", n
+ 1);
75 pthread_testcancel ();
77 printf ("thread %ld: testcancel returned\n", n
+ 1);
85 pthread_mutexattr_t a
;
86 if (pthread_mutexattr_init (&a
) != 0)
88 puts ("mutexattr_init failed");
92 if (pthread_mutexattr_setrobust_np (&a
, PTHREAD_MUTEX_ROBUST_NP
) != 0)
94 puts ("mutexattr_setrobust failed");
99 if (pthread_mutexattr_setprotocol (&a
, PTHREAD_PRIO_INHERIT
) != 0)
101 puts ("pthread_mutexattr_setprotocol failed");
107 e
= pthread_mutex_init (&m
, &a
);
113 puts ("PI robust mutexes not supported");
117 puts ("mutex_init failed");
121 if (pthread_mutexattr_destroy (&a
) != 0)
123 puts ("mutexattr_destroy failed");
127 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
129 puts ("barrier_init failed");
135 for (long int n
= 0; n
< N
; ++n
)
137 if (pthread_create (&th
[n
], NULL
, tf
, (void *) n
) != 0)
139 printf ("pthread_create loop %ld failed\n", n
+ 1);
143 e
= pthread_barrier_wait (&b
);
144 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
146 printf ("parent: barrier_wait failed in round %ld\n", n
+ 1);
151 if (pthread_mutex_lock (&m
) != 0)
153 puts ("parent: mutex_lock failed");
157 if (pthread_mutex_unlock (&m
) != 0)
159 puts ("parent: mutex_unlock failed");
163 if (pthread_cond_broadcast (&c
) != 0)
165 puts ("cond_broadcast failed");
169 for (int n
= 0; n
< N
; ++n
)
172 if (pthread_join (th
[n
], &res
) != 0)
174 printf ("join round %d failed\n", n
+ 1);
177 if (res
!= PTHREAD_CANCELED
)
179 printf ("thread %d not canceled\n", n
+ 1);
184 e
= pthread_mutex_lock (&m
);
187 puts ("parent: 2nd mutex_lock succeeded");
192 puts ("parent: mutex_lock did not return EOWNERDEAD");
196 if (pthread_mutex_unlock (&m
) != 0)
198 puts ("parent: 2nd mutex_unlock failed");
202 if (pthread_mutex_destroy (&m
) != 0)
204 puts ("mutex_destroy failed");
211 #define TEST_FUNCTION do_test ()
212 #include "../test-skeleton.c"