1 # pthread_rwlock_rdlock.m4 serial 1
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:2008 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 test verifies the guarantee regardless of TPS and regardless of
31 dnl scheduling policy.
32 AC_DEFUN([gl_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER],
34 AC_REQUIRE([gl_THREADLIB_EARLY])
35 AC_CACHE_CHECK([whether pthread_rwlock_rdlock prefers a writer to a reader],
36 [gl_cv_pthread_rwlock_rdlock_prefer_writer],
38 LIBS="$LIBS $LIBMULTITHREAD"
46 #define SUCCEED() exit (0)
47 #define FAILURE() exit (1)
48 #define UNEXPECTED(n) (exit (10 + (n)))
50 /* The main thread creates the waiting writer and the requesting reader threads
51 in the default way; this guarantees that they have the same priority.
52 We can reuse the main thread as first reader thread. */
54 static pthread_rwlock_t lock;
55 static pthread_t reader1;
56 static pthread_t writer;
57 static pthread_t reader2;
58 static pthread_t timer;
59 /* Used to pass control from writer to reader2 and from reader2 to timer,
61 Passing control from one running thread to another running thread
62 is most likely faster than to create the second thread. */
63 static pthread_mutex_t baton;
66 timer_func (void *ignored)
68 /* Step 13 (can be before or after step 12):
69 The timer thread takes the baton, then waits a moment to make sure
70 it can tell whether the second reader thread is blocked at step 12. */
71 if (pthread_mutex_lock (&baton))
74 /* By the time we get here, it's clear that the second reader thread is
75 blocked at step 12. This is the desired behaviour. */
80 reader2_func (void *ignored)
84 /* Step 8 (can be before or after step 7):
85 The second reader thread takes the baton, then waits a moment to make sure
86 the writer thread has reached step 7. */
87 if (pthread_mutex_lock (&baton))
90 /* Step 9: The second reader thread requests the lock. */
91 err = pthread_rwlock_tryrdlock (&lock);
94 else if (err != EBUSY)
96 /* Step 10: Launch a timer, to test whether the next call blocks. */
97 if (pthread_create (&timer, NULL, timer_func, NULL))
99 /* Step 11: Release the baton. */
100 if (pthread_mutex_unlock (&baton))
102 /* Step 12: The second reader thread requests the lock. */
103 err = pthread_rwlock_rdlock (&lock);
111 writer_func (void *ignored)
113 /* Step 4: Take the baton, so that the second reader thread does not go ahead
115 if (pthread_mutex_lock (&baton))
117 /* Step 5: Create the second reader thread. */
118 if (pthread_create (&reader2, NULL, reader2_func, NULL))
120 /* Step 6: Release the baton. */
121 if (pthread_mutex_unlock (&baton))
123 /* Step 7: The writer thread requests the lock. */
124 if (pthread_rwlock_wrlock (&lock))
132 reader1 = pthread_self ();
134 /* Step 1: The main thread initializes the lock and the baton. */
135 if (pthread_rwlock_init (&lock, NULL))
137 if (pthread_mutex_init (&baton, NULL))
139 /* Step 2: The main thread acquires the lock as a reader. */
140 if (pthread_rwlock_rdlock (&lock))
142 /* Step 3: Create the writer thread. */
143 if (pthread_create (&writer, NULL, writer_func, NULL))
145 /* Job done. Go to sleep. */
152 [gl_cv_pthread_rwlock_rdlock_prefer_writer=yes],
153 [gl_cv_pthread_rwlock_rdlock_prefer_writer=no],
154 [gl_cv_pthread_rwlock_rdlock_prefer_writer="guessing yes"])
157 case "$gl_cv_pthread_rwlock_rdlock_prefer_writer" in
159 AC_DEFINE([HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER], [1],
160 [Define if the 'pthread_rwlock_rdlock' function prefers a writer to a reader.])