* configure.ac: Update minimum autoconf version. * lib/config.charset, lib/ref-add.sin, lib/ref-del.sin, m4/closeout.m4, m4/close-stream.m4, m4/configmake.m4, m4/glibc21.m4, m4/hard-locale.m4: Remove. * lib/alloca.c, lib/alloca.in.h, lib/argmatch.c, lib/argmatch.h, lib/arg-nonnull.h, lib/argp-ba.c, lib/argp-eexst.c, lib/argp-fmtstream.c, lib/argp-fmtstream.h, lib/argp-fs-xinl.c, lib/argp.h, lib/argp-help.c, lib/argp-namefrob.h, lib/argp-parse.c, lib/argp-pin.c, lib/argp-pv.c, lib/argp-pvh.c, lib/argp-xinl.c, lib/asnprintf.c, lib/basename-lgpl.c, lib/c-ctype.h, lib/c++defs.h, lib/closeout.c, lib/closeout.h, lib/close-stream.c, lib/c-strcasecmp.c, lib/c-strcaseeq.h, lib/c-strcase.h, lib/c-strncasecmp.c, lib/dirname.h, lib/dirname-lgpl.c, lib/dosname.h, lib/errno.in.h, lib/error.c, lib/error.h, lib/exitfail.c, lib/exitfail.h, lib/fcntl.in.h, lib/filename.h, lib/float.c, lib/float+.h, lib/float.in.h, lib/fpending.c, lib/fpending.h, lib/getopt1.c, lib/getopt.c, lib/getopt-cdefs.in.h, lib/getopt-core.h, lib/getopt-ext.h, lib/getopt.in.h, lib/getopt_int.h, lib/getopt-pfx-core.h, lib/getopt-pfx-ext.h, lib/getprogname.c, lib/getprogname.h, lib/gettext.h, lib/gettimeofday.c, lib/hard-locale.c, lib/hard-locale.h, lib/intprops.h, lib/isatty.c, lib/itold.c, lib/limits.in.h, lib/localcharset.c, lib/localcharset.h, lib/localtime-buffer.c, lib/localtime-buffer.h, lib/lstat.c, lib/Makefile.am, lib/malloca.c, lib/malloca.h, lib/malloc.c, lib/mbrtowc.c, lib/mbsinit.c, lib/memchr.c, lib/memchr.valgrind, lib/mempcpy.c, lib/minmax.h, lib/mkstemp.c, lib/mkstemps.c, lib/msvc-inval.c, lib/msvc-inval.h, lib/msvc-nothrow.c, lib/msvc-nothrow.h, lib/_Noreturn.h, lib/pathmax.h, lib/printf-args.c, lib/printf-args.h, lib/printf-parse.c, lib/printf-parse.h, lib/progname.c, lib/progname.h, lib/quotearg.c, lib/quotearg.h, lib/quote.h, lib/rawmemchr.c, lib/rawmemchr.valgrind, lib/secure_getenv.c, lib/size_max.h, lib/sleep.c, lib/stat.c, lib/stat-time.h, lib/stat-w32.c, lib/stat-w32.h, lib/stdalign.in.h, lib/stdbool.in.h, lib/stddef.in.h, lib/stdint.in.h, lib/stdio-impl.h, lib/stdio.in.h, lib/stdlib.in.h, lib/stpcpy.c, lib/strcasecmp.c, lib/strchrnul.c, lib/strchrnul.valgrind, lib/streq.h, lib/strerror.c, lib/strerror-override.c, lib/strerror-override.h, lib/string.in.h, lib/strings.in.h, lib/stripslash.c, lib/strncasecmp.c, lib/strndup.c, lib/strnlen.c, lib/strverscmp.c, lib/sysexits.in.h, lib/sys_stat.in.h, lib/sys_time.in.h, lib/sys_types.in.h, lib/sys_wait.in.h, lib/tempname.c, lib/tempname.h, lib/time.in.h, lib/unistd.in.h, lib/vasnprintf.c, lib/vasnprintf.h, lib/verify.h, lib/vsnprintf.c, lib/warn-on-use.h, lib/wchar.in.h, lib/wctype.in.h, lib/xalloc-die.c, lib/xalloc.h, lib/xalloc-oversized.h, lib/xmalloc.c, lib/xsize.h, m4/00gnulib.m4, m4/absolute-header.m4, m4/alloca.m4, m4/argp.m4, m4/codeset.m4, m4/dirname.m4, m4/double-slash-root.m4, m4/eealloc.m4, m4/errno_h.m4, m4/error.m4, m4/exponentd.m4, m4/extensions.m4, m4/extern-inline.m4, m4/fcntl_h.m4, m4/fcntl-o.m4, m4/float_h.m4, m4/fpending.m4, m4/getopt.m4, m4/getprogname.m4, m4/gettimeofday.m4, m4/gnulib-cache.m4, m4/gnulib-common.m4, m4/gnulib-comp.m4, m4/gnulib-tool.m4, m4/include_next.m4, m4/intmax_t.m4, m4/inttypes_h.m4, m4/isatty.m4, m4/largefile.m4, m4/limits-h.m4, m4/localcharset.m4, m4/locale-fr.m4, m4/locale-ja.m4, m4/locale-zh.m4, m4/localtime-buffer.m4, m4/longlong.m4, m4/lstat.m4, m4/malloca.m4, m4/malloc.m4, m4/math_h.m4, m4/mbrtowc.m4, m4/mbsinit.m4, m4/mbstate_t.m4, m4/memchr.m4, m4/mempcpy.m4, m4/minmax.m4, m4/mkstemp.m4, m4/mkstemps.m4, m4/mmap-anon.m4, m4/msvc-inval.m4, m4/msvc-nothrow.m4, m4/multiarch.m4, m4/nocrash.m4, m4/off_t.m4, m4/pathmax.m4, m4/printf.m4, m4/quotearg.m4, m4/quote.m4, m4/rawmemchr.m4, m4/secure_getenv.m4, m4/size_max.m4, m4/sleep.m4, m4/ssize_t.m4, m4/stat.m4, m4/stat-time.m4, m4/stdalign.m4, m4/stdbool.m4, m4/stddef_h.m4, m4/stdint_h.m4, m4/stdint.m4, m4/stdio_h.m4, m4/stdlib_h.m4, m4/stpcpy.m4, m4/strcase.m4, m4/strchrnul.m4, m4/strerror.m4, m4/string_h.m4, m4/strings_h.m4, m4/strndup.m4, m4/strnlen.m4, m4/strverscmp.m4, m4/sysexits.m4, m4/sys_socket_h.m4, m4/sys_stat_h.m4, m4/sys_time_h.m4, m4/sys_types_h.m4, m4/sys_wait_h.m4, m4/tempname.m4, m4/time_h.m4, m4/unistd_h.m4, m4/vasnprintf.m4, m4/vsnprintf.m4, m4/warn-on-use.m4, m4/wchar_h.m4, m4/wchar_t.m4, m4/wctype_h.m4, m4/wint_t.m4, m4/xalloc.m4, m4/xsize.m4: Update. * lib/cdefs.h, lib/glthread/lock.c, lib/glthread/lock.h, lib/glthread/threadlib.c, lib/libc-config.h, lib/mkdir.c, lib/windows-initguard.h, lib/windows-mutex.c, lib/windows-mutex.h, lib/windows-once.c, lib/windows-once.h, lib/windows-recmutex.c, lib/windows-recmutex.h, lib/windows-rwlock.c, lib/windows-rwlock.h, m4/host-cpu-c-abi.m4, m4/__inline.m4, m4/lib-ld.m4, m4/lib-link.m4, m4/lib-prefix.m4, m4/lock.m4, m4/mkdir.m4, m4/pthread_rwlock_rdlock.m4, m4/std-gnu11.m4, m4/threadlib.m4, tools/config.rpath: New files.
165 lines
5.6 KiB
Text
165 lines
5.6 KiB
Text
# pthread_rwlock_rdlock.m4 serial 2
|
|
dnl Copyright (C) 2017-2019 Free Software Foundation, Inc.
|
|
dnl This file is free software; the Free Software Foundation
|
|
dnl gives unlimited permission to copy and/or distribute it,
|
|
dnl with or without modifications, as long as this notice is preserved.
|
|
|
|
dnl From Bruno Haible.
|
|
dnl Inspired by
|
|
dnl https://github.com/linux-test-project/ltp/blob/master/testcases/open_posix_testsuite/conformance/interfaces/pthread_rwlock_rdlock/2-2.c
|
|
dnl by Intel Corporation.
|
|
|
|
dnl Test whether in a situation where
|
|
dnl - an rwlock is taken by a reader and has a writer waiting,
|
|
dnl - an additional reader requests the lock,
|
|
dnl - the waiting writer and the requesting reader threads have the same
|
|
dnl priority,
|
|
dnl the requesting reader thread gets blocked, so that at some point the
|
|
dnl waiting writer can acquire the lock.
|
|
dnl Without such a guarantee, when there a N readers and each of the readers
|
|
dnl spends more than 1/Nth of the time with the lock held, there is a high
|
|
dnl probability that the waiting writer will not get the lock in a given finite
|
|
dnl time, a phenomenon called "writer starvation".
|
|
dnl Without such a guarantee, applications have a hard time avoiding writer
|
|
dnl starvation.
|
|
dnl
|
|
dnl POSIX:2017 makes this requirement only for implementations that support TPS
|
|
dnl (Thread Priority Scheduling) and only for the scheduling policies SCHED_FIFO
|
|
dnl and SCHED_RR, see
|
|
dnl http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_rdlock.html
|
|
dnl but this test verifies the guarantee regardless of TPS and regardless of
|
|
dnl scheduling policy.
|
|
dnl Glibc currently does not provide this guarantee, see
|
|
dnl https://sourceware.org/bugzilla/show_bug.cgi?id=13701
|
|
AC_DEFUN([gl_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER],
|
|
[
|
|
AC_REQUIRE([gl_THREADLIB_EARLY])
|
|
AC_CACHE_CHECK([whether pthread_rwlock_rdlock prefers a writer to a reader],
|
|
[gl_cv_pthread_rwlock_rdlock_prefer_writer],
|
|
[save_LIBS="$LIBS"
|
|
LIBS="$LIBS $LIBMULTITHREAD"
|
|
AC_RUN_IFELSE(
|
|
[AC_LANG_SOURCE([[
|
|
#include <errno.h>
|
|
#include <pthread.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
#define SUCCEED() exit (0)
|
|
#define FAILURE() exit (1)
|
|
#define UNEXPECTED(n) (exit (10 + (n)))
|
|
|
|
/* The main thread creates the waiting writer and the requesting reader threads
|
|
in the default way; this guarantees that they have the same priority.
|
|
We can reuse the main thread as first reader thread. */
|
|
|
|
static pthread_rwlock_t lock;
|
|
static pthread_t reader1;
|
|
static pthread_t writer;
|
|
static pthread_t reader2;
|
|
static pthread_t timer;
|
|
/* Used to pass control from writer to reader2 and from reader2 to timer,
|
|
as in a relay race.
|
|
Passing control from one running thread to another running thread
|
|
is most likely faster than to create the second thread. */
|
|
static pthread_mutex_t baton;
|
|
|
|
static void *
|
|
timer_func (void *ignored)
|
|
{
|
|
/* Step 13 (can be before or after step 12):
|
|
The timer thread takes the baton, then waits a moment to make sure
|
|
it can tell whether the second reader thread is blocked at step 12. */
|
|
if (pthread_mutex_lock (&baton))
|
|
UNEXPECTED (13);
|
|
usleep (100000);
|
|
/* By the time we get here, it's clear that the second reader thread is
|
|
blocked at step 12. This is the desired behaviour. */
|
|
SUCCEED ();
|
|
}
|
|
|
|
static void *
|
|
reader2_func (void *ignored)
|
|
{
|
|
int err;
|
|
|
|
/* Step 8 (can be before or after step 7):
|
|
The second reader thread takes the baton, then waits a moment to make sure
|
|
the writer thread has reached step 7. */
|
|
if (pthread_mutex_lock (&baton))
|
|
UNEXPECTED (8);
|
|
usleep (100000);
|
|
/* Step 9: The second reader thread requests the lock. */
|
|
err = pthread_rwlock_tryrdlock (&lock);
|
|
if (err == 0)
|
|
FAILURE ();
|
|
else if (err != EBUSY)
|
|
UNEXPECTED (9);
|
|
/* Step 10: Launch a timer, to test whether the next call blocks. */
|
|
if (pthread_create (&timer, NULL, timer_func, NULL))
|
|
UNEXPECTED (10);
|
|
/* Step 11: Release the baton. */
|
|
if (pthread_mutex_unlock (&baton))
|
|
UNEXPECTED (11);
|
|
/* Step 12: The second reader thread requests the lock. */
|
|
err = pthread_rwlock_rdlock (&lock);
|
|
if (err == 0)
|
|
FAILURE ();
|
|
else
|
|
UNEXPECTED (12);
|
|
}
|
|
|
|
static void *
|
|
writer_func (void *ignored)
|
|
{
|
|
/* Step 4: Take the baton, so that the second reader thread does not go ahead
|
|
too early. */
|
|
if (pthread_mutex_lock (&baton))
|
|
UNEXPECTED (4);
|
|
/* Step 5: Create the second reader thread. */
|
|
if (pthread_create (&reader2, NULL, reader2_func, NULL))
|
|
UNEXPECTED (5);
|
|
/* Step 6: Release the baton. */
|
|
if (pthread_mutex_unlock (&baton))
|
|
UNEXPECTED (6);
|
|
/* Step 7: The writer thread requests the lock. */
|
|
if (pthread_rwlock_wrlock (&lock))
|
|
UNEXPECTED (7);
|
|
return NULL;
|
|
}
|
|
|
|
int
|
|
main ()
|
|
{
|
|
reader1 = pthread_self ();
|
|
|
|
/* Step 1: The main thread initializes the lock and the baton. */
|
|
if (pthread_rwlock_init (&lock, NULL))
|
|
UNEXPECTED (1);
|
|
if (pthread_mutex_init (&baton, NULL))
|
|
UNEXPECTED (1);
|
|
/* Step 2: The main thread acquires the lock as a reader. */
|
|
if (pthread_rwlock_rdlock (&lock))
|
|
UNEXPECTED (2);
|
|
/* Step 3: Create the writer thread. */
|
|
if (pthread_create (&writer, NULL, writer_func, NULL))
|
|
UNEXPECTED (3);
|
|
/* Job done. Go to sleep. */
|
|
for (;;)
|
|
{
|
|
sleep (1);
|
|
}
|
|
}
|
|
]])],
|
|
[gl_cv_pthread_rwlock_rdlock_prefer_writer=yes],
|
|
[gl_cv_pthread_rwlock_rdlock_prefer_writer=no],
|
|
[gl_cv_pthread_rwlock_rdlock_prefer_writer="guessing yes"])
|
|
LIBS="$save_LIBS"
|
|
])
|
|
case "$gl_cv_pthread_rwlock_rdlock_prefer_writer" in
|
|
*yes)
|
|
AC_DEFINE([HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER], [1],
|
|
[Define if the 'pthread_rwlock_rdlock' function prefers a writer to a reader.])
|
|
;;
|
|
esac
|
|
])
|