1 /* Verify that condition variables synchronized by PI mutexes don't hang on
3 Copyright (C) 2012 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
25 #include <sys/types.h>
26 #include <sys/syscall.h>
35 typedef void *(*thr_func
) (void *);
37 pthread_mutex_t mutex
;
40 void cleanup (void *u
)
42 /* pthread_cond_wait should always return with the mutex locked. */
43 if (pthread_mutex_unlock (&mutex
))
53 for (i
= 0; i
< ITERS
; i
++)
55 if ((ret
= pthread_mutex_lock (&mutex
)) != 0)
58 printf ("signaller:mutex_lock failed: %s\n", strerror (ret
));
61 if ((ret
= pthread_cond_signal (&cond
)) != 0)
64 printf ("signaller:signal failed: %s\n", strerror (ret
));
67 if ((ret
= pthread_mutex_unlock (&mutex
)) != 0)
70 printf ("signaller:mutex_unlock failed: %s\n", strerror (ret
));
73 pthread_testcancel ();
80 if ((ret
= pthread_mutex_unlock (&mutex
)) != 0)
81 printf ("signaller:mutex_unlock[2] failed: %s\n", strerror (ret
));
92 for (i
= 0; i
< ITERS
/ NUM
; i
++)
94 if ((ret
= pthread_mutex_lock (&mutex
)) != 0)
97 printf ("waiter[%u]:mutex_lock failed: %s\n", seq
, strerror (ret
));
100 pthread_cleanup_push (cleanup
, NULL
);
102 if ((ret
= pthread_cond_wait (&cond
, &mutex
)) != 0)
105 printf ("waiter[%u]:wait failed: %s\n", seq
, strerror (ret
));
109 if ((ret
= pthread_mutex_unlock (&mutex
)) != 0)
112 printf ("waiter[%u]:mutex_unlock failed: %s\n", seq
, strerror (ret
));
115 pthread_cleanup_pop (0);
119 puts ("waiter tests done");
123 if ((ret
= pthread_mutex_unlock (&mutex
)) != 0)
124 printf ("waiter:mutex_unlock[2] failed: %s\n", strerror (ret
));
129 timed_waiter (void *u
)
135 for (i
= 0; i
< ITERS
/ NUM
; i
++)
139 if ((ret
= clock_gettime(CLOCK_REALTIME
, &ts
)) != 0)
142 printf ("%u:clock_gettime failed: %s\n", seq
, strerror (errno
));
147 if ((ret
= pthread_mutex_lock (&mutex
)) != 0)
150 printf ("waiter[%u]:mutex_lock failed: %s\n", seq
, strerror (ret
));
153 pthread_cleanup_push (cleanup
, NULL
);
155 /* We should not time out either. */
156 if ((ret
= pthread_cond_timedwait (&cond
, &mutex
, &ts
)) != 0)
159 printf ("waiter[%u]:timedwait failed: %s\n", seq
, strerror (ret
));
162 if ((ret
= pthread_mutex_unlock (&mutex
)) != 0)
165 printf ("waiter[%u]:mutex_unlock failed: %s\n", seq
, strerror (ret
));
168 pthread_cleanup_pop (0);
172 puts ("timed_waiter tests done");
176 if ((ret
= pthread_mutex_unlock (&mutex
)) != 0)
177 printf ("waiter[%u]:mutex_unlock[2] failed: %s\n", seq
, strerror (ret
));
182 do_test_wait (thr_func f
)
186 pthread_mutexattr_t attr
;
190 for (i
= 0; i
< COUNT
; i
++)
192 if ((ret
= pthread_mutexattr_init (&attr
)) != 0)
194 printf ("mutexattr_init failed: %s\n", strerror (ret
));
198 if ((ret
= pthread_mutexattr_setprotocol (&attr
, PTHREAD_PRIO_INHERIT
)) != 0)
200 printf ("mutexattr_setprotocol failed: %s\n", strerror (ret
));
204 if ((ret
= pthread_cond_init (&cond
, NULL
)) != 0)
206 printf ("cond_init failed: %s\n", strerror (ret
));
210 if ((ret
= pthread_mutex_init (&mutex
, &attr
)) != 0)
212 printf ("mutex_init failed: %s\n", strerror (ret
));
216 for (j
= 0; j
< NUM
; j
++)
217 if ((ret
= pthread_create (&w
[j
], NULL
, f
, (void *)j
)) != 0)
219 printf ("waiter[%d]: create failed: %s\n", j
, strerror (ret
));
223 if ((ret
= pthread_create (&s
, NULL
, signaller
, NULL
)) != 0)
225 printf ("signaller: create failed: %s\n", strerror (ret
));
229 for (j
= 0; j
< NUM
; j
++)
231 pthread_cancel (w
[j
]);
233 if ((ret
= pthread_join (w
[j
], &thr_ret
)) != 0)
235 printf ("waiter[%d]: join failed: %s\n", j
, strerror (ret
));
239 if (thr_ret
!= NULL
&& thr_ret
!= PTHREAD_CANCELED
)
246 /* The signalling thread could have ended before it was cancelled. */
249 if ((ret
= pthread_join (s
, &thr_ret
)) != 0)
251 printf ("signaller: join failed: %s\n", strerror (ret
));
255 if (thr_ret
!= NULL
&& thr_ret
!= PTHREAD_CANCELED
)
267 do_test (int argc
, char **argv
)
269 int ret
= do_test_wait (waiter
);
274 return do_test_wait (timed_waiter
);
278 #include "../test-skeleton.c"