(tests): Add tst-endian.
[glibc/history.git] / nptl / tst-mutex3.c
blob284809653861973674cace92c622881c9f9ee5f6
1 /* Copyright (C) 2002, 2003, 2006 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, 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 static void *
31 tf (void *arg)
33 int e = pthread_mutex_unlock (&m);
34 if (e == 0)
36 puts ("1st mutex_unlock in child succeeded");
37 exit (1);
39 if (e != EPERM)
41 puts ("1st mutex_unlock in child didn't return EPERM");
42 exit (1);
45 e = pthread_mutex_trylock (&m);
46 if (e == 0)
48 puts ("mutex_trylock in second thread succeeded");
49 exit (1);
51 if (e != EBUSY)
53 puts ("mutex_trylock returned wrong value");
54 exit (1);
57 e = pthread_barrier_wait (&b);
58 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
60 puts ("barrier_wait failed");
61 exit (1);
64 e = pthread_barrier_wait (&b);
65 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
67 puts ("barrier_wait failed");
68 exit (1);
71 e = pthread_mutex_unlock (&m);
72 if (e == 0)
74 puts ("2nd mutex_unlock in child succeeded");
75 exit (1);
77 if (e != EPERM)
79 puts ("2nd mutex_unlock in child didn't return EPERM");
80 exit (1);
83 if (pthread_mutex_trylock (&m) != 0)
85 puts ("2nd mutex_trylock in second thread failed");
86 exit (1);
89 if (pthread_mutex_unlock (&m) != 0)
91 puts ("3rd mutex_unlock in second thread failed");
92 exit (1);
95 return NULL;
99 static int
100 do_test (void)
102 pthread_mutexattr_t a;
104 if (pthread_mutexattr_init (&a) != 0)
106 puts ("mutexattr_init failed");
107 return 1;
110 if (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_RECURSIVE) != 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 int e;
125 e = pthread_mutex_init (&m, &a);
126 if (e != 0)
128 #ifdef ENABLE_PI
129 if (e == ENOTSUP)
131 puts ("PI mutexes unsupported");
132 return 0;
134 #endif
135 puts ("mutex_init failed");
136 return 1;
139 if (pthread_barrier_init (&b, NULL, 2) != 0)
141 puts ("barrier_init failed");
142 return 1;
145 if (pthread_mutex_lock (&m) != 0)
147 puts ("mutex_lock failed");
148 return 1;
151 if (pthread_mutex_lock (&m) != 0)
153 puts ("2nd mutex_lock failed");
154 return 1;
157 if (pthread_mutex_trylock (&m) != 0)
159 puts ("1st trylock failed");
160 return 1;
163 if (pthread_mutex_unlock (&m) != 0)
165 puts ("mutex_unlock failed");
166 return 1;
169 if (pthread_mutex_unlock (&m) != 0)
171 puts ("2nd mutex_unlock failed");
172 return 1;
175 pthread_t th;
176 if (pthread_create (&th, NULL, tf, NULL) != 0)
178 puts ("create failed");
179 return 1;
182 e = pthread_barrier_wait (&b);
183 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
185 puts ("barrier_wait failed");
186 return 1;
189 if (pthread_mutex_unlock (&m) != 0)
191 puts ("3rd mutex_unlock failed");
192 return 1;
195 e = pthread_mutex_unlock (&m);
196 if (e == 0)
198 puts ("4th mutex_unlock succeeded");
199 return 1;
201 if (e != EPERM)
203 puts ("4th mutex_unlock didn't return EPERM");
204 return 1;
207 e = pthread_barrier_wait (&b);
208 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
210 puts ("barrier_wait failed");
211 return 1;
214 if (pthread_join (th, NULL) != 0)
216 puts ("join failed");
217 return 1;
220 if (pthread_barrier_destroy (&b) != 0)
222 puts ("barrier_destroy failed");
223 return 1;
226 if (pthread_mutex_destroy (&m) != 0)
228 puts ("mutex_destroy failed");
229 return 1;
232 if (pthread_mutexattr_destroy (&a) != 0)
234 puts ("mutexattr_destroy failed");
235 return 1;
238 return 0;
241 #define TEST_FUNCTION do_test ()
242 #include "../test-skeleton.c"