1 /* Copyright (C) 2003-2018 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, see
17 <http://www.gnu.org/licenses/>. */
19 /* This test checks behavior not required by POSIX. */
27 static pthread_mutex_t
*m
;
28 static pthread_barrier_t b
;
29 static pthread_cond_t c
;
36 if (pthread_mutex_unlock (m
) != 0)
38 puts ("cl: mutex_unlocked failed");
47 if (pthread_mutex_lock (m
) != 0)
49 puts ("tf: mutex_lock failed");
53 int e
= pthread_barrier_wait (&b
);
54 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
56 puts ("barrier_wait failed");
62 if (pthread_cond_wait (&c
, m
) != 0)
64 puts ("tf: cond_wait failed");
71 pthread_cleanup_push (cl
, NULL
);
73 if (pthread_cond_wait (&c
, m
) != 0)
75 puts ("tf: cond_wait failed");
79 pthread_cleanup_pop (0);
83 if (pthread_mutex_unlock (m
) != 0)
85 puts ("tf: mutex_unlock failed");
94 check_type (const char *mas
, pthread_mutexattr_t
*ma
)
98 e
= pthread_mutex_init (m
, ma
);
104 puts ("PI mutexes unsupported");
108 printf ("1st mutex_init failed for %s\n", mas
);
112 if (pthread_mutex_destroy (m
) != 0)
114 printf ("immediate mutex_destroy failed for %s\n", mas
);
118 if (pthread_mutex_init (m
, ma
) != 0)
120 printf ("2nd mutex_init failed for %s\n", mas
);
124 if (pthread_mutex_lock (m
) != 0)
126 printf ("1st mutex_lock failed for %s\n", mas
);
130 /* Elided mutexes don't fail destroy, but this test is run with
131 elision disabled so we can test them. */
132 e
= pthread_mutex_destroy (m
);
135 printf ("mutex_destroy of self-locked mutex succeeded for %s\n", mas
);
140 printf ("mutex_destroy of self-locked mutex did not return EBUSY %s\n",
145 if (pthread_mutex_unlock (m
) != 0)
147 printf ("1st mutex_unlock failed for %s\n", mas
);
151 if (pthread_mutex_trylock (m
) != 0)
153 printf ("mutex_trylock failed for %s\n", mas
);
157 /* Elided mutexes don't fail destroy. */
158 e
= pthread_mutex_destroy (m
);
161 printf ("mutex_destroy of self-trylocked mutex succeeded for %s\n", mas
);
167 mutex_destroy of self-trylocked mutex did not return EBUSY %s\n",
172 if (pthread_mutex_unlock (m
) != 0)
174 printf ("2nd mutex_unlock failed for %s\n", mas
);
179 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
181 puts ("1st create failed");
186 e
= pthread_barrier_wait (&b
);
187 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
189 puts ("1st barrier_wait failed");
193 if (pthread_mutex_lock (m
) != 0)
195 printf ("2nd mutex_lock failed for %s\n", mas
);
199 if (pthread_mutex_unlock (m
) != 0)
201 printf ("3rd mutex_unlock failed for %s\n", mas
);
205 /* Elided mutexes don't fail destroy. */
206 e
= pthread_mutex_destroy (m
);
209 printf ("mutex_destroy of condvar-used mutex succeeded for %s\n", mas
);
215 mutex_destroy of condvar-used mutex did not return EBUSY for %s\n", mas
);
220 if (pthread_cond_signal (&c
) != 0)
222 puts ("cond_signal failed");
227 if (pthread_join (th
, &r
) != 0)
229 puts ("join failed");
234 puts ("thread didn't return NULL");
238 if (pthread_mutex_destroy (m
) != 0)
240 printf ("mutex_destroy after condvar-use failed for %s\n", mas
);
244 if (pthread_mutex_init (m
, ma
) != 0)
246 printf ("3rd mutex_init failed for %s\n", mas
);
250 if (pthread_create (&th
, NULL
, tf
, (void *) 1) != 0)
252 puts ("2nd create failed");
257 e
= pthread_barrier_wait (&b
);
258 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
260 puts ("2nd barrier_wait failed");
264 if (pthread_mutex_lock (m
) != 0)
266 printf ("3rd mutex_lock failed for %s\n", mas
);
270 if (pthread_mutex_unlock (m
) != 0)
272 printf ("4th mutex_unlock failed for %s\n", mas
);
276 /* Elided mutexes don't fail destroy. */
277 e
= pthread_mutex_destroy (m
);
280 printf ("2nd mutex_destroy of condvar-used mutex succeeded for %s\n",
287 2nd mutex_destroy of condvar-used mutex did not return EBUSY for %s\n",
292 if (pthread_cancel (th
) != 0)
294 puts ("cond_cancel failed");
298 if (pthread_join (th
, &r
) != 0)
300 puts ("join failed");
303 if (r
!= PTHREAD_CANCELED
)
305 puts ("thread not canceled");
309 if (pthread_mutex_destroy (m
) != 0)
311 printf ("mutex_destroy after condvar-canceled failed for %s\n", mas
);
325 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
327 puts ("barrier_init failed");
331 if (pthread_cond_init (&c
, NULL
) != 0)
333 puts ("cond_init failed");
337 puts ("check normal mutex");
338 int res
= check_type ("normal", NULL
);
340 pthread_mutexattr_t ma
;
341 if (pthread_mutexattr_init (&ma
) != 0)
343 puts ("1st mutexattr_init failed");
346 if (pthread_mutexattr_settype (&ma
, PTHREAD_MUTEX_RECURSIVE
) != 0)
348 puts ("1st mutexattr_settype failed");
352 if (pthread_mutexattr_setprotocol (&ma
, PTHREAD_PRIO_INHERIT
))
354 puts ("1st pthread_mutexattr_setprotocol failed");
358 puts ("check recursive mutex");
359 res
|= check_type ("recursive", &ma
);
360 if (pthread_mutexattr_destroy (&ma
) != 0)
362 puts ("1st mutexattr_destroy failed");
366 if (pthread_mutexattr_init (&ma
) != 0)
368 puts ("2nd mutexattr_init failed");
371 if (pthread_mutexattr_settype (&ma
, PTHREAD_MUTEX_ERRORCHECK
) != 0)
373 puts ("2nd mutexattr_settype failed");
377 if (pthread_mutexattr_setprotocol (&ma
, PTHREAD_PRIO_INHERIT
))
379 puts ("2nd pthread_mutexattr_setprotocol failed");
383 puts ("check error-checking mutex");
384 res
|= check_type ("error-checking", &ma
);
385 if (pthread_mutexattr_destroy (&ma
) != 0)
387 puts ("2nd mutexattr_destroy failed");
394 #define TEST_FUNCTION do_test ()
395 #include "../test-skeleton.c"