[BZ #3306]
[glibc.git] / nptl / tst-robust7.c
blobd0bc91cc8ed494bc42d3ce4750b146bbe0e9b68c
1 /* Copyright (C) 2005, 2006 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 #ifdef ENABLE_PI
99 if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
101 puts ("pthread_mutexattr_setprotocol failed");
102 return 1;
104 #endif
106 int e;
107 e = pthread_mutex_init (&m, &a);
108 if (e != 0)
110 #ifdef ENABLE_PI
111 if (e == ENOTSUP)
113 puts ("PI robust mutexes not supported");
114 return 0;
116 #endif
117 puts ("mutex_init failed");
118 return 1;
121 if (pthread_mutexattr_destroy (&a) != 0)
123 puts ("mutexattr_destroy failed");
124 return 1;
127 if (pthread_barrier_init (&b, NULL, 2) != 0)
129 puts ("barrier_init failed");
130 return 1;
133 #define N 5
134 pthread_t th[N];
135 for (long int n = 0; n < N; ++n)
137 if (pthread_create (&th[n], NULL, tf, (void *) n) != 0)
139 printf ("pthread_create loop %ld failed\n", n + 1);
140 return 1;
143 e = pthread_barrier_wait (&b);
144 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
146 printf ("parent: barrier_wait failed in round %ld\n", n + 1);
147 return 1;
151 if (pthread_mutex_lock (&m) != 0)
153 puts ("parent: mutex_lock failed");
154 return 1;
157 if (pthread_mutex_unlock (&m) != 0)
159 puts ("parent: mutex_unlock failed");
160 return 1;
163 if (pthread_cond_broadcast (&c) != 0)
165 puts ("cond_broadcast failed");
166 return 1;
169 for (int n = 0; n < N; ++n)
171 void *res;
172 if (pthread_join (th[n], &res) != 0)
174 printf ("join round %d failed\n", n + 1);
175 return 1;
177 if (res != PTHREAD_CANCELED)
179 printf ("thread %d not canceled\n", n + 1);
180 return 1;
184 e = pthread_mutex_lock (&m);
185 if (e == 0)
187 puts ("parent: 2nd mutex_lock succeeded");
188 return 1;
190 if (e != EOWNERDEAD)
192 puts ("parent: mutex_lock did not return EOWNERDEAD");
193 return 1;
196 if (pthread_mutex_unlock (&m) != 0)
198 puts ("parent: 2nd mutex_unlock failed");
199 return 1;
202 if (pthread_mutex_destroy (&m) != 0)
204 puts ("mutex_destroy failed");
205 return 1;
208 return 0;
211 #define TEST_FUNCTION do_test ()
212 #include "../test-skeleton.c"