]> Savannah Git Hosting - gnulib.git/commitdiff
mtx: New module.
authorBruno Haible <bruno@clisp.org>
Thu, 20 Jun 2019 02:30:11 +0000 (04:30 +0200)
committerBruno Haible <bruno@clisp.org>
Thu, 20 Jun 2019 02:46:53 +0000 (04:46 +0200)
* lib/mtx.c: New file.
* modules/mtx: New file.
* doc/posix-functions/call_once.texi: Mention the new module.
* doc/posix-functions/mtx_init.texi: Likewise.
* doc/posix-functions/mtx_lock.texi: Likewise.
* doc/posix-functions/mtx_trylock.texi: Likewise.
* doc/posix-functions/mtx_timedlock.texi: Likewise.
* doc/posix-functions/mtx_unlock.texi: Likewise.
* doc/posix-functions/mtx_destroy.texi: Likewise.

ChangeLog
doc/posix-functions/call_once.texi
doc/posix-functions/mtx_destroy.texi
doc/posix-functions/mtx_init.texi
doc/posix-functions/mtx_lock.texi
doc/posix-functions/mtx_timedlock.texi
doc/posix-functions/mtx_trylock.texi
doc/posix-functions/mtx_unlock.texi
lib/mtx.c [new file with mode: 0644]
modules/mtx [new file with mode: 0644]

index 5d96988342f110d51a9c8ba6f1eaffde984e2f04..0e73bd689bdb3039fa17c4619616bd5d7ddeda4d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2019-06-20  Bruno Haible  <bruno@clisp.org>
+
+       mtx: New module.
+       * lib/mtx.c: New file.
+       * modules/mtx: New file.
+       * doc/posix-functions/call_once.texi: Mention the new module.
+       * doc/posix-functions/mtx_init.texi: Likewise.
+       * doc/posix-functions/mtx_lock.texi: Likewise.
+       * doc/posix-functions/mtx_trylock.texi: Likewise.
+       * doc/posix-functions/mtx_timedlock.texi: Likewise.
+       * doc/posix-functions/mtx_unlock.texi: Likewise.
+       * doc/posix-functions/mtx_destroy.texi: Likewise.
+
 2019-06-20  Bruno Haible  <bruno@clisp.org>
 
        thrd: New module.
