OSDN Git Service

h8300: O_DIRECT and O_DIRECTIRY swapping.
[uclinux-h8/uclibc-ng.git] / test / nptl / tst-mutex2.c
1 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
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/>.  */
18
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24
25 static pthread_mutex_t m;
26 static pthread_barrier_t b;
27
28
29 static void *
30 tf (void *arg)
31 {
32   int e = pthread_mutex_unlock (&m);
33   if (e == 0)
34     {
35       puts ("child: 1st mutex_unlock succeeded");
36       exit (1);
37     }
38   else if (e != EPERM)
39     {
40       puts ("child: 1st mutex_unlock error != EPERM");
41       exit (1);
42     }
43
44   e = pthread_mutex_trylock (&m);
45   if (e == 0)
46     {
47       puts ("child: 1st trylock suceeded");
48       exit (1);
49     }
50   if (e != EBUSY)
51     {
52       puts ("child: 1st trylock didn't return EBUSY");
53       exit (1);
54     }
55
56   e = pthread_barrier_wait (&b);
57   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
58     {
59       puts ("child: 1st barrier_wait failed");
60       exit (1);
61     }
62
63   e = pthread_barrier_wait (&b);
64   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
65     {
66       puts ("child: 2nd barrier_wait failed");
67       exit (1);
68     }
69
70   e = pthread_mutex_unlock (&m);
71   if (e == 0)
72     {
73       puts ("child: 2nd mutex_unlock succeeded");
74       exit (1);
75     }
76   else if (e != EPERM)
77     {
78       puts ("child: 2nd mutex_unlock error != EPERM");
79       exit (1);
80     }
81
82   if (pthread_mutex_trylock (&m) != 0)
83     {
84       puts ("child: 2nd trylock failed");
85       exit (1);
86     }
87
88   if (pthread_mutex_unlock (&m) != 0)
89     {
90       puts ("child: 3rd mutex_unlock failed");
91       exit (1);
92     }
93
94   return NULL;
95 }
96
97
98 static int
99 do_test (void)
100 {
101   pthread_mutexattr_t a;
102   int e;
103
104   if (pthread_mutexattr_init (&a) != 0)
105     {
106       puts ("mutexattr_init failed");
107       exit (1);
108     }
109
110   if (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_ERRORCHECK) != 0)
111     {
112       puts ("mutexattr_settype failed");
113       exit (1);
114     }
115
116   if (pthread_mutex_init (&m, &a) != 0)
117     {
118       puts ("mutex_init failed");
119       exit (1);
120     }
121
122   if (pthread_barrier_init (&b, NULL, 2) != 0)
123     {
124       puts ("barrier_init failed");
125       exit (1);
126     }
127
128   if ((e = pthread_mutex_unlock (&m)) == 0)
129     {
130       puts ("1st mutex_unlock succeeded");
131       exit (1);
132     }
133   else if (e != EPERM)
134     {
135       puts ("1st mutex_unlock error != EPERM");
136       exit (1);
137     }
138
139   if (pthread_mutex_lock (&m) != 0)
140     {
141       puts ("mutex_lock failed");
142       exit (1);
143     }
144
145   if ((e = pthread_mutex_lock (&m)) == 0)
146     {
147       puts ("2nd mutex_lock succeeded");
148       exit (1);
149     }
150   else if (e != EDEADLK)
151     {
152       puts ("2nd mutex_lock error != EDEADLK");
153       exit (1);
154     }
155
156   pthread_t th;
157   if (pthread_create (&th, NULL, tf, NULL) != 0)
158     {
159       puts ("create failed");
160       exit (1);
161     }
162
163   e = pthread_barrier_wait (&b);
164   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
165     {
166       puts ("1st barrier_wait failed");
167       exit (1);
168     }
169
170   if (pthread_mutex_unlock (&m) != 0)
171     {
172       puts ("2nd mutex_unlock failed");
173       exit (1);
174     }
175
176   if ((e = pthread_mutex_unlock (&m)) == 0)
177     {
178       puts ("3rd mutex_unlock succeeded");
179       exit (1);
180     }
181   else if (e != EPERM)
182     {
183       puts ("3rd mutex_unlock error != EPERM");
184       exit (1);
185     }
186
187   e = pthread_barrier_wait (&b);
188   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
189     {
190       puts ("2nd barrier_wait failed");
191       exit (1);
192     }
193
194   if (pthread_join (th, NULL) != 0)
195     {
196       puts ("join failed");
197       exit (1);
198     }
199
200   if (pthread_mutex_destroy (&m) != 0)
201     {
202       puts ("mutex_destroy failed");
203       exit (1);
204     }
205
206   if (pthread_barrier_destroy (&b) != 0)
207     {
208       puts ("barrier_destroy failed");
209       exit (1);
210     }
211
212   if (pthread_mutexattr_destroy (&a) != 0)
213     {
214       puts ("mutexattr_destroy failed");
215       exit (1);
216     }
217
218   return 0;
219 }
220
221 #define TEST_FUNCTION do_test ()
222 #include "../test-skeleton.c"