diff options
author | Bruno Haible <bruno@clisp.org> | 2019-06-20 04:30:11 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2019-06-20 04:46:53 +0200 |
commit | 6b4c64f06648b95f942d5428991979b9d8f2c69b (patch) | |
tree | 6f9c6885f65d1f859fc242860802823edf7228a5 | |
parent | 3696a934e60b3c390bc217bf3273e0c5399e5fd4 (diff) | |
download | gnulib-6b4c64f06648b95f942d5428991979b9d8f2c69b.tar.gz |
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.
-rw-r--r-- | ChangeLog | 13 | ||||
-rw-r--r-- | doc/posix-functions/call_once.texi | 8 | ||||
-rw-r--r-- | doc/posix-functions/mtx_destroy.texi | 8 | ||||
-rw-r--r-- | doc/posix-functions/mtx_init.texi | 8 | ||||
-rw-r--r-- | doc/posix-functions/mtx_lock.texi | 8 | ||||
-rw-r--r-- | doc/posix-functions/mtx_timedlock.texi | 8 | ||||
-rw-r--r-- | doc/posix-functions/mtx_trylock.texi | 8 | ||||
-rw-r--r-- | doc/posix-functions/mtx_unlock.texi | 8 | ||||
-rw-r--r-- | lib/mtx.c | 288 | ||||
-rw-r--r-- | modules/mtx | 34 |
10 files changed, 363 insertions, 28 deletions
@@ -1,5 +1,18 @@ 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. * lib/thrd.c: New file. * m4/thrd.m4: New file. diff --git a/doc/posix-functions/call_once.texi b/doc/posix-functions/call_once.texi index c3b10fb5a4..84b9dfe1f2 100644 --- a/doc/posix-functions/call_once.texi +++ b/doc/posix-functions/call_once.texi @@ -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 diff --git a/doc/posix-functions/mtx_destroy.texi b/doc/posix-functions/mtx_destroy.texi index ec8fe870ac..fda5048167 100644 --- a/doc/posix-functions/mtx_destroy.texi +++ b/doc/posix-functions/mtx_destroy.texi @@ -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/doc/posix-functions/mtx_init.texi b/doc/posix-functions/mtx_init.texi index 867f6a259a..297ad34c8c 100644 --- a/doc/posix-functions/mtx_init.texi +++ b/doc/posix-functions/mtx_init.texi @@ -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/doc/posix-functions/mtx_lock.texi b/doc/posix-functions/mtx_lock.texi index 8e43992efe..958589d290 100644 --- a/doc/posix-functions/mtx_lock.texi +++ b/doc/posix-functions/mtx_lock.texi @@ -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/doc/posix-functions/mtx_timedlock.texi b/doc/posix-functions/mtx_timedlock.texi index 31e766ee42..d42f1aee94 100644 --- a/doc/posix-functions/mtx_timedlock.texi +++ b/doc/posix-functions/mtx_timedlock.texi @@ -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/doc/posix-functions/mtx_trylock.texi b/doc/posix-functions/mtx_trylock.texi index 26c38d6dbd..e5beb6485e 100644 --- a/doc/posix-functions/mtx_trylock.texi +++ b/doc/posix-functions/mtx_trylock.texi @@ -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/doc/posix-functions/mtx_unlock.texi b/doc/posix-functions/mtx_unlock.texi index e7779d8e62..f77ea17f21 100644 --- a/doc/posix-functions/mtx_unlock.texi +++ b/doc/posix-functions/mtx_unlock.texi @@ -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 index 0000000000..6acb8ae44c --- /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 index 0000000000..a1d2a8ad4c --- /dev/null +++ b/modules/mtx @@ -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 |