Updated to fedora-glibc-20090424T1908
[glibc/history.git] / nptl / tst-cond12.c
blob03e4881c9b34bd102065c9dbf1c022f413057920
1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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 <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/wait.h>
30 static char fname[] = "/tmp/tst-cond12-XXXXXX";
31 static int fd;
34 static void prepare (void);
35 #define PREPARE(argc, argv) prepare ()
37 static int do_test (void);
38 #define TEST_FUNCTION do_test ()
40 #include "../test-skeleton.c"
43 static void
44 prepare (void)
46 fd = mkstemp (fname);
47 if (fd == -1)
49 printf ("mkstemp failed: %m\n");
50 exit (1);
52 add_temp_file (fname);
53 if (ftruncate (fd, 1000) < 0)
55 printf ("ftruncate failed: %m\n");
56 exit (1);
61 static int
62 do_test (void)
64 struct
66 pthread_mutex_t m;
67 pthread_cond_t c;
68 int var;
69 } *p = mmap (NULL, sizeof (*p), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
70 if (p == MAP_FAILED)
72 printf ("initial mmap failed: %m\n");
73 return 1;
76 pthread_mutexattr_t ma;
77 if (pthread_mutexattr_init (&ma) != 0)
79 puts ("mutexattr_init failed");
80 return 1;
82 if (pthread_mutexattr_setpshared (&ma, 1) != 0)
84 puts ("mutexattr_setpshared failed");
85 return 1;
87 if (pthread_mutex_init (&p->m, &ma) != 0)
89 puts ("mutex_init failed");
90 return 1;
92 if (pthread_mutexattr_destroy (&ma) != 0)
94 puts ("mutexattr_destroy failed");
95 return 1;
98 pthread_condattr_t ca;
99 if (pthread_condattr_init (&ca) != 0)
101 puts ("condattr_init failed");
102 return 1;
104 if (pthread_condattr_setpshared (&ca, 1) != 0)
106 puts ("condattr_setpshared failed");
107 return 1;
109 if (pthread_cond_init (&p->c, &ca) != 0)
111 puts ("mutex_init failed");
112 return 1;
114 if (pthread_condattr_destroy (&ca) != 0)
116 puts ("condattr_destroy failed");
117 return 1;
120 if (pthread_mutex_lock (&p->m) != 0)
122 puts ("initial mutex_lock failed");
123 return 1;
126 p->var = 42;
128 pid_t pid = fork ();
129 if (pid == -1)
131 printf ("fork failed: %m\n");
132 return 1;
135 if (pid == 0)
137 void *oldp = p;
138 p = mmap (NULL, sizeof (*p), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
140 if (p == oldp)
142 puts ("child: mapped to same address");
143 kill (getppid (), SIGKILL);
144 exit (1);
147 munmap (oldp, sizeof (*p));
149 if (pthread_mutex_lock (&p->m) != 0)
151 puts ("child: mutex_lock failed");
152 kill (getppid (), SIGKILL);
153 exit (1);
156 p->var = 0;
158 #ifndef USE_COND_SIGNAL
159 if (pthread_cond_broadcast (&p->c) != 0)
161 puts ("child: cond_broadcast failed");
162 kill (getppid (), SIGKILL);
163 exit (1);
165 #else
166 if (pthread_cond_signal (&p->c) != 0)
168 puts ("child: cond_signal failed");
169 kill (getppid (), SIGKILL);
170 exit (1);
172 #endif
174 if (pthread_mutex_unlock (&p->m) != 0)
176 puts ("child: mutex_unlock failed");
177 kill (getppid (), SIGKILL);
178 exit (1);
181 exit (0);
185 pthread_cond_wait (&p->c, &p->m);
186 while (p->var != 0);
188 if (TEMP_FAILURE_RETRY (waitpid (pid, NULL, 0)) != pid)
190 printf ("waitpid failed: %m\n");
191 kill (pid, SIGKILL);
192 return 1;
195 return 0;