diff options
author | daurnimator <quae@daurnimator.com> | 2017-04-04 00:29:45 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-09-02 04:09:47 +1000 |
commit | 328b61f0730a544200538945badb927274c0bd2b (patch) | |
tree | 49d77d81095f8208e3f66ed15589b40420a0fe07 | |
parent | 5c8fd3906e6bac0914c6968a33719cc1bf5cdbf0 (diff) | |
download | luaossl-328b61f0730a544200538945badb927274c0bd2b.tar.gz luaossl-328b61f0730a544200538945badb927274c0bd2b.tar.bz2 luaossl-328b61f0730a544200538945badb927274c0bd2b.zip |
Add win32 implementation of locking
-rw-r--r-- | src/openssl.c | 44 |
1 files 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 <fcntl.h> /* O_RDONLY O_CLOEXEC open(2) */ #include <unistd.h> /* close(2) getpid(2) */ -#include <pthread.h> /* pthread_mutex_init(3) pthread_mutex_lock(3) pthread_mutex_unlock(3) */ #ifdef __WIN32 #define EXPORT __declspec (dllexport) #else +#include <pthread.h> /* pthread_mutex_init(3) pthread_mutex_lock(3) pthread_mutex_unlock(3) */ #include <sys/resource.h> /* RUSAGE_SELF struct rusage getrusage(2) */ #include <sys/utsname.h> /* struct utsname uname(3) */ #include <dlfcn.h> /* 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"); |