Fix Wundef warning for __STDC_VERSION__
[glibc.git] / nptl / tst-mutex2.c
blob161cb28818161219fd14f4904c7660a54c835003
1 /* Copyright (C) 2002-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
25 static pthread_mutex_t m;
26 static pthread_barrier_t b;
29 static void *
30 tf (void *arg)
32 int e = pthread_mutex_unlock (&m);
33 if (e == 0)
35 puts ("child: 1st mutex_unlock succeeded");
36 exit (1);
38 else if (e != EPERM)
40 puts ("child: 1st mutex_unlock error != EPERM");
41 exit (1);
44 e = pthread_mutex_trylock (&m);
45 if (e == 0)
47 puts ("child: 1st trylock suceeded");
48 exit (1);
50 if (e != EBUSY)
52 puts ("child: 1st trylock didn't return EBUSY");
53 exit (1);
56 e = pthread_barrier_wait (&b);
57 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
59 puts ("child: 1st barrier_wait failed");
60 exit (1);
63 e = pthread_barrier_wait (&b);
64 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
66 puts ("child: 2nd barrier_wait failed");
67 exit (1);
70 e = pthread_mutex_unlock (&m);
71 if (e == 0)
73 puts ("child: 2nd mutex_unlock succeeded");
74 exit (1);
76 else if (e != EPERM)
78 puts ("child: 2nd mutex_unlock error != EPERM");
79 exit (1);
82 if (pthread_mutex_trylock (&m) != 0)
84 puts ("child: 2nd trylock failed");
85 exit (1);
88 if (pthread_mutex_unlock (&m) != 0)
90 puts ("child: 3rd mutex_unlock failed");
91 exit (1);
94 return NULL;
98 static int
99 do_test (void)
101 pthread_mutexattr_t a;
102 int e;
104 if (pthread_mutexattr_init (&a) != 0)
106 puts ("mutexattr_init failed");
107 return 1;
110 if (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_ERRORCHECK) != 0)
112 puts ("mutexattr_settype failed");
113 return 1;
116 #ifdef ENABLE_PI
117 if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
119 puts ("pthread_mutexattr_setprotocol failed");
120 return 1;
122 #endif
124 e = pthread_mutex_init (&m, &a);
125 if (e != 0)
127 #ifdef ENABLE_PI
128 if (e == ENOTSUP)
130 puts ("PI mutexes unsupported");
131 return 0;
133 #endif
134 puts ("mutex_init failed");
135 return 1;
138 if (pthread_barrier_init (&b, NULL, 2) != 0)
140 puts ("barrier_init failed");
141 return 1;
144 e = pthread_mutex_unlock (&m);
145 if (e == 0)
147 puts ("1st mutex_unlock succeeded");
148 return 1;
150 else if (e != EPERM)
152 puts ("1st mutex_unlock error != EPERM");
153 return 1;
156 if (pthread_mutex_lock (&m) != 0)
158 puts ("mutex_lock failed");
159 return 1;
162 e = pthread_mutex_lock (&m);
163 if (e == 0)
165 puts ("2nd mutex_lock succeeded");
166 return 1;
168 else if (e != EDEADLK)
170 puts ("2nd mutex_lock error != EDEADLK");
171 return 1;
174 pthread_t th;
175 if (pthread_create (&th, NULL, tf, NULL) != 0)
177 puts ("create failed");
178 return 1;
181 e = pthread_barrier_wait (&b);
182 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
184 puts ("1st barrier_wait failed");
185 return 1;
188 if (pthread_mutex_unlock (&m) != 0)
190 puts ("2nd mutex_unlock failed");
191 return 1;
194 e = pthread_mutex_unlock (&m);
195 if (e == 0)
197 puts ("3rd mutex_unlock succeeded");
198 return 1;
200 else if (e != EPERM)
202 puts ("3rd mutex_unlock error != EPERM");
203 return 1;
206 e = pthread_barrier_wait (&b);
207 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
209 puts ("2nd barrier_wait failed");
210 return 1;
213 if (pthread_join (th, NULL) != 0)
215 puts ("join failed");
216 return 1;
219 if (pthread_mutex_destroy (&m) != 0)
221 puts ("mutex_destroy failed");
222 return 1;
225 if (pthread_barrier_destroy (&b) != 0)
227 puts ("barrier_destroy failed");
228 return 1;
231 if (pthread_mutexattr_destroy (&a) != 0)
233 puts ("mutexattr_destroy failed");
234 return 1;
237 return 0;
240 #define TEST_FUNCTION do_test ()
241 #include "../test-skeleton.c"