OSDN Git Service

4d88596164b0c62f9f3ede516aed607f69e5f8c6
[uclinux-h8/uClibc.git] / test / nptl / tst-cond4.c
1 /* Copyright (C) 2002, 2003, 2004 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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/wait.h>
28 #include <stdint.h>
29
30
31 int *condition;
32
33 static int
34 do_test (void)
35 {
36   size_t ps = sysconf (_SC_PAGESIZE);
37   char tmpfname[] = "/tmp/tst-cond4.XXXXXX";
38   char data[ps];
39   void *mem;
40   int fd;
41   pthread_mutexattr_t ma;
42   pthread_mutex_t *mut1;
43   pthread_mutex_t *mut2;
44   pthread_condattr_t ca;
45   pthread_cond_t *cond;
46   pid_t pid;
47   int result = 0;
48   int p;
49
50   fd = mkstemp (tmpfname);
51   if (fd == -1)
52     {
53       printf ("cannot open temporary file: %m\n");
54       return 1;
55     }
56
57   /* Make sure it is always removed.  */
58   unlink (tmpfname);
59
60   /* Create one page of data.  */
61   memset (data, '\0', ps);
62
63   /* Write the data to the file.  */
64   if (write (fd, data, ps) != (ssize_t) ps)
65     {
66       puts ("short write");
67       return 1;
68     }
69
70   mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
71   if (mem == MAP_FAILED)
72     {
73       printf ("mmap failed: %m\n");
74       return 1;
75     }
76
77   mut1 = (pthread_mutex_t *) (((uintptr_t) mem
78                                + __alignof (pthread_mutex_t))
79                               & ~(__alignof (pthread_mutex_t) - 1));
80   mut2 = mut1 + 1;
81
82   cond = (pthread_cond_t *) (((uintptr_t) (mut2 + 1)
83                               + __alignof (pthread_cond_t))
84                              & ~(__alignof (pthread_cond_t) - 1));
85
86   condition = (int *) (((uintptr_t) (cond + 1) + __alignof (int))
87                        & ~(__alignof (int) - 1));
88
89   if (pthread_mutexattr_init (&ma) != 0)
90     {
91       puts ("mutexattr_init failed");
92       return 1;
93     }
94
95   if (pthread_mutexattr_getpshared (&ma, &p) != 0)
96     {
97       puts ("1st mutexattr_getpshared failed");
98       return 1;
99     }
100
101   if (p != PTHREAD_PROCESS_PRIVATE)
102     {
103       puts ("default pshared value wrong");
104       return 1;
105     }
106
107   if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0)
108     {
109       puts ("mutexattr_setpshared failed");
110       return 1;
111     }
112
113   if (pthread_mutexattr_getpshared (&ma, &p) != 0)
114     {
115       puts ("2nd mutexattr_getpshared failed");
116       return 1;
117     }
118
119   if (p != PTHREAD_PROCESS_SHARED)
120     {
121       puts ("pshared value after setpshared call wrong");
122       return 1;
123     }
124
125   if (pthread_mutex_init (mut1, &ma) != 0)
126     {
127       puts ("1st mutex_init failed");
128       return 1;
129     }
130
131   if (pthread_mutex_init (mut2, &ma) != 0)
132     {
133       puts ("2nd mutex_init failed");
134       return 1;
135     }
136
137   if (pthread_condattr_init (&ca) != 0)
138     {
139       puts ("condattr_init failed");
140       return 1;
141     }
142
143   if (pthread_condattr_getpshared (&ca, &p) != 0)
144     {
145       puts ("1st condattr_getpshared failed");
146       return 1;
147     }
148
149   if (p != PTHREAD_PROCESS_PRIVATE)
150     {
151       puts ("default value for pshared in condattr wrong");
152       return 1;
153     }
154
155   if (pthread_condattr_setpshared (&ca, PTHREAD_PROCESS_SHARED) != 0)
156     {
157       puts ("condattr_setpshared failed");
158       return 1;
159     }
160
161   if (pthread_condattr_getpshared (&ca, &p) != 0)
162     {
163       puts ("2nd condattr_getpshared failed");
164       return 1;
165     }
166
167   if (p != PTHREAD_PROCESS_SHARED)
168     {
169       puts ("pshared condattr still not set");
170       return 1;
171     }
172
173   if (pthread_cond_init (cond, &ca) != 0)
174     {
175       puts ("cond_init failed");
176       return 1;
177     }
178
179   if (pthread_mutex_lock (mut1) != 0)
180     {
181       puts ("parent: 1st mutex_lock failed");
182       return 1;
183     }
184
185   puts ("going to fork now");
186   pid = fork ();
187   if (pid == -1)
188     {
189       puts ("fork failed");
190       return 1;
191     }
192   else if (pid == 0)
193     {
194       if (pthread_mutex_lock (mut2) != 0)
195         {
196           puts ("child: mutex_lock failed");
197           return 1;
198         }
199
200       if (pthread_mutex_unlock (mut1) != 0)
201         {
202           puts ("child: 1st mutex_unlock failed");
203           return 1;
204         }
205
206       do
207         if (pthread_cond_wait (cond, mut2) != 0)
208           {
209             puts ("child: cond_wait failed");
210             return 1;
211           }
212       while (*condition == 0);
213
214       if (pthread_mutex_unlock (mut2) != 0)
215         {
216           puts ("child: 2nd mutex_unlock failed");
217           return 1;
218         }
219
220       puts ("child done");
221     }
222   else
223     {
224       int status;
225
226       if (pthread_mutex_lock (mut1) != 0)
227         {
228           puts ("parent: 2nd mutex_lock failed");
229           return 1;
230         }
231
232       if (pthread_mutex_lock (mut2) != 0)
233         {
234           puts ("parent: 3rd mutex_lock failed");
235           return 1;
236         }
237
238       if (pthread_cond_signal (cond) != 0)
239         {
240           puts ("parent: cond_signal failed");
241           return 1;
242         }
243
244       *condition = 1;
245
246       if (pthread_mutex_unlock (mut2) != 0)
247         {
248           puts ("parent: mutex_unlock failed");
249           return 1;
250         }
251
252       puts ("waiting for child");
253
254       waitpid (pid, &status, 0);
255       result |= status;
256
257       puts ("parent done");
258     }
259
260  return result;
261 }
262
263 #define TEST_FUNCTION do_test ()
264 #include "../test-skeleton.c"