]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/pthread/tst-cond4.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / pthread / tst-cond4.c
CommitLineData
2b778ceb 1/* Copyright (C) 2002-2021 Free Software Foundation, Inc.
76a50749
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
76a50749
UD
18
19#include <errno.h>
20#include <pthread.h>
ceaa9889 21#include <stdint.h>
76a50749
UD
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <sys/mman.h>
27#include <sys/wait.h>
28
29
30int *condition;
31
b910f788
RM
32static int
33do_test (void)
76a50749
UD
34{
35 size_t ps = sysconf (_SC_PAGESIZE);
36 char tmpfname[] = "/tmp/tst-cond4.XXXXXX";
37 char data[ps];
38 void *mem;
39 int fd;
40 pthread_mutexattr_t ma;
41 pthread_mutex_t *mut1;
42 pthread_mutex_t *mut2;
43 pthread_condattr_t ca;
44 pthread_cond_t *cond;
45 pid_t pid;
46 int result = 0;
47 int p;
48
49 fd = mkstemp (tmpfname);
50 if (fd == -1)
51 {
52 printf ("cannot open temporary file: %m\n");
b910f788 53 return 1;
76a50749
UD
54 }
55
56 /* Make sure it is always removed. */
57 unlink (tmpfname);
58
59 /* Create one page of data. */
60 memset (data, '\0', ps);
61
62 /* Write the data to the file. */
f9657e88 63 if (write (fd, data, ps) != (ssize_t) ps)
76a50749
UD
64 {
65 puts ("short write");
b910f788 66 return 1;
76a50749
UD
67 }
68
69 mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
70 if (mem == MAP_FAILED)
71 {
72 printf ("mmap failed: %m\n");
b910f788 73 return 1;
76a50749
UD
74 }
75
76 mut1 = (pthread_mutex_t *) (((uintptr_t) mem
77 + __alignof (pthread_mutex_t))
78 & ~(__alignof (pthread_mutex_t) - 1));
79 mut2 = mut1 + 1;
80
81 cond = (pthread_cond_t *) (((uintptr_t) (mut2 + 1)
82 + __alignof (pthread_cond_t))
83 & ~(__alignof (pthread_cond_t) - 1));
84
85 condition = (int *) (((uintptr_t) (cond + 1) + __alignof (int))
86 & ~(__alignof (int) - 1));
87
88 if (pthread_mutexattr_init (&ma) != 0)
89 {
90 puts ("mutexattr_init failed");
b910f788 91 return 1;
76a50749
UD
92 }
93
94 if (pthread_mutexattr_getpshared (&ma, &p) != 0)
95 {
96 puts ("1st mutexattr_getpshared failed");
b910f788 97 return 1;
76a50749
UD
98 }
99
100 if (p != PTHREAD_PROCESS_PRIVATE)
101 {
102 puts ("default pshared value wrong");
b910f788 103 return 1;
76a50749
UD
104 }
105
106 if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0)
107 {
108 puts ("mutexattr_setpshared failed");
b910f788 109 return 1;
76a50749
UD
110 }
111
112 if (pthread_mutexattr_getpshared (&ma, &p) != 0)
113 {
114 puts ("2nd mutexattr_getpshared failed");
b910f788 115 return 1;
76a50749
UD
116 }
117
118 if (p != PTHREAD_PROCESS_SHARED)
119 {
120 puts ("pshared value after setpshared call wrong");
b910f788 121 return 1;
76a50749
UD
122 }
123
124 if (pthread_mutex_init (mut1, &ma) != 0)
125 {
126 puts ("1st mutex_init failed");
b910f788 127 return 1;
76a50749
UD
128 }
129
130 if (pthread_mutex_init (mut2, &ma) != 0)
131 {
132 puts ("2nd mutex_init failed");
b910f788 133 return 1;
76a50749
UD
134 }
135
136 if (pthread_condattr_init (&ca) != 0)
137 {
138 puts ("condattr_init failed");
b910f788 139 return 1;
76a50749
UD
140 }
141
700bf7af
UD
142 if (pthread_condattr_getpshared (&ca, &p) != 0)
143 {
144 puts ("1st condattr_getpshared failed");
b910f788 145 return 1;
700bf7af
UD
146 }
147
148 if (p != PTHREAD_PROCESS_PRIVATE)
149 {
150 puts ("default value for pshared in condattr wrong");
b910f788 151 return 1;
700bf7af
UD
152 }
153
76a50749
UD
154 if (pthread_condattr_setpshared (&ca, PTHREAD_PROCESS_SHARED) != 0)
155 {
156 puts ("condattr_setpshared failed");
b910f788 157 return 1;
76a50749
UD
158 }
159
700bf7af
UD
160 if (pthread_condattr_getpshared (&ca, &p) != 0)
161 {
162 puts ("2nd condattr_getpshared failed");
b910f788 163 return 1;
700bf7af
UD
164 }
165
166 if (p != PTHREAD_PROCESS_SHARED)
167 {
168 puts ("pshared condattr still not set");
b910f788 169 return 1;
700bf7af
UD
170 }
171
76a50749
UD
172 if (pthread_cond_init (cond, &ca) != 0)
173 {
174 puts ("cond_init failed");
b910f788 175 return 1;
76a50749
UD
176 }
177
178 if (pthread_mutex_lock (mut1) != 0)
179 {
180 puts ("parent: 1st mutex_lock failed");
b910f788 181 return 1;
76a50749
UD
182 }
183
184 puts ("going to fork now");
185 pid = fork ();
186 if (pid == -1)
187 {
188 puts ("fork failed");
b910f788 189 return 1;
76a50749
UD
190 }
191 else if (pid == 0)
192 {
193 if (pthread_mutex_lock (mut2) != 0)
194 {
195 puts ("child: mutex_lock failed");
b910f788 196 return 1;
76a50749
UD
197 }
198
199 if (pthread_mutex_unlock (mut1) != 0)
200 {
201 puts ("child: 1st mutex_unlock failed");
b910f788 202 return 1;
76a50749
UD
203 }
204
205 do
206 if (pthread_cond_wait (cond, mut2) != 0)
207 {
208 puts ("child: cond_wait failed");
b910f788 209 return 1;
76a50749
UD
210 }
211 while (*condition == 0);
212
213 if (pthread_mutex_unlock (mut2) != 0)
214 {
215 puts ("child: 2nd mutex_unlock failed");
b910f788 216 return 1;
76a50749
UD
217 }
218
219 puts ("child done");
220 }
221 else
222 {
223 int status;
224
225 if (pthread_mutex_lock (mut1) != 0)
226 {
227 puts ("parent: 2nd mutex_lock failed");
b910f788 228 return 1;
76a50749
UD
229 }
230
231 if (pthread_mutex_lock (mut2) != 0)
232 {
233 puts ("parent: 3rd mutex_lock failed");
b910f788 234 return 1;
76a50749
UD
235 }
236
237 if (pthread_cond_signal (cond) != 0)
238 {
239 puts ("parent: cond_signal failed");
b910f788 240 return 1;
76a50749
UD
241 }
242
243 *condition = 1;
244
245 if (pthread_mutex_unlock (mut2) != 0)
246 {
247 puts ("parent: mutex_unlock failed");
b910f788 248 return 1;
76a50749
UD
249 }
250
251 puts ("waiting for child");
252
253 waitpid (pid, &status, 0);
254 result |= status;
255
256 puts ("parent done");
257 }
258
259 return result;
260}
b910f788
RM
261
262#define TEST_FUNCTION do_test ()
263#include "../test-skeleton.c"