Fix powerpc software sqrtf (bug 17967).
[glibc.git] / nptl / tst-cond25.c
blobd80d8f7e7626d887ca996373eef64e9d69e83dd8
1 /* Verify that condition variables synchronized by PI mutexes don't hang on
2 on cancellation.
3 Copyright (C) 2012-2015 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/>. */
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/syscall.h>
28 #include <unistd.h>
29 #include <sys/time.h>
30 #include <time.h>
32 #define NUM 5
33 #define ITERS 10000
34 #define COUNT 100
36 typedef void *(*thr_func) (void *);
38 pthread_mutex_t mutex;
39 pthread_cond_t cond;
41 void cleanup (void *u)
43 /* pthread_cond_wait should always return with the mutex locked. */
44 if (pthread_mutex_unlock (&mutex))
45 abort ();
48 void *
49 signaller (void *u)
51 int i, ret = 0;
52 void *tret = NULL;
54 for (i = 0; i < ITERS; i++)
56 if ((ret = pthread_mutex_lock (&mutex)) != 0)
58 tret = (void *)1;
59 printf ("signaller:mutex_lock failed: %s\n", strerror (ret));
60 goto out;
62 if ((ret = pthread_cond_signal (&cond)) != 0)
64 tret = (void *)1;
65 printf ("signaller:signal failed: %s\n", strerror (ret));
66 goto unlock_out;
68 if ((ret = pthread_mutex_unlock (&mutex)) != 0)
70 tret = (void *)1;
71 printf ("signaller:mutex_unlock failed: %s\n", strerror (ret));
72 goto out;
74 pthread_testcancel ();
77 out:
78 return tret;
80 unlock_out:
81 if ((ret = pthread_mutex_unlock (&mutex)) != 0)
82 printf ("signaller:mutex_unlock[2] failed: %s\n", strerror (ret));
83 goto out;
86 void *
87 waiter (void *u)
89 int i, ret = 0;
90 void *tret = NULL;
91 int seq = (uintptr_t) u;
93 for (i = 0; i < ITERS / NUM; i++)
95 if ((ret = pthread_mutex_lock (&mutex)) != 0)
97 tret = (void *) (uintptr_t) 1;
98 printf ("waiter[%u]:mutex_lock failed: %s\n", seq, strerror (ret));
99 goto out;
101 pthread_cleanup_push (cleanup, NULL);
103 if ((ret = pthread_cond_wait (&cond, &mutex)) != 0)
105 tret = (void *) (uintptr_t) 1;
106 printf ("waiter[%u]:wait failed: %s\n", seq, strerror (ret));
107 goto unlock_out;
110 if ((ret = pthread_mutex_unlock (&mutex)) != 0)
112 tret = (void *) (uintptr_t) 1;
113 printf ("waiter[%u]:mutex_unlock failed: %s\n", seq, strerror (ret));
114 goto out;
116 pthread_cleanup_pop (0);
119 out:
120 puts ("waiter tests done");
121 return tret;
123 unlock_out:
124 if ((ret = pthread_mutex_unlock (&mutex)) != 0)
125 printf ("waiter:mutex_unlock[2] failed: %s\n", strerror (ret));
126 goto out;
129 void *
130 timed_waiter (void *u)
132 int i, ret;
133 void *tret = NULL;
134 int seq = (uintptr_t) u;
136 for (i = 0; i < ITERS / NUM; i++)
138 struct timespec ts;
140 if ((ret = clock_gettime(CLOCK_REALTIME, &ts)) != 0)
142 tret = (void *) (uintptr_t) 1;
143 printf ("%u:clock_gettime failed: %s\n", seq, strerror (errno));
144 goto out;
146 ts.tv_sec += 20;
148 if ((ret = pthread_mutex_lock (&mutex)) != 0)
150 tret = (void *) (uintptr_t) 1;
151 printf ("waiter[%u]:mutex_lock failed: %s\n", seq, strerror (ret));
152 goto out;
154 pthread_cleanup_push (cleanup, NULL);
156 /* We should not time out either. */
157 if ((ret = pthread_cond_timedwait (&cond, &mutex, &ts)) != 0)
159 tret = (void *) (uintptr_t) 1;
160 printf ("waiter[%u]:timedwait failed: %s\n", seq, strerror (ret));
161 goto unlock_out;
163 if ((ret = pthread_mutex_unlock (&mutex)) != 0)
165 tret = (void *) (uintptr_t) 1;
166 printf ("waiter[%u]:mutex_unlock failed: %s\n", seq, strerror (ret));
167 goto out;
169 pthread_cleanup_pop (0);
172 out:
173 puts ("timed_waiter tests done");
174 return tret;
176 unlock_out:
177 if ((ret = pthread_mutex_unlock (&mutex)) != 0)
178 printf ("waiter[%u]:mutex_unlock[2] failed: %s\n", seq, strerror (ret));
179 goto out;
183 do_test_wait (thr_func f)
185 pthread_t w[NUM];
186 pthread_t s;
187 pthread_mutexattr_t attr;
188 int i, j, ret = 0;
189 void *thr_ret;
191 for (i = 0; i < COUNT; i++)
193 if ((ret = pthread_mutexattr_init (&attr)) != 0)
195 printf ("mutexattr_init failed: %s\n", strerror (ret));
196 goto out;
199 if ((ret = pthread_mutexattr_setprotocol (&attr,
200 PTHREAD_PRIO_INHERIT)) != 0)
202 printf ("mutexattr_setprotocol failed: %s\n", strerror (ret));
203 goto out;
206 if ((ret = pthread_cond_init (&cond, NULL)) != 0)
208 printf ("cond_init failed: %s\n", strerror (ret));
209 goto out;
212 if ((ret = pthread_mutex_init (&mutex, &attr)) != 0)
214 printf ("mutex_init failed: %s\n", strerror (ret));
215 goto out;
218 for (j = 0; j < NUM; j++)
219 if ((ret = pthread_create (&w[j], NULL,
220 f, (void *) (uintptr_t) j)) != 0)
222 printf ("waiter[%d]: create failed: %s\n", j, strerror (ret));
223 goto out;
226 if ((ret = pthread_create (&s, NULL, signaller, NULL)) != 0)
228 printf ("signaller: create failed: %s\n", strerror (ret));
229 goto out;
232 for (j = 0; j < NUM; j++)
234 pthread_cancel (w[j]);
236 if ((ret = pthread_join (w[j], &thr_ret)) != 0)
238 printf ("waiter[%d]: join failed: %s\n", j, strerror (ret));
239 goto out;
242 if (thr_ret != NULL && thr_ret != PTHREAD_CANCELED)
244 ret = 1;
245 goto out;
249 /* The signalling thread could have ended before it was cancelled. */
250 pthread_cancel (s);
252 if ((ret = pthread_join (s, &thr_ret)) != 0)
254 printf ("signaller: join failed: %s\n", strerror (ret));
255 goto out;
258 if (thr_ret != NULL && thr_ret != PTHREAD_CANCELED)
260 ret = 1;
261 goto out;
265 out:
266 return ret;
270 do_test (int argc, char **argv)
272 int ret = do_test_wait (waiter);
274 if (ret)
275 return ret;
277 return do_test_wait (timed_waiter);
280 #define TIMEOUT 5
281 #include "../test-skeleton.c"