* nis/nis_callback.c (__nis_create_callback): Always call xdr_free
[glibc.git] / nptl / tst-robust7.c
blob2c5acb44ce211e64f46334f936acf71394171639
1 /* Copyright (C) 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2005.
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 #include <errno.h>
21 #include <pthread.h>
22 #include <stdbool.h>
23 #include <stdio.h>
26 static pthread_barrier_t b;
27 static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
28 static pthread_mutex_t m;
29 static bool first = true;
32 static void *
33 tf (void *arg)
35 long int n = (long int) arg;
37 if (pthread_mutex_lock (&m) != 0)
39 printf ("thread %ld: mutex_lock failed\n", n + 1);
40 exit (1);
43 int e = pthread_barrier_wait (&b);
44 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
46 printf ("thread %ld: barrier_wait failed\n", n + 1);
47 exit (1);
50 e = pthread_cond_wait (&c, &m);
51 if (first)
53 if (e != 0)
55 printf ("thread %ld: cond_wait failed\n", n + 1);
56 exit (1);
58 first = false;
60 else
62 if (e != EOWNERDEAD)
64 printf ("thread %ld: cond_wait did not return EOWNERDEAD\n", n + 1);
65 exit (1);
69 if (pthread_cancel (pthread_self ()) != 0)
71 printf ("thread %ld: cancel failed\n", n + 1);
72 exit (1);
75 pthread_testcancel ();
77 printf ("thread %ld: testcancel returned\n", n + 1);
78 exit (1);
82 static int
83 do_test (void)
85 pthread_mutexattr_t a;
86 if (pthread_mutexattr_init (&a) != 0)
88 puts ("mutexattr_init failed");
89 return 1;
92 if (pthread_mutexattr_setrobust_np (&a, PTHREAD_MUTEX_ROBUST_NP) != 0)
94 puts ("mutexattr_setrobust failed");
95 return 1;
98 if (pthread_mutex_init (&m, &a) != 0)
100 puts ("mutex_init failed");
101 return 1;
104 if (pthread_mutexattr_destroy (&a) != 0)
106 puts ("mutexattr_destroy failed");
107 return 1;
110 if (pthread_barrier_init (&b, NULL, 2) != 0)
112 puts ("barrier_init failed");
113 return 1;
116 #define N 5
117 pthread_t th[N];
118 for (long int n = 0; n < N; ++n)
120 if (pthread_create (&th[n], NULL, tf, (void *) n) != 0)
122 printf ("pthread_create loop %ld failed\n", n + 1);
123 return 1;
126 int e = pthread_barrier_wait (&b);
127 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
129 printf ("parent: barrier_wait failed in round %ld\n", n + 1);
130 return 1;
134 if (pthread_mutex_lock (&m) != 0)
136 puts ("parent: mutex_lock failed");
137 return 1;
140 if (pthread_mutex_unlock (&m) != 0)
142 puts ("parent: mutex_unlock failed");
143 return 1;
146 if (pthread_cond_broadcast (&c) != 0)
148 puts ("cond_broadcast failed");
149 return 1;
152 for (int n = 0; n < N; ++n)
154 void *res;
155 if (pthread_join (th[n], &res) != 0)
157 printf ("join round %d failed\n", n + 1);
158 return 1;
160 if (res != PTHREAD_CANCELED)
162 printf ("thread %d not canceled\n", n + 1);
163 return 1;
167 int e = pthread_mutex_lock (&m);
168 if (e == 0)
170 puts ("parent: 2nd mutex_lock succeeded");
171 return 1;
173 if (e != EOWNERDEAD)
175 puts ("parent: mutex_lock did not return EOWNERDEAD");
176 return 1;
179 if (pthread_mutex_unlock (&m) != 0)
181 puts ("parent: 2nd mutex_unlock failed");
182 return 1;
185 if (pthread_mutex_destroy (&m) != 0)
187 puts ("mutex_destroy failed");
188 return 1;
191 return 0;
194 #define TEST_FUNCTION do_test ()
195 #include "../test-skeleton.c"