]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
pthread-cond: New module.
authorBruno Haible <bruno@clisp.org>
Mon, 15 Jul 2019 00:40:57 +0000 (02:40 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 15 Jul 2019 00:40:57 +0000 (02:40 +0200)
* 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.

13 files changed:
ChangeLog
doc/posix-functions/pthread_cond_broadcast.texi
doc/posix-functions/pthread_cond_destroy.texi
doc/posix-functions/pthread_cond_init.texi
doc/posix-functions/pthread_cond_signal.texi
doc/posix-functions/pthread_cond_timedwait.texi
doc/posix-functions/pthread_cond_wait.texi
doc/posix-functions/pthread_condattr_destroy.texi
doc/posix-functions/pthread_condattr_init.texi
lib/pthread-cond.c [new file with mode: 0644]
lib/pthread.in.h
m4/pthread-cond.m4 [new file with mode: 0644]
modules/pthread-cond [new file with mode: 0644]

index 1b0786db3c02202f154fe6d1155b3a0eace3831d..23d596ebff60135457beff40beba80bb0b475a48 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2019-07-14  Bruno Haible  <bruno@clisp.org>
+
+       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  <bruno@clisp.org>
 
        pthread-rwlock: New module.
index 0cdcafd9e87c321e22e8d4629d7dbc4a626285d8..37083c201736fd77baec31ac7563a1ae6e0fc65a 100644 (file)
@@ -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
index 9e41cc0ece1ea6b854dc9050f17257372d757736..5718c1b3fcdc30c35a92f7e0807b3a877d1e495c 100644 (file)
@@ -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
index c0b05a96d587f31ddf775ded4988992214a83496..685274ac16403fc52c64b77edf24d719cfcb0dc1 100644 (file)
@@ -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
index 9d2e918264fc5315e1c135413782d7ed495fd4b2..d987eb07a1efc62f49f5f06cebac48dd276776a2 100644 (file)
@@ -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
index 624952ce6397d5c87db07490e931baeb783c9089..a48bdc6f3970ef4fb81665c2ba877fa6b62ef33a 100644 (file)
@@ -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
index 39e0cdbdcb41ee3853d2ca9522eb4dc2c8b314d5..3af537232fd2b5b26e209eb32159b9764bc30d2e 100644 (file)
@@ -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
index 792bdebd1cea5bab888b86ff4a578a40363ee4d5..6faa52e65b9dc65cbe310a6e22eba357563818bf 100644 (file)
@@ -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
index ba5983340642e4ae46cb3511bc756fb87de87c7c..5de0f73d87ad4fa08aa90b1d931b4867e54802a0 100644 (file)
@@ -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 (file)
index 0000000..cea1d9a
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.  */
+
+/* Written by Paul Eggert, 2010, and Bruno Haible <bruno@clisp.org>, 2019.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <pthread.h>
+
+#if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
+# include "windows-thread.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_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
index b3e6b596aac23d8da63da2246ef6b52a4ffe70a2..514b77745b957b0005ba65c1a79012179bbde79e 100644 (file)
@@ -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 (file)
index 0000000..06cb0dc
--- /dev/null
@@ -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 (file)
index 0000000..c07aa63
--- /dev/null
@@ -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:
+<pthread.h>
+
+Link:
+$(LIBMULTITHREAD)
+
+License:
+LGPLv2+
+
+Maintainer:
+all