From e08989ca11a13c8a09cb0c34f797af5a40753379 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 15 Jul 2019 02:40:57 +0200 Subject: [PATCH] pthread-cond: New module. * lib/pthread.in.h (pthread_cond_destroy, pthread_cond_init, pthread_cond_signal, pthread_cond_wait): Remove inline definitions. * lib/pthread-cond.c: New file. * m4/pthread-cond.m4: New file. * modules/pthread-cond: New file. * doc/posix-functions/pthread_cond_init.texi: Mention the new module. * doc/posix-functions/pthread_condattr_init.texi: Likewise. * doc/posix-functions/pthread_condattr_destroy.texi: Likewise. * doc/posix-functions/pthread_cond_wait.texi: Likewise. * doc/posix-functions/pthread_cond_timedwait.texi: Likewise. * doc/posix-functions/pthread_cond_signal.texi: Likewise. * doc/posix-functions/pthread_cond_broadcast.texi: Likewise. * doc/posix-functions/pthread_cond_destroy.texi: Likewise. --- ChangeLog | 17 ++ .../pthread_cond_broadcast.texi | 10 +- doc/posix-functions/pthread_cond_destroy.texi | 10 +- doc/posix-functions/pthread_cond_init.texi | 10 +- doc/posix-functions/pthread_cond_signal.texi | 10 +- .../pthread_cond_timedwait.texi | 10 +- doc/posix-functions/pthread_cond_wait.texi | 10 +- .../pthread_condattr_destroy.texi | 10 +- .../pthread_condattr_init.texi | 10 +- lib/pthread-cond.c | 199 ++++++++++++++++++ lib/pthread.in.h | 53 ----- m4/pthread-cond.m4 | 36 ++++ modules/pthread-cond | 32 +++ 13 files changed, 332 insertions(+), 85 deletions(-) create mode 100644 lib/pthread-cond.c create mode 100644 m4/pthread-cond.m4 create mode 100644 modules/pthread-cond diff --git a/ChangeLog b/ChangeLog index 1b0786db3c..23d596ebff 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2019-07-14 Bruno Haible + + pthread-cond: New module. + * lib/pthread.in.h (pthread_cond_destroy, pthread_cond_init, + pthread_cond_signal, pthread_cond_wait): Remove inline definitions. + * lib/pthread-cond.c: New file. + * m4/pthread-cond.m4: New file. + * modules/pthread-cond: New file. + * doc/posix-functions/pthread_cond_init.texi: Mention the new module. + * doc/posix-functions/pthread_condattr_init.texi: Likewise. + * doc/posix-functions/pthread_condattr_destroy.texi: Likewise. + * doc/posix-functions/pthread_cond_wait.texi: Likewise. + * doc/posix-functions/pthread_cond_timedwait.texi: Likewise. + * doc/posix-functions/pthread_cond_signal.texi: Likewise. + * doc/posix-functions/pthread_cond_broadcast.texi: Likewise. + * doc/posix-functions/pthread_cond_destroy.texi: Likewise. + 2019-07-14 Bruno Haible pthread-rwlock: New module. diff --git a/doc/posix-functions/pthread_cond_broadcast.texi b/doc/posix-functions/pthread_cond_broadcast.texi index 0cdcafd9e8..37083c2017 100644 --- a/doc/posix-functions/pthread_cond_broadcast.texi +++ b/doc/posix-functions/pthread_cond_broadcast.texi @@ -4,15 +4,17 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_broadcast.html} -Gnulib module: --- +Gnulib module: pthread-cond 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 diff --git a/doc/posix-functions/pthread_cond_destroy.texi b/doc/posix-functions/pthread_cond_destroy.texi index 9e41cc0ece..5718c1b3fc 100644 --- a/doc/posix-functions/pthread_cond_destroy.texi +++ b/doc/posix-functions/pthread_cond_destroy.texi @@ -4,15 +4,17 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_destroy.html} -Gnulib module: --- +Gnulib module: pthread-cond 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 diff --git a/doc/posix-functions/pthread_cond_init.texi b/doc/posix-functions/pthread_cond_init.texi index c0b05a96d5..685274ac16 100644 --- a/doc/posix-functions/pthread_cond_init.texi +++ b/doc/posix-functions/pthread_cond_init.texi @@ -4,15 +4,17 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_init.html} -Gnulib module: --- +Gnulib module: pthread-cond 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, mingw, MSVC 14. @end itemize diff --git a/doc/posix-functions/pthread_cond_signal.texi b/doc/posix-functions/pthread_cond_signal.texi index 9d2e918264..d987eb07a1 100644 --- a/doc/posix-functions/pthread_cond_signal.texi +++ b/doc/posix-functions/pthread_cond_signal.texi @@ -4,15 +4,17 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_signal.html} -Gnulib module: --- +Gnulib module: pthread-cond 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, mingw, MSVC 14. @end itemize diff --git a/doc/posix-functions/pthread_cond_timedwait.texi b/doc/posix-functions/pthread_cond_timedwait.texi index 624952ce63..a48bdc6f39 100644 --- a/doc/posix-functions/pthread_cond_timedwait.texi +++ b/doc/posix-functions/pthread_cond_timedwait.texi @@ -4,15 +4,17 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html} -Gnulib module: --- +Gnulib module: pthread-cond 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 diff --git a/doc/posix-functions/pthread_cond_wait.texi b/doc/posix-functions/pthread_cond_wait.texi index 39e0cdbdcb..3af537232f 100644 --- a/doc/posix-functions/pthread_cond_wait.texi +++ b/doc/posix-functions/pthread_cond_wait.texi @@ -4,15 +4,17 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_wait.html} -Gnulib module: --- +Gnulib module: pthread-cond 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, mingw, MSVC 14. @end itemize diff --git a/doc/posix-functions/pthread_condattr_destroy.texi b/doc/posix-functions/pthread_condattr_destroy.texi index 792bdebd1c..6faa52e65b 100644 --- a/doc/posix-functions/pthread_condattr_destroy.texi +++ b/doc/posix-functions/pthread_condattr_destroy.texi @@ -4,15 +4,17 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_condattr_destroy.html} -Gnulib module: --- +Gnulib module: pthread-cond 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 diff --git a/doc/posix-functions/pthread_condattr_init.texi b/doc/posix-functions/pthread_condattr_init.texi index ba59833406..5de0f73d87 100644 --- a/doc/posix-functions/pthread_condattr_init.texi +++ b/doc/posix-functions/pthread_condattr_init.texi @@ -4,15 +4,17 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_condattr_init.html} -Gnulib module: --- +Gnulib module: pthread-cond 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 diff --git a/lib/pthread-cond.c b/lib/pthread-cond.c new file mode 100644 index 0000000000..cea1d9a91a --- /dev/null +++ b/lib/pthread-cond.c @@ -0,0 +1,199 @@ +/* POSIX condition variables. + Copyright (C) 2010-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 . */ + +/* Written by Paul Eggert, 2010, and Bruno Haible , 2019. */ + +#include + +/* Specification. */ +#include + +#if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS +# include "windows-thread.h" +#else +# include +# include +# include +# include +#endif + +#if ((defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS) || !HAVE_PTHREAD_H + +int +pthread_condattr_init (pthread_condattr_t *attr) +{ + *attr = 0; + return 0; +} + +int +pthread_condattr_destroy (pthread_condattr_t *attr _GL_UNUSED) +{ + return 0; +} + +#endif + +#if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS +/* Use Windows threads. */ + +int +pthread_cond_init (pthread_cond_t *cond, + const pthread_condattr_t *attr _GL_UNUSED) +{ + return glwthread_cond_init (cond); +} + +int +pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex) +{ + return glwthread_cond_wait (cond, mutex, + (int (*) (void *)) pthread_mutex_lock, + (int (*) (void *)) pthread_mutex_unlock); +} + +int +pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, + const struct timespec *abstime) +{ + return glwthread_cond_timedwait (cond, mutex, + (int (*) (void *)) pthread_mutex_lock, + (int (*) (void *)) pthread_mutex_unlock, + abstime); +} + +int +pthread_cond_signal (pthread_cond_t *cond) +{ + return glwthread_cond_signal (cond); +} + +int +pthread_cond_broadcast (pthread_cond_t *cond) +{ + return glwthread_cond_broadcast (cond); +} + +int +pthread_cond_destroy (pthread_cond_t *cond) +{ + return glwthread_cond_destroy (cond); +} + +#elif HAVE_PTHREAD_H +/* Provide workarounds for POSIX threads. */ + +#else +/* Provide a dummy implementation for single-threaded applications. */ + +int +pthread_cond_init (pthread_cond_t *cond _GL_UNUSED, + const pthread_condattr_t *attr _GL_UNUSED) +{ + /* COND is never seriously used. */ + return 0; +} + +int +pthread_cond_wait (pthread_cond_t *cond _GL_UNUSED, + pthread_mutex_t *mutex _GL_UNUSED) +{ + /* No other thread can signal this condition variable. + Wait endlessly. */ + for (;;) + { + struct timespec duration; + + duration.tv_sec = 86400; + duration.tv_usec = 0; + nanosleep (&duration, NULL); + } +} + +int +pthread_cond_timedwait (pthread_cond_t *cond _GL_UNUSED, + pthread_mutex_t *mutex _GL_UNUSED, + const struct timespec *abstime) +{ + /* No other thread can signal this condition variable. + Wait until ABSTIME is reached. */ + for (;;) + { + struct timeval currtime; + unsigned long remaining; + struct timespec duration; + + 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 up to REMAINING ns. */ + duration.tv_sec = remaining / 1000000000; + duration.tv_nsec = remaining % 1000000000; + nanosleep (&duration, NULL); + } +} + +int +pthread_cond_signal (pthread_cond_t *cond _GL_UNUSED) +{ + /* No threads can currently be blocked on COND. */ + return 0; +} + +int +pthread_cond_broadcast (pthread_cond_t *cond _GL_UNUSED) +{ + /* No threads can currently be blocked on COND. */ + return 0; +} + +int +pthread_cond_destroy (pthread_cond_t *cond _GL_UNUSED) +{ + /* COND is never seriously used. */ + return 0; +} + +#endif diff --git a/lib/pthread.in.h b/lib/pthread.in.h index b3e6b596aa..514b77745b 100644 --- a/lib/pthread.in.h +++ b/lib/pthread.in.h @@ -362,59 +362,6 @@ typedef unsigned int pthread_barrierattr_t; #endif -#if ! @HAVE_PTHREAD_T@ - -# if @GNULIB_PTHREAD@ - -# if !GNULIB_defined_pthread_functions - -/* Provide substitutes for the thread functions that should work - adequately on a single-threaded implementation, where - pthread_create always fails. The goal is to let programs compile - on non-pthread hosts with minimal runtime overhead. - - Omit interfaces that have not been analyzed and for which we do not - know what to do, so that they elicit a compile-time error for - now. */ - -_GL_PTHREAD_INLINE int -pthread_cond_destroy (pthread_cond_t *cond) -{ - /* COND is never seriously used. */ - return 0; -} - -_GL_PTHREAD_INLINE int -pthread_cond_init (pthread_cond_t *restrict cond, - pthread_condattr_t const *restrict attr) -{ - /* COND is never seriously used. */ - return 0; -} - -_GL_PTHREAD_INLINE int -pthread_cond_signal (pthread_cond_t *cond) -{ - /* No threads can currently be blocked on COND. */ - return 0; -} - -_GL_PTHREAD_INLINE int -pthread_cond_wait (pthread_cond_t *restrict cond, - pthread_mutex_t *restrict mutex) -{ - /* Properly-written applications never come here. */ - abort (); - return 0; -} - -# define GNULIB_defined_pthread_functions 1 -# endif - -# endif - -#endif - #if ! @HAVE_PTHREAD_SPINLOCK_T@ # if @GNULIB_PTHREAD@ diff --git a/m4/pthread-cond.m4 b/m4/pthread-cond.m4 new file mode 100644 index 0000000000..06cb0dc26c --- /dev/null +++ b/m4/pthread-cond.m4 @@ -0,0 +1,36 @@ +# pthread-cond.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_COND], +[ + 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_COND_INIT=1 + REPLACE_PTHREAD_CONDATTR_INIT=1 + REPLACE_PTHREAD_CONDATTR_DESTROY=1 + REPLACE_PTHREAD_COND_WAIT=1 + REPLACE_PTHREAD_COND_TIMEDWAIT=1 + REPLACE_PTHREAD_COND_SIGNAL=1 + REPLACE_PTHREAD_COND_BROADCAST=1 + REPLACE_PTHREAD_COND_DESTROY=1 + else + if test $HAVE_PTHREAD_H = 0; then + HAVE_PTHREAD_COND_INIT=1 + HAVE_PTHREAD_CONDATTR_INIT=1 + HAVE_PTHREAD_CONDATTR_DESTROY=1 + HAVE_PTHREAD_COND_WAIT=1 + HAVE_PTHREAD_COND_TIMEDWAIT=1 + HAVE_PTHREAD_COND_SIGNAL=1 + HAVE_PTHREAD_COND_BROADCAST=1 + HAVE_PTHREAD_COND_DESTROY=1 + fi + fi +]) diff --git a/modules/pthread-cond b/modules/pthread-cond new file mode 100644 index 0000000000..c07aa6316b --- /dev/null +++ b/modules/pthread-cond @@ -0,0 +1,32 @@ +Description: +POSIX condition variables. + +Files: +lib/pthread-cond.c +m4/pthread-cond.m4 + +Depends-on: +pthread-h +windows-cond [test $gl_threads_api = windows] +pthread-mutex [test $gl_threads_api = windows] + +configure.ac: +gl_PTHREAD_COND +if test $HAVE_PTHREAD_COND_INIT = 0 || test $REPLACE_PTHREAD_COND_INIT = 1; then + AC_LIBOBJ([pthread-cond]) +fi +gl_PTHREAD_MODULE_INDICATOR([pthread-cond]) + +Makefile.am: + +Include: + + +Link: +$(LIBMULTITHREAD) + +License: +LGPLv2+ + +Maintainer: +all -- 2.39.5