]> Savannah Git Hosting - gnulib.git/commitdiff
pthread-spin: New module.
authorBruno Haible <bruno@clisp.org>
Mon, 15 Jul 2019 00:41:02 +0000 (02:41 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 15 Jul 2019 00:41:02 +0000 (02:41 +0200)
* lib/pthread.in.h (pthread_spin_init, pthread_spin_destroy,
pthread_spin_lock, pthread_spin_trylock, pthread_spin_unlock): Remove
inline definitions.
* lib/pthread-spin.c: New file.
* m4/pthread-spin.m4: New file.
* modules/pthread-spin: New file.
* doc/posix-functions/pthread_spin_init.texi: Mention the new module.
* doc/posix-functions/pthread_spin_lock.texi: Likewise.
* doc/posix-functions/pthread_spin_trylock.texi: Likewise.
* doc/posix-functions/pthread_spin_unlock.texi: Likewise.
* doc/posix-functions/pthread_spin_destroy.texi: Likewise.

ChangeLog
doc/posix-functions/pthread_spin_destroy.texi
doc/posix-functions/pthread_spin_init.texi
doc/posix-functions/pthread_spin_lock.texi
doc/posix-functions/pthread_spin_trylock.texi
doc/posix-functions/pthread_spin_unlock.texi
lib/pthread-spin.c [new file with mode: 0644]
lib/pthread.in.h
m4/pthread-spin.m4 [new file with mode: 0644]
modules/pthread-spin [new file with mode: 0644]

index e3acb25dbd43f9d26114871886386763b5f09da3..ebe77e15c220d4146422704c505023a301cabab2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2019-07-14  Bruno Haible  <bruno@clisp.org>
+
+       pthread-spin: New module.
+       * lib/pthread.in.h (pthread_spin_init, pthread_spin_destroy,
+       pthread_spin_lock, pthread_spin_trylock, pthread_spin_unlock): Remove
+       inline definitions.
+       * lib/pthread-spin.c: New file.
+       * m4/pthread-spin.m4: New file.
+       * modules/pthread-spin: New file.
+       * doc/posix-functions/pthread_spin_init.texi: Mention the new module.
+       * doc/posix-functions/pthread_spin_lock.texi: Likewise.
+       * doc/posix-functions/pthread_spin_trylock.texi: Likewise.
+       * doc/posix-functions/pthread_spin_unlock.texi: Likewise.
+       * doc/posix-functions/pthread_spin_destroy.texi: Likewise.
+
 2019-07-14  Bruno Haible  <bruno@clisp.org>
 
        pthread-tss: New module.
index bb34331941fa45ccfa1d1d147bae7c5a424605fb..c9a671327c4abc6683fe2bf01e39e223f7b1f259 100644 (file)
@@ -4,15 +4,17 @@
 
 POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_spin_destroy.html}
 
-Gnulib module: ---
+Gnulib module: pthread-spin
 
 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 1.7.9, mingw, MSVC 14, Android 6.0.
+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 1.7.9, mingw, MSVC 14, Android 6.0.
 @end itemize
index cf1f95f7720272d11bea61af916e85329b61bdda..fc511da21412f6033948bc9cc04e9498a88e87ee 100644 (file)
@@ -4,15 +4,17 @@
 
 POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_spin_init.html}
 
-Gnulib module: ---
+Gnulib module: pthread-spin
 
 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 1.7.9, mingw, MSVC 14, Android 6.0.
+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 1.7.9, mingw, MSVC 14, Android 6.0.
 @end itemize
index bbec5a3dacec6b20d3cb713cbb88eadfd52123b5..c5a8087ab8921e5e47e2b03ff041813d588bd7d6 100644 (file)
@@ -4,15 +4,17 @@
 
 POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_spin_lock.html}
 
-Gnulib module: ---
+Gnulib module: pthread-spin
 
 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 1.7.9, mingw, MSVC 14, Android 6.0.
+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 1.7.9, mingw, MSVC 14, Android 6.0.
 @end itemize
index 8dcb7934d65261b4b0f9b3ca3b8813d137fa761d..75b0ff24d254a465de1d390254655141569309d7 100644 (file)
@@ -4,15 +4,17 @@
 
 POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_spin_trylock.html}
 
