* lib/pthread-rwlock.c: New file, based on lib/glthread/lock.c.
* m4/pthread-rwlock.m4: New file.
* modules/pthread-rwlock: New file.
* doc/posix-functions/pthread_rwlock_init.texi: Mention the new module
and the Android problem.
* doc/posix-functions/pthread_rwlockattr_init.texi: Likewise.
* doc/posix-functions/pthread_rwlockattr_destroy.texi: Likewise.
* doc/posix-functions/pthread_rwlock_rdlock.texi: Likewise.
* doc/posix-functions/pthread_rwlock_wrlock.texi: Likewise.
* doc/posix-functions/pthread_rwlock_tryrdlock.texi: Likewise.
* doc/posix-functions/pthread_rwlock_trywrlock.texi: Likewise.
* doc/posix-functions/pthread_rwlock_timedrdlock.texi: Likewise.
* doc/posix-functions/pthread_rwlock_timedwrlock.texi: Likewise.
* doc/posix-functions/pthread_rwlock_unlock.texi: Likewise.
* doc/posix-functions/pthread_rwlock_destroy.texi: Likewise.
+2019-07-14 Bruno Haible <bruno@clisp.org>
+
+ pthread-rwlock: New module.
+ * lib/pthread-rwlock.c: New file, based on lib/glthread/lock.c.
+ * m4/pthread-rwlock.m4: New file.
+ * modules/pthread-rwlock: New file.
+ * doc/posix-functions/pthread_rwlock_init.texi: Mention the new module
+ and the Android problem.
+ * doc/posix-functions/pthread_rwlockattr_init.texi: Likewise.
+ * doc/posix-functions/pthread_rwlockattr_destroy.texi: Likewise.
+ * doc/posix-functions/pthread_rwlock_rdlock.texi: Likewise.
+ * doc/posix-functions/pthread_rwlock_wrlock.texi: Likewise.
+ * doc/posix-functions/pthread_rwlock_tryrdlock.texi: Likewise.
+ * doc/posix-functions/pthread_rwlock_trywrlock.texi: Likewise.
+ * doc/posix-functions/pthread_rwlock_timedrdlock.texi: Likewise.
+ * doc/posix-functions/pthread_rwlock_timedwrlock.texi: Likewise.
+ * doc/posix-functions/pthread_rwlock_unlock.texi: Likewise.
+ * doc/posix-functions/pthread_rwlock_destroy.texi: Likewise.
+
2019-07-14 Bruno Haible <bruno@clisp.org>
pthread-mutex: New module.
@itemize
@item
This function is missing on some platforms:
-Minix 3.1.8, mingw, MSVC 14.
+Minix 3.1.8, mingw, MSVC 14, Android 4.3.
But the provided replacement is just a dummy on some of these platforms:
Minix 3.1.8.
@end itemize
POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_destroy.html}
-Gnulib module: ---
+Gnulib module: pthread-rwlock
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+Minix 3.1.8, mingw, MSVC 14, Android 4.3.
+But the provided replacement is just a dummy on some of these platforms:
+Minix 3.1.8.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
-@item
-This function is missing on some platforms:
-Minix 3.1.8, HP-UX 11, mingw, MSVC 14.
@end itemize
POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_init.html}
-Gnulib module: ---
+Gnulib module: pthread-rwlock
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+Minix 3.1.8, mingw, MSVC 14, Android 4.3.
+But the provided replacement is just a dummy on some of these platforms:
+Minix 3.1.8.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
-@item
-This function is missing on some platforms:
-Minix 3.1.8, mingw, MSVC 14.
@end itemize
POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_rdlock.html}
-Gnulib module: ---
+Gnulib module: pthread-rwlock
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+Minix 3.1.8, mingw, MSVC 14, Android 4.3.
+But the provided replacement is just a dummy on some of these platforms:
+Minix 3.1.8.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
@item
-This function is missing on some platforms:
-Minix 3.1.8, HP-UX 11, mingw, MSVC 14.
-@item
This function prefers readers to writers (meaning, when this function is
called on an rwlock that is already taken by one or more readers, and
another writer is already waiting to take it, this function may return
POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_timedrdlock.html}
-Gnulib module: ---
+Gnulib module: pthread-rwlock
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+Mac OS X 10.5, FreeBSD 5.2.1, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 9, Cygwin, mingw, MSVC 14, Android 4.3.
+But the provided replacement is just a dummy on some of these platforms:
+Minix 3.1.8.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
@item
-This function is missing on some platforms:
-Mac OS X 10.5, FreeBSD 5.2.1, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 9, Cygwin, mingw, MSVC 14.
-@item
This function prefers readers to writers (meaning, when this function is
called on an rwlock that is already taken by one or more readers, and
another writer is already waiting to take it, this function may return
POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_timedwrlock.html}
-Gnulib module: ---
+Gnulib module: pthread-rwlock
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+Mac OS X 10.5, FreeBSD 5.2.1, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 9, Cygwin, mingw, MSVC 14, Android 4.3.
+But the provided replacement is just a dummy on some of these platforms:
+Minix 3.1.8.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
-@item
-This function is missing on some platforms:
-Mac OS X 10.5, FreeBSD 5.2.1, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 9, Cygwin, mingw, MSVC 14.
@end itemize
POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_tryrdlock.html}
-Gnulib module: ---
+Gnulib module: pthread-rwlock
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+Minix 3.1.8, mingw, MSVC 14, Android 4.3.
+But the provided replacement is just a dummy on some of these platforms:
+Minix 3.1.8.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
@item
-This function is missing on some platforms:
-Minix 3.1.8, HP-UX 11, mingw, MSVC 14.
-@item
This function prefers readers to writers (meaning, when this function is
called on an rwlock that is already taken by one or more readers, and
another writer is already waiting to take it, this function may return
POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_trywrlock.html}
-Gnulib module: ---
+Gnulib module: pthread-rwlock
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+Minix 3.1.8, mingw, MSVC 14, Android 4.3.
+But the provided replacement is just a dummy on some of these platforms:
+Minix 3.1.8.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
-@item
-This function is missing on some platforms:
-Minix 3.1.8, HP-UX 11, mingw, MSVC 14.
@end itemize
POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_unlock.html}
-Gnulib module: ---
+Gnulib module: pthread-rwlock
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+Minix 3.1.8, mingw, MSVC 14, Android 4.3.
+But the provided replacement is just a dummy on some of these platforms:
+Minix 3.1.8.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
-@item
-This function is missing on some platforms:
-Minix 3.1.8, HP-UX 11, mingw, MSVC 14.
@end itemize
POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlock_wrlock.html}
-Gnulib module: ---
+Gnulib module: pthread-rwlock
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+Minix 3.1.8, mingw, MSVC 14, Android 4.3.
+But the provided replacement is just a dummy on some of these platforms:
+Minix 3.1.8.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
-@item
-This function is missing on some platforms:
-Minix 3.1.8, HP-UX 11, mingw, MSVC 14.
@end itemize
POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlockattr_destroy.html}
-Gnulib module: ---
+Gnulib module: pthread-rwlock
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+Minix 3.1.8, mingw, MSVC 14.
+But the provided replacement is just a dummy on some of these platforms:
+Minix 3.1.8.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
-@item
-This function is missing on some platforms:
-Minix 3.1.8, HP-UX 11, mingw, MSVC 14.
@end itemize
POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_rwlockattr_init.html}
-Gnulib module: ---
+Gnulib module: pthread-rwlock
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+Minix 3.1.8, mingw, MSVC 14.
+But the provided replacement is just a dummy on some of these platforms:
+Minix 3.1.8.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
-@item
-This function is missing on some platforms:
-Minix 3.1.8, HP-UX 11, mingw, MSVC 14.
@end itemize
--- /dev/null
+/* POSIX read-write locks.
+ Copyright (C) 2019 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, see <https://www.gnu.org/licenses/>. */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2019. */
+
+#include <config.h>
+
+/* Specification. */
+#include <pthread.h>
+
+#if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
+# include "windows-timedrwlock.h"
+#else
+# include <errno.h>
+# include <limits.h>
+# include <sys/time.h>
+# include <time.h>
+#endif
+
+#if ((defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS) || !HAVE_PTHREAD_H
+
+int
+pthread_rwlockattr_init (pthread_rwlockattr_t *attr)
+{
+ *attr = 0;
+ return 0;
+}
+
+int
+pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr _GL_UNUSED)
+{
+ return 0;
+}
+
+#endif
+
+#if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
+/* Use Windows threads. */
+
+int
+pthread_rwlock_init (pthread_rwlock_t *lock,
+ const pthread_rwlockattr_t *attr _GL_UNUSED)
+{
+ glwthread_timedrwlock_init (lock);
+ return 0;
+}
+
+int
+pthread_rwlock_rdlock (pthread_rwlock_t *lock)
+{
+ return glwthread_timedrwlock_rdlock (lock);
+}
+
+int
+pthread_rwlock_wrlock (pthread_rwlock_t *lock)
+{
+ return glwthread_timedrwlock_wrlock (lock);
+}
+
+int
+pthread_rwlock_tryrdlock (pthread_rwlock_t *lock)
+{
+ return glwthread_timedrwlock_tryrdlock (lock);
+}
+
+int
+pthread_rwlock_trywrlock (pthread_rwlock_t *lock)
+{
+ return glwthread_timedrwlock_trywrlock (lock);
+}
+
+int
+pthread_rwlock_timedrdlock (pthread_rwlock_t *lock,
+ const struct timespec *abstime)
+{
+ return glwthread_timedrwlock_timedrdlock (lock, abstime);
+}
+
+int
+pthread_rwlock_timedwrlock (pthread_rwlock_t *lock,
+ const struct timespec *abstime)
+{
+ return glwthread_timedrwlock_timedwrlock (lock, abstime);
+}
+
+int
+pthread_rwlock_unlock (pthread_rwlock_t *lock)
+{
+ return glwthread_timedrwlock_unlock (lock);
+}
+
+int
+pthread_rwlock_destroy (pthread_rwlock_t *lock)
+{
+ return glwthread_timedrwlock_destroy (lock);
+}
+
+#elif HAVE_PTHREAD_H
+/* Provide workarounds for POSIX threads. */
+
+# if PTHREAD_RWLOCK_UNIMPLEMENTED
+
+int
+pthread_rwlock_init (pthread_rwlock_t *lock,
+ const pthread_rwlockattr_t *attr _GL_UNUSED)
+{
+ int err;
+
+ err = pthread_mutex_init (&lock->lock, NULL);
+ if (err != 0)
+ return err;
+ err = pthread_cond_init (&lock->waiting_readers, NULL);
+ if (err != 0)
+ return err;
+ err = pthread_cond_init (&lock->waiting_writers, NULL);
+ if (err != 0)
+ return err;
+ lock->waiting_writers_count = 0;
+ lock->runcount = 0;
+ return 0;
+}
+
+int
+pthread_rwlock_rdlock (pthread_rwlock_t *lock)
+{
+ int err;
+
+ err = pthread_mutex_lock (&lock->lock);
+ if (err != 0)
+ return err;
+ /* Test whether only readers are currently running, and whether the runcount
+ field will not overflow, and whether no writer is waiting. The latter
+ condition is because POSIX recommends that "write locks shall take
+ precedence over read locks", to avoid "writer starvation". */
+ while (!(lock->runcount + 1 > 0 && lock->waiting_writers_count == 0))
+ {
+ /* This thread has to wait for a while. Enqueue it among the
+ waiting_readers. */
+ err = pthread_cond_wait (&lock->waiting_readers, &lock->lock);
+ if (err != 0)
+ {
+ pthread_mutex_unlock (&lock->lock);
+ return err;
+ }
+ }
+ lock->runcount++;
+ return pthread_mutex_unlock (&lock->lock);
+}
+
+int
+pthread_rwlock_wrlock (pthread_rwlock_t *lock)
+{
+ int err;
+
+ err = pthread_mutex_lock (&lock->lock);
+ if (err != 0)
+ return err;
+ /* Test whether no readers or writers are currently running. */
+ while (!(lock->runcount == 0))
+ {
+ /* This thread has to wait for a while. Enqueue it among the
+ waiting_writers. */
+ lock->waiting_writers_count++;
+ err = pthread_cond_wait (&lock->waiting_writers, &lock->lock);
+ if (err != 0)
+ {
+ lock->waiting_writers_count--;
+ pthread_mutex_unlock (&lock->lock);
+ return err;
+ }
+ lock->waiting_writers_count--;
+ }
+ lock->runcount--; /* runcount becomes -1 */
+ return pthread_mutex_unlock (&lock->lock);
+}
+
+int
+pthread_rwlock_tryrdlock (pthread_rwlock_t *lock)
+{
+ int err;
+
+ err = pthread_mutex_lock (&lock->lock);
+ if (err != 0)
+ return err;
+ /* Test whether only readers are currently running, and whether the runcount
+ field will not overflow, and whether no writer is waiting. The latter
+ condition is because POSIX recommends that "write locks shall take
+ precedence over read locks", to avoid "writer starvation". */
+ if (!(lock->runcount + 1 > 0 && lock->waiting_writers_count == 0))
+ {
+ /* This thread would have to wait for a while. Return instead. */
+ pthread_mutex_unlock (&lock->lock);
+ return EBUSY;
+ }
+ lock->runcount++;
+ return pthread_mutex_unlock (&lock->lock);
+}
+
+int
+pthread_rwlock_trywrlock (pthread_rwlock_t *lock)
+{
+ int err;
+
+ err = pthread_mutex_lock (&lock->lock);
+ if (err != 0)
+ return err;
+ /* Test whether no readers or writers are currently running. */
+ if (!(lock->runcount == 0))
+ {
+ /* This thread would have to wait for a while. Return instead. */
+ pthread_mutex_unlock (&lock->lock);
+ return EBUSY;
+ }
+ lock->runcount--; /* runcount becomes -1 */
+ return pthread_mutex_unlock (&lock->lock);
+}
+
+int
+pthread_rwlock_timedrdlock (pthread_rwlock_t *lock,
+ const struct timespec *abstime)
+{
+ int err;
+
+ err = pthread_mutex_lock (&lock->lock);
+ if (err != 0)
+ return err;
+ /* Test whether only readers are currently running, and whether the runcount
+ field will not overflow, and whether no writer is waiting. The latter
+ condition is because POSIX recommends that "write locks shall take
+ precedence over read locks", to avoid "writer starvation". */
+ while (!(lock->runcount + 1 > 0 && lock->waiting_writers_count == 0))
+ {
+ /* This thread has to wait for a while. Enqueue it among the
+ waiting_readers. */
+ err = pthread_cond_timedwait (&lock->waiting_readers, &lock->lock,
+ abstime);
+ if (err != 0)
+ {
+ pthread_mutex_unlock (&lock->lock);
+ return err;
+ }
+ }
+ lock->runcount++;
+ return pthread_mutex_unlock (&lock->lock);
+}
+
+int
+pthread_rwlock_timedwrlock (pthread_rwlock_t *lock,
+ const struct timespec *abstime)
+{
+ int err;
+
+ err = pthread_mutex_lock (&lock->lock);
+ if (err != 0)
+ return err;
+ /* Test whether no readers or writers are currently running. */
+ while (!(lock->runcount == 0))
+ {
+ /* This thread has to wait for a while. Enqueue it among the
+ waiting_writers. */
+ lock->waiting_writers_count++;
+ err = pthread_cond_timedwait (&lock->waiting_writers, &lock->lock,
+ abstime);
+ if (err != 0)
+ {
+ lock->waiting_writers_count--;
+ pthread_mutex_unlock (&lock->lock);
+ return err;
+ }
+ lock->waiting_writers_count--;
+ }
+ lock->runcount--; /* runcount becomes -1 */
+ return pthread_mutex_unlock (&lock->lock);
+}
+
+int
+pthread_rwlock_unlock (pthread_rwlock_t *lock)
+{
+ int err;
+
+ err = pthread_mutex_lock (&lock->lock);
+ if (err != 0)
+ return err;
+ if (lock->runcount < 0)
+ {
+ /* Drop a writer lock. */
+ if (!(lock->runcount == -1))
+ {
+ pthread_mutex_unlock (&lock->lock);
+ return EINVAL;
+ }
+ lock->runcount = 0;
+ }
+ else
+ {
+ /* Drop a reader lock. */
+ if (!(lock->runcount > 0))
+ {
+ pthread_mutex_unlock (&lock->lock);
+ return EINVAL;
+ }
+ lock->runcount--;
+ }
+ if (lock->runcount == 0)
+ {
+ /* POSIX recommends that "write locks shall take precedence over read
+ locks", to avoid "writer starvation". */
+ if (lock->waiting_writers_count > 0)
+ {
+ /* Wake up one of the waiting writers. */
+ err = pthread_cond_signal (&lock->waiting_writers);
+ if (err != 0)
+ {
+ pthread_mutex_unlock (&lock->lock);
+ return err;
+ }
+ }
+ else
+ {
+ /* Wake up all waiting readers. */
+ err = pthread_cond_broadcast (&lock->waiting_readers);
+ if (err != 0)
+ {
+ pthread_mutex_unlock (&lock->lock);
+ return err;
+ }
+ }
+ }
+ return pthread_mutex_unlock (&lock->lock);
+}
+
+int
+pthread_rwlock_destroy (pthread_rwlock_t *lock)
+{
+ int err;
+
+ err = pthread_mutex_destroy (&lock->lock);
+ if (err != 0)
+ return err;
+ err = pthread_cond_destroy (&lock->waiting_readers);
+ if (err != 0)
+ return err;
+ err = pthread_cond_destroy (&lock->waiting_writers);
+ if (err != 0)
+ return err;
+ return 0;
+}
+
+# elif PTHREAD_RWLOCK_LACKS_TIMEOUT
+
+int
+pthread_rwlock_timedrdlock (pthread_rwlock_t *lock,
+ const struct timespec *abstime)
+{
+ /* Poll the lock's state in regular intervals. Ugh. */
+ for (;;)
+ {
+ int err;
+ struct timeval currtime;
+ unsigned long remaining;
+ struct timespec duration;
+
+ err = pthread_rwlock_tryrdlock (lock);
+ if (err != EBUSY)
+ return err;
+
+ gettimeofday (&currtime, NULL);
+
+ if (currtime.tv_sec > abstime->tv_sec)
+ remaining = 0;
+ else
+ {
+ unsigned long seconds = abstime->tv_sec - currtime.tv_sec;
+ remaining = seconds * 1000000000;
+ if (remaining / 1000000000 != seconds) /* overflow? */
+ remaining = ULONG_MAX;
+ else
+ {
+ long nanoseconds =
+ abstime->tv_nsec - currtime.tv_usec * 1000;
+ if (nanoseconds >= 0)
+ {
+ remaining += nanoseconds;
+ if (remaining < nanoseconds) /* overflow? */
+ remaining = ULONG_MAX;
+ }
+ else
+ {
+ if (remaining >= - nanoseconds)
+ remaining -= (- nanoseconds);
+ else
+ remaining = 0;
+ }
+ }
+ }
+ if (remaining == 0)
+ return ETIMEDOUT;
+
+ /* Sleep 1 ms. */
+ duration.tv_sec = 0;
+ duration.tv_nsec = 1000000;
+ if (duration.tv_nsec > remaining)
+ duration.tv_nsec = remaining;
+ nanosleep (&duration, NULL);
+ }
+}
+
+int
+pthread_rwlock_timedwrlock (pthread_rwlock_t *lock,
+ const struct timespec *abstime)
+{
+ /* Poll the lock's state in regular intervals. Ugh. */
+ for (;;)
+ {
+ int err;
+ struct timeval currtime;
+ unsigned long remaining;
+ struct timespec duration;
+
+ err = pthread_rwlock_trywrlock (lock);
+ if (err != EBUSY)
+ return err;
+
+ gettimeofday (&currtime, NULL);
+
+ if (currtime.tv_sec > abstime->tv_sec)
+ remaining = 0;
+ else
+ {
+ unsigned long seconds = abstime->tv_sec - currtime.tv_sec;
+ remaining = seconds * 1000000000;
+ if (remaining / 1000000000 != seconds) /* overflow? */
+ remaining = ULONG_MAX;
+ else
+ {
+ long nanoseconds =
+ abstime->tv_nsec - currtime.tv_usec * 1000;
+ if (nanoseconds >= 0)
+ {
+ remaining += nanoseconds;
+ if (remaining < nanoseconds) /* overflow? */
+ remaining = ULONG_MAX;
+ }
+ else
+ {
+ if (remaining >= - nanoseconds)
+ remaining -= (- nanoseconds);
+ else
+ remaining = 0;
+ }
+ }
+ }
+ if (remaining == 0)
+ return ETIMEDOUT;
+
+ /* Sleep 1 ms. */
+ duration.tv_sec = 0;
+ duration.tv_nsec = 1000000;
+ if (duration.tv_nsec > remaining)
+ duration.tv_nsec = remaining;
+ nanosleep (&duration, NULL);
+ }
+}
+
+# endif
+
+#else
+/* Provide a dummy implementation for single-threaded applications. */
+
+/* The pthread_rwlock_t is an 'int', representing the number of readers running,
+ or -1 when a writer runs. */
+
+int
+pthread_rwlock_init (pthread_rwlock_t *lock,
+ const pthread_rwlockattr_t *attr _GL_UNUSED)
+{
+ *lock = 0;
+ return 0;
+}
+
+int
+pthread_rwlock_rdlock (pthread_rwlock_t *lock)
+{
+ if (*lock < 0)
+ return EDEADLK;
+ (*lock)++;
+ return 0;
+}
+
+int
+pthread_rwlock_wrlock (pthread_rwlock_t *lock)
+{
+ if (*lock != 0)
+ return EDEADLK;
+ *lock = -1;
+ return 0;
+}
+
+int
+pthread_rwlock_tryrdlock (pthread_rwlock_t *lock)
+{
+ return pthread_rwlock_rdlock (lock);
+}
+
+int
+pthread_rwlock_trywrlock (pthread_rwlock_t *lock)
+{
+ return pthread_rwlock_wrlock (lock);
+}
+
+int
+pthread_rwlock_timedrdlock (pthread_rwlock_t *lock,
+ const struct timespec *abstime _GL_UNUSED)
+{
+ return pthread_rwlock_rdlock (lock);
+}
+
+int
+pthread_rwlock_timedwrlock (pthread_rwlock_t *lock,
+ const struct timespec *abstime _GL_UNUSED)
+{
+ return pthread_rwlock_wrlock (lock);
+}
+
+int
+pthread_rwlock_unlock (pthread_rwlock_t *lock)
+{
+ if (*lock == 0)
+ return EPERM;
+ if (*lock < 0)
+ *lock = 0;
+ else /* *lock > 0 */
+ (*lock)--;
+ return 0;
+}
+
+int
+pthread_rwlock_destroy (pthread_rwlock_t *lock)
+{
+ if (*lock)
+ return EBUSY;
+ return 0;
+}
+
+#endif
--- /dev/null
+# pthread-rwlock.m4 serial 1
+dnl Copyright (C) 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.
+
+AC_DEFUN([gl_PTHREAD_RWLOCK],
+[
+ AC_REQUIRE([gl_PTHREAD_H])
+ AC_REQUIRE([AC_CANONICAL_HOST])
+
+ if { case "$host_os" in mingw*) true;; *) false;; esac; } \
+ && test $gl_threads_api = windows; then
+ dnl Choose function names that don't conflict with the mingw-w64 winpthreads
+ dnl library.
+ REPLACE_PTHREAD_RWLOCK_INIT=1
+ REPLACE_PTHREAD_RWLOCKATTR_INIT=1
+ REPLACE_PTHREAD_RWLOCKATTR_DESTROY=1
+ REPLACE_PTHREAD_RWLOCK_RDLOCK=1
+ REPLACE_PTHREAD_RWLOCK_WRLOCK=1
+ REPLACE_PTHREAD_RWLOCK_TRYRDLOCK=1
+ REPLACE_PTHREAD_RWLOCK_TRYWRLOCK=1
+ REPLACE_PTHREAD_RWLOCK_TIMEDRDLOCK=1
+ REPLACE_PTHREAD_RWLOCK_TIMEDWRLOCK=1
+ REPLACE_PTHREAD_RWLOCK_UNLOCK=1
+ REPLACE_PTHREAD_RWLOCK_DESTROY=1
+ else
+ if test $HAVE_PTHREAD_H = 0; then
+ HAVE_PTHREAD_RWLOCK_INIT=0
+ HAVE_PTHREAD_RWLOCKATTR_INIT=0
+ HAVE_PTHREAD_RWLOCKATTR_DESTROY=0
+ HAVE_PTHREAD_RWLOCK_RDLOCK=0
+ HAVE_PTHREAD_RWLOCK_WRLOCK=0
+ HAVE_PTHREAD_RWLOCK_TRYRDLOCK=0
+ HAVE_PTHREAD_RWLOCK_TRYWRLOCK=0
+ HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK=0
+ HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK=0
+ HAVE_PTHREAD_RWLOCK_UNLOCK=0
+ HAVE_PTHREAD_RWLOCK_DESTROY=0
+ else
+ dnl On Android 4.3, the pthread_rwlock_* functions are declared in
+ dnl <pthread.h> but don't exist in libc.
+ AC_CACHE_CHECK([for pthread_rwlock_init],
+ [gl_cv_func_pthread_rwlock_init],
+ [saved_LIBS="$LIBS"
+ LIBS="$LIBS $LIBMULTITHREAD"
+ AC_LINK_IFELSE(
+ [AC_LANG_SOURCE(
+ [[extern
+ #ifdef __cplusplus
+ "C"
+ #endif
+ int pthread_rwlock_init (void);
+ int main ()
+ {
+ return pthread_rwlock_init ();
+ }
+ ]])],
+ [gl_cv_func_pthread_rwlock_init=yes],
+ [gl_cv_func_pthread_rwlock_init=no])
+ LIBS="$saved_LIBS"
+ ])
+ if test $gl_cv_func_pthread_rwlock_init = no; then
+ REPLACE_PTHREAD_RWLOCK_INIT=1
+ REPLACE_PTHREAD_RWLOCKATTR_INIT=1
+ REPLACE_PTHREAD_RWLOCKATTR_DESTROY=1
+ REPLACE_PTHREAD_RWLOCK_RDLOCK=1
+ REPLACE_PTHREAD_RWLOCK_WRLOCK=1
+ REPLACE_PTHREAD_RWLOCK_TRYRDLOCK=1
+ REPLACE_PTHREAD_RWLOCK_TRYWRLOCK=1
+ REPLACE_PTHREAD_RWLOCK_TIMEDRDLOCK=1
+ REPLACE_PTHREAD_RWLOCK_TIMEDWRLOCK=1
+ REPLACE_PTHREAD_RWLOCK_UNLOCK=1
+ REPLACE_PTHREAD_RWLOCK_DESTROY=1
+ AC_DEFINE([PTHREAD_RWLOCK_UNIMPLEMENTED], [1],
+ [Define if all pthread_rwlock* functions don't exist.])
+ else
+ dnl On Mac OS X 10.5, FreeBSD 5.2.1, OpenBSD 3.8, AIX 5.1, HP-UX 11,
+ dnl IRIX 6.5, Solaris 9, Cygwin, the pthread_rwlock_timed*lock functions
+ dnl don't exist, although the other pthread_rwlock* functions exist.
+ AC_CHECK_DECL([pthread_rwlock_timedrdlock], ,
+ [HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK=0
+ HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK=0
+ AC_DEFINE([PTHREAD_RWLOCK_LACKS_TIMEOUT], [1],
+ [Define if the functions pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock don't exist.])
+ ],
+ [[#include <pthread.h>]])
+ fi
+ fi
+ fi
+])
--- /dev/null
+Description:
+POSIX read-write locks.
+
+Files:
+lib/pthread-rwlock.c
+m4/pthread-rwlock.m4
+
+Depends-on:
+pthread-h
+pthread-cond [test $gl_threads_api = posix && test $REPLACE_PTHREAD_RWLOCK_INIT = 1]
+nanosleep [test $gl_threads_api = posix && test $HAVE_PTHREAD_RWLOCK_INIT = 1 && test $HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK = 0]
+windows-timedrwlock [test $gl_threads_api = windows]
+
+configure.ac:
+gl_PTHREAD_RWLOCK
+if test $HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK = 0 || test $REPLACE_PTHREAD_RWLOCK_TIMEDRDLOCK = 1; then
+ AC_LIBOBJ([pthread-rwlock])
+fi
+gl_PTHREAD_MODULE_INDICATOR([pthread-rwlock])
+
+Makefile.am:
+
+Include:
+<pthread.h>
+
+Link:
+$(LIBTHREAD) or $(LIBMULTITHREAD)
+
+License:
+LGPLv2+
+
+Maintainer:
+all