diff options
author | Alan Antonuk <alan.antonuk@gmail.com> | 2017-02-20 12:51:24 -0800 |
---|---|---|
committer | Alan Antonuk <alan.antonuk@gmail.com> | 2017-02-20 13:00:56 -0800 |
commit | 205b271e745478749ebec921f084b387c622c4d8 (patch) | |
tree | 68b29561a828016225345bf02eb06364218e1d1c | |
parent | ab256d1f23785845aa4514db6ca75982b590e345 (diff) | |
download | rabbitmq-c-205b271e745478749ebec921f084b387c622c4d8.tar.gz |
Win32: use SRWLOCK instead of CRITIAL_SECTION
SRWLOCK allows for static-initialization of the lock, matching the
semantics of pthread_mutex that is used in OpenSSL bits, simplifying the
code somewhat.
Note that this API only became available in Windows Vista and newer.
Given Windows XP and Windows Server 2003 are no longer supported by
Microsoft, using this newer API is an acceptable tradeoff.
-rw-r--r-- | librabbitmq/amqp_openssl.c | 21 | ||||
-rw-r--r-- | librabbitmq/win32/threads.c | 33 | ||||
-rw-r--r-- | librabbitmq/win32/threads.h | 8 |
3 files changed, 16 insertions, 46 deletions
diff --git a/librabbitmq/amqp_openssl.c b/librabbitmq/amqp_openssl.c index 333c7ed..d7ff38d 100644 --- a/librabbitmq/amqp_openssl.c +++ b/librabbitmq/amqp_openssl.c @@ -59,12 +59,8 @@ static amqp_boolean_t openssl_initialized = 0; static unsigned long amqp_ssl_threadid_callback(void); static void amqp_ssl_locking_callback(int mode, int n, const char *file, int line); -#ifdef _WIN32 -static long win32_create_mutex = 0; -static pthread_mutex_t openssl_init_mutex = NULL; -#else static pthread_mutex_t openssl_init_mutex = PTHREAD_MUTEX_INITIALIZER; -#endif + static pthread_mutex_t *amqp_openssl_lockarray = NULL; #endif /* ENABLE_THREAD_SAFETY */ @@ -600,21 +596,6 @@ static int initialize_openssl(void) { #ifdef ENABLE_THREAD_SAFETY -#ifdef _WIN32 - /* No such thing as PTHREAD_INITIALIZE_MUTEX macro on Win32, so we use this */ - if (NULL == openssl_init_mutex) { - while (InterlockedExchange(&win32_create_mutex, 1) == 1) - /* Loop, someone else is holding this lock */ ; - - if (NULL == openssl_init_mutex) { - if (pthread_mutex_init(&openssl_init_mutex, NULL)) { - return -1; - } - } - InterlockedExchange(&win32_create_mutex, 0); - } -#endif /* _WIN32 */ - if (pthread_mutex_lock(&openssl_init_mutex)) { return -1; } diff --git a/librabbitmq/win32/threads.c b/librabbitmq/win32/threads.c index 60f00b3..49257cc 100644 --- a/librabbitmq/win32/threads.c +++ b/librabbitmq/win32/threads.c @@ -25,41 +25,28 @@ #include <stdlib.h> -DWORD -pthread_self(void) -{ - return GetCurrentThreadId(); -} +DWORD pthread_self(void) { return GetCurrentThreadId(); } -int -pthread_mutex_init(pthread_mutex_t *mutex, void *attr) -{ - *mutex = malloc(sizeof(CRITICAL_SECTION)); - if (!*mutex) { +int pthread_mutex_init(pthread_mutex_t *mutex, void *attr) { + if (!mutex) { return 1; } - InitializeCriticalSection(*mutex); + InitializeSRWLock(mutex); return 0; } -int -pthread_mutex_lock(pthread_mutex_t *mutex) -{ - if (!*mutex) { +int pthread_mutex_lock(pthread_mutex_t *mutex) { + if (!mutex) { return 1; } - - EnterCriticalSection(*mutex); + AcquireSRWLockExclusive(mutex); return 0; } -int -pthread_mutex_unlock(pthread_mutex_t *mutex) -{ - if (!*mutex) { +int pthread_mutex_unlock(pthread_mutex_t *mutex) { + if (!mutex) { return 1; } - - LeaveCriticalSection(*mutex); + ReleaseSRWLockExclusive(mutex); return 0; } diff --git a/librabbitmq/win32/threads.h b/librabbitmq/win32/threads.h index 1d1c53f..9a17598 100644 --- a/librabbitmq/win32/threads.h +++ b/librabbitmq/win32/threads.h @@ -29,19 +29,21 @@ #define AMQP_THREAD_H #ifndef WINVER -# define WINVER 0x0502 + /* Windows Vista or newer */ +# define WINVER 0x0600 #endif #ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN #endif #include <windows.h> -typedef CRITICAL_SECTION *pthread_mutex_t; -typedef int pthread_once_t; +typedef SRWLOCK pthread_mutex_t; +#define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT; DWORD pthread_self(void); int pthread_mutex_init(pthread_mutex_t *, void *attr); int pthread_mutex_lock(pthread_mutex_t *); int pthread_mutex_unlock(pthread_mutex_t *); + #endif /* AMQP_THREAD_H */ |