1 # pthread_rwlock_rdlock.m4 serial 2
2 dnl Copyright (C) 2017-2018 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 http://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 currently does not provide this guarantee, see
33 dnl https://sourceware.org/bugzilla/show_bug.cgi?id=13701
34 AC_DEFUN([gl_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER],
36 AC_REQUIRE([gl_THREADLIB_EARLY])
37 AC_CACHE_CHECK([whether pthread_rwlock_rdlock prefers a writer to a reader],
38 [gl_cv_pthread_rwlock_rdlock_prefer_writer],
40 LIBS="$LIBS $LIBMULTITHREAD"
48 #define SUCCEED() exit (0)
49 #define FAILURE() exit (1)
50 #define UNEXPECTED(n) (exit (10 + (n)))
52 /* The main thread creates the waiting writer and the requesting reader threads
53 in the default way; this guarantees that they have the same priority.
54 We can reuse the main thread as first reader thread. */
56 static pthread_rwlock_t lock;
57 static pthread_t reader1;
58 static pthread_t writer;
59 static pthread_t reader2;
60 static pthread_t timer;
61 /* Used to pass control from writer to reader2 and from reader2 to timer,
63 Passing control from one running thread to another running thread
64 is most likely faster than to create the second thread. */
65 static pthread_mutex_t baton;
68 timer_func (void *ignored)
70 /* Step 13 (can be before or after step 12):
71 The timer thread takes the baton, then waits a moment to make sure
72 it can tell whether the second reader thread is blocked at step 12. */
73 if (pthread_mutex_lock (&baton))
76 /* By the time we get here, it's clear that the second reader thread is
77 blocked at step 12. This is the desired behaviour. */
82 reader2_func (void *ignored)
86 /* Step 8 (can be before or after step 7):
87 The second reader thread takes the baton, then waits a moment to make sure
88 the writer thread has reached step 7. */
89 if (pthread_mutex_lock (&baton))
92 /* Step 9: The second reader thread requests the lock. */
93 err = pthread_rwlock_tryrdlock (&lock);
96 else if (err != EBUSY)
98 /* Step 10: Launch a timer, to test whether the next call blocks. */
99 if (pthread_create (&timer, NULL, timer_func, NULL))
101 /* Step 11: Release the baton. */
102 if (pthread_mutex_unlock (&baton))
104 /* Step 12: The second reader thread requests the lock. */
105 err = pthread_rwlock_rdlock (&lock);
113 writer_func (void *ignored)
115 /* Step 4: Take the baton, so that the second reader thread does not go ahead
117 if (pthread_mutex_lock (&baton))
119 /* Step 5: Create the second reader thread. */
120 if (pthread_create (&reader2, NULL, reader2_func, NULL))
122 /* Step 6: Release the baton. */
123 if (pthread_mutex_unlock (&baton))
125 /* Step 7: The writer thread requests the lock. */
126 if (pthread_rwlock_wrlock (&lock))
134 reader1 = pthread_self ();
136 /* Step 1: The main thread initializes the lock and the baton. */
137 if (pthread_rwlock_init (&lock, NULL))
139 if (pthread_mutex_init (&baton, NULL))
141 /* Step 2: The main thread acquires the lock as a reader. */
142 if (pthread_rwlock_rdlock (&lock))
144 /* Step 3: Create the writer thread. */
145 if (pthread_create (&writer, NULL, writer_func, NULL))
147 /* Job done. Go to sleep. */
154 [gl_cv_pthread_rwlock_rdlock_prefer_writer=yes],
155 [gl_cv_pthread_rwlock_rdlock_prefer_writer=no],
156 [gl_cv_pthread_rwlock_rdlock_prefer_writer="guessing yes"])
159 case "$gl_cv_pthread_rwlock_rdlock_prefer_writer" in
161 AC_DEFINE([HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER], [1],
162 [Define if the 'pthread_rwlock_rdlock' function prefers a writer to a reader.])