]> git.ipfire.org Git - thirdparty/bash.git/blame - m4/pthread_rwlock_rdlock.m4
Bash-5.1 patch 4: fix key-value pair associative array assignment word expansions
[thirdparty/bash.git] / m4 / pthread_rwlock_rdlock.m4
CommitLineData
8868edaf
CR
1# pthread_rwlock_rdlock.m4 serial 2
2dnl Copyright (C) 2017-2019 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7dnl From Bruno Haible.
8dnl Inspired by
9dnl https://github.com/linux-test-project/ltp/blob/master/testcases/open_posix_testsuite/conformance/interfaces/pthread_rwlock_rdlock/2-2.c
10dnl by Intel Corporation.
11
12dnl Test whether in a situation where
13dnl - an rwlock is taken by a reader and has a writer waiting,
14dnl - an additional reader requests the lock,
15dnl - the waiting writer and the requesting reader threads have the same
16dnl priority,
17dnl the requesting reader thread gets blocked, so that at some point the
18dnl waiting writer can acquire the lock.
19dnl Without such a guarantee, when there a N readers and each of the readers
20dnl spends more than 1/Nth of the time with the lock held, there is a high
21dnl probability that the waiting writer will not get the lock in a given finite
22dnl time, a phenomenon called "writer starvation".
23dnl Without such a guarantee, applications have a hard time avoiding writer
24dnl starvation.
25dnl
26dnl POSIX:2017 makes this requirement only for implementations that support TPS
27dnl (Thread Priority Scheduling) and only for the scheduling policies SCHED_FIFO
28dnl and SCHED_RR, see
29dnl http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_rdlock.html
30dnl but this test verifies the guarantee regardless of TPS and regardless of
31dnl scheduling policy.
32dnl Glibc currently does not provide this guarantee, see
33dnl https://sourceware.org/bugzilla/show_bug.cgi?id=13701
34AC_DEFUN([gl_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER],
35[
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],
39 [save_LIBS="$LIBS"
40 LIBS="$LIBS $LIBMULTITHREAD"
41 AC_RUN_IFELSE(
42 [AC_LANG_SOURCE([[
43#include <errno.h>
44#include <pthread.h>
45#include <stdlib.h>
46#include <unistd.h>
47
48#define SUCCEED() exit (0)
49#define FAILURE() exit (1)
50#define UNEXPECTED(n) (exit (10 + (n)))
51
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. */
55
56static pthread_rwlock_t lock;
57static pthread_t reader1;
58static pthread_t writer;
59static pthread_t reader2;
60static pthread_t timer;
61/* Used to pass control from writer to reader2 and from reader2 to timer,
62 as in a relay race.
63 Passing control from one running thread to another running thread
64 is most likely faster than to create the second thread. */
65static pthread_mutex_t baton;
66
67static void *
68timer_func (void *ignored)
69{
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))
74 UNEXPECTED (13);
75 usleep (100000);
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. */
78 SUCCEED ();
79}
80
81static void *
82reader2_func (void *ignored)
83{
84 int err;
85
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))
90 UNEXPECTED (8);
91 usleep (100000);
92 /* Step 9: The second reader thread requests the lock. */
93 err = pthread_rwlock_tryrdlock (&lock);
94 if (err == 0)
95 FAILURE ();
96 else if (err != EBUSY)
97 UNEXPECTED (9);
98 /* Step 10: Launch a timer, to test whether the next call blocks. */
99 if (pthread_create (&timer, NULL, timer_func, NULL))
100 UNEXPECTED (10);
101 /* Step 11: Release the baton. */
102 if (pthread_mutex_unlock (&baton))
103 UNEXPECTED (11);
104 /* Step 12: The second reader thread requests the lock. */
105 err = pthread_rwlock_rdlock (&lock);
106 if (err == 0)
107 FAILURE ();
108 else
109 UNEXPECTED (12);
110}
111
112static void *
113writer_func (void *ignored)
114{
115 /* Step 4: Take the baton, so that the second reader thread does not go ahead
116 too early. */
117 if (pthread_mutex_lock (&baton))
118 UNEXPECTED (4);
119 /* Step 5: Create the second reader thread. */
120 if (pthread_create (&reader2, NULL, reader2_func, NULL))
121 UNEXPECTED (5);
122 /* Step 6: Release the baton. */
123 if (pthread_mutex_unlock (&baton))
124 UNEXPECTED (6);
125 /* Step 7: The writer thread requests the lock. */
126 if (pthread_rwlock_wrlock (&lock))
127 UNEXPECTED (7);
128 return NULL;
129}
130
131int
132main ()
133{
134 reader1 = pthread_self ();
135
136 /* Step 1: The main thread initializes the lock and the baton. */
137 if (pthread_rwlock_init (&lock, NULL))
138 UNEXPECTED (1);
139 if (pthread_mutex_init (&baton, NULL))
140 UNEXPECTED (1);
141 /* Step 2: The main thread acquires the lock as a reader. */
142 if (pthread_rwlock_rdlock (&lock))
143 UNEXPECTED (2);
144 /* Step 3: Create the writer thread. */
145 if (pthread_create (&writer, NULL, writer_func, NULL))
146 UNEXPECTED (3);
147 /* Job done. Go to sleep. */
148 for (;;)
149 {
150 sleep (1);
151 }
152}
153]])],
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"])
157 LIBS="$save_LIBS"
158 ])
159 case "$gl_cv_pthread_rwlock_rdlock_prefer_writer" in
160 *yes)
161 AC_DEFINE([HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER], [1],
162 [Define if the 'pthread_rwlock_rdlock' function prefers a writer to a reader.])
163 ;;
164 esac
165])