+2019-06-20 Bruno Haible <bruno@clisp.org>
+
+ pthread_mutex_timedlock: New module.
+ * lib/pthread.in.h (pthread_mutex_timedlock): New dummy function and
+ new declaration.
+ * lib/pthread_mutex_timedlock.c: New file.
+ * m4/pthread_mutex_timedlock.m4: New file.
+ * m4/pthread.m4 (gl_PTHREAD_CHECK): Don't call AC_LIBOBJ here. Test
+ whether pthread_mutex_timedlock is declared.
+ (gl_PTHREAD_MODULE_INDICATOR): New macro.
+ (gl_PTHREAD_DEFAULTS): Initialize GNULIB_PTHREAD_MUTEX_TIMEDLOCK,
+ HAVE_PTHREAD_MUTEX_TIMEDLOCK.
+ * modules/pthread (configure.ac): Call AC_LIBOBJ here.
+ (Makefile.am): Substitute GNULIB_PTHREAD_MUTEX_TIMEDLOCK,
+ HAVE_PTHREAD_MUTEX_TIMEDLOCK.
+ * modules/pthread_mutex_timedlock: New file.
+ * doc/posix-functions/pthread_mutex_timedlock.texi: Mention the new
+ module.
+
2019-06-20 Bruno Haible <bruno@clisp.org>
thread, lock, cond, tls: Recognize C11 multithreaded applications.
POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_timedlock.html}
-Gnulib module: ---
+Gnulib module: pthread_mutex_timedlock
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+Mac OS X 10.5, FreeBSD 5.2.1, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 9, Cygwin, mingw, MSVC 14, BeOS, Android 4.4.
+But the provided replacement is just a dummy on some of these platforms:
+Minix 3.1.8, HP-UX 11, IRIX 5.3, Solaris 2.4, mingw, MSVC 14, BeOS.
@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, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 9, Cygwin, mingw, MSVC 14, BeOS, Android 4.4.
@end itemize
return pthread_mutex_lock (mutex);
}
+_GL_PTHREAD_INLINE int
+pthread_mutex_timedlock (pthread_mutex_t *mutex, const struct timespec *abstime)
+{
+ /* There is only one thread, so it always gets the lock. This
+ implementation does not support PTHREAD_MUTEX_ERRORCHECK. */
+ return 0;
+}
+
_GL_PTHREAD_INLINE int
pthread_mutex_unlock (pthread_mutex_t *mutex)
{
# define GNULIB_defined_pthread_functions 1
# endif
+#else
+
+# if @GNULIB_PTHREAD_MUTEX_TIMEDLOCK@
+# if !@HAVE_PTHREAD_MUTEX_TIMEDLOCK@
+_GL_FUNCDECL_SYS (pthread_mutex_timedlock, int,
+ (pthread_mutex_t *, const struct timespec *));
+# endif
+_GL_CXXALIAS_SYS (pthread_mutex_timedlock, int,
+ (pthread_mutex_t *, const struct timespec *));
+_GL_CXXALIASWARN (pthread_mutex_timedlock);
+# elif defined GNULIB_POSIXCHECK
+# undef pthread_mutex_timedlock
+# if HAVE_RAW_DECL_PTHREAD_MUTEX_TIMEDLOCK
+_GL_WARN_ON_USE (pthread_mutex_timedlock, "pthread_mutex_timedlock is unportable - "
+ "use gnulib module pthread_mutex_timedlock for portability");
+# endif
+# endif
+
#endif
#if ! @HAVE_PTHREAD_SPINLOCK_T@
--- /dev/null
+/* Lock a mutex, abandoning after a certain time.
+ 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 3 of the License, 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/>. */
+
+#include <config.h>
+
+/* Specification. */
+#include <pthread.h>
+
+#include <errno.h>
+#include <limits.h>
+#include <sys/time.h>
+#include <time.h>
+
+int
+pthread_mutex_timedlock (pthread_mutex_t *mutex, const struct timespec *abstime)
+{
+ /* Poll the mutex's state in regular intervals. Ugh. */
+ /* POSIX says:
+ "Under no circumstance shall the function fail with a timeout if
+ the mutex can be locked immediately. The validity of the abstime
+ parameter need not be checked if the mutex can be locked
+ immediately."
+ Therefore start the loop with a pthread_mutex_trylock call. */
+ for (;;)
+ {
+ int err;
+ struct timeval currtime;
+ unsigned long remaining;
+ struct timespec duration;
+
+ err = pthread_mutex_trylock (mutex);
+ 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);
+ }
+}
-# pthread.m4 serial 10
+# pthread.m4 serial 11
dnl Copyright (C) 2009-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,
HAVE_PTHREAD_SPINLOCK_T=0
fi
- if test $ac_cv_header_pthread_h != yes ||
- test $ac_cv_type_pthread_t != yes ||
- test $ac_cv_type_pthread_spinlock_t != yes; then
+ if test $HAVE_PTHREAD_H = 0 || test $HAVE_PTHREAD_T = 0 || test $HAVE_PTHREAD_SPINLOCK_T = 0; then
PTHREAD_H='pthread.h'
- AC_LIBOBJ([pthread])
elif test $gl_cv_header_pthread_h_pollution = yes; then
- PTHREAD_H=pthread.h
+ PTHREAD_H='pthread.h'
else
PTHREAD_H=
fi
AC_SUBST([PTHREAD_H])
AM_CONDITIONAL([GL_GENERATE_PTHREAD_H], [test -n "$PTHREAD_H"])
+ dnl Check for declarations of anything we want to poison if the
+ dnl corresponding gnulib module is not in use, if it is not common
+ dnl enough to be declared everywhere.
+ gl_WARN_ON_USE_PREPARE([[#include <pthread.h>
+ ]], [pthread_mutex_timedlock])
+
LIB_PTHREAD=
if test $ac_cv_header_pthread_h = yes; then
dnl We cannot use AC_SEARCH_LIBS here, because on OSF/1 5.1 pthread_join
AC_REQUIRE([AC_C_RESTRICT])
])
+AC_DEFUN([gl_PTHREAD_MODULE_INDICATOR],
+[
+ dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
+ AC_REQUIRE([gl_PTHREAD_DEFAULTS])
+ gl_MODULE_INDICATOR_SET_VARIABLE([$1])
+ dnl Define it also as a C macro, for the benefit of the unit tests.
+ gl_MODULE_INDICATOR_FOR_TESTS([$1])
+])
+
AC_DEFUN([gl_PTHREAD_DEFAULTS],
[
+ GNULIB_PTHREAD_MUTEX_TIMEDLOCK=0; AC_SUBST([GNULIB_PTHREAD_MUTEX_TIMEDLOCK])
dnl Assume proper GNU behavior unless another module says otherwise.
- HAVE_PTHREAD_H=1; AC_SUBST([HAVE_PTHREAD_H])
- HAVE_PTHREAD_T=1; AC_SUBST([HAVE_PTHREAD_T])
- HAVE_PTHREAD_SPINLOCK_T=1; AC_SUBST([HAVE_PTHREAD_SPINLOCK_T])
+ HAVE_PTHREAD_H=1; AC_SUBST([HAVE_PTHREAD_H])
+ HAVE_PTHREAD_T=1; AC_SUBST([HAVE_PTHREAD_T])
+ HAVE_PTHREAD_SPINLOCK_T=1; AC_SUBST([HAVE_PTHREAD_SPINLOCK_T])
+ HAVE_PTHREAD_MUTEX_TIMEDLOCK=1; AC_SUBST([HAVE_PTHREAD_MUTEX_TIMEDLOCK])
])
--- /dev/null
+# pthread_mutex_timedlock.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_FUNC_PTHREAD_MUTEX_TIMEDLOCK],
+[
+ AC_REQUIRE([gl_PTHREAD_DEFAULTS])
+
+ AC_CHECK_DECL([pthread_mutex_timedlock], , [HAVE_PTHREAD_MUTEX_TIMEDLOCK=0],
+ [[#include <pthread.h>]])
+])
configure.ac:
gl_PTHREAD_CHECK
+if test $HAVE_PTHREAD_H = 0 || test $HAVE_PTHREAD_T = 0 || test $HAVE_PTHREAD_SPINLOCK_T = 0; then
+ AC_LIBOBJ([pthread])
+fi
gl_MODULE_INDICATOR([pthread])
Makefile.am:
-e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \
-e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \
-e 's|@''NEXT_PTHREAD_H''@|$(NEXT_PTHREAD_H)|g' \
+ -e 's/@''GNULIB_PTHREAD_MUTEX_TIMEDLOCK''@/$(GNULIB_PTHREAD_MUTEX_TIMEDLOCK)/g' \
-e 's|@''HAVE_PTHREAD_T''@|$(HAVE_PTHREAD_T)|g' \
-e 's|@''HAVE_PTHREAD_SPINLOCK_T''@|$(HAVE_PTHREAD_SPINLOCK_T)|g' \
+ -e 's|@''HAVE_PTHREAD_MUTEX_TIMEDLOCK''@|$(HAVE_PTHREAD_MUTEX_TIMEDLOCK)|g' \
< $(srcdir)/pthread.in.h; \
} > $@-t && \
mv $@-t $@
--- /dev/null
+Description:
+Lock a mutex, abandoning after a certain time.
+
+Files:
+lib/pthread_mutex_timedlock.c
+m4/pthread_mutex_timedlock.m4
+
+Depends-on:
+pthread
+nanosleep [test $HAVE_PTHREAD_T = 1 && test $HAVE_PTHREAD_MUTEX_TIMEDLOCK = 0]
+
+configure.ac:
+gl_FUNC_PTHREAD_MUTEX_TIMEDLOCK
+if test $HAVE_PTHREAD_T = 1 && test $HAVE_PTHREAD_MUTEX_TIMEDLOCK = 0; then
+ AC_LIBOBJ([pthread_mutex_timedlock])
+fi
+gl_PTHREAD_MODULE_INDICATOR([pthread_mutex_timedlock])
+
+Makefile.am:
+
+Include:
+<pthread.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+all