* sysdeps/unix/sysv/linux/m68k/semtimedop.S: New file.
[glibc/pb-stable.git] / nptl / tst-cond6.c
blob02a0bdfbd18001ba37765c2ad93f9d5d70362a9f
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 <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 #if ! _POSIX_THREAD_PROCESS_SHARED
39 puts ("_POSIX_THREAD_PROCESS_SHARED not supported, test skipped");
40 return 0;
42 #else
44 size_t ps = sysconf (_SC_PAGESIZE);
45 char tmpfname[] = "/tmp/tst-cond6.XXXXXX";
46 char data[ps];
47 void *mem;
48 int fd;
49 pthread_mutexattr_t ma;
50 pthread_mutex_t *mut1;
51 pthread_mutex_t *mut2;
52 pthread_condattr_t ca;
53 pthread_cond_t *cond;
54 pid_t pid;
55 int result = 0;
57 fd = mkstemp (tmpfname);
58 if (fd == -1)
60 printf ("cannot open temporary file: %m\n");
61 exit (1);
64 /* Make sure it is always removed. */
65 unlink (tmpfname);
67 /* Create one page of data. */
68 memset (data, '\0', ps);
70 /* Write the data to the file. */
71 if (write (fd, data, ps) != (ssize_t) ps)
73 puts ("short write");
74 exit (1);
77 mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
78 if (mem == MAP_FAILED)
80 printf ("mmap failed: %m\n");
81 exit (1);
84 mut1 = (pthread_mutex_t *) (((uintptr_t) mem
85 + __alignof (pthread_mutex_t))
86 & ~(__alignof (pthread_mutex_t) - 1));
87 mut2 = mut1 + 1;
89 cond = (pthread_cond_t *) (((uintptr_t) (mut2 + 1)
90 + __alignof (pthread_cond_t))
91 & ~(__alignof (pthread_cond_t) - 1));
93 condition = (int *) (((uintptr_t) (cond + 1) + __alignof (int))
94 & ~(__alignof (int) - 1));
96 if (pthread_mutexattr_init (&ma) != 0)
98 puts ("mutexattr_init failed");
99 exit (1);
102 if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0)
104 puts ("mutexattr_setpshared failed");
105 exit (1);
108 if (pthread_mutex_init (mut1, &ma) != 0)
110 puts ("1st mutex_init failed");
111 exit (1);
114 if (pthread_mutex_init (mut2, &ma) != 0)
116 puts ("2nd mutex_init failed");
117 exit (1);
120 if (pthread_condattr_init (&ca) != 0)
122 puts ("condattr_init failed");
123 exit (1);
126 if (pthread_condattr_setpshared (&ca, PTHREAD_PROCESS_SHARED) != 0)
128 puts ("condattr_setpshared failed");
129 exit (1);
132 if (pthread_cond_init (cond, &ca) != 0)
134 puts ("cond_init failed");
135 exit (1);
138 if (pthread_mutex_lock (mut1) != 0)
140 puts ("parent: 1st mutex_lock failed");
141 exit (1);
144 puts ("going to fork now");
145 pid = fork ();
146 if (pid == -1)
148 puts ("fork failed");
149 exit (1);
151 else if (pid == 0)
153 struct timespec ts;
154 struct timeval tv;
156 if (pthread_mutex_lock (mut2) != 0)
158 puts ("child: mutex_lock failed");
159 exit (1);
162 if (pthread_mutex_unlock (mut1) != 0)
164 puts ("child: 1st mutex_unlock failed");
165 exit (1);
168 /* Waiting for the condition will fail. But we want the timeout
169 here. */
170 if (gettimeofday (&tv, NULL) != 0)
172 puts ("gettimeofday failed");
173 exit (1);
176 TIMEVAL_TO_TIMESPEC (&tv, &ts);
177 ts.tv_nsec += 500000000;
178 if (ts.tv_nsec >= 1000000000)
180 ts.tv_nsec -= 1000000000;
181 ++ts.tv_sec;
185 if (pthread_cond_timedwait (cond, mut2, &ts) != 0)
187 puts ("child: cond_wait failed");
188 exit (1);
190 while (*condition == 0);
192 if (pthread_mutex_unlock (mut2) != 0)
194 puts ("child: 2nd mutex_unlock failed");
195 exit (1);
198 puts ("child done");
200 else
202 int status;
204 if (pthread_mutex_lock (mut1) != 0)
206 puts ("parent: 2nd mutex_lock failed");
207 exit (1);
210 if (pthread_mutex_lock (mut2) != 0)
212 puts ("parent: 3rd mutex_lock failed");
213 exit (1);
216 if (pthread_cond_signal (cond) != 0)
218 puts ("parent: cond_signal failed");
219 exit (1);
222 *condition = 1;
224 if (pthread_mutex_unlock (mut2) != 0)
226 puts ("parent: mutex_unlock failed");
227 exit (1);
230 puts ("waiting for child");
232 waitpid (pid, &status, 0);
233 result |= status;
235 puts ("parent done");
238 return result;
239 #endif
242 #define TEST_FUNCTION do_test ()
243 #include "../test-skeleton.c"