diff options
author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2013-11-30 11:50:22 +0100 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2013-11-30 11:50:28 +0100 |
commit | 18ab6ffcb476062379ff46700edf0aaf56c7e240 (patch) | |
tree | a88182a1ed3d1bcdde95939192b75aa64206f254 /lib/locks.h | |
parent | d62af18c996a09a115e6610650be37e5e4daef57 (diff) | |
download | gnutls-18ab6ffcb476062379ff46700edf0aaf56c7e240.tar.gz |
gnutls_global_init() and gnutls_global_deinit() are thread-safe.
They utilize static mutex initializers.
Diffstat (limited to 'lib/locks.h')
-rw-r--r-- | lib/locks.h | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/locks.h b/lib/locks.h index ba190a1fcd..68d73e4ae9 100644 --- a/lib/locks.h +++ b/lib/locks.h @@ -25,10 +25,59 @@ #include <gnutls/gnutls.h> #include <gnutls_int.h> +#include <system.h> 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: + * http://stackoverflow.com/questions/3555859/is-it-possible-to-do-static-initialization-of-mutexes-in-windows + */ +# 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) + +# define GNUTLS_STATIC_MUTEX_DEINIT(mutex) \ + DeleteCriticalSection(mutex); \ + free(mutex); mutex = NULL + +#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) + +# define GNUTLS_STATIC_MUTEX_DEINIT(mutex) \ + pthread_mutex_destroy(&mutex) + +#else +# define GNUTLS_STATIC_MUTEX(mutex) +# define GNUTLS_STATIC_MUTEX_LOCK(mutex) +# define GNUTLS_STATIC_MUTEX_UNLOCK(mutex) +# define GNUTLS_STATIC_MUTEX_DEINIT(mutex) +#endif + #endif |