]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
locks: replace custom mutex wrappers with "glthread/lock.h"
authorDaiki Ueno <ueno@gnu.org>
Sun, 14 Nov 2021 13:04:59 +0000 (14:04 +0100)
committerDaiki Ueno <ueno@gnu.org>
Wed, 17 Nov 2021 06:36:49 +0000 (07:36 +0100)
As Gnulib provides portability wrappers of mutex implementations, we
don't need to provide similar wrappers by ourselves.

Signed-off-by: Daiki Ueno <ueno@gnu.org>
lib/global.c
lib/kx.c
lib/locks.c
lib/locks.h
lib/random.c
lib/system/threads.c

index 37314188358c91ff0a1cc85593f34a4f9df331f8..6684f12b98983a37e9834c9e16ef26e0a4f47a9a 100644 (file)
@@ -232,7 +232,10 @@ static int _gnutls_global_init(unsigned constructor)
        const char* e;
 
        if (!constructor) {
-               GNUTLS_STATIC_MUTEX_LOCK(global_init_mutex);
+               ret = gnutls_static_mutex_lock(&global_init_mutex);
+               if (ret < 0) {
+                       return gnutls_assert_val(ret);
+               }
        }
 
        _gnutls_init++;
@@ -383,7 +386,7 @@ static int _gnutls_global_init(unsigned constructor)
       out:
        _gnutls_init_ret = ret;
        if (!constructor) {
-               GNUTLS_STATIC_MUTEX_UNLOCK(global_init_mutex);
+               (void)gnutls_static_mutex_unlock(&global_init_mutex);
        }
        return ret;
 }
@@ -391,7 +394,9 @@ static int _gnutls_global_init(unsigned constructor)
 static void _gnutls_global_deinit(unsigned destructor)
 {
        if (!destructor) {
-               GNUTLS_STATIC_MUTEX_LOCK(global_init_mutex);
+               if (gnutls_static_mutex_lock(&global_init_mutex) < 0) {
+                       return;
+               }
        }
 
        if (_gnutls_init == 1) {
@@ -441,7 +446,7 @@ static void _gnutls_global_deinit(unsigned destructor)
 
  fail:
        if (!destructor) {
-               GNUTLS_STATIC_MUTEX_UNLOCK(global_init_mutex);
+               (void)gnutls_static_mutex_unlock(&global_init_mutex);
        }
 }
 
index 43a7e295188acaf8808a6f86e7157fce166d8386..d69e1ef0c1a24426bfeb7c1c9d30ddd45145b160 100644 (file)
--- a/lib/kx.c
+++ b/lib/kx.c
@@ -154,7 +154,9 @@ void _gnutls_nss_keylog_write(gnutls_session_t session,
                char client_random_hex[2*GNUTLS_RANDOM_SIZE+1];
                char secret_hex[2*MAX_HASH_SIZE+1];
 
-               GNUTLS_STATIC_MUTEX_LOCK(keylog_mutex);
+               if (gnutls_static_mutex_lock(&keylog_mutex) < 0) {
+                       return;
+               }
                fprintf(keylog, "%s %s %s\n",
                        label,
                        _gnutls_bin2hex(session->security_parameters.
@@ -164,7 +166,7 @@ void _gnutls_nss_keylog_write(gnutls_session_t session,
                        _gnutls_bin2hex(secret, secret_size,
                                        secret_hex, sizeof(secret_hex), NULL));
                fflush(keylog);
-               GNUTLS_STATIC_MUTEX_UNLOCK(keylog_mutex);
+               (void)gnutls_static_mutex_unlock(&keylog_mutex);
        }
 }
 
index d9ea4d77b5f37e88d0b8bcba134b82dc92422740..67f4e1b329f764efa264c74fb9f1dc31e66807e4 100644 (file)
@@ -72,3 +72,21 @@ int ret;
        if (ret < 0)
                _gnutls_debug_log("error in gnutls_global_init(): %s\n", gnutls_strerror(ret));
 }
+
+int
+gnutls_static_mutex_lock(gnutls_static_mutex_t lock)
+{
+       if (unlikely(glthread_lock_lock(lock))) {
+               return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR);
+       }
+       return 0;
+}
+
+int
+gnutls_static_mutex_unlock(gnutls_static_mutex_t lock)
+{
+       if (unlikely(glthread_lock_unlock(lock))) {
+               return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR);
+       }
+       return 0;
+}
index 51a0f56f43ac564090e7435884f588cb2464f2b4..99bf083fd168ad2a32e51ebf464b8ba5eda17913 100644 (file)
 
 #include <gnutls/gnutls.h>
 #include "gnutls_int.h"