-Gnulib module: ---
+Gnulib module: pthread-spin
 
 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 1.7.9, mingw, MSVC 14, Android 6.0.
+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 1.7.9, mingw, MSVC 14, Android 6.0.
 @end itemize
index 9ee936a051d685b17ced4316a5d6dcd910390991..e243fd48bdeb5d22a1910f416cfdaf9e87f37289 100644 (file)
@@ -4,15 +4,17 @@
 
 POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_spin_unlock.html}
 
-Gnulib module: ---
+Gnulib module: pthread-spin
 
 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 1.7.9, mingw, MSVC 14, Android 6.0.
+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 1.7.9, mingw, MSVC 14, Android 6.0.
 @end itemize
diff --git a/lib/pthread-spin.c b/lib/pthread-spin.c
new file mode 100644 (file)
index 0000000..0b34109
--- /dev/null
@@ -0,0 +1,182 @@
+/* POSIX spin locks.
+   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-spin.h"
+#endif
+
+#if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
+/* Use Windows threads.  */
+
+int
+pthread_spin_init (pthread_spinlock_t *lock,
+                   int shared_across_processes _GL_UNUSED)
+{
+  glwthread_spin_init (lock);
+  return 0;
+}
+
+int
+pthread_spin_lock (pthread_spinlock_t *lock)
+{
+  return glwthread_spin_lock (lock);
+}
+
+int
+pthread_spin_trylock (pthread_spinlock_t *lock)
+{
+  return glwthread_spin_trylock (lock);
+}
+
+int
+pthread_spin_unlock (pthread_spinlock_t *lock)
+{
+  return glwthread_spin_unlock (lock);
+}
+
+int
+pthread_spin_destroy (pthread_spinlock_t *lock)
+{
+  return glwthread_spin_destroy (lock);
+}
+
+#elif HAVE_PTHREAD_H
+/* Provide workarounds for POSIX threads.  */
+
+/* We don't use the C11 <stdatomic.h> (available in GCC >= 4.9) because it would
+   require to link with -latomic.  */
+
+# if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
+/* Use GCC built-ins (available in GCC >= 4.7) that operate on the first byte
+   of the lock.
+   Documentation:
+   <https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/_005f_005fatomic-Builtins.html>  */
+
+int
+pthread_spin_init (pthread_spinlock_t *lock,
+                   int shared_across_processes _GL_UNUSED)
+{
+  __atomic_clear (lock, __ATOMIC_SEQ_CST);
+  return 0;
+}
+
+int
+pthread_spin_lock (pthread_spinlock_t *lock)
+{
+  while (__atomic_test_and_set (lock, __ATOMIC_SEQ_CST))
+    ;
+  return 0;
+}
+
+int
+pthread_spin_trylock (pthread_spinlock_t *lock)
+{
+  if (__atomic_test_and_set (lock, __ATOMIC_SEQ_CST))
+    return EBUSY;
+  return 0;
+}
+
+int
+pthread_spin_unlock (pthread_spinlock_t *lock)
+{
+  __atomic_clear (lock, __ATOMIC_SEQ_CST);
+  return 0;
+}
+
+int
+pthread_spin_destroy (pthread_spinlock_t *lock)
+{
+  return 0;
+}
+
+# else
+/* Emulate a spin lock through a mutex.  */
+
+int
+pthread_spin_init (pthread_spinlock_t *lock,
+                   int shared_across_processes _GL_UNUSED)
+{
+  return pthread_mutex_init (lock, NULL);
+}
+
+int
+pthread_spin_lock (pthread_spinlock_t *lock)
+{
+  return pthread_mutex_lock (lock);
+}
+
+int
+pthread_spin_trylock (pthread_spinlock_t *lock)
+{
+  return pthread_mutex_trylock (lock);
+}
+
+int
+pthread_spin_unlock (pthread_spinlock_t *lock)
+{
+  return pthread_mutex_unlock (lock);
+}
+
+int
+pthread_spin_destroy (pthread_spinlock_t *lock)
+{
+  return pthread_mutex_destroy (lock);
+}
+
+# endif
+
+#else
+/* Provide a dummy implementation for single-threaded applications.  */
+
+int
+pthread_spin_init (pthread_spinlock_t *lock _GL_UNUSED,
+                   int shared_across_processes _GL_UNUSED)
+{
+  return 0;
+}
+
+int
+pthread_spin_lock (pthread_spinlock_t *lock _GL_UNUSED)
+{
+  return 0;
+}
+
+int
+pthread_spin_trylock (pthread_spinlock_t *lock _GL_UNUSED)
+{
+  return 0;
+}
+
+int
+pthread_spin_unlock (pthread_spinlock_t *lock _GL_UNUSED)
+{
+  return 0;
+}
+
+int
+pthread_spin_destroy (pthread_spinlock_t *lock _GL_UNUSED)
+{
+  return 0;
+}
+
+#endif
index 514b77745b957b0005ba65c1a79012179bbde79e..d0f3ad8bda98d9be10bc9619aa3def329148c285 100644 (file)
@@ -362,49 +362,6 @@ typedef unsigned int pthread_barrierattr_t;
 
 #endif
 
