New upstream version 1.9
[debian/gzip] / m4 / pthread_rwlock_rdlock.m4
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.
6
7 dnl From Bruno Haible.
8 dnl Inspired by
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.
11
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
16 dnl     priority,
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
24 dnl starvation.
25 dnl
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
28 dnl and SCHED_RR, see
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],
33 [
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],
37     [save_LIBS="$LIBS"
38      LIBS="$LIBS $LIBMULTITHREAD"
39      AC_RUN_IFELSE(
40        [AC_LANG_SOURCE([[
41 #include <errno.h>
42 #include <pthread.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45
46 #define SUCCEED() exit (0)
47 #define FAILURE() exit (1)
48 #define UNEXPECTED(n) (exit (10 + (n)))
49
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.  */
53
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,
60    as in a relay race.
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;
64
65 static void *
66 timer_func (void *ignored)
67 {
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))
72     UNEXPECTED (13);
73   usleep (100000);
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.  */
76   SUCCEED ();
77 }
78
79 static void *
80 reader2_func (void *ignored)
81 {
82   int err;
83
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))
88     UNEXPECTED (8);
89   usleep (100000);
90   /* Step 9: The second reader thread requests the lock.  */
91   err = pthread_rwlock_tryrdlock (&lock);
92   if (err == 0)
93     FAILURE ();
94   else if (err != EBUSY)
95     UNEXPECTED (9);
96   /* Step 10: Launch a timer, to test whether the next call blocks.  */
97   if (pthread_create (&timer, NULL, timer_func, NULL))
98     UNEXPECTED (10);
99   /* Step 11: Release the baton.  */
100   if (pthread_mutex_unlock (&baton))
101     UNEXPECTED (11);
102   /* Step 12: The second reader thread requests the lock.  */
103   err = pthread_rwlock_rdlock (&lock);
104   if (err == 0)
105     FAILURE ();
106   else
107     UNEXPECTED (12);
108 }
109
110 static void *
111 writer_func (void *ignored)
112 {
113   /* Step 4: Take the baton, so that the second reader thread does not go ahead
114      too early.  */
115   if (pthread_mutex_lock (&baton))
116     UNEXPECTED (4);
117   /* Step 5: Create the second reader thread.  */
118   if (pthread_create (&reader2, NULL, reader2_func, NULL))
119     UNEXPECTED (5);
120   /* Step 6: Release the baton.  */
121   if (pthread_mutex_unlock (&baton))
122     UNEXPECTED (6);
123   /* Step 7: The writer thread requests the lock.  */
124   if (pthread_rwlock_wrlock (&lock))
125     UNEXPECTED (7);
126   return NULL;
127 }
128
129 int
130 main ()
131 {
132   reader1 = pthread_self ();
133
134   /* Step 1: The main thread initializes the lock and the baton.  */
135   if (pthread_rwlock_init (&lock, NULL))
136     UNEXPECTED (1);
137   if (pthread_mutex_init (&baton, NULL))
138     UNEXPECTED (1);
139   /* Step 2: The main thread acquires the lock as a reader.  */
140   if (pthread_rwlock_rdlock (&lock))
141     UNEXPECTED (2);
142   /* Step 3: Create the writer thread.  */
143   if (pthread_create (&writer, NULL, writer_func, NULL))
144     UNEXPECTED (3);
145   /* Job done.  Go to sleep.  */
146   for (;;)
147     {
148       sleep (1);
149     }
150 }
151 ]])],
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"])
155      LIBS="$save_LIBS"
156     ])
157   case "$gl_cv_pthread_rwlock_rdlock_prefer_writer" in
158     *yes)
159       AC_DEFINE([HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER], [1],
160         [Define if the 'pthread_rwlock_rdlock' function prefers a writer to a reader.])
161       ;;
162   esac
163 ])