1 /* Verify that condition variables synchronized by PI mutexes don't hang.
2 Copyright (C) 2012-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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/>. */
24 #include <sys/types.h>
25 #include <sys/syscall.h>
33 static pthread_mutex_t mutex;
34 static pthread_mutexattr_t mutex_attr;
35 static pthread_cond_t cond;
36 static pthread_t threads[THREADS_NUM];
37 static int pending = 0;
39 typedef void * (*threadfunc) (void *);
42 thread_fun_timed (void *arg)
47 printf ("Started thread_fun_timed[%d]\n", *ret);
49 for (i = 0; i < MAXITER / THREADS_NUM; i++)
51 rv = pthread_mutex_lock (&mutex);
54 printf ("pthread_mutex_lock: %s(%d)\n", strerror (rv), rv);
62 clock_gettime(CLOCK_REALTIME, &ts);
64 rv = pthread_cond_timedwait (&cond, &mutex, &ts);
66 /* There should be no timeout either. */
69 printf ("pthread_cond_wait: %s(%d)\n", strerror (rv), rv);
77 rv = pthread_mutex_unlock (&mutex);
80 printf ("pthread_mutex_unlock: %s(%d)\n", strerror (rv), rv);
93 thread_fun (void *arg)
98 printf ("Started thread_fun[%d]\n", *ret);
100 for (i = 0; i < MAXITER / THREADS_NUM; i++)
102 rv = pthread_mutex_lock (&mutex);
105 printf ("pthread_mutex_lock: %s(%d)\n", strerror (rv), rv);
112 rv = pthread_cond_wait (&cond, &mutex);
116 printf ("pthread_cond_wait: %s(%d)\n", strerror (rv), rv);
124 rv = pthread_mutex_unlock (&mutex);
127 printf ("pthread_mutex_unlock: %s(%d)\n", strerror (rv), rv);
140 do_test_wait (threadfunc f)
145 int retval[THREADS_NUM];
147 puts ("Starting test");
149 rv = pthread_mutexattr_init (&mutex_attr);
152 printf ("pthread_mutexattr_init: %s(%d)\n", strerror (rv), rv);
156 rv = pthread_mutexattr_setprotocol (&mutex_attr, PTHREAD_PRIO_INHERIT);
159 printf ("pthread_mutexattr_setprotocol: %s(%d)\n", strerror (rv), rv);
163 rv = pthread_mutex_init (&mutex, &mutex_attr);
166 printf ("pthread_mutex_init: %s(%d)\n", strerror (rv), rv);
170 rv = pthread_cond_init (&cond, NULL);
173 printf ("pthread_cond_init: %s(%d)\n", strerror (rv), rv);
177 for (i = 0; i < THREADS_NUM; i++)
180 rv = pthread_create (&threads[i], NULL, f, &retval[i]);
183 printf ("pthread_create: %s(%d)\n", strerror (rv), rv);
188 for (; counter < MAXITER; counter++)
190 rv = pthread_mutex_lock (&mutex);
193 printf ("pthread_mutex_lock: %s(%d)\n", strerror (rv), rv);
197 if (!(counter % 100))
198 printf ("counter: %d\n", counter);
201 rv = pthread_cond_signal (&cond);
204 printf ("pthread_cond_signal: %s(%d)\n", strerror (rv), rv);
208 rv = pthread_mutex_unlock (&mutex);
211 printf ("pthread_mutex_unlock: %s(%d)\n", strerror (rv), rv);
216 for (i = 0; i < THREADS_NUM; i++)
219 rv = pthread_join (threads[i], &ret);
222 printf ("pthread_join: %s(%d)\n", strerror (rv), rv);
225 if (ret && *(int *)ret)
227 printf ("Thread %d returned with an error\n", i);
238 puts ("Testing pthread_cond_wait");
239 int ret = do_test_wait (thread_fun);
243 puts ("Testing pthread_cond_timedwait");
244 return do_test_wait (thread_fun_timed);
248 #define TEST_FUNCTION do_test ()
249 #include "../test-skeleton.c"