From 328b61f0730a544200538945badb927274c0bd2b Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 4 Apr 2017 00:29:45 +1000 Subject: Add win32 implementation of locking --- src/openssl.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index 9c87bc9..92f372c 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -52,10 +52,10 @@ #endif #include /* O_RDONLY O_CLOEXEC open(2) */ #include /* close(2) getpid(2) */ -#include /* pthread_mutex_init(3) pthread_mutex_lock(3) pthread_mutex_unlock(3) */ #ifdef __WIN32 #define EXPORT __declspec (dllexport) #else +#include /* pthread_mutex_init(3) pthread_mutex_lock(3) pthread_mutex_unlock(3) */ #include /* RUSAGE_SELF struct rusage getrusage(2) */ #include /* struct utsname uname(3) */ #include /* dladdr(3) dlopen(3) */ @@ -10414,15 +10414,27 @@ EXPORT int luaopen__openssl_des(lua_State *L) { * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ static struct { +#if _WIN32 + HANDLE *lock; +#else pthread_mutex_t *lock; +#endif int nlock; } mt_state; static void mt_lock(int mode, int type, const char *file NOTUSED, int line NOTUSED) { if (mode & CRYPTO_LOCK) +#if _WIN32 + WaitForSingleObject(mt_state.lock[type], INFINITE); +#else pthread_mutex_lock(&mt_state.lock[type]); +#endif else +#if _WIN32 + ReleaseMutex(mt_state.lock[type]); +#else pthread_mutex_unlock(&mt_state.lock[type]); +#endif } /* mt_lock() */ /* @@ -10448,6 +10460,8 @@ static unsigned long mt_gettid(void) { return id; #elif __NetBSD__ return _lwp_self(); +#elif _WIN32 + return GetCurrentThreadId(); #else /* * pthread_t is an integer on Solaris and Linux, an unsigned integer @@ -10477,9 +10491,18 @@ static int mt_init(void) { } for (i = 0; i < mt_state.nlock; i++) { +#if _WIN32 + if (!(mt_state.lock[i] = CreateMutex(NULL, FALSE, NULL))) { + error = GetLastError(); +#else if ((error = pthread_mutex_init(&mt_state.lock[i], NULL))) { +#endif while (i > 0) { +#if _WIN32 + CloseHandle(mt_state.lock[--i]); +#else pthread_mutex_destroy(&mt_state.lock[--i]); +#endif } free(mt_state.lock); @@ -10511,11 +10534,24 @@ epilog: static void initall(lua_State *L) { - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static int initssl; int error = 0; +#if _WIN32 + static volatile HANDLE mutex = NULL; + if (mutex == NULL) { + HANDLE p; + if (!(p = CreateMutex(NULL, FALSE, NULL))) + auxL_error(L, GetLastError(), "openssl.init"); + if (InterlockedCompareExchangePointer((PVOID*)&mutex, (PVOID)p, NULL) != NULL) + CloseHandle(p); + } + if (WaitForSingleObject(mutex, INFINITE) == WAIT_FAILED) + auxL_error(L, GetLastError(), "openssl.init"); +#else + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&mutex); +#endif #if !OPENSSL_PREREQ(1,1,0) if (!error) @@ -10542,7 +10578,11 @@ static void initall(lua_State *L) { if (!error) error = ex_init(); +#if _WIN32 + ReleaseMutex(mutex); +#else pthread_mutex_unlock(&mutex); +#endif if (error) auxL_error(L, error, "openssl.init"); -- cgit v1.2.3-59-g8ed1b