From 09b64554328445e99a8cc78fc34ea49c2ea2e7f9 Mon Sep 17 00:00:00 2001 From: Andre Heinecke Date: Tue, 8 Nov 2016 15:32:14 +0100 Subject: core: Use gpgrt locking for thread safeness * configure.ac: Require libgpg-error 1.17. No longer check for pthread. * doc/gpgme.texi: Document removed neccessity for thread safe gpgme flavours. * src/sema.h (DEFINE_GLOBAL_LOCK), (DEFINE_STATIC_LOCK, INIT_LOCK, DECLARE_LOCK) (DESTROY_LOCK, LOCK, UNLOCK): Change to gpgrt equivalents. * src/posix-sema.c, src/w32-sema.c: Removed. * src/Makefile.am: Remove libpthread and Update accordingly. * src/ath.c, src/ath.h (ath_mutex_init) (ath_mutex_destroy, ath_mutex_lock, ath_mutex_unlock): Removed. * src/ath.h (ATH_MUTEX_INITIALIZER): Removed. * src/version.c (do_subsystem_inits): sema_subsystem_init is no longer required. * tests/gpg/Makefile.am: Add new threading tests. (t_thread1_LDADD, t_cancel_LDADD): Use just gpgme. * tests/gpg/t-thread-keylist-verify.c, tests/gpg/t-thread-keylist.c: New. * src/gpgme-config.in: Use -lgpgme for thread-model pthread. -- Using gpgrt locks instead of pthread locks removes the neccessity to link pthread directly to gpgme and have a different, thread safe flavor of gpgme. Now gpgme is thread-safe if the conditions mentioned in the doc are met. As the cpp bindings linked against libgpgme and not libgpgme-pthread this fixes threading problems with them. libgpgme-pthread is removed but gpgme-config still supports --thread=pthread for compatibility with find scripts. --- src/Makefile.am | 29 ++------ src/ath-pthread.c | 188 ---------------------------------------------------- src/ath.c | 51 -------------- src/ath.h | 13 ---- src/gpgme-config.in | 8 ++- src/posix-sema.c | 68 ------------------- src/sema.h | 43 +++--------- src/version.c | 1 - src/w32-sema.c | 117 -------------------------------- 9 files changed, 21 insertions(+), 497 deletions(-) delete mode 100644 src/ath-pthread.c delete mode 100644 src/posix-sema.c delete mode 100644 src/w32-sema.c (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index eddd1920..dfe480c9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -28,20 +28,13 @@ nodist_include_HEADERS = gpgme.h bin_PROGRAMS = gpgme-tool -if HAVE_PTHREAD -ltlib_gpgme_pthread = libgpgme-pthread.la -else -ltlib_gpgme_pthread = -endif - if BUILD_W32_GLIB ltlib_gpgme_glib = libgpgme-glib.la else ltlib_gpgme_glib = endif -lib_LTLIBRARIES = libgpgme.la $(ltlib_gpgme_glib) $(ltlib_gpgme_qt) \ - $(ltlib_gpgme_pthread) +lib_LTLIBRARIES = libgpgme.la $(ltlib_gpgme_glib) if HAVE_LD_VERSION_SCRIPT libgpgme_version_script_cmd = -Wl,--version-script=$(srcdir)/libgpgme.vers @@ -50,10 +43,10 @@ libgpgme_version_script_cmd = endif if HAVE_DOSISH_SYSTEM -system_components = w32-util.c w32-sema.c +system_components = w32-util.c system_components_not_extra = w32-io.c else -system_components = ath.h posix-util.c posix-sema.c posix-io.c +system_components = ath.h posix-util.c posix-io.c system_components_not_extra = endif @@ -93,12 +86,10 @@ main_sources = \ engine-spawn.c \ gpgconf.c queryswdb.c \ sema.h priv-io.h $(system_components) sys-util.h dirinfo.c \ - debug.c debug.h gpgme.c version.c error.c - -libgpgme_la_SOURCES = $(main_sources) \ + debug.c debug.h gpgme.c version.c error.c \ ath.h ath.c $(system_components_not_extra) -libgpgme_pthread_la_SOURCES = $(main_sources) \ - ath.h ath-pthread.c $(system_components_not_extra) + +libgpgme_la_SOURCES = $(main_sources) if BUILD_W32_GLIB libgpgme_glib_la_SOURCES = $(main_sources) ath.h ath.c w32-glib-io.c @@ -163,14 +154,6 @@ libgpgme_la_DEPENDENCIES = @LTLIBOBJS@ $(srcdir)/libgpgme.vers $(gpgme_deps) libgpgme_la_LIBADD = $(gpgme_res) @LIBASSUAN_LIBS@ @LTLIBOBJS@ \ @GPG_ERROR_LIBS@ -libgpgme_pthread_la_LDFLAGS = \ - $(no_undefined) $(export_symbols) $(extra_ltoptions) \ - $(libgpgme_version_script_cmd) -version-info \ - @LIBGPGME_LT_CURRENT@:@LIBGPGME_LT_REVISION@:@LIBGPGME_LT_AGE@ -libgpgme_pthread_la_DEPENDENCIES = @LTLIBOBJS@ $(srcdir)/libgpgme.vers -libgpgme_pthread_la_LIBADD = $(gpgme_res) @LIBASSUAN_LIBS@ @LTLIBOBJS@ \ - -lpthread @GPG_ERROR_LIBS@ - if BUILD_W32_GLIB libgpgme_glib_la_LDFLAGS = \ $(no_undefined) $(export_symbols) $(extra_ltoptions) \ diff --git a/src/ath-pthread.c b/src/ath-pthread.c deleted file mode 100644 index 47b38ee0..00000000 --- a/src/ath-pthread.c +++ /dev/null @@ -1,188 +0,0 @@ -/* ath-pthread.c - pthread module for self-adapting thread-safeness library - Copyright (C) 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - 02111-1307, USA. */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#ifdef HAVE_UNISTD_H -# include -#endif -#ifdef HAVE_SYS_SELECT_H -# include -#else -# ifdef HAVE_SYS_TIME_H -# include -# endif -#endif -#include -#include - -#include - -#include "gpgme.h" - -#include "ath.h" - - -/* The lock we take while checking for lazy lock initialization. */ -static pthread_mutex_t check_init_lock = PTHREAD_MUTEX_INITIALIZER; - -/* Initialize the mutex *PRIV. If JUST_CHECK is true, only do this if - it is not already initialized. */ -static int -mutex_pthread_init (ath_mutex_t *priv, int just_check) -{ - int err = 0; - - if (just_check) - pthread_mutex_lock (&check_init_lock); - if (!*priv || !just_check) - { - pthread_mutex_t *lock = malloc (sizeof (pthread_mutex_t)); - if (!lock) - err = ENOMEM; - if (!err) - { - err = pthread_mutex_init (lock, NULL); - if (err) - free (lock); - else - *priv = (ath_mutex_t) lock; - } - } - if (just_check) - pthread_mutex_unlock (&check_init_lock); - return err; -} - - -void -ath_init (void) -{ - /* Nothing to do. */ -} - - -uintptr_t -ath_self (void) -{ - return (uintptr_t) pthread_self (); -} - - -int -ath_mutex_init (ath_mutex_t *lock) -{ - return mutex_pthread_init (lock, 0); -} - - -int -ath_mutex_destroy (ath_mutex_t *lock) -{ - int err = mutex_pthread_init (lock, 1); - if (!err) - { - err = pthread_mutex_destroy ((pthread_mutex_t *) *lock); - free (*lock); - } - return err; -} - - -int -ath_mutex_lock (ath_mutex_t *lock) -{ - int ret = mutex_pthread_init (lock, 1); - if (ret) - return ret; - - return pthread_mutex_lock ((pthread_mutex_t *) *lock); -} - - -int -ath_mutex_unlock (ath_mutex_t *lock) -{ - int ret = mutex_pthread_init (lock, 1); - if (ret) - return ret; - - return pthread_mutex_unlock ((pthread_mutex_t *) *lock); -} - - -gpgme_ssize_t -ath_read (int fd, void *buf, size_t nbytes) -{ - return read (fd, buf, nbytes); -} - - -gpgme_ssize_t -ath_write (int fd, const void *buf, size_t nbytes) -{ - return write (fd, buf, nbytes); -} - - -gpgme_ssize_t -ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset, - struct timeval *timeout) -{ - return select (nfd, rset, wset, eset, timeout); -} - - -gpgme_ssize_t -ath_waitpid (pid_t pid, int *status, int options) -{ - return waitpid (pid, status, options); -} - - -int -ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr) -{ - return accept (s, addr, length_ptr); -} - - -int -ath_connect (int s, const struct sockaddr *addr, socklen_t length) -{ - return connect (s, addr, length); -} - -int -ath_sendmsg (int s, const struct msghdr *msg, int flags) -{ - return sendmsg (s, msg, flags); -} - - -int -ath_recvmsg (int s, struct msghdr *msg, int flags) -{ - return recvmsg (s, msg, flags); -} diff --git a/src/ath.c b/src/ath.c index ddd8a872..6b4667ef 100644 --- a/src/ath.c +++ b/src/ath.c @@ -49,11 +49,6 @@ #include "ath.h" -#define MUTEX_UNLOCKED ((ath_mutex_t) 0) -#define MUTEX_LOCKED ((ath_mutex_t) 1) -#define MUTEX_DESTROYED ((ath_mutex_t) 2) - - #ifdef HAVE_W32_SYSTEM #include uintptr_t @@ -80,52 +75,6 @@ ath_self (void) #endif -int -ath_mutex_init (ath_mutex_t *lock) -{ -#ifndef NDEBUG - *lock = MUTEX_UNLOCKED; -#endif - return 0; -} - - -int -ath_mutex_destroy (ath_mutex_t *lock) -{ -#ifndef NDEBUG - assert (*lock == MUTEX_UNLOCKED); - - *lock = MUTEX_DESTROYED; -#endif - return 0; -} - - -int -ath_mutex_lock (ath_mutex_t *lock) -{ -#ifndef NDEBUG - assert (*lock == MUTEX_UNLOCKED); - - *lock = MUTEX_LOCKED; -#endif - return 0; -} - - -int -ath_mutex_unlock (ath_mutex_t *lock) -{ -#ifndef NDEBUG - assert (*lock == MUTEX_LOCKED); - - *lock = MUTEX_UNLOCKED; -#endif - return 0; -} - - gpgme_ssize_t ath_read (int fd, void *buf, size_t nbytes) { diff --git a/src/ath.h b/src/ath.h index 8eb9eb95..a1be9e58 100644 --- a/src/ath.h +++ b/src/ath.h @@ -60,10 +60,6 @@ #define _ATH_PREFIX1(x,y) x ## y #define _ATH_PREFIX2(x,y) _ATH_PREFIX1(x,y) #define _ATH_PREFIX(x) _ATH_PREFIX2(_ATH_EXT_SYM_PREFIX,x) -#define ath_mutex_init _ATH_PREFIX(ath_mutex_init) -#define ath_mutex_destroy _ATH_PREFIX(ath_mutex_destroy) -#define ath_mutex_lock _ATH_PREFIX(ath_mutex_lock) -#define ath_mutex_unlock _ATH_PREFIX(ath_mutex_unlock) #define ath_read _ATH_PREFIX(ath_read) #define ath_write _ATH_PREFIX(ath_write) #define ath_select _ATH_PREFIX(ath_select) @@ -75,17 +71,8 @@ #endif -typedef void *ath_mutex_t; -#define ATH_MUTEX_INITIALIZER 0; - uintptr_t ath_self (void); -/* Functions for mutual exclusion. */ -int ath_mutex_init (ath_mutex_t *mutex); -int ath_mutex_destroy (ath_mutex_t *mutex); -int ath_mutex_lock (ath_mutex_t *mutex); -int ath_mutex_unlock (ath_mutex_t *mutex); - /* Replacement for the POSIX functions, which can be used to allow other (user-level) threads to run. */ gpgme_ssize_t ath_read (int fd, void *buf, size_t nbytes); diff --git a/src/gpgme-config.in b/src/gpgme-config.in index 0d9fda21..a4d152e1 100644 --- a/src/gpgme-config.in +++ b/src/gpgme-config.in @@ -32,7 +32,9 @@ gpg_error_libs="@GPG_ERROR_LIBS@" # Configure thread packages. thread_modules="" -@HAVE_PTHREAD_TRUE@thread_modules="$thread_modules pthread" +# For compatibility we keep proving the +# thread modules variable. +thread_modules="$thread_modules pthread" libs_pthread="-lpthread" cflags_pthread="" @@ -50,7 +52,6 @@ usage() cat < -#endif -#include -#include -#include -#include -#include -#include -#include -#ifdef HAVE_UNISTD_H -# include -#endif -#ifdef HAVE_SYS_TIME_H -# include -#endif -#ifdef HAVE_SYS_TYPES_H -# include -#endif - -#include "util.h" -#include "sema.h" -#include "ath.h" - -void -_gpgme_sema_subsystem_init () -{ -} - -void -_gpgme_sema_cs_enter (struct critsect_s *s) -{ - _gpgme_ath_mutex_lock (&s->priv); -} - -void -_gpgme_sema_cs_leave (struct critsect_s *s) -{ - _gpgme_ath_mutex_unlock (&s->priv); -} - -void -_gpgme_sema_cs_destroy (struct critsect_s *s) -{ - _gpgme_ath_mutex_destroy (&s->priv); - s->priv = NULL; -} diff --git a/src/sema.h b/src/sema.h index 4b7c0af6..5b0d53dd 100644 --- a/src/sema.h +++ b/src/sema.h @@ -22,46 +22,23 @@ #ifndef SEMA_H #define SEMA_H -struct critsect_s -{ - const char *name; - void *priv; -}; +#include #define DEFINE_GLOBAL_LOCK(name) \ - struct critsect_s name = { #name, NULL } + gpgrt_lock_t name = GPGRT_LOCK_INITIALIZER + #define DEFINE_STATIC_LOCK(name) \ - static struct critsect_s name = { #name, NULL } + static gpgrt_lock_t name = GPGRT_LOCK_INITIALIZER -#define DECLARE_LOCK(name) \ - struct critsect_s name -#define INIT_LOCK(a) \ - do \ - { \ - (a).name = #a; \ - (a).priv = NULL; \ - } \ - while (0) -#define DESTROY_LOCK(name) _gpgme_sema_cs_destroy (&(name)) +#define INIT_LOCK(name) \ + name = (gpgrt_lock_t) GPGRT_LOCK_INITIALIZER +#define DECLARE_LOCK(name) gpgrt_lock_t name -#define LOCK(name) \ - do \ - { \ - _gpgme_sema_cs_enter (&(name)); \ - } \ - while (0) +#define DESTROY_LOCK(name) gpgrt_lock_destroy(&name) -#define UNLOCK(name) \ - do \ - { \ - _gpgme_sema_cs_leave (&(name)); \ - } \ - while (0) +#define LOCK(name) gpgrt_lock_lock(&name) -void _gpgme_sema_subsystem_init (void); -void _gpgme_sema_cs_enter (struct critsect_s *s); -void _gpgme_sema_cs_leave (struct critsect_s *s); -void _gpgme_sema_cs_destroy (struct critsect_s *s); +#define UNLOCK(name) gpgrt_lock_unlock(&name) #endif /* SEMA_H */ diff --git a/src/version.c b/src/version.c index 8bc898ff..99698fa7 100644 --- a/src/version.c +++ b/src/version.c @@ -74,7 +74,6 @@ do_subsystem_inits (void) } #endif - _gpgme_sema_subsystem_init (); _gpgme_debug_subsystem_init (); _gpgme_io_subsystem_init (); _gpgme_status_init (); diff --git a/src/w32-sema.c b/src/w32-sema.c deleted file mode 100644 index 648a6bbd..00000000 --- a/src/w32-sema.c +++ /dev/null @@ -1,117 +0,0 @@ -/* w32-sema.c - Copyright (C) 2001 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2004, 2007 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - 02111-1307, USA. */ - -#ifdef HAVE_CONFIG_H -#include -#endif -#include -#include -#include -#include -#include -#include -#ifdef HAVE_SYS_TIME_H -# include -#endif -#ifdef HAVE_SYS_TYPES_H -# include -#endif -#include - -#include "util.h" -#include "sema.h" -#include "debug.h" - -static void -sema_fatal (const char *text) -{ - fprintf (stderr, "sema.c: %s\n", text); - abort (); -} - - -static void -critsect_init (struct critsect_s *s) -{ - CRITICAL_SECTION *mp; - static CRITICAL_SECTION init_lock; - static int initialized; - - if (!initialized) { - /* The very first time we call this function, we assume that - only one thread is running, so that we can bootstrap the - semaphore code. */ - InitializeCriticalSection (&init_lock); - initialized = 1; - } - if (!s) - return; /* we just want to initialize ourself */ - - /* first test whether it is really not initialized */ - EnterCriticalSection (&init_lock); - if ( s->priv ) { - LeaveCriticalSection (&init_lock); - return; - } - /* now init it */ - mp = malloc ( sizeof *mp ); - if (!mp) { - LeaveCriticalSection (&init_lock); - sema_fatal ("out of core while creating critical section lock"); - } - InitializeCriticalSection (mp); - s->priv = mp; - LeaveCriticalSection (&init_lock); -} - -void -_gpgme_sema_subsystem_init () -{ - /* fixme: we should check that there is only one thread running */ - critsect_init (NULL); -} - - -void -_gpgme_sema_cs_enter ( struct critsect_s *s ) -{ - if (!s->priv) - critsect_init (s); - EnterCriticalSection ( (CRITICAL_SECTION*)s->priv ); -} - -void -_gpgme_sema_cs_leave (struct critsect_s *s) -{ - if (!s->priv) - critsect_init (s); - LeaveCriticalSection ((CRITICAL_SECTION*)s->priv); -} - -void -_gpgme_sema_cs_destroy ( struct critsect_s *s ) -{ - if (s && s->priv) { - DeleteCriticalSection ((CRITICAL_SECTION*)s->priv); - free (s->priv); - s->priv = NULL; - } -} -- cgit v1.2.3