-#if ! @HAVE_PTHREAD_SPINLOCK_T@
-
-# if @GNULIB_PTHREAD@
-
-#  if !GNULIB_defined_pthread_spinlock_functions
-
-_GL_PTHREAD_INLINE int
-pthread_spin_init (pthread_spinlock_t *lock, int pshared)
-{
-  return pthread_mutex_init (lock, NULL);
-}
-
-_GL_PTHREAD_INLINE int
-pthread_spin_destroy (pthread_spinlock_t *lock)
-{
-  return pthread_mutex_destroy (lock);
-}
-
-_GL_PTHREAD_INLINE int
-pthread_spin_lock (pthread_spinlock_t *lock)
-{
-  return pthread_mutex_lock (lock);
-}
-
-_GL_PTHREAD_INLINE int
-pthread_spin_trylock (pthread_spinlock_t *lock)
-{
-  return pthread_mutex_trylock (lock);
-}
-
-_GL_PTHREAD_INLINE int
-pthread_spin_unlock (pthread_spinlock_t *lock)
-{
-  return pthread_mutex_unlock (lock);
-}
-
-#   define GNULIB_defined_pthread_spinlock_functions 1
-#  endif
-
-# endif
-
-#endif
-
 /* =========== Thread functions =========== */
 
 #if @GNULIB_PTHREAD_THREAD@
diff --git a/m4/pthread-spin.m4 b/m4/pthread-spin.m4
new file mode 100644 (file)
index 0000000..9352d5a
--- /dev/null
@@ -0,0 +1,30 @@
+# pthread-spin.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_SPIN],
+[
+  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_SPIN_INIT=1
+    REPLACE_PTHREAD_SPIN_LOCK=1
+    REPLACE_PTHREAD_SPIN_TRYLOCK=1
+    REPLACE_PTHREAD_SPIN_UNLOCK=1
+    REPLACE_PTHREAD_SPIN_DESTROY=1
+  else
+    if test $HAVE_PTHREAD_H = 0; then
+      HAVE_PTHREAD_SPIN_INIT=0
+      HAVE_PTHREAD_SPIN_LOCK=0
+      HAVE_PTHREAD_SPIN_TRYLOCK=0
+      HAVE_PTHREAD_SPIN_UNLOCK=0
+      HAVE_PTHREAD_SPIN_DESTROY=0
+    fi
+  fi
+])
diff --git a/modules/pthread-spin b/modules/pthread-spin
new file mode 100644 (file)
index 0000000..a58b9f7
--- /dev/null
@@ -0,0 +1,31 @@
+Description:
+POSIX spin locks.
+
+Files:
+lib/pthread-spin.c
+m4/pthread-spin.m4
+
+Depends-on:
+pthread-h
+windows-spin    [test $gl_threads_api = windows]
+
+configure.ac:
+gl_PTHREAD_SPIN
+if test $HAVE_PTHREAD_SPIN_INIT = 0 || test $REPLACE_PTHREAD_SPIN_INIT = 1; then
+  AC_LIBOBJ([pthread-spin])
+fi
+gl_PTHREAD_MODULE_INDICATOR([pthread-spin])
+
+Makefile.am:
+
+Include:
+<pthread.h>
+
+Link:
+$(LIBMULTITHREAD)
+
+License:
+LGPLv2+
+
+Maintainer:
+all