1 # pthread_rwlock_rdlock.m4 serial 4
2 dnl Copyright (C) 2017-2022 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
9 dnl https://github.com/linux-test-project/ltp/blob/master/testcases/open_posix_testsuite/conformance/interfaces/pthread_rwlock_rdlock/2-2.c
10 dnl by Intel Corporation.
12 dnl Test whether in a situation where
13 dnl - an rwlock is taken by a reader and has a writer waiting,
14 dnl - an additional reader requests the lock,
15 dnl - the waiting writer and the requesting reader threads have the same
17 dnl the requesting reader thread gets blocked, so that at some point the
18 dnl waiting writer can acquire the lock.
19 dnl Without such a guarantee, when there a N readers and each of the readers
20 dnl spends more than 1/Nth of the time with the lock held, there is a high
21 dnl probability that the waiting writer will not get the lock in a given finite
22 dnl time, a phenomenon called "writer starvation".
23 dnl Without such a guarantee, applications have a hard time avoiding writer
26 dnl POSIX:2017 makes this requirement only for implementations that support TPS
27 dnl (Thread Priority Scheduling) and only for the scheduling policies SCHED_FIFO
29 dnl https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_rdlock.html
30 dnl but this test verifies the guarantee regardless of TPS and regardless of
31 dnl scheduling policy.
32 dnl Glibc does not provide this guarantee (and never will on Linux), see
33 dnl https://sourceware.org/bugzilla/show_bug.cgi?id=13701
34 dnl https://bugzilla.redhat.com/show_bug.cgi?id=1410052
35 AC_DEFUN([gl_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER],
37 AC_REQUIRE([gl_THREADLIB_EARLY])
38 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
39 AC_CACHE_CHECK([whether pthread_rwlock_rdlock prefers a writer to a reader],
40 [gl_cv_pthread_rwlock_rdlock_prefer_writer],
42 LIBS="$LIBS $LIBMULTITHREAD"
50 #define SUCCEED() exit (0)
51 #define FAILURE() exit (1)
52 #define UNEXPECTED(n) (exit (10 + (n)))
54 /* The main thread creates the waiting writer and the requesting reader threads
55 in the default way; this guarantees that they have the same priority.
56 We can reuse the main thread as first reader thread. */
58 static pthread_rwlock_t lock;
59 static pthread_t reader1;
60 static pthread_t writer;
61 static pthread_t reader2;
62 static pthread_t timer;
63 /* Used to pass control from writer to reader2 and from reader2 to timer,
65 Passing control from one running thread to another running thread
66 is most likely faster than to create the second thread. */
67 static pthread_mutex_t baton;
70 timer_func (void *ignored)
72 /* Step 13 (can be before or after step 12):
73 The timer thread takes the baton, then waits a moment to make sure
74 it can tell whether the second reader thread is blocked at step 12. */
75 if (pthread_mutex_lock (&baton))
78 /* By the time we get here, it's clear that the second reader thread is
79 blocked at step 12. This is the desired behaviour. */
84 reader2_func (void *ignored)
88 /* Step 8 (can be before or after step 7):
89 The second reader thread takes the baton, then waits a moment to make sure
90 the writer thread has reached step 7. */
91 if (pthread_mutex_lock (&baton))
94 /* Step 9: The second reader thread requests the lock. */
95 err = pthread_rwlock_tryrdlock (&lock);
98 else if (err != EBUSY)
100 /* Step 10: Launch a timer, to test whether the next call blocks. */
101 if (pthread_create (&timer, NULL, timer_func, NULL))
103 /* Step 11: Release the baton. */
104 if (pthread_mutex_unlock (&baton))
106 /* Step 12: The second reader thread requests the lock. */
107 err = pthread_rwlock_rdlock (&lock);
115 writer_func (void *ignored)
117 /* Step 4: Take the baton, so that the second reader thread does not go ahead
119 if (pthread_mutex_lock (&baton))
121 /* Step 5: Create the second reader thread. */
122 if (pthread_create (&reader2, NULL, reader2_func, NULL))
124 /* Step 6: Release the baton. */
125 if (pthread_mutex_unlock (&baton))
127 /* Step 7: The writer thread requests the lock. */
128 if (pthread_rwlock_wrlock (&lock))
136 reader1 = pthread_self ();
138 /* Step 1: The main thread initializes the lock and the baton. */
139 if (pthread_rwlock_init (&lock, NULL))
141 if (pthread_mutex_init (&baton, NULL))
143 /* Step 2: The main thread acquires the lock as a reader. */
144 if (pthread_rwlock_rdlock (&lock))
146 /* Step 3: Create the writer thread. */
147 if (pthread_create (&writer, NULL, writer_func, NULL))
149 /* Job done. Go to sleep. */
156 [gl_cv_pthread_rwlock_rdlock_prefer_writer=yes],
157 [gl_cv_pthread_rwlock_rdlock_prefer_writer=no],
159 # Guess no on glibc systems.
160 *-gnu* | gnu*) gl_cv_pthread_rwlock_rdlock_prefer_writer="guessing no" ;;
161 # Guess no on musl systems.
162 *-musl*) gl_cv_pthread_rwlock_rdlock_prefer_writer="guessing no" ;;
163 # Guess no on bionic systems.
164 *-android*) gl_cv_pthread_rwlock_rdlock_prefer_writer="guessing no" ;;
165 # Guess yes on native Windows with the mingw-w64 winpthreads library.
166 # Guess no on native Windows with the gnulib windows-rwlock module.
167 mingw*) if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
168 gl_cv_pthread_rwlock_rdlock_prefer_writer="guessing yes"
170 gl_cv_pthread_rwlock_rdlock_prefer_writer="guessing no"
173 # If we don't know, obey --enable-cross-guesses.
174 *) gl_cv_pthread_rwlock_rdlock_prefer_writer="$gl_cross_guess_normal" ;;
179 case "$gl_cv_pthread_rwlock_rdlock_prefer_writer" in
181 AC_DEFINE([HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER], [1],
182 [Define if the 'pthread_rwlock_rdlock' function prefers a writer to a reader.])