hppa: Optimize syscall sequence.
[glibc.git] / nptl / tst-cond6.c
bloba4c13ba736887bd325ae95e94cfd9256e30c8364
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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/time.h>
28 #include <sys/wait.h>
31 int *condition;
33 static int
34 do_test (void)
36 size_t ps = sysconf (_SC_PAGESIZE);
37 char tmpfname[] = "/tmp/tst-cond6.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;
49 fd = mkstemp (tmpfname);
50 if (fd == -1)
52 printf ("cannot open temporary file: %m\n");
53 exit (1);
56 /* Make sure it is always removed. */
57 unlink (tmpfname);
59 /* Create one page of data. */
60 memset (data, '\0', ps);
62 /* Write the data to the file. */
63 if (write (fd, data, ps) != (ssize_t) ps)
65 puts ("short write");
66 exit (1);
69 mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
70 if (mem == MAP_FAILED)
72 printf ("mmap failed: %m\n");
73 exit (1);
76 mut1 = (pthread_mutex_t *) (((uintptr_t) mem
77 + __alignof (pthread_mutex_t))
78 & ~(__alignof (pthread_mutex_t) - 1));
79 mut2 = mut1 + 1;
81 cond = (pthread_cond_t *) (((uintptr_t) (mut2 + 1)
82 + __alignof (pthread_cond_t))
83 & ~(__alignof (pthread_cond_t) - 1));
85 condition = (int *) (((uintptr_t) (cond + 1) + __alignof (int))
86 & ~(__alignof (int) - 1));
88 if (pthread_mutexattr_init (&ma) != 0)
90 puts ("mutexattr_init failed");
91 exit (1);
94 if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0)
96 puts ("mutexattr_setpshared failed");
97 exit (1);
100 if (pthread_mutex_init (mut1, &ma) != 0)
102 puts ("1st mutex_init failed");
103 exit (1);
106 if (pthread_mutex_init (mut2, &ma) != 0)
108 puts ("2nd mutex_init failed");
109 exit (1);
112 if (pthread_condattr_init (&ca) != 0)
114 puts ("condattr_init failed");
115 exit (1);
118 if (pthread_condattr_setpshared (&ca, PTHREAD_PROCESS_SHARED) != 0)
120 puts ("condattr_setpshared failed");
121 exit (1);
124 if (pthread_cond_init (cond, &ca) != 0)
126 puts ("cond_init failed");
127 exit (1);
130 if (pthread_mutex_lock (mut1) != 0)
132 puts ("parent: 1st mutex_lock failed");
133 exit (1);
136 puts ("going to fork now");
137 pid = fork ();
138 if (pid == -1)
140 puts ("fork failed");
141 exit (1);
143 else if (pid == 0)
145 struct timespec ts;
146 struct timeval tv;
148 if (pthread_mutex_lock (mut2) != 0)
150 puts ("child: mutex_lock failed");
151 exit (1);
154 if (pthread_mutex_unlock (mut1) != 0)
156 puts ("child: 1st mutex_unlock failed");
157 exit (1);
160 if (gettimeofday (&tv, NULL) != 0)
162 puts ("gettimeofday failed");
163 exit (1);
166 TIMEVAL_TO_TIMESPEC (&tv, &ts);
167 ts.tv_nsec += 500000000;
168 if (ts.tv_nsec >= 1000000000)
170 ts.tv_nsec -= 1000000000;
171 ++ts.tv_sec;
175 if (pthread_cond_timedwait (cond, mut2, &ts) != 0)
177 puts ("child: cond_wait failed");
178 exit (1);
180 while (*condition == 0);
182 if (pthread_mutex_unlock (mut2) != 0)
184 puts ("child: 2nd mutex_unlock failed");
185 exit (1);
188 puts ("child done");
190 else
192 int status;
194 if (pthread_mutex_lock (mut1) != 0)
196 puts ("parent: 2nd mutex_lock failed");
197 exit (1);
200 if (pthread_mutex_lock (mut2) != 0)
202 puts ("parent: 3rd mutex_lock failed");
203 exit (1);
206 if (pthread_cond_signal (cond) != 0)
208 puts ("parent: cond_signal failed");
209 exit (1);
212 *condition = 1;
214 if (pthread_mutex_unlock (mut2) != 0)
216 puts ("parent: mutex_unlock failed");
217 exit (1);
220 puts ("waiting for child");
222 waitpid (pid, &status, 0);
223 result |= status;
225 puts ("parent done");
228 return result;
231 #define TEST_FUNCTION do_test ()
232 #include "../test-skeleton.c"