]>
Commit | Line | Data |
---|---|---|
8868edaf CR |
1 | # pthread_rwlock_rdlock.m4 serial 2 |
2 | dnl Copyright (C) 2017-2019 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:2017 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 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], | |
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 | ||
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, | |
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. */ | |
65 | static pthread_mutex_t baton; | |
66 | ||
67 | static void * | |
68 | timer_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 | ||
81 | static void * | |
82 | reader2_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 | ||
112 | static void * | |
113 | writer_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 | ||
131 | int | |
132 | main () | |
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 | ]) |