Debian Codegraph @

m4/pthread-rwlock.m4

raw
1 # pthread-rwlock.m4
2 # serial 8
3 dnl Copyright (C) 2019-2025 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 dnl This file is offered as-is, without any warranty.
8
9 AC_DEFUN([gl_PTHREAD_RWLOCK],
10 [
11 AC_REQUIRE([gl_PTHREAD_H])
12 AC_REQUIRE([AC_CANONICAL_HOST])
13
14 if { case "$host_os" in mingw* | windows*) true;; *) false;; esac; } \
15 && test $gl_threads_api = windows; then
16 dnl Choose function names that don't conflict with the mingw-w64 winpthreads
17 dnl library.
18 REPLACE_PTHREAD_RWLOCK_INIT=1
19 REPLACE_PTHREAD_RWLOCKATTR_INIT=1
20 REPLACE_PTHREAD_RWLOCKATTR_DESTROY=1
21 REPLACE_PTHREAD_RWLOCK_RDLOCK=1
22 REPLACE_PTHREAD_RWLOCK_WRLOCK=1
23 REPLACE_PTHREAD_RWLOCK_TRYRDLOCK=1
24 REPLACE_PTHREAD_RWLOCK_TRYWRLOCK=1
25 REPLACE_PTHREAD_RWLOCK_TIMEDRDLOCK=1
26 REPLACE_PTHREAD_RWLOCK_TIMEDWRLOCK=1
27 REPLACE_PTHREAD_RWLOCK_UNLOCK=1
28 REPLACE_PTHREAD_RWLOCK_DESTROY=1
29 else
30 if test $HAVE_PTHREAD_H = 0; then
31 HAVE_PTHREAD_RWLOCK_INIT=0
32 HAVE_PTHREAD_RWLOCKATTR_INIT=0
33 HAVE_PTHREAD_RWLOCKATTR_DESTROY=0
34 HAVE_PTHREAD_RWLOCK_RDLOCK=0
35 HAVE_PTHREAD_RWLOCK_WRLOCK=0
36 HAVE_PTHREAD_RWLOCK_TRYRDLOCK=0
37 HAVE_PTHREAD_RWLOCK_TRYWRLOCK=0
38 HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK=0
39 HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK=0
40 HAVE_PTHREAD_RWLOCK_UNLOCK=0
41 HAVE_PTHREAD_RWLOCK_DESTROY=0
42 else
43 dnl On Mac OS X 10.4, the pthread_rwlock_* functions exist but are not
44 dnl usable because PTHREAD_RWLOCK_INITIALIZER is not defined.
45 dnl On Android 4.3, the pthread_rwlock_* functions are declared in
46 dnl <pthread.h> but don't exist in libc.
47 AC_CACHE_CHECK([for pthread_rwlock_init],
48 [gl_cv_func_pthread_rwlock_init],
49 [case "$host_os" in
50 darwin*)
51 AC_COMPILE_IFELSE(
52 [AC_LANG_SOURCE(
53 [[#include <pthread.h>
54 pthread_rwlock_t l = PTHREAD_RWLOCK_INITIALIZER;
55 ]])],
56 [gl_cv_func_pthread_rwlock_init=yes],
57 [gl_cv_func_pthread_rwlock_init=no])
58 ;;
59 *)
60 saved_LIBS="$LIBS"
61 LIBS="$LIBS $LIBPMULTITHREAD"
62 AC_LINK_IFELSE(
63 [AC_LANG_SOURCE(
64 [[extern
65 #ifdef __cplusplus
66 "C"
67 #endif
68 int pthread_rwlock_init (void);
69 int main ()
70 {
71 return pthread_rwlock_init ();
72 }
73 ]])],
74 [gl_cv_func_pthread_rwlock_init=yes],
75 [gl_cv_func_pthread_rwlock_init=no])
76 LIBS="$saved_LIBS"
77 ;;
78 esac
79 ])
80 if test $gl_cv_func_pthread_rwlock_init = no; then
81 REPLACE_PTHREAD_RWLOCK_INIT=1
82 REPLACE_PTHREAD_RWLOCKATTR_INIT=1
83 REPLACE_PTHREAD_RWLOCKATTR_DESTROY=1
84 REPLACE_PTHREAD_RWLOCK_RDLOCK=1
85 REPLACE_PTHREAD_RWLOCK_WRLOCK=1
86 REPLACE_PTHREAD_RWLOCK_TRYRDLOCK=1
87 REPLACE_PTHREAD_RWLOCK_TRYWRLOCK=1
88 REPLACE_PTHREAD_RWLOCK_TIMEDRDLOCK=1
89 REPLACE_PTHREAD_RWLOCK_TIMEDWRLOCK=1
90 REPLACE_PTHREAD_RWLOCK_UNLOCK=1
91 REPLACE_PTHREAD_RWLOCK_DESTROY=1
92 AC_DEFINE([PTHREAD_RWLOCK_UNIMPLEMENTED], [1],
93 [Define if all pthread_rwlock* functions don't exist.])
94 else
95 dnl On Mac OS X 10.5, FreeBSD 5.2.1, OpenBSD 3.8, AIX 5.1, HP-UX 11,
96 dnl IRIX 6.5, Solaris 9, Cygwin, the pthread_rwlock_timed*lock functions
97 dnl don't exist, although the other pthread_rwlock* functions exist.
98 AC_CHECK_DECL([pthread_rwlock_timedrdlock], ,
99 [HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK=0
100 HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK=0
101 AC_DEFINE([PTHREAD_RWLOCK_LACKS_TIMEOUT], [1],
102 [Define if the functions pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock don't exist.])
103 ],
104 [[#include <pthread.h>]])
105 dnl In glibc ≥ 2.25 on Linux, test-pthread-rwlock-waitqueue reports
106 dnl "This implementation always prefers readers.", and this wait queue
107 dnl handling is unsuitable, because it leads to writer starvation:
108 dnl On machines with 8 or more CPUs, test-pthread-rwlock may never
109 dnl terminate. See
110 dnl <https://lists.gnu.org/archive/html/bug-gnulib/2024-06/msg00291.html>
111 dnl <https://lists.gnu.org/archive/html/bug-gnulib/2024-07/msg00081.html>
112 dnl for details.
113 AC_CACHE_CHECK([for reasonable pthread_rwlock wait queue handling],
114 [gl_cv_func_pthread_rwlock_good_waitqueue],
115 [case "$host_os" in
116 linux*-gnu*)
117 saved_LIBS="$LIBS"
118 LIBS="$LIBS $LIBPMULTITHREAD"
119 AC_RUN_IFELSE(
120 [AC_LANG_SOURCE([[
121 /* This test is a simplified variant of tests/test-pthread-rwlock-waitqueue.c. */
122 #include <pthread.h>
123 #include <stdbool.h>
124 #include <stdio.h>
125 #include <stdlib.h>
126 #include <string.h>
127 #include <time.h>
128
129 #if defined __hppa
130 # define STEP_INTERVAL 20000000 /* nanoseconds */
131 #else
132 # define STEP_INTERVAL 10000000 /* nanoseconds */
133 #endif
134
135 static pthread_rwlock_t lock;
136
137 static pthread_rwlock_t sprintf_lock;
138
139 struct locals
140 {
141 const char *name;
142 unsigned int wait_before;
143 unsigned int wait_after;
144 char *result;
145 };
146
147 static void *
148 reader_func (void *arg)
149 {
150 struct locals *l = arg;
151 int err;
152
153 if (l->wait_before > 0)
154 {
155 struct timespec duration;
156 duration.tv_sec = l->wait_before / 1000000000;
157 duration.tv_nsec = l->wait_before % 1000000000;
158 nanosleep (&duration, NULL);
159 }
160 err = pthread_rwlock_rdlock (&lock);
161 if (err)
162 {
163 fprintf (stderr, "pthread_rwlock_rdlock failed, error = %d\n", err);
164 abort ();
165 }
166 if (pthread_rwlock_wrlock (&sprintf_lock))
167 {
168 fprintf (stderr, "pthread_rwlock_wrlock on sprintf_lock failed\n");
169 abort ();
170 }
171 sprintf (l->result + strlen (l->result), " %s", l->name);
172 if (pthread_rwlock_unlock (&sprintf_lock))
173 {
174 fprintf (stderr, "pthread_rwlock_unlock on sprintf_lock failed\n");
175 abort ();
176 }
177 if (l->wait_after > 0)
178 {
179 struct timespec duration;
180 duration.tv_sec = l->wait_after / 1000000000;
181 duration.tv_nsec = l->wait_after % 1000000000;
182 nanosleep (&duration, NULL);
183 }
184 err = pthread_rwlock_unlock (&lock);
185 if (err)
186 {
187 fprintf (stderr, "pthread_rwlock_unlock failed, error = %d\n", err);
188 abort ();
189 }
190
191 return NULL;
192 }
193
194 static void *
195 writer_func (void *arg)
196 {
197 struct locals *l = arg;
198 int err;
199
200 if (l->wait_before > 0)
201 {
202 struct timespec duration;
203 duration.tv_sec = l->wait_before / 1000000000;
204 duration.tv_nsec = l->wait_before % 1000000000;
205 nanosleep (&duration, NULL);
206 }
207 err = pthread_rwlock_wrlock (&lock);
208 if (err)
209 {
210 fprintf (stderr, "pthread_rwlock_rdlock failed, error = %d\n", err);
211 abort ();
212 }
213 if (pthread_rwlock_wrlock (&sprintf_lock))
214 {
215 fprintf (stderr, "pthread_rwlock_wrlock on sprintf_lock failed\n");
216 abort ();
217 }
218 sprintf (l->result + strlen (l->result), " %s", l->name);
219 if (pthread_rwlock_unlock (&sprintf_lock))
220 {
221 fprintf (stderr, "pthread_rwlock_unlock on sprintf_lock failed\n");
222 abort ();
223 }
224 if (l->wait_after > 0)
225 {
226 struct timespec duration;
227 duration.tv_sec = l->wait_after / 1000000000;
228 duration.tv_nsec = l->wait_after % 1000000000;
229 nanosleep (&duration, NULL);
230 }
231 err = pthread_rwlock_unlock (&lock);
232 if (err)
233 {
234 fprintf (stderr, "pthread_rwlock_unlock failed, error = %d\n", err);
235 abort ();
236 }
237
238 return NULL;
239 }
240
241 static const char *
242 do_test (const char *rw_string)
243 {
244 size_t n = strlen (rw_string);
245 int err;
246 char resultbuf[100];
247
248 char **names = (char **) malloc (n * sizeof (char *));
249 for (size_t i = 0; i < n; i++)
250 {
251 char name[12];
252 sprintf (name, "%c%u", rw_string[i], (unsigned int) (i+1));
253 names[i] = strdup (name);
254 }
255
256 resultbuf[0] = '\0';
257
258 /* Create the threads. */
259 struct locals *locals = (struct locals *) malloc (n * sizeof (struct locals));
260 pthread_t *threads = (pthread_t *) malloc (n * sizeof (pthread_t));
261 for (size_t i = 0; i < n; i++)
262 {
263 locals[i].name = names[i];
264 locals[i].wait_before = i * STEP_INTERVAL;
265 locals[i].wait_after = (i == 0 ? n * STEP_INTERVAL : 0);
266 locals[i].result = resultbuf;
267 err = pthread_create (&threads[i], NULL,
268 rw_string[i] == 'R' ? reader_func :
269 rw_string[i] == 'W' ? writer_func :
270 (abort (), (void * (*) (void *)) NULL),
271 &locals[i]);
272 if (err)
273 {
274 fprintf (stderr, "pthread_create failed to create thread %u, error = %d\n",
275 (unsigned int) (i+1), err);
276 abort ();
277 }
278 }
279
280 /* Wait until the threads are done. */
281 for (size_t i = 0; i < n; i++)
282 {
283 void *retcode;
284 err = pthread_join (threads[i], &retcode);
285 if (err)
286 {
287 fprintf (stderr, "pthread_join failed to wait for thread %u, error = %d\n",
288 (unsigned int) (i+1), err);
289 abort ();
290 }
291 }
292
293 /* Clean up. */
294 free (threads);
295 free (locals);
296 for (size_t i = 0; i < n; i++)
297 free (names[i]);
298 free (names);
299
300 return strdup (resultbuf);
301 }
302
303 static bool
304 startswith (const char *str, const char *prefix)
305 {
306 return strncmp (str, prefix, strlen (prefix)) == 0;
307 }
308
309 static int
310 find_wait_queue_handling (void)
311 {
312 bool final_r_prefers_readers = true;
313 bool final_w_prefers_readers = true;
314
315 /* Perform the test a few times, so that in case of a non-deterministic
316 behaviour that happens to look like deterministic in one round, we get
317 a higher probability of finding that it is non-deterministic. */
318 for (int repeat = 3; repeat > 0; repeat--)
319 {
320 bool r_prefers_readers = false;
321 bool w_prefers_readers = false;
322
323 {
324 const char * RWR = do_test ("RWR");
325 const char * RWRR = do_test ("RWRR");
326 const char * RWRW = do_test ("RWRW");
327 const char * RWWR = do_test ("RWWR");
328 const char * RWRRR = do_test ("RWRRR");
329 const char * RWRRW = do_test ("RWRRW");
330 const char * RWRWR = do_test ("RWRWR");
331 const char * RWRWW = do_test ("RWRWW");
332 const char * RWWRR = do_test ("RWWRR");
333 const char * RWWRW = do_test ("RWWRW");
334 const char * RWWWR = do_test ("RWWWR");
335
336 if ( startswith (RWR, " R1 R")
337 && startswith (RWRR, " R1 R")
338 && startswith (RWRW, " R1 R")
339 && startswith (RWWR, " R1 R")
340 && startswith (RWRRR, " R1 R")
341 && startswith (RWRRW, " R1 R")
342 && startswith (RWRWR, " R1 R")
343 && startswith (RWRWW, " R1 R")
344 && startswith (RWWRR, " R1 R")
345 && startswith (RWWRW, " R1 R")
346 && startswith (RWWWR, " R1 R"))
347 r_prefers_readers = true;
348 }
349
350 {
351 const char * WRR = do_test ("WRR");
352 const char * WRW = do_test ("WRW");
353 const char * WWR = do_test ("WWR");
354 const char * WRRR = do_test ("WRRR");
355 const char * WRRW = do_test ("WRRW");
356 const char * WRWR = do_test ("WRWR");
357 const char * WRWW = do_test ("WRWW");
358 const char * WWRR = do_test ("WWRR");
359 const char * WWRW = do_test ("WWRW");
360 const char * WWWR = do_test ("WWWR");
361 const char * WRRRR = do_test ("WRRRR");
362 const char * WRRRW = do_test ("WRRRW");
363 const char * WRRWR = do_test ("WRRWR");
364 const char * WRRWW = do_test ("WRRWW");
365 const char * WRWRR = do_test ("WRWRR");
366 const char * WRWRW = do_test ("WRWRW");
367 const char * WRWWR = do_test ("WRWWR");
368 const char * WRWWW = do_test ("WRWWW");
369 const char * WWRRR = do_test ("WWRRR");
370 const char * WWRRW = do_test ("WWRRW");
371 const char * WWRWR = do_test ("WWRWR");
372 const char * WWRWW = do_test ("WWRWW");
373 const char * WWWRR = do_test ("WWWRR");
374 const char * WWWRW = do_test ("WWWRW");
375 const char * WWWWR = do_test ("WWWWR");
376
377 if ( startswith (WRR, " W1 R")
378 && startswith (WRW, " W1 R")
379 && startswith (WWR, " W1 R")
380 && startswith (WRRR, " W1 R")
381 && startswith (WRRW, " W1 R")
382 && startswith (WRWR, " W1 R")
383 && startswith (WRWW, " W1 R")
384 && startswith (WWRR, " W1 R")
385 && startswith (WWRW, " W1 R")
386 && startswith (WWWR, " W1 R")
387 && startswith (WRRRR, " W1 R")
388 && startswith (WRRRW, " W1 R")
389 && startswith (WRRWR, " W1 R")
390 && startswith (WRRWW, " W1 R")
391 && startswith (WRWRR, " W1 R")
392 && startswith (WRWRW, " W1 R")
393 && startswith (WRWWR, " W1 R")
394 && startswith (WRWWW, " W1 R")
395 && startswith (WWRRR, " W1 R")
396 && startswith (WWRRW, " W1 R")
397 && startswith (WWRWR, " W1 R")
398 && startswith (WWRWW, " W1 R")
399 && startswith (WWWRR, " W1 R")
400 && startswith (WWWRW, " W1 R")
401 && startswith (WWWWR, " W1 R"))
402 w_prefers_readers = true;
403 }
404
405 final_r_prefers_readers &= r_prefers_readers;
406 final_w_prefers_readers &= w_prefers_readers;
407 }
408
409 /* The wait queue handling is unsuitable if it always prefers readers,
410 because it leads to writer starvation: On machines with 8 or more CPUs,
411 test-pthread-rwlock may never terminate. */
412 return final_r_prefers_readers && final_w_prefers_readers;
413 }
414
415 int
416 main ()
417 {
418 /* Initialize the sprintf_lock. */
419 if (pthread_rwlock_init (&sprintf_lock, NULL))
420 {
421 fprintf (stderr, "pthread_rwlock_init failed\n");
422 abort ();
423 }
424
425 /* Find the wait queue handling of a default-initialized lock. */
426 if (pthread_rwlock_init (&lock, NULL))
427 {
428 fprintf (stderr, "pthread_rwlock_init failed\n");
429 abort ();
430 }
431 {
432 int fail = find_wait_queue_handling ();
433 return fail;
434 }
435 }
436 ]])
437 ],
438 [gl_cv_func_pthread_rwlock_good_waitqueue=yes],
439 [gl_cv_func_pthread_rwlock_good_waitqueue=no],
440 [dnl Guess no on glibc/Linux.
441 gl_cv_func_pthread_rwlock_good_waitqueue="guessing no"
442 ])
443 LIBS="$saved_LIBS"
444 ;;
445 *) dnl Guess yes on other platforms.
446 gl_cv_func_pthread_rwlock_good_waitqueue="guessing yes"
447 ;;
448 esac
449 ])
450 case "$gl_cv_func_pthread_rwlock_good_waitqueue" in
451 *yes) ;;
452 *no)
453 REPLACE_PTHREAD_RWLOCK_INIT=1
454 REPLACE_PTHREAD_RWLOCKATTR_INIT=1
455 AC_DEFINE([PTHREAD_RWLOCK_BAD_WAITQUEUE], [1],
456 [Define if the pthread_rwlock wait queue handling is not reasonable.])
457 ;;
458 esac
459 fi
460 fi
461 fi
462 ])