index c3b10fb5a474315f24394e62b9457b83894a79e3..84b9dfe1f28bf87218d93ca613b75c5fd17a15f3 100644 (file)
@@ -10,15 +10,15 @@ Documentation:@*
 @url{https://www.gnu.org/software/libc/manual/html_node/Call-Once.html}.
 @end ifnotinfo
 
-Gnulib module: ---
+Gnulib module: mtx
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on many platforms:
+glibc 2.27, Mac OS X 10.5, FreeBSD 9.3, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, OSF/1 5.1, Solaris 11.3, Cygwin, mingw, MSVC 14, Interix 3.5, BeOS, Android 9.0.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-This function is missing on many platforms:
-glibc 2.27, Mac OS X 10.5, FreeBSD 9.3, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, OSF/1 5.1, Solaris 11.3, Cygwin, mingw, MSVC 14, Interix 3.5, BeOS, Android 9.0.
 @end itemize
index ec8fe870acdd05736028ac42ebd86618a2e7e1f5..fda504816706c6bd9918102cd3f2cc48040ab49d 100644 (file)
@@ -10,15 +10,15 @@ Documentation:@*
 @url{https://www.gnu.org/software/libc/manual/html_node/ISO-C-Mutexes.html}.
 @end ifnotinfo
 
-Gnulib module: ---
+Gnulib module: mtx
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on many platforms:
+glibc 2.27, Mac OS X 10.5, FreeBSD 9.3, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, OSF/1 5.1, Solaris 11.3, Cygwin, mingw, MSVC 14, Interix 3.5, BeOS, Android 9.0.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-This function is missing on many platforms:
-glibc 2.27, Mac OS X 10.5, FreeBSD 9.3, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, OSF/1 5.1, Solaris 11.3, Cygwin, mingw, MSVC 14, Interix 3.5, BeOS, Android 9.0.
 @end itemize
index 867f6a259a0238f2122ad169b5ee9e1c54de3f1b..297ad34c8c8e24944754ad8c368ae44db21b4d94 100644 (file)
@@ -10,15 +10,15 @@ Documentation:@*
 @url{https://www.gnu.org/software/libc/manual/html_node/ISO-C-Mutexes.html}.
 @end ifnotinfo
 
-Gnulib module: ---
+Gnulib module: mtx
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on many platforms:
+glibc 2.27, Mac OS X 10.5, FreeBSD 9.3, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, OSF/1 5.1, Solaris 11.3, Cygwin, mingw, MSVC 14, Interix 3.5, BeOS, Android 9.0.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-This function is missing on many platforms:
-glibc 2.27, Mac OS X 10.5, FreeBSD 9.3, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, OSF/1 5.1, Solaris 11.3, Cygwin, mingw, MSVC 14, Interix 3.5, BeOS, Android 9.0.
 @end itemize
index 8e43992efe0c8d8d0dd1058d0daab49c745d6c98..958589d290c860bc77c49ecc60b53102eba4aa28 100644 (file)
@@ -10,15 +10,15 @@ Documentation:@*
 @url{https://www.gnu.org/software/libc/manual/html_node/ISO-C-Mutexes.html}.
 @end ifnotinfo
 
-Gnulib module: ---
+Gnulib module: mtx
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on many platforms:
+glibc 2.27, Mac OS X 10.5, FreeBSD 9.3, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, OSF/1 5.1, Solaris 11.3, Cygwin, mingw, MSVC 14, Interix 3.5, BeOS, Android 9.0.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-This function is missing on many platforms:
-glibc 2.27, Mac OS X 10.5, FreeBSD 9.3, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, OSF/1 5.1, Solaris 11.3, Cygwin, mingw, MSVC 14, Interix 3.5, BeOS, Android 9.0.
 @end itemize
index 31e766ee42052bf9b095e2a91f105e6dc256a589..d42f1aee943395538234507be27fd09254509d72 100644 (file)
@@ -10,15 +10,15 @@ Documentation:@*
 @url{https://www.gnu.org/software/libc/manual/html_node/ISO-C-Mutexes.html}.
 @end ifnotinfo
 
-Gnulib module: ---
+Gnulib module: mtx
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on many platforms:
+glibc 2.27, Mac OS X 10.5, FreeBSD 9.3, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, OSF/1 5.1, Solaris 11.3, Cygwin, mingw, MSVC 14, Interix 3.5, BeOS, Android 9.0.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-This function is missing on many platforms:
-glibc 2.27, Mac OS X 10.5, FreeBSD 9.3, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, OSF/1 5.1, Solaris 11.3, Cygwin, mingw, MSVC 14, Interix 3.5, BeOS, Android 9.0.
 @end itemize
index 26c38d6dbdf7d307bc890d54d51829ff1ed35a4a..e5beb6485ec0c00f63df4b4bd9f7d4269b9b9cab 100644 (file)
@@ -10,15 +10,15 @@ Documentation:@*
 @url{https://www.gnu.org/software/libc/manual/html_node/ISO-C-Mutexes.html}.
 @end ifnotinfo
 
-Gnulib module: ---
+Gnulib module: mtx
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on many platforms:
+glibc 2.27, Mac OS X 10.5, FreeBSD 9.3, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, OSF/1 5.1, Solaris 11.3, Cygwin, mingw, MSVC 14, Interix 3.5, BeOS, Android 9.0.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-This function is missing on many platforms:
-glibc 2.27, Mac OS X 10.5, FreeBSD 9.3, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, OSF/1 5.1, Solaris 11.3, Cygwin, mingw, MSVC 14, Interix 3.5, BeOS, Android 9.0.
 @end itemize
index e7779d8e627c021da2da0c930b54b373e3b23f92..f77ea17f21a2cca54c653f0b46c8ccae6a731893 100644 (file)
@@ -10,15 +10,15 @@ Documentation:@*
 @url{https://www.gnu.org/software/libc/manual/html_node/ISO-C-Mutexes.html}.
 @end ifnotinfo
 
-Gnulib module: ---
+Gnulib module: mtx
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on many platforms:
+glibc 2.27, Mac OS X 10.5, FreeBSD 9.3, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, OSF/1 5.1, Solaris 11.3, Cygwin, mingw, MSVC 14, Interix 3.5, BeOS, Android 9.0.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-This function is missing on many platforms:
-glibc 2.27, Mac OS X 10.5, FreeBSD 9.3, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 7.1, HP-UX 11.31, IRIX 6.5, OSF/1 5.1, Solaris 11.3, Cygwin, mingw, MSVC 14, Interix 3.5, BeOS, Android 9.0.
 @end itemize
diff --git a/lib/mtx.c b/lib/mtx.c
new file mode 100644 (file)
index 0000000..6acb8ae
--- /dev/null
+++ b/lib/mtx.c
@@ -0,0 +1,288 @@
+/* ISO C 11 locking in multithreaded situations.
+   Copyright (C) 2005-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>, 2005, 2019.
+   Based on GCC's gthr-posix.h, gthr-posix95.h, gthr-win32.h.  */
+
+#include <config.h>
+
+#include <threads.h>
+
+#include <errno.h>
+
+#if defined _WIN32 && ! defined __CYGWIN__
+/* Use Windows threads.  */
+
+# define WIN32_LEAN_AND_MEAN  /* avoid including junk */
+# include <windows.h>
+
+# include <stdlib.h>
+
+#else
+/* Use POSIX threads.  */
+
+# include <pthread.h>
+
+#endif
+
+#if defined _WIN32 && ! defined __CYGWIN__
+/* Use Windows threads.  */
+
+int
+mtx_init (mtx_t *mutex, int type)
+{
+  switch (type)
+    {
+    case mtx_plain:
+      glwthread_mutex_init (&mutex->u.u_mutex);
+      break;
+    case mtx_plain | mtx_recursive:
+      glwthread_recmutex_init (&mutex->u.u_recmutex);
+      break;
+    case mtx_timed:
+      if (glwthread_timedmutex_init (&mutex->u.u_timedmutex) != 0)
+        return thrd_error;
+      break;
+    case mtx_timed | mtx_recursive:
+      if (glwthread_timedrecmutex_init (&mutex->u.u_timedrecmutex) != 0)
+        return thrd_error;
+      break;
+    default:
+      return thrd_error;
+    }
+  mutex->type = type;
+  return thrd_success;
+}
+
+int
+mtx_lock (mtx_t *mutex)
+{
+  int err;
+
+  switch (mutex->type)
+    {
+    case mtx_plain:
+      err = glwthread_mutex_lock (&mutex->u.u_mutex);
+      break;
+    case mtx_plain | mtx_recursive:
+      err = glwthread_recmutex_lock (&mutex->u.u_recmutex);
+      break;
+    case mtx_timed:
+      err = glwthread_timedmutex_lock (&mutex->u.u_timedmutex);
+      break;
+    case mtx_timed | mtx_recursive:
+      err = glwthread_timedrecmutex_lock (&mutex->u.u_timedrecmutex);
+      break;
+    default:
+      abort ();
+    }
+  return (err == 0 ? thrd_success : thrd_error);
+}
+
+int
+mtx_trylock (mtx_t *mutex)
+{
+  int err;
+
+  switch (mutex->type)
+    {
+    case mtx_plain:
+      err = glwthread_mutex_trylock (&mutex->u.u_mutex);
+      break;
+    case mtx_plain | mtx_recursive:
+      err = glwthread_recmutex_trylock (&mutex->u.u_recmutex);
+      break;
+    case mtx_timed:
+      err = glwthread_timedmutex_trylock (&mutex->u.u_timedmutex);
+      break;
+    case mtx_timed | mtx_recursive:
+      err = glwthread_timedrecmutex_trylock (&mutex->u.u_timedrecmutex);
+      break;
+    default:
+      abort ();
+    }
+  return (err == 0 ? thrd_success : err == EBUSY ? thrd_busy : thrd_error);
+}
+
+int
+mtx_timedlock (mtx_t *mutex, const struct timespec *abstime)
+{
+  int err;
+
+  switch (mutex->type)
+    {
+    case mtx_plain:
+    case mtx_plain | mtx_recursive:
+      return thrd_error;
+    case mtx_timed:
+      err = glwthread_timedmutex_timedlock (&mutex->u.u_timedmutex, abstime);
+      break;
+    case mtx_timed | mtx_recursive:
+      err =
+        glwthread_timedrecmutex_timedlock (&mutex->u.u_timedrecmutex, abstime);
+      break;
+    default:
+      abort ();
+    }
+  return (err == 0 ? thrd_success : err == EBUSY ? thrd_busy : thrd_error);
+}
+
+int
+mtx_unlock (mtx_t *mutex)
+{
+  int err;
+
+  switch (mutex->type)
+    {
+    case mtx_plain:
+      err = glwthread_mutex_unlock (&mutex->u.u_mutex);
+      break;
+    case mtx_plain | mtx_recursive:
+      err = glwthread_recmutex_unlock (&mutex->u.u_recmutex);
+      break;
+    case mtx_timed:
+      err = glwthread_timedmutex_unlock (&mutex->u.u_timedmutex);
+      break;
+    case mtx_timed | mtx_recursive:
+      err = glwthread_timedrecmutex_unlock (&mutex->u.u_timedrecmutex);
+      break;
+    default:
+      abort ();
+    }
+  return (err == 0 ? thrd_success : thrd_error);
+}
+
+void
+mtx_destroy (mtx_t *mutex)
+{
+  switch (mutex->type)
+    {
+    case mtx_plain:
+      glwthread_mutex_destroy (&mutex->u.u_mutex);
+      break;
+    case mtx_plain | mtx_recursive:
+      glwthread_recmutex_destroy (&mutex->u.u_recmutex);
+      break;
+    case mtx_timed:
+      glwthread_timedmutex_destroy (&mutex->u.u_timedmutex);
+      break;
+    case mtx_timed | mtx_recursive:
+      glwthread_timedrecmutex_destroy (&mutex->u.u_timedrecmutex);
+      break;
+    default:
+      abort ();
+    }
+}
+
+void
+call_once (once_flag *flagp, void (*func) (void))
+{
+  glwthread_once (flagp, func);
+}
+
+#else
+/* Use POSIX threads.  */
+
+int
+mtx_init (mtx_t *mutex, int type)
+{
+  switch (type)
+    {
+    case mtx_plain:
+    case mtx_timed:
+    case mtx_plain | mtx_recursive:
+    case mtx_timed | mtx_recursive:
+      break;
+    default:
+      return thrd_error;
+    }
+
+  if ((type & mtx_recursive) != 0)
+    {
+      pthread_mutexattr_t attributes;
+      int err;
+
+      err = pthread_mutexattr_init (&attributes);
+      if (err != 0)
+        return thrd_error;
+      err = pthread_mutexattr_settype (&attributes, PTHREAD_MUTEX_RECURSIVE);
+      if (err != 0)
+        {
+          pthread_mutexattr_destroy (&attributes);
+          return thrd_error;
+        }
+      err = pthread_mutex_init (mutex, &attributes);
+      if (err != 0)
+        {
+          pthread_mutexattr_destroy (&attributes);
+          return thrd_error;
+        }
+      err = pthread_mutexattr_destroy (&attributes);
+      if (err != 0)
+        return thrd_error;
+    }
+  else
+    {
+      int err = pthread_mutex_init (mutex, NULL);
+      if (err != 0)
+        return thrd_error;
+    }
+  return thrd_success;
+}
+
+int
+mtx_lock (mtx_t *mutex)
+{
+  int err = pthread_mutex_lock (mutex);
+  return (err == 0 ? thrd_success : thrd_error);
+}
+
+int
+mtx_trylock (mtx_t *mutex)
+{
+  int err = pthread_mutex_trylock (mutex);
+  return (err == 0 ? thrd_success : err == EBUSY ? thrd_busy : thrd_error);
+}
+
+int
+mtx_timedlock (mtx_t *mutex, const struct timespec *abstime)
+{
+  int err = pthread_mutex_timedlock (mutex, abstime);
+  return (err == 0 ? thrd_success :
+          err == ETIMEDOUT ? thrd_timedout :
+          thrd_error);
+}
+
+int
+mtx_unlock (mtx_t *mutex)
+{
+  int err = pthread_mutex_unlock (mutex);
+  return (err == 0 ? thrd_success : thrd_error);
+}
+
+void
+mtx_destroy (mtx_t *mutex)
+{
+  pthread_mutex_destroy (mutex);
+}
+
+void
+call_once (once_flag *flagp, void (*func) (void))
+{
+  pthread_once (flagp, func);
+}
+
+#endif
diff --git a/modules/mtx b/modules/mtx
new file mode 100644 (file)
index 0000000..a1d2a8a
--- /dev/null
@@ -0,0 +1,34 @@
+Description:
+ISO C 11 mutex functions.
+
+Files:
+lib/mtx.c
+
+Depends-on:
+threads-h
+pthread_mutex_timedlock
+windows-mutex
+windows-recmutex
+windows-timedmutex
+windows-timedrecmutex
+
+configure.ac:
+AC_REQUIRE([gl_THREADS_H])
+if test $HAVE_THREADS_H = 0; then
+  AC_LIBOBJ([mtx])
+fi
+gl_THREADS_MODULE_INDICATOR([mtx])
+
+Makefile.am:
+
+Include:
+<threads.h>
+
+Link:
+$(LTLIBSTDTHREAD) when linking with libtool, $(LIBSTDTHREAD) otherwise
+
+License:
+LGPLv2+
+
+Maintainer:
+all