1/* Copyright (C) 2002-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#include <errno.h>
19#include <pthread.h>
20#include <stdint.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>
29
30
31int *condition;
32
33static int
34do_test (void)
35{
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;
48
49 fd = mkstemp (template: tmpfname);
50 if (fd == -1)
51 {
52 printf (format: "cannot open temporary file: %m\n");
53 exit (1);
54 }
55
56 /* Make sure it is always removed. */
57 unlink (name: tmpfname);
58
59 /* Create one page of data. */
60 memset (data, '\0', ps);
61
62 /* Write the data to the file. */
63 if (write (fd, data, ps) != (ssize_t) ps)
64 {
65 puts (s: "short write");
66 exit (1);
67 }
68
69 mem = mmap (NULL, len: ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd: fd, offset: 0);
70 if (mem == MAP_FAILED)
71 {
72 printf (format: "mmap failed: %m\n");
73 exit (1);
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 (attr: &ma) != 0)
89 {
90 puts (s: "mutexattr_init failed");
91 exit (1);
92 }
93
94 if (pthread_mutexattr_setpshared (attr: &ma, PTHREAD_PROCESS_SHARED) != 0)
95 {
96 puts (s: "mutexattr_setpshared failed");
97 exit (1);
98 }
99
100 if (pthread_mutex_init (mutex: mut1, mutexattr: &ma) != 0)
101 {
102 puts (s: "1st mutex_init failed");
103 exit (1);
104 }
105
106 if (pthread_mutex_init (mutex: mut2, mutexattr: &ma) != 0)
107 {
108 puts (s: "2nd mutex_init failed");
109 exit (1);
110 }
111
112 if (pthread_condattr_init (attr: &ca) != 0)
113 {
114 puts (s: "condattr_init failed");
115 exit (1);
116 }
117
118 if (pthread_condattr_setpshared (attr: &ca, PTHREAD_PROCESS_SHARED) != 0)
119 {
120 puts (s: "condattr_setpshared failed");
121 exit (1);
122 }
123
124 if (pthread_cond_init (cond: cond, cond_attr: &ca) != 0)
125 {
126 puts (s: "cond_init failed");
127 exit (1);
128 }
129
130 if (pthread_mutex_lock (mutex: mut1) != 0)
131 {
132 puts (s: "parent: 1st mutex_lock failed");
133 exit (1);
134 }
135
136 puts (s: "going to fork now");
137 pid = fork ();
138 if (pid == -1)
139 {
140 puts (s: "fork failed");
141 exit (1);
142 }
143 else if (pid == 0)
144 {
145 struct timespec ts;
146 struct timeval tv;
147
148 if (pthread_mutex_lock (mutex: mut2) != 0)
149 {
150 puts (s: "child: mutex_lock failed");
151 exit (1);
152 }
153
154 if (pthread_mutex_unlock (mutex: mut1) != 0)
155 {
156 puts (s: "child: 1st mutex_unlock failed");
157 exit (1);
158 }
159
160 if (gettimeofday (tv: &tv, NULL) != 0)
161 {
162 puts (s: "gettimeofday failed");
163 exit (1);
164 }
165
166 TIMEVAL_TO_TIMESPEC (&tv, &ts);
167 ts.tv_nsec += 500000000;
168 if (ts.tv_nsec >= 1000000000)
169 {
170 ts.tv_nsec -= 1000000000;
171 ++ts.tv_sec;
172 }
173
174 do
175 if (pthread_cond_timedwait (cond: cond, mutex: mut2, abstime: &ts) != 0)
176 {
177 puts (s: "child: cond_wait failed");
178 exit (1);
179 }
180 while (*condition == 0);
181
182 if (pthread_mutex_unlock (mutex: mut2) != 0)
183 {
184 puts (s: "child: 2nd mutex_unlock failed");
185 exit (1);
186 }
187
188 puts (s: "child done");
189 }
190 else
191 {
192 int status;
193
194 if (pthread_mutex_lock (mutex: mut1) != 0)
195 {
196 puts (s: "parent: 2nd mutex_lock failed");
197 exit (1);
198 }
199
200 if (pthread_mutex_lock (mutex: mut2) != 0)
201 {
202 puts (s: "parent: 3rd mutex_lock failed");
203 exit (1);
204 }
205
206 if (pthread_cond_signal (cond: cond) != 0)
207 {
208 puts (s: "parent: cond_signal failed");
209 exit (1);
210 }
211
212 *condition = 1;
213
214 if (pthread_mutex_unlock (mutex: mut2) != 0)
215 {
216 puts (s: "parent: mutex_unlock failed");
217 exit (1);
218 }
219
220 puts (s: "waiting for child");
221
222 waitpid (pid: pid, stat_loc: &status, options: 0);
223 result |= status;
224
225 puts (s: "parent done");
226 }
227
228 return result;
229}
230
231#define TEST_FUNCTION do_test ()
232#include "../test-skeleton.c"
233

source code of glibc/sysdeps/pthread/tst-cond6.c