Debian Codegraph @

gnulib-tests/test-pthread-cond.c

raw
1 /* Test of condition variables in multithreaded situations.
2 Copyright (C) 2008-2025 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program 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
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #if USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS
20
21 /* Which tests to perform.
22 Uncomment some of these, to verify that all tests crash if no locking
23 is enabled. */
24 #define DO_TEST_COND 1
25 #define DO_TEST_TIMEDCOND 1
26
27 /* Whether to help the scheduler through explicit sched_yield().
28 Uncomment this to see if the operating system has a fair scheduler. */
29 #define EXPLICIT_YIELD 1
30
31 /* Whether to print debugging messages. */
32 #define ENABLE_DEBUGGING 0
33
34 #include <pthread.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/time.h>
39
40 #if EXPLICIT_YIELD
41 # include <sched.h>
42 #endif
43
44 #if HAVE_DECL_ALARM
45 # include <signal.h>
46 # include <unistd.h>
47 #endif
48
49 #include "virtualbox.h"
50 #include "macros.h"
51
52 #if ENABLE_DEBUGGING
53 # define dbgprintf printf
54 #else
55 # define dbgprintf if (0) printf
56 #endif
57
58 #if EXPLICIT_YIELD
59 # define yield() sched_yield ()
60 #else
61 # define yield()
62 #endif
63
64
65 /*
66 * Condition check
67 */
68
69 /* Marked volatile so that different threads see the same value. This is
70 good enough in practice, although in theory stdatomic.h should be used. */
71 static int volatile cond_value;
72 static pthread_cond_t condtest;
73 static pthread_mutex_t lockcond;
74
75 static void *
76 pthread_cond_wait_routine (void *arg)
77 {
78 ASSERT (pthread_mutex_lock (&lockcond) == 0);
79 if (cond_value)
80 {
81 /* The main thread already slept, and nevertheless this thread comes
82 too late. */
83 *(int *)arg = 1;
84 }
85 else
86 {
87 do
88 {
89 ASSERT (pthread_cond_wait (&condtest, &lockcond) == 0);
90 }
91 while (!cond_value);
92 }
93 ASSERT (pthread_mutex_unlock (&lockcond) == 0);
94
95 cond_value = 2;
96
97 return NULL;
98 }
99
100 static int
101 test_pthread_cond_wait ()
102 {
103 int skipped = 0;
104 pthread_t thread;
105 int ret;
106
107 cond_value = 0;
108
109 /* Create a separate thread. */
110 ASSERT (pthread_create (&thread, NULL, pthread_cond_wait_routine, &skipped)
111 == 0);
112
113 /* Sleep for 2 seconds. */
114 {
115 struct timespec remaining;
116
117 remaining.tv_sec = 2;
118 remaining.tv_nsec = 0;
119
120 do
121 {
122 yield ();
123 ret = nanosleep (&remaining, &remaining);
124 ASSERT (ret >= -1);
125 }
126 while (ret == -1 && (remaining.tv_sec != 0 || remaining.tv_nsec != 0));
127 }
128
129 /* Tell one of the waiting threads (if any) to continue. */
130 ASSERT (pthread_mutex_lock (&lockcond) == 0);
131 cond_value = 1;
132 ASSERT (pthread_cond_signal (&condtest) == 0);
133 ASSERT (pthread_mutex_unlock (&lockcond) == 0);
134
135 ASSERT (pthread_join (thread, NULL) == 0);
136
137 if (cond_value != 2)
138 abort ();
139
140 return skipped;
141 }
142
143
144 /*
145 * Timed Condition check
146 */
147
148 /* Marked volatile so that different threads see the same value. This is
149 good enough in practice, although in theory stdatomic.h should be used. */
150 static int volatile cond_timed_out;
151
152 /* Stores in *TS the current time plus 1 second. */
153 static void
154 get_ts (struct timespec *ts)
155 {
156 struct timeval now;
157
158 gettimeofday (&now, NULL);
159
160 ts->tv_sec = now.tv_sec + 1;
161 ts->tv_nsec = now.tv_usec * 1000;
162 }
163
164 static void *
165 pthread_cond_timedwait_routine (void *arg)
166 {
167 int ret;
168 struct timespec ts;
169
170 ASSERT (pthread_mutex_lock (&lockcond) == 0);
171 if (cond_value)
172 {
173 /* The main thread already slept, and nevertheless this thread comes
174 too late. */
175 *(int *)arg = 1;
176 }
177 else
178 {
179 do
180 {
181 get_ts (&ts);
182 ret = pthread_cond_timedwait (&condtest, &lockcond, &ts);
183 if (ret == ETIMEDOUT)
184 cond_timed_out = 1;
185 }
186 while (!cond_value);
187 }
188 ASSERT (pthread_mutex_unlock (&lockcond) == 0);
189
190 return NULL;
191 }
192
193 static int
194 test_pthread_cond_timedwait (void)
195 {
196 int skipped = 0;
197 pthread_t thread;
198 int ret;
199
200 cond_value = cond_timed_out = 0;
201
202 /* Create a separate thread. */
203 ASSERT (pthread_create (&thread, NULL,
204 pthread_cond_timedwait_routine, &skipped)
205 == 0);
206
207 /* Sleep for 2 seconds. */
208 {
209 struct timespec remaining;
210
211 remaining.tv_sec = 2;
212 remaining.tv_nsec = 0;
213
214 do
215 {
216 yield ();
217 ret = nanosleep (&remaining, &remaining);
218 ASSERT (ret >= -1);
219 }
220 while (ret == -1 && (remaining.tv_sec != 0 || remaining.tv_nsec != 0));
221 }
222
223 /* Tell one of the waiting threads (if any) to continue. */
224 ASSERT (pthread_mutex_lock (&lockcond) == 0);
225 cond_value = 1;
226 ASSERT (pthread_cond_signal (&condtest) == 0);
227 ASSERT (pthread_mutex_unlock (&lockcond) == 0);
228
229 ASSERT (pthread_join (thread, NULL) == 0);
230
231 if (!cond_timed_out)
232 abort ();
233
234 return skipped;
235 }
236
237
238 int
239 main ()
240 {
241 /* This test occasionally fails on Linux (glibc or musl libc), in a
242 VirtualBox VM with paravirtualization = Default or KVM, with ≥ 2 CPUs.
243 Skip the test in this situation. */
244 if (is_running_under_virtualbox_kvm () && num_cpus () > 1)
245 {
246 fputs ("Skipping test: avoiding VirtualBox bug with KVM paravirtualization\n",
247 stderr);
248 return 77;
249 }
250
251 #if HAVE_DECL_ALARM
252 /* Declare failure if test takes too long, by using default abort
253 caused by SIGALRM. */
254 int alarm_value = 600;
255 signal (SIGALRM, SIG_DFL);
256 alarm (alarm_value);
257 #endif
258
259 ASSERT (pthread_cond_init (&condtest, NULL) == 0);
260
261 {
262 pthread_mutexattr_t attr;
263
264 ASSERT (pthread_mutexattr_init (&attr) == 0);
265 ASSERT (pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_NORMAL) == 0);
266 ASSERT (pthread_mutex_init (&lockcond, &attr) == 0);
267 ASSERT (pthread_mutexattr_destroy (&attr) == 0);
268 }
269
270 #if DO_TEST_COND
271 printf ("Starting test_pthread_cond_wait ..."); fflush (stdout);
272 {
273 int skipped = test_pthread_cond_wait ();
274 printf (skipped ? " SKIP\n" : " OK\n"); fflush (stdout);
275 }
276 #endif
277 #if DO_TEST_TIMEDCOND
278 printf ("Starting test_pthread_cond_timedwait ..."); fflush (stdout);
279 {
280 int skipped = test_pthread_cond_timedwait ();
281 printf (skipped ? " SKIP\n" : " OK\n"); fflush (stdout);
282 }
283 #endif
284
285 return test_exit_status;
286 }
287
288 #else
289
290 /* No multithreading available. */
291
292 #include <stdio.h>
293
294 int
295 main ()
296 {
297 fputs ("Skipping test: multithreading not enabled\n", stderr);
298 return 77;
299 }
300
301 #endif