| 1 |
# pthread_rwlock_rdlock.m4 |
| 2 |
# serial 8 |
| 3 |
dnl Copyright (C) 2017-2024 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 |
dnl From Bruno Haible. |
| 10 |
dnl Inspired by |
| 11 |
dnl https://github.com/linux-test-project/ltp/blob/master/testcases/open_posix_testsuite/conformance/interfaces/pthread_rwlock_rdlock/2-2.c |
| 12 |
dnl by Intel Corporation. |
| 13 |
|
| 14 |
dnl Test whether in a situation where |
| 15 |
dnl - an rwlock is taken by a reader and has a writer waiting, |
| 16 |
dnl - an additional reader requests the lock, |
| 17 |
dnl - the waiting writer and the requesting reader threads have the same |
| 18 |
dnl priority, |
| 19 |
dnl the requesting reader thread gets blocked, so that at some point the |
| 20 |
dnl waiting writer can acquire the lock. |
| 21 |
dnl Without such a guarantee, when there a N readers and each of the readers |
| 22 |
dnl spends more than 1/Nth of the time with the lock held, there is a high |
| 23 |
dnl probability that the waiting writer will not get the lock in a given finite |
| 24 |
dnl time, a phenomenon called "writer starvation". |
| 25 |
dnl Without such a guarantee, applications have a hard time avoiding writer |
| 26 |
dnl starvation. |
| 27 |
dnl |
| 28 |
dnl POSIX:2017 makes this requirement only for implementations that support TPS |
| 29 |
dnl (Thread Priority Scheduling) and only for the scheduling policies SCHED_FIFO |
| 30 |
dnl and SCHED_RR, see |
| 31 |
dnl https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_rdlock.html |
| 32 |
dnl but this test verifies the guarantee regardless of TPS and regardless of |
| 33 |
dnl scheduling policy. |
| 34 |
dnl Glibc does not provide this guarantee (and never will on Linux), see |
| 35 |
dnl https://sourceware.org/bugzilla/show_bug.cgi?id=13701 |
| 36 |
dnl https://bugzilla.redhat.com/show_bug.cgi?id=1410052 |
| 37 |
AC_DEFUN([gl_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER], |
| 38 |
[ |
| 39 |
AC_REQUIRE([gl_THREADLIB]) |
| 40 |
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 41 |
AC_CACHE_CHECK([whether pthread_rwlock_rdlock prefers a writer to a reader], |
| 42 |
[gl_cv_pthread_rwlock_rdlock_prefer_writer], |
| 43 |
[saved_LIBS="$LIBS" |
| 44 |
LIBS="$LIBS $LIBMULTITHREAD" |
| 45 |
AC_RUN_IFELSE( |
| 46 |
[AC_LANG_SOURCE([[ |
| 47 |
#include <errno.h> |
| 48 |
#include <pthread.h> |
| 49 |
#include <stdlib.h> |
| 50 |
#include <unistd.h> |
| 51 |
|
| 52 |
#define SUCCEED() exit (0) |
| 53 |
#define FAILURE() exit (1) |
| 54 |
#define UNEXPECTED(n) (exit (10 + (n))) |
| 55 |
|
| 56 |
/* The main thread creates the waiting writer and the requesting reader threads |
| 57 |
in the default way; this guarantees that they have the same priority. |
| 58 |
We can reuse the main thread as first reader thread. */ |
| 59 |
|
| 60 |
static pthread_rwlock_t lock; |
| 61 |
static pthread_t reader1; |
| 62 |
static pthread_t writer; |
| 63 |
static pthread_t reader2; |
| 64 |
static pthread_t timer; |
| 65 |
/* Used to pass control from writer to reader2 and from reader2 to timer, |
| 66 |
as in a relay race. |
| 67 |
Passing control from one running thread to another running thread |
| 68 |
is most likely faster than to create the second thread. */ |
| 69 |
static pthread_mutex_t baton; |
| 70 |
|
| 71 |
static void * |
| 72 |
timer_func (void *ignored) |
| 73 |
{ |
| 74 |
/* Step 13 (can be before or after step 12): |
| 75 |
The timer thread takes the baton, then waits a moment to make sure |
| 76 |
it can tell whether the second reader thread is blocked at step 12. */ |
| 77 |
if (pthread_mutex_lock (&baton)) |
| 78 |
UNEXPECTED (13); |
| 79 |
usleep (100000); |
| 80 |
/* By the time we get here, it's clear that the second reader thread is |
| 81 |
blocked at step 12. This is the desired behaviour. */ |
| 82 |
SUCCEED (); |
| 83 |
} |
| 84 |
|
| 85 |
static void * |
| 86 |
reader2_func (void *ignored) |
| 87 |
{ |
| 88 |
int err; |
| 89 |
|
| 90 |
/* Step 8 (can be before or after step 7): |
| 91 |
The second reader thread takes the baton, then waits a moment to make sure |
| 92 |
the writer thread has reached step 7. */ |
| 93 |
if (pthread_mutex_lock (&baton)) |
| 94 |
UNEXPECTED (8); |
| 95 |
usleep (100000); |
| 96 |
/* Step 9: The second reader thread requests the lock. */ |
| 97 |
err = pthread_rwlock_tryrdlock (&lock); |
| 98 |
if (err == 0) |
| 99 |
FAILURE (); |
| 100 |
else if (err != EBUSY) |
| 101 |
UNEXPECTED (9); |
| 102 |
/* Step 10: Launch a timer, to test whether the next call blocks. */ |
| 103 |
if (pthread_create (&timer, NULL, timer_func, NULL)) |
| 104 |
UNEXPECTED (10); |
| 105 |
/* Step 11: Release the baton. */ |
| 106 |
if (pthread_mutex_unlock (&baton)) |
| 107 |
UNEXPECTED (11); |
| 108 |
/* Step 12: The second reader thread requests the lock. */ |
| 109 |
err = pthread_rwlock_rdlock (&lock); |
| 110 |
if (err == 0) |
| 111 |
FAILURE (); |
| 112 |
else |
| 113 |
UNEXPECTED (12); |
| 114 |
} |
| 115 |
|
| 116 |
static void * |
| 117 |
writer_func (void *ignored) |
| 118 |
{ |
| 119 |
/* Step 4: Take the baton, so that the second reader thread does not go ahead |
| 120 |
too early. */ |
| 121 |
if (pthread_mutex_lock (&baton)) |
| 122 |
UNEXPECTED (4); |
| 123 |
/* Step 5: Create the second reader thread. */ |
| 124 |
if (pthread_create (&reader2, NULL, reader2_func, NULL)) |
| 125 |
UNEXPECTED (5); |
| 126 |
/* Step 6: Release the baton. */ |
| 127 |
if (pthread_mutex_unlock (&baton)) |
| 128 |
UNEXPECTED (6); |
| 129 |
/* Step 7: The writer thread requests the lock. */ |
| 130 |
if (pthread_rwlock_wrlock (&lock)) |
| 131 |
UNEXPECTED (7); |
| 132 |
return NULL; |
| 133 |
} |
| 134 |
|
| 135 |
int |
| 136 |
main () |
| 137 |
{ |
| 138 |
reader1 = pthread_self (); |
| 139 |
|
| 140 |
/* Step 1: The main thread initializes the lock and the baton. */ |
| 141 |
if (pthread_rwlock_init (&lock, NULL)) |
| 142 |
UNEXPECTED (1); |
| 143 |
if (pthread_mutex_init (&baton, NULL)) |
| 144 |
UNEXPECTED (1); |
| 145 |
/* Step 2: The main thread acquires the lock as a reader. */ |
| 146 |
if (pthread_rwlock_rdlock (&lock)) |
| 147 |
UNEXPECTED (2); |
| 148 |
/* Step 3: Create the writer thread. */ |
| 149 |
if (pthread_create (&writer, NULL, writer_func, NULL)) |
| 150 |
UNEXPECTED (3); |
| 151 |
/* Job done. Go to sleep. */ |
| 152 |
for (;;) |
| 153 |
{ |
| 154 |
sleep (1); |
| 155 |
} |
| 156 |
} |
| 157 |
]])], |
| 158 |
[gl_cv_pthread_rwlock_rdlock_prefer_writer=yes], |
| 159 |
[gl_cv_pthread_rwlock_rdlock_prefer_writer=no], |
| 160 |
[case "$host_os" in |
| 161 |
# Guess no on glibc systems. |
| 162 |
*-gnu* | gnu*) gl_cv_pthread_rwlock_rdlock_prefer_writer="guessing no" ;; |
| 163 |
# Guess no on musl systems. |
| 164 |
*-musl* | midipix*) gl_cv_pthread_rwlock_rdlock_prefer_writer="guessing no" ;; |
| 165 |
# Guess no on bionic systems. |
| 166 |
*-android*) gl_cv_pthread_rwlock_rdlock_prefer_writer="guessing no" ;; |
| 167 |
# Guess yes on native Windows with the mingw-w64 winpthreads library. |
| 168 |
# Guess no on native Windows with the gnulib windows-rwlock module. |
| 169 |
mingw* | windows*) if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then |
| 170 |
gl_cv_pthread_rwlock_rdlock_prefer_writer="guessing yes" |
| 171 |
else |
| 172 |
gl_cv_pthread_rwlock_rdlock_prefer_writer="guessing no" |
| 173 |
fi |
| 174 |
;; |
| 175 |
# If we don't know, obey --enable-cross-guesses. |
| 176 |
*) gl_cv_pthread_rwlock_rdlock_prefer_writer="$gl_cross_guess_normal" ;; |
| 177 |
esac |
| 178 |
]) |
| 179 |
LIBS="$saved_LIBS" |
| 180 |
]) |
| 181 |
case "$gl_cv_pthread_rwlock_rdlock_prefer_writer" in |
| 182 |
*yes) |
| 183 |
AC_DEFINE([HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER], [1], |
| 184 |
[Define if the 'pthread_rwlock_rdlock' function prefers a writer to a reader.]) |
| 185 |
;; |
| 186 |
esac |
| 187 |
]) |