* sysdeps/unix/sysv/linux/alpha/bits/pthreadtypes.h (pthread_mutex_t):
[glibc.git] / nptl / tst-robust1.c
blob13267a5efd5adcb670cf40a8b230ca57f2cb012a
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 <stdio.h>
23 #include <stdlib.h>
26 static pthread_mutex_t m;
27 static pthread_barrier_t b;
30 #ifndef LOCK
31 # define LOCK(m) pthread_mutex_lock (m)
32 #endif
35 static void *
36 tf (void *arg)
38 long int round = (long int) arg;
40 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0)
42 printf ("%ld: setcancelstate failed\n", round);
43 exit (1);
46 int e = LOCK (&m);
47 if (e != 0)
49 printf ("%ld: child: mutex_lock failed with error %d\n", round, e);
50 exit (1);
53 e = pthread_barrier_wait (&b);
54 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
56 printf ("%ld: child: 1st barrier_wait failed\n", round);
57 exit (1);
60 e = pthread_barrier_wait (&b);
61 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
63 printf ("%ld: child: 2nd barrier_wait failed\n", round);
64 exit (1);
67 pthread_testcancel ();
69 printf ("%ld: testcancel returned\n", round);
70 exit (1);
74 static int
75 do_test (void)
77 #ifdef PREPARE_TMO
78 PREPARE_TMO;
79 #endif
81 pthread_mutexattr_t a;
82 if (pthread_mutexattr_init (&a) != 0)
84 puts ("mutexattr_init failed");
85 return 1;
87 if (pthread_mutexattr_setrobust_np (&a, PTHREAD_MUTEX_ROBUST_NP) != 0)
89 puts ("mutexattr_setrobust failed");
90 return 1;
92 #ifndef NOT_CONSISTENT
93 if (pthread_mutex_init (&m, &a) != 0)
95 puts ("mutex_init failed");
96 return 1;
98 #endif
100 if (pthread_barrier_init (&b, NULL, 2) != 0)
102 puts ("barrier_init failed");
103 return 1;
106 for (long int round = 1; round < 5; ++round)
108 #ifdef NOT_CONSISTENT
109 if (pthread_mutex_init (&m, &a) != 0)
111 puts ("mutex_init failed");
112 return 1;
114 #endif
116 pthread_t th;
117 if (pthread_create (&th, NULL, tf, (void *) round) != 0)
119 printf ("%ld: create failed\n", round);
120 return 1;
123 int e = pthread_barrier_wait (&b);
124 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
126 printf ("%ld: parent: 1st barrier_wait failed\n", round);
127 return 1;
130 if (pthread_cancel (th) != 0)
132 printf ("%ld: cancel failed\n", round);
133 return 1;
136 e = pthread_barrier_wait (&b);
137 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
139 printf ("%ld: parent: 2nd barrier_wait failed\n", round);
140 return 1;
143 #ifndef AFTER_JOIN
144 if (round & 1)
145 #endif
147 void *res;
148 if (pthread_join (th, &res) != 0)
150 printf ("%ld: join failed\n", round);
151 return 1;
153 if (res != PTHREAD_CANCELED)
155 printf ("%ld: thread not canceled\n", round);
156 return 1;
160 e = LOCK (&m);
161 if (e == 0)
163 printf ("%ld: parent: mutex_lock succeeded\n", round);
164 return 1;
166 if (e != EOWNERDEAD)
168 printf ("%ld: parent: mutex_lock returned wrong code\n", round);
169 return 1;
172 #ifndef AFTER_JOIN
173 if ((round & 1) == 0)
175 void *res;
176 if (pthread_join (th, &res) != 0)
178 printf ("%ld: join failed\n", round);
179 return 1;
181 if (res != PTHREAD_CANCELED)
183 printf ("%ld: thread not canceled\n", round);
184 return 1;
187 #endif
189 #ifndef NOT_CONSISTENT
190 e = pthread_mutex_consistent_np (&m);
191 if (e != 0)
193 printf ("%ld: mutex_consistent failed with error %d\n", round, e);
194 return 1;
196 #endif
198 e = pthread_mutex_unlock (&m);
199 if (e != 0)
201 printf ("%ld: mutex_unlocked failed\n", round);
202 return 1;
205 #ifdef NOT_CONSISTENT
206 e = LOCK (&m);
207 if (e == 0)
209 printf ("%ld: locking inconsistent mutex succeeded\n", round);
210 return 1;
212 if (e != ENOTRECOVERABLE)
214 printf ("%ld: locking inconsistent mutex failed with error %d\n",
215 round, e);
216 return 1;
219 if (pthread_mutex_destroy (&m) != 0)
221 puts ("mutex_destroy failed");
222 return 1;
224 #endif
227 #ifndef NOT_CONSISTENT
228 if (pthread_mutex_destroy (&m) != 0)
230 puts ("mutex_destroy failed");
231 return 1;
233 #endif
235 if (pthread_mutexattr_destroy (&a) != 0)
237 puts ("mutexattr_destroy failed");
238 return 1;
241 return 0;
244 #define TEST_FUNCTION do_test ()
245 #include "../test-skeleton.c"