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
20 /* This test checks behavior not required by POSIX. */
28 static pthread_mutex_t
*m
;
29 static pthread_barrier_t b
;
30 static pthread_cond_t c
;
37 if (pthread_mutex_unlock (m
) != 0)
39 puts ("cl: mutex_unlocked failed");
48 if (pthread_mutex_lock (m
) != 0)
50 puts ("tf: mutex_lock failed");
54 int e
= pthread_barrier_wait (&b
);
55 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
57 puts ("barrier_wait failed");
63 if (pthread_cond_wait (&c
, m
) != 0)
65 puts ("tf: cond_wait failed");
72 pthread_cleanup_push (cl
, NULL
);
74 if (pthread_cond_wait (&c
, m
) != 0)
76 puts ("tf: cond_wait failed");
80 pthread_cleanup_pop (0);
84 if (pthread_mutex_unlock (m
) != 0)
86 puts ("tf: mutex_unlock failed");
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
);
103 if (pthread_mutex_destroy (m
) != 0)
105 printf ("immediate mutex_destroy failed for %s\n", mas
);
109 if (pthread_mutex_init (m
, ma
) != 0)
111 printf ("2nd mutex_init failed for %s\n", mas
);
115 if (pthread_mutex_lock (m
) != 0)
117 printf ("1st mutex_lock failed for %s\n", mas
);
121 int e
= pthread_mutex_destroy (m
);
124 printf ("mutex_destroy of self-locked mutex succeeded for %s\n", mas
);
129 printf ("mutex_destroy of self-locked mutex did not return EBUSY %s\n",
134 if (pthread_mutex_unlock (m
) != 0)
136 printf ("1st mutex_unlock failed for %s\n", mas
);
140 if (pthread_mutex_trylock (m
) != 0)
142 printf ("mutex_trylock failed for %s\n", mas
);
146 e
= pthread_mutex_destroy (m
);
149 printf ("mutex_destroy of self-trylocked mutex succeeded for %s\n", mas
);
155 mutex_destroy of self-trylocked mutex did not return EBUSY %s\n",
160 if (pthread_mutex_unlock (m
) != 0)
162 printf ("2nd mutex_unlock failed for %s\n", mas
);
167 if (pthread_create (&th
, NULL
, tf
, NULL
) != 0)
169 puts ("1st create failed");
174 e
= pthread_barrier_wait (&b
);
175 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
177 puts ("1st barrier_wait failed");
181 if (pthread_mutex_lock (m
) != 0)
183 printf ("2nd mutex_lock failed for %s\n", mas
);
187 if (pthread_mutex_unlock (m
) != 0)
189 printf ("3rd mutex_unlock failed for %s\n", mas
);
193 e
= pthread_mutex_destroy (m
);
196 printf ("mutex_destroy of condvar-used mutex succeeded for %s\n", mas
);
202 mutex_destroy of condvar-used mutex did not return EBUSY for %s\n", mas
);
207 if (pthread_cond_signal (&c
) != 0)
209 puts ("cond_signal failed");
214 if (pthread_join (th
, &r
) != 0)
216 puts ("join failed");
221 puts ("thread didn't return NULL");
225 if (pthread_mutex_destroy (m
) != 0)
227 printf ("mutex_destroy after condvar-use failed for %s\n", mas
);
231 if (pthread_mutex_init (m
, ma
) != 0)
233 printf ("3rd mutex_init failed for %s\n", mas
);
237 if (pthread_create (&th
, NULL
, tf
, (void *) 1) != 0)
239 puts ("2nd create failed");
244 e
= pthread_barrier_wait (&b
);
245 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
247 puts ("2nd barrier_wait failed");
251 if (pthread_mutex_lock (m
) != 0)
253 printf ("3rd mutex_lock failed for %s\n", mas
);
257 if (pthread_mutex_unlock (m
) != 0)
259 printf ("4th mutex_unlock failed for %s\n", mas
);
263 e
= pthread_mutex_destroy (m
);
266 printf ("2nd mutex_destroy of condvar-used mutex succeeded for %s\n",
273 2nd mutex_destroy of condvar-used mutex did not return EBUSY for %s\n",
278 if (pthread_cancel (th
) != 0)
280 puts ("cond_cancel failed");
284 if (pthread_join (th
, &r
) != 0)
286 puts ("join failed");
289 if (r
!= PTHREAD_CANCELED
)
291 puts ("thread not canceled");
295 if (pthread_mutex_destroy (m
) != 0)
297 printf ("mutex_destroy after condvar-canceled failed for %s\n", mas
);
311 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
313 puts ("barrier_init failed");
317 if (pthread_cond_init (&c
, NULL
) != 0)
319 puts ("cond_init failed");
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");
332 if (pthread_mutexattr_settype (&ma
, PTHREAD_MUTEX_RECURSIVE
) != 0)
334 puts ("1st mutexattr_settype failed");
337 puts ("check recursive mutex");
338 res
|= check_type ("recursive", &ma
);
339 if (pthread_mutexattr_destroy (&ma
) != 0)
341 puts ("1st mutexattr_destroy failed");
345 if (pthread_mutexattr_init (&ma
) != 0)
347 puts ("2nd mutexattr_init failed");
350 if (pthread_mutexattr_settype (&ma
, PTHREAD_MUTEX_ERRORCHECK
) != 0)
352 puts ("2nd mutexattr_settype failed");
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");
366 #define TEST_FUNCTION do_test ()
367 #include "../test-skeleton.c"