Add tests for strfrom functions
[glibc.git] / nptl / tst-cond12.c
blobe012172a3ba4bb5da0a7447d6818a12babdeec48
1 /* Copyright (C) 2003-2016 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <pthread.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/mman.h>
26 #include <sys/wait.h>
29 static char fname[] = "/tmp/tst-cond12-XXXXXX";
30 static int fd;
33 static void prepare (void);
34 #define PREPARE(argc, argv) prepare ()
36 static int do_test (void);
37 #define TEST_FUNCTION do_test ()
39 #include "../test-skeleton.c"
42 static void
43 prepare (void)
45 fd = mkstemp (fname);
46 if (fd == -1)
48 printf ("mkstemp failed: %m\n");
49 exit (1);
51 add_temp_file (fname);
52 if (ftruncate (fd, 1000) < 0)
54 printf ("ftruncate failed: %m\n");
55 exit (1);
60 static int
61 do_test (void)
63 struct
65 pthread_mutex_t m;
66 pthread_cond_t c;
67 int var;
68 } *p = mmap (NULL, sizeof (*p), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
69 if (p == MAP_FAILED)
71 printf ("initial mmap failed: %m\n");
72 return 1;
75 pthread_mutexattr_t ma;
76 if (pthread_mutexattr_init (&ma) != 0)
78 puts ("mutexattr_init failed");
79 return 1;
81 if (pthread_mutexattr_setpshared (&ma, 1) != 0)
83 puts ("mutexattr_setpshared failed");
84 return 1;
86 if (pthread_mutex_init (&p->m, &ma) != 0)
88 puts ("mutex_init failed");
89 return 1;
91 if (pthread_mutexattr_destroy (&ma) != 0)
93 puts ("mutexattr_destroy failed");
94 return 1;
97 pthread_condattr_t ca;
98 if (pthread_condattr_init (&ca) != 0)
100 puts ("condattr_init failed");
101 return 1;
103 if (pthread_condattr_setpshared (&ca, 1) != 0)
105 puts ("condattr_setpshared failed");
106 return 1;
108 if (pthread_cond_init (&p->c, &ca) != 0)
110 puts ("mutex_init failed");
111 return 1;
113 if (pthread_condattr_destroy (&ca) != 0)
115 puts ("condattr_destroy failed");
116 return 1;
119 if (pthread_mutex_lock (&p->m) != 0)
121 puts ("initial mutex_lock failed");
122 return 1;
125 p->var = 42;
127 pid_t pid = fork ();
128 if (pid == -1)
130 printf ("fork failed: %m\n");
131 return 1;
134 if (pid == 0)
136 void *oldp = p;
137 p = mmap (NULL, sizeof (*p), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
139 if (p == oldp)
141 puts ("child: mapped to same address");
142 kill (getppid (), SIGKILL);
143 exit (1);
146 munmap (oldp, sizeof (*p));
148 if (pthread_mutex_lock (&p->m) != 0)
150 puts ("child: mutex_lock failed");
151 kill (getppid (), SIGKILL);
152 exit (1);
155 p->var = 0;
157 #ifndef USE_COND_SIGNAL
158 if (pthread_cond_broadcast (&p->c) != 0)
160 puts ("child: cond_broadcast failed");
161 kill (getppid (), SIGKILL);
162 exit (1);
164 #else
165 if (pthread_cond_signal (&p->c) != 0)
167 puts ("child: cond_signal failed");
168 kill (getppid (), SIGKILL);
169 exit (1);
171 #endif
173 if (pthread_mutex_unlock (&p->m) != 0)
175 puts ("child: mutex_unlock failed");
176 kill (getppid (), SIGKILL);
177 exit (1);
180 exit (0);
184 pthread_cond_wait (&p->c, &p->m);
185 while (p->var != 0);
187 if (TEMP_FAILURE_RETRY (waitpid (pid, NULL, 0)) != pid)
189 printf ("waitpid failed: %m\n");
190 kill (pid, SIGKILL);
191 return 1;
194 return 0;