Updated to fedora-glibc-20060424T0820
[glibc/history.git] / nptl / tst-cond6.c
blobb5dcaa810d41465ea388bdd6335e537dd4c19dca
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.
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 <time.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
28 #include <sys/time.h>
29 #include <sys/wait.h>
32 int *condition;
34 static int
35 do_test (void)
37 size_t ps = sysconf (_SC_PAGESIZE);
38 char tmpfname[] = "/tmp/tst-cond6.XXXXXX";
39 char data[ps];
40 void *mem;
41 int fd;
42 pthread_mutexattr_t ma;
43 pthread_mutex_t *mut1;
44 pthread_mutex_t *mut2;
45 pthread_condattr_t ca;
46 pthread_cond_t *cond;
47 pid_t pid;
48 int result = 0;
50 fd = mkstemp (tmpfname);
51 if (fd == -1)
53 printf ("cannot open temporary file: %m\n");
54 exit (1);
57 /* Make sure it is always removed. */
58 unlink (tmpfname);
60 /* Create one page of data. */
61 memset (data, '\0', ps);
63 /* Write the data to the file. */
64 if (write (fd, data, ps) != (ssize_t) ps)
66 puts ("short write");
67 exit (1);
70 mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
71 if (mem == MAP_FAILED)
73 printf ("mmap failed: %m\n");
74 exit (1);
77 mut1 = (pthread_mutex_t *) (((uintptr_t) mem
78 + __alignof (pthread_mutex_t))
79 & ~(__alignof (pthread_mutex_t) - 1));
80 mut2 = mut1 + 1;
82 cond = (pthread_cond_t *) (((uintptr_t) (mut2 + 1)
83 + __alignof (pthread_cond_t))
84 & ~(__alignof (pthread_cond_t) - 1));
86 condition = (int *) (((uintptr_t) (cond + 1) + __alignof (int))
87 & ~(__alignof (int) - 1));
89 if (pthread_mutexattr_init (&ma) != 0)
91 puts ("mutexattr_init failed");
92 exit (1);
95 if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0)
97 puts ("mutexattr_setpshared failed");
98 exit (1);
101 if (pthread_mutex_init (mut1, &ma) != 0)
103 puts ("1st mutex_init failed");
104 exit (1);
107 if (pthread_mutex_init (mut2, &ma) != 0)
109 puts ("2nd mutex_init failed");
110 exit (1);
113 if (pthread_condattr_init (&ca) != 0)
115 puts ("condattr_init failed");
116 exit (1);
119 if (pthread_condattr_setpshared (&ca, PTHREAD_PROCESS_SHARED) != 0)
121 puts ("condattr_setpshared failed");
122 exit (1);
125 if (pthread_cond_init (cond, &ca) != 0)
127 puts ("cond_init failed");
128 exit (1);
131 if (pthread_mutex_lock (mut1) != 0)
133 puts ("parent: 1st mutex_lock failed");
134 exit (1);
137 puts ("going to fork now");
138 pid = fork ();
139 if (pid == -1)
141 puts ("fork failed");
142 exit (1);
144 else if (pid == 0)
146 struct timespec ts;
147 struct timeval tv;
149 if (pthread_mutex_lock (mut2) != 0)
151 puts ("child: mutex_lock failed");
152 exit (1);
155 if (pthread_mutex_unlock (mut1) != 0)
157 puts ("child: 1st mutex_unlock failed");
158 exit (1);
161 if (gettimeofday (&tv, NULL) != 0)
163 puts ("gettimeofday failed");
164 exit (1);
167 TIMEVAL_TO_TIMESPEC (&tv, &ts);
168 ts.tv_nsec += 500000000;
169 if (ts.tv_nsec >= 1000000000)
171 ts.tv_nsec -= 1000000000;
172 ++ts.tv_sec;
176 if (pthread_cond_timedwait (cond, mut2, &ts) != 0)
178 puts ("child: cond_wait failed");
179 exit (1);
181 while (*condition == 0);
183 if (pthread_mutex_unlock (mut2) != 0)
185 puts ("child: 2nd mutex_unlock failed");
186 exit (1);
189 puts ("child done");
191 else
193 int status;
195 if (pthread_mutex_lock (mut1) != 0)
197 puts ("parent: 2nd mutex_lock failed");
198 exit (1);
201 if (pthread_mutex_lock (mut2) != 0)
203 puts ("parent: 3rd mutex_lock failed");
204 exit (1);
207 if (pthread_cond_signal (cond) != 0)
209 puts ("parent: cond_signal failed");
210 exit (1);
213 *condition = 1;
215 if (pthread_mutex_unlock (mut2) != 0)
217 puts ("parent: mutex_unlock failed");
218 exit (1);
221 puts ("waiting for child");
223 waitpid (pid, &status, 0);
224 result |= status;
226 puts ("parent done");
229 return result;
232 #define TEST_FUNCTION do_test ()
233 #include "../test-skeleton.c"