Skip to content

Commit

Permalink
Pick up configured thread implementation as default
Browse files Browse the repository at this point in the history
Signed-off-by: Ondřej Kuzník <[email protected]>
  • Loading branch information
mistotebe committed Sep 18, 2023
1 parent 6f490d1 commit bfe9fd0
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions lib/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@
#include <unistd.h>
#endif

#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif

#ifdef HAVE_NT_THREADS
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <process.h>
#endif

static const char *implementation_string = "Cyrus SASL";
static const char *sasl_root_key = SASL_ROOT_KEY;

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit bfe9fd0

Please sign in to comment.