2004-09-27 Roland McGrath <roland@redhat.com>
[glibc.git] / nptl / tst-cond4.c
blob071528df0ed195b10f636bebbe7501585ce901ec
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.
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>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/wait.h>
30 int *condition;
32 static int
33 do_test (void)
35 #if ! _POSIX_THREAD_PROCESS_SHARED
37 puts ("_POSIX_THREAD_PROCESS_SHARED not supported, test skipped");
38 return 0;
40 #else
42 size_t ps = sysconf (_SC_PAGESIZE);
43 char tmpfname[] = "/tmp/tst-cond4.XXXXXX";
44 char data[ps];
45 void *mem;
46 int fd;
47 pthread_mutexattr_t ma;
48 pthread_mutex_t *mut1;
49 pthread_mutex_t *mut2;
50 pthread_condattr_t ca;
51 pthread_cond_t *cond;
52 pid_t pid;
53 int result = 0;
54 int p;
56 fd = mkstemp (tmpfname);
57 if (fd == -1)
59 printf ("cannot open temporary file: %m\n");
60 return 1;
63 /* Make sure it is always removed. */
64 unlink (tmpfname);
66 /* Create one page of data. */
67 memset (data, '\0', ps);
69 /* Write the data to the file. */
70 if (write (fd, data, ps) != (ssize_t) ps)
72 puts ("short write");
73 return 1;
76 mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
77 if (mem == MAP_FAILED)
79 printf ("mmap failed: %m\n");
80 return 1;
83 mut1 = (pthread_mutex_t *) (((uintptr_t) mem
84 + __alignof (pthread_mutex_t))
85 & ~(__alignof (pthread_mutex_t) - 1));
86 mut2 = mut1 + 1;
88 cond = (pthread_cond_t *) (((uintptr_t) (mut2 + 1)
89 + __alignof (pthread_cond_t))
90 & ~(__alignof (pthread_cond_t) - 1));
92 condition = (int *) (((uintptr_t) (cond + 1) + __alignof (int))
93 & ~(__alignof (int) - 1));
95 if (pthread_mutexattr_init (&ma) != 0)
97 puts ("mutexattr_init failed");
98 return 1;
101 if (pthread_mutexattr_getpshared (&ma, &p) != 0)
103 puts ("1st mutexattr_getpshared failed");
104 return 1;
107 if (p != PTHREAD_PROCESS_PRIVATE)
109 puts ("default pshared value wrong");
110 return 1;
113 if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0)
115 puts ("mutexattr_setpshared failed");
116 return 1;
119 if (pthread_mutexattr_getpshared (&ma, &p) != 0)
121 puts ("2nd mutexattr_getpshared failed");
122 return 1;
125 if (p != PTHREAD_PROCESS_SHARED)
127 puts ("pshared value after setpshared call wrong");
128 return 1;
131 if (pthread_mutex_init (mut1, &ma) != 0)
133 puts ("1st mutex_init failed");
134 return 1;
137 if (pthread_mutex_init (mut2, &ma) != 0)
139 puts ("2nd mutex_init failed");
140 return 1;
143 if (pthread_condattr_init (&ca) != 0)
145 puts ("condattr_init failed");
146 return 1;
149 if (pthread_condattr_getpshared (&ca, &p) != 0)
151 puts ("1st condattr_getpshared failed");
152 return 1;
155 if (p != PTHREAD_PROCESS_PRIVATE)
157 puts ("default value for pshared in condattr wrong");
158 return 1;
161 if (pthread_condattr_setpshared (&ca, PTHREAD_PROCESS_SHARED) != 0)
163 puts ("condattr_setpshared failed");
164 return 1;
167 if (pthread_condattr_getpshared (&ca, &p) != 0)
169 puts ("2nd condattr_getpshared failed");
170 return 1;
173 if (p != PTHREAD_PROCESS_SHARED)
175 puts ("pshared condattr still not set");
176 return 1;
179 if (pthread_cond_init (cond, &ca) != 0)
181 puts ("cond_init failed");
182 return 1;
185 if (pthread_mutex_lock (mut1) != 0)
187 puts ("parent: 1st mutex_lock failed");
188 return 1;
191 puts ("going to fork now");
192 pid = fork ();
193 if (pid == -1)
195 puts ("fork failed");
196 return 1;
198 else if (pid == 0)
200 if (pthread_mutex_lock (mut2) != 0)
202 puts ("child: mutex_lock failed");
203 return 1;
206 if (pthread_mutex_unlock (mut1) != 0)
208 puts ("child: 1st mutex_unlock failed");
209 return 1;
213 if (pthread_cond_wait (cond, mut2) != 0)
215 puts ("child: cond_wait failed");
216 return 1;
218 while (*condition == 0);
220 if (pthread_mutex_unlock (mut2) != 0)
222 puts ("child: 2nd mutex_unlock failed");
223 return 1;
226 puts ("child done");
228 else
230 int status;
232 if (pthread_mutex_lock (mut1) != 0)
234 puts ("parent: 2nd mutex_lock failed");
235 return 1;
238 if (pthread_mutex_lock (mut2) != 0)
240 puts ("parent: 3rd mutex_lock failed");
241 return 1;
244 if (pthread_cond_signal (cond) != 0)
246 puts ("parent: cond_signal failed");
247 return 1;
250 *condition = 1;
252 if (pthread_mutex_unlock (mut2) != 0)
254 puts ("parent: mutex_unlock failed");
255 return 1;
258 puts ("waiting for child");
260 waitpid (pid, &status, 0);
261 result |= status;
263 puts ("parent done");
266 return result;
267 #endif
270 #define TEST_FUNCTION do_test ()
271 #include "../test-skeleton.c"