* nss/nss_files/files-parse.c (nss_files_parse_hidden_def): Define to
[glibc.git] / nptl / tst-mutex8.c
blob80ebe71f0c7754cf1184ed25cab9bd9feb6d188e
1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 /* This test checks behavior not required by POSIX. */
21 #include <errno.h>
22 #include <pthread.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
28 static pthread_mutex_t *m;
29 static pthread_barrier_t b;
30 static pthread_cond_t c;
31 static bool done;
34 static void
35 cl (void *arg)
37 if (pthread_mutex_unlock (m) != 0)
39 puts ("cl: mutex_unlocked failed");
40 exit (1);
45 static void *
46 tf (void *arg)
48 if (pthread_mutex_lock (m) != 0)
50 puts ("tf: mutex_lock failed");
51 return (void *) 1l;
54 int e = pthread_barrier_wait (&b);
55 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
57 puts ("barrier_wait failed");
58 return (void *) 1l;
61 if (arg == NULL)
63 if (pthread_cond_wait (&c, m) != 0)
65 puts ("tf: cond_wait failed");
66 return (void *) 1l;
68 while (! done);
69 else
72 pthread_cleanup_push (cl, NULL);
74 if (pthread_cond_wait (&c, m) != 0)
76 puts ("tf: cond_wait failed");
77 return (void *) 1l;
80 pthread_cleanup_pop (0);
82 while (! done);
84 if (pthread_mutex_unlock (m) != 0)
86 puts ("tf: mutex_unlock failed");
87 return (void *) 1l;
90 return NULL;
94 static int
95 check_type (const char *mas, pthread_mutexattr_t *ma)
97 if (pthread_mutex_init (m, ma) != 0)
99 printf ("1st mutex_init failed for %s\n", mas);
100 return 1;
103 if (pthread_mutex_destroy (m) != 0)
105 printf ("immediate mutex_destroy failed for %s\n", mas);
106 return 1;
109 if (pthread_mutex_init (m, ma) != 0)
111 printf ("2nd mutex_init failed for %s\n", mas);
112 return 1;
115 if (pthread_mutex_lock (m) != 0)
117 printf ("1st mutex_lock failed for %s\n", mas);
118 return 1;
121 int e = pthread_mutex_destroy (m);
122 if (e == 0)
124 printf ("mutex_destroy of self-locked mutex succeeded for %s\n", mas);
125 return 1;
127 if (e != EBUSY)
129 printf ("mutex_destroy of self-locked mutex did not return EBUSY %s\n",
130 mas);
131 return 1;
134 if (pthread_mutex_unlock (m) != 0)
136 printf ("1st mutex_unlock failed for %s\n", mas);
137 return 1;
140 if (pthread_mutex_trylock (m) != 0)
142 printf ("mutex_trylock failed for %s\n", mas);
143 return 1;
146 e = pthread_mutex_destroy (m);
147 if (e == 0)
149 printf ("mutex_destroy of self-trylocked mutex succeeded for %s\n", mas);
150 return 1;
152 if (e != EBUSY)
154 printf ("\
155 mutex_destroy of self-trylocked mutex did not return EBUSY %s\n",
156 mas);
157 return 1;
160 if (pthread_mutex_unlock (m) != 0)
162 printf ("2nd mutex_unlock failed for %s\n", mas);
163 return 1;
166 pthread_t th;
167 if (pthread_create (&th, NULL, tf, NULL) != 0)
169 puts ("1st create failed");
170 return 1;
172 done = false;
174 e = pthread_barrier_wait (&b);
175 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
177 puts ("1st barrier_wait failed");
178 return 1;
181 if (pthread_mutex_lock (m) != 0)
183 printf ("2nd mutex_lock failed for %s\n", mas);
184 return 1;
187 if (pthread_mutex_unlock (m) != 0)
189 printf ("3rd mutex_unlock failed for %s\n", mas);
190 return 1;
193 e = pthread_mutex_destroy (m);
194 if (e == 0)
196 printf ("mutex_destroy of condvar-used mutex succeeded for %s\n", mas);
197 return 1;
199 if (e != EBUSY)
201 printf ("\
202 mutex_destroy of condvar-used mutex did not return EBUSY for %s\n", mas);
203 return 1;
206 done = true;
207 if (pthread_cond_signal (&c) != 0)
209 puts ("cond_signal failed");
210 return 1;
213 void *r;
214 if (pthread_join (th, &r) != 0)
216 puts ("join failed");
217 return 1;
219 if (r != NULL)
221 puts ("thread didn't return NULL");
222 return 1;
225 if (pthread_mutex_destroy (m) != 0)
227 printf ("mutex_destroy after condvar-use failed for %s\n", mas);
228 return 1;
231 if (pthread_mutex_init (m, ma) != 0)
233 printf ("3rd mutex_init failed for %s\n", mas);
234 return 1;
237 if (pthread_create (&th, NULL, tf, (void *) 1) != 0)
239 puts ("2nd create failed");
240 return 1;
242 done = false;
244 e = pthread_barrier_wait (&b);
245 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
247 puts ("2nd barrier_wait failed");
248 return 1;
251 if (pthread_mutex_lock (m) != 0)
253 printf ("3rd mutex_lock failed for %s\n", mas);
254 return 1;
257 if (pthread_mutex_unlock (m) != 0)
259 printf ("4th mutex_unlock failed for %s\n", mas);
260 return 1;
263 e = pthread_mutex_destroy (m);
264 if (e == 0)
266 printf ("2nd mutex_destroy of condvar-used mutex succeeded for %s\n",
267 mas);
268 return 1;
270 if (e != EBUSY)
272 printf ("\
273 2nd mutex_destroy of condvar-used mutex did not return EBUSY for %s\n",
274 mas);
275 return 1;
278 if (pthread_cancel (th) != 0)
280 puts ("cond_cancel failed");
281 return 1;
284 if (pthread_join (th, &r) != 0)
286 puts ("join failed");
287 return 1;
289 if (r != PTHREAD_CANCELED)
291 puts ("thread not canceled");
292 return 1;
295 if (pthread_mutex_destroy (m) != 0)
297 printf ("mutex_destroy after condvar-canceled failed for %s\n", mas);
298 return 1;
301 return 0;
305 static int
306 do_test (void)
308 pthread_mutex_t mm;
309 m = &mm;
311 if (pthread_barrier_init (&b, NULL, 2) != 0)
313 puts ("barrier_init failed");
314 return 1;
317 if (pthread_cond_init (&c, NULL) != 0)
319 puts ("cond_init failed");
320 return 1;
323 puts ("check normal mutex");
324 int res = check_type ("normal", NULL);
326 pthread_mutexattr_t ma;
327 if (pthread_mutexattr_init (&ma) != 0)
329 puts ("1st mutexattr_init failed");
330 return 1;
332 if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_RECURSIVE) != 0)
334 puts ("1st mutexattr_settype failed");
335 return 1;
337 puts ("check recursive mutex");
338 res |= check_type ("recursive", &ma);
339 if (pthread_mutexattr_destroy (&ma) != 0)
341 puts ("1st mutexattr_destroy failed");
342 return 1;
345 if (pthread_mutexattr_init (&ma) != 0)
347 puts ("2nd mutexattr_init failed");
348 return 1;
350 if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_ERRORCHECK) != 0)
352 puts ("2nd mutexattr_settype failed");
353 return 1;
355 puts ("check error-checking mutex");
356 res |= check_type ("error-checking", &ma);
357 if (pthread_mutexattr_destroy (&ma) != 0)
359 puts ("2nd mutexattr_destroy failed");
360 return 1;
363 return res;
366 #define TEST_FUNCTION do_test ()
367 #include "../test-skeleton.c"