-#include <system.h>
+#include "system.h"
 #include "glthread/lock.h"
 
-#ifdef HAVE_STDATOMIC_H
-# include <stdatomic.h>
-#endif
-
 extern mutex_init_func gnutls_mutex_init;
 extern mutex_deinit_func gnutls_mutex_deinit;
 extern mutex_lock_func gnutls_mutex_lock;
 extern mutex_unlock_func gnutls_mutex_unlock;
 
-#if defined(HAVE_WIN32_LOCKS)
-# include <windows.h>
-
-/* Idea based based on comment 2 at:
- * https://stackoverflow.com/questions/3555859/is-it-possible-to-do-static-initialization-of-mutexes-in-windows
+/* If a mutex is initialized with GNUTLS_STATIC_MUTEX, it must be
+ * locked/unlocked with the gnutls_static_mutex_* functions defined
+ * below instead of the above gnutls_mutex_* functions, because the
+ * latter can be replaced with gnutls_global_set_mutex().
  */
-# define GNUTLS_STATIC_MUTEX(mutex) \
-       static CRITICAL_SECTION *mutex = NULL
-
-# define GNUTLS_STATIC_MUTEX_LOCK(mutex) \
-       if (mutex == NULL) { \
-               CRITICAL_SECTION *mutex##tmp = malloc(sizeof(CRITICAL_SECTION)); \
-               InitializeCriticalSection(mutex##tmp); \
-               if (InterlockedCompareExchangePointer((PVOID*)&mutex, (PVOID)mutex##tmp, NULL) != NULL) { \
-                       DeleteCriticalSection(mutex##tmp); \
-                       free(mutex##tmp); \
-               } \
-       } \
-       EnterCriticalSection(mutex)
-
-# define GNUTLS_STATIC_MUTEX_UNLOCK(mutex) \
-       LeaveCriticalSection(mutex)
-
-#elif defined(HAVE_PTHREAD_LOCKS)
-# include <pthread.h>
-# define GNUTLS_STATIC_MUTEX(mutex) \
-       static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER
-
-# define GNUTLS_STATIC_MUTEX_LOCK(mutex) \
-       pthread_mutex_lock(&mutex)
-
-# define GNUTLS_STATIC_MUTEX_UNLOCK(mutex) \
-       pthread_mutex_unlock(&mutex)
-
-#else
-# define GNUTLS_STATIC_MUTEX(mutex)
-# define GNUTLS_STATIC_MUTEX_LOCK(mutex)
-# define GNUTLS_STATIC_MUTEX_UNLOCK(mutex)
-#endif
+#define GNUTLS_STATIC_MUTEX(lock) gl_lock_define_initialized(static, lock)
+typedef gl_lock_t *gnutls_static_mutex_t;
+int gnutls_static_mutex_lock(gnutls_static_mutex_t lock);
+int gnutls_static_mutex_unlock(gnutls_static_mutex_t lock);
 
 #define GNUTLS_STATIC_RWLOCK(rwlock) gl_rwlock_define_initialized(static, rwlock)
 #define GNUTLS_STATIC_RWLOCK_RDLOCK gl_rwlock_rdlock
index 605fc8d51afdfc4e1d0b329f0390e46b0760f3c5..f1abe743c390503b864353ef2e6a2e148b9ac0bc 100644 (file)
@@ -79,9 +79,12 @@ inline static int _gnutls_rnd_init(void)
                        return GNUTLS_E_RANDOM_FAILED;
                }
 
-               GNUTLS_STATIC_MUTEX_LOCK(gnutls_rnd_ctx_list_mutex);
+               ret = gnutls_static_mutex_lock(&gnutls_rnd_ctx_list_mutex);
+               if (ret < 0) {
+                       return gnutls_assert_val(ret);
+               }
                ret = append(gnutls_rnd_ctx);
-               GNUTLS_STATIC_MUTEX_UNLOCK(gnutls_rnd_ctx_list_mutex);
+               (void)gnutls_static_mutex_unlock(&gnutls_rnd_ctx_list_mutex);
                if (ret < 0) {
                        gnutls_assert();
                        _gnutls_rnd_ops.deinit(gnutls_rnd_ctx);
index cbc7c920e9f9c2176b013f047cd5853936a47046..89b5959ee6260d9cc14adae4ed1502a9bbf267e4 100644 (file)
 #include <sys/stat.h>
 #include <sys/types.h>
 
-#ifdef _WIN32
-# include <windows.h>
-# include <wincrypt.h>
-
-#else /* !_WIN32 */
-
-# ifdef HAVE_PTHREAD_LOCKS
-#  include <pthread.h>
-# endif
-
-#endif
+#include "glthread/lock.h"
 
 /* System specific lock function wrappers.
  */
 
 /* Thread stuff */
 
-#ifdef HAVE_WIN32_LOCKS
 static int gnutls_system_mutex_init(void **priv)
 {
-       CRITICAL_SECTION *lock = malloc(sizeof(CRITICAL_SECTION));
+       gl_lock_t *lock = malloc(sizeof(gl_lock_t));
 
-       if (lock == NULL)
-               return GNUTLS_E_MEMORY_ERROR;
-
-       InitializeCriticalSection(lock);
-
-       *priv = lock;
-
-       return 0;
-}
-
-static int gnutls_system_mutex_deinit(void **priv)
-{
-       DeleteCriticalSection((CRITICAL_SECTION *) * priv);
-       free(*priv);
-
-       return 0;
-}
-
-static int gnutls_system_mutex_lock(void **priv)
-{
-       EnterCriticalSection((CRITICAL_SECTION *) * priv);
-       return 0;
-}
-
-static int gnutls_system_mutex_unlock(void **priv)
-{
-       LeaveCriticalSection((CRITICAL_SECTION *) * priv);
-       return 0;
-}
-
-#endif                         /* WIN32_LOCKS */
-
-#ifdef HAVE_PTHREAD_LOCKS
-
-static int gnutls_system_mutex_init(void **priv)
-{
-       pthread_mutex_t *lock = malloc(sizeof(pthread_mutex_t));
-       int ret;
-
-       if (lock == NULL)
+       if (!lock) {
                return GNUTLS_E_MEMORY_ERROR;
+       }
 
-       ret = pthread_mutex_init(lock, NULL);
-       if (ret) {
+       if (glthread_lock_init(lock)) {
                free(lock);
-               gnutls_assert();
-               return GNUTLS_E_LOCKING_ERROR;
+               return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR);
        }
 
        *priv = lock;
-
        return 0;
 }
 
 static int gnutls_system_mutex_deinit(void **priv)
 {
-       pthread_mutex_destroy((pthread_mutex_t *) * priv);
+       if (glthread_lock_destroy((gl_lock_t *) * priv)) {
+               return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR);
+       }
        free(*priv);
        return 0;
 }
 
 static int gnutls_system_mutex_lock(void **priv)
 {
-       if (pthread_mutex_lock((pthread_mutex_t *) * priv)) {
-               gnutls_assert();
-               return GNUTLS_E_LOCKING_ERROR;
+       if (glthread_lock_lock((gl_lock_t *) * priv)) {
+               return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR);
        }
-
        return 0;
 }
 
 static int gnutls_system_mutex_unlock(void **priv)
 {
-       if (pthread_mutex_unlock((pthread_mutex_t *) * priv)) {
-               gnutls_assert();
-               return GNUTLS_E_LOCKING_ERROR;
+       if (glthread_lock_unlock((gl_lock_t *) * priv)) {
+               return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR);
        }
-
-       return 0;
-}
-
-#endif                         /* PTHREAD_LOCKS */
-
-#ifdef HAVE_NO_LOCKS
-
-static int gnutls_system_mutex_init(void **priv)
-{
-       return 0;
-}
-
-static int gnutls_system_mutex_deinit(void **priv)
-{
        return 0;
 }
 
-static int gnutls_system_mutex_lock(void **priv)
-{
-       return 0;
-}
-
-static int gnutls_system_mutex_unlock(void **priv)
-{
-       return 0;
-}
-
-#endif                         /* NO_LOCKS */
-
 mutex_init_func gnutls_mutex_init = gnutls_system_mutex_init;
 mutex_deinit_func gnutls_mutex_deinit = gnutls_system_mutex_deinit;
 mutex_lock_func gnutls_mutex_lock = gnutls_system_mutex_lock;