From bfe9fd008a9b7ac9306d82455918296676e5df64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Kuzn=C3=ADk?= Date: Tue, 5 Sep 2023 09:40:07 +0100 Subject: [PATCH] Pick up configured thread implementation as default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ondřej Kuzník --- lib/common.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/common.c b/lib/common.c index 43b8bb9c..9d3cff02 100644 --- a/lib/common.c +++ b/lib/common.c @@ -64,6 +64,16 @@ #include #endif +#ifdef HAVE_PTHREAD +#include +#endif + +#ifdef HAVE_NT_THREADS +#define _WIN32_WINNT 0x0400 +#include +#include +#endif + static const char *implementation_string = "Cyrus SASL"; static const char *sasl_root_key = SASL_ROOT_KEY; @@ -123,24 +133,53 @@ static int _sasl_global_getopt(void *context, const char ** result, unsigned *len); -/* Intenal mutex functions do as little as possible (no thread protection) */ +#ifdef HAVE_PTHREAD +static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER; +#endif + static void *sasl_mutex_alloc(void) { - return (void *)0x1; +#if defined(HAVE_PTHREAD) + pthread_mutex_t *mutex = sasl_ALLOC(sizeof(pthread_mutex_t)); + if (!mutex) return NULL; + if (pthread_mutex_init(mutex, NULL)) return NULL; + return mutex; +#elif defined(HAVE_NT_THREADS) + return CreateMutex(NULL, 0, NULL); +#else + return (void *)0x1; +#endif } static int sasl_mutex_lock(void *mutex __attribute__((unused))) { +#if defined(HAVE_PTHREAD) + if (pthread_mutex_lock(mutex)) return SASL_FAIL; +#elif defined(HAVE_NT_THREADS) + DWORD status; + if (WaitForSingleObject(mutex, INFINITE) == WAIT_FAILED) + return SASL_FAIL; +#endif return SASL_OK; } static int sasl_mutex_unlock(void *mutex __attribute__((unused))) { +#if defined(HAVE_PTHREAD) + if (pthread_mutex_unlock(mutex)) return SASL_FAIL; +#elif defined(HAVE_NT_THREADS) + ReleaseMutex(mutex); +#endif return SASL_OK; } static void sasl_mutex_free(void *mutex __attribute__((unused))) { +#if defined(HAVE_PTHREAD) + pthread_mutex_destroy(mutex); +#elif defined(HAVE_NT_THREADS) + CloseHandle(mutex); +#endif return; }