diff options
author | Daniel Cohen Gindi <danielgindi@gmail.com> | 2014-08-29 11:00:26 +0300 |
---|---|---|
committer | Johannes Schindelin <johannes.schindelin@gmx.de> | 2014-09-02 16:43:17 +0200 |
commit | 54b2400dd761f484c9a5ac4cb63ac63fa1f6b503 (patch) | |
tree | 70764988d22bc5eb501f04ed4d1188b2e16f1b8b /libvncclient/tls_openssl.c | |
parent | dfdb11144afc3ef1ea32f0300696471767d640d6 (diff) | |
download | libtdevnc-54b2400dd761f484c9a5ac4cb63ac63fa1f6b503.tar.gz libtdevnc-54b2400dd761f484c9a5ac4cb63ac63fa1f6b503.zip |
Use Windows' critical sections to emulate pthread's mutexes
With Microsoft Visual C++, we cannot use pthreads (MinGW sports an
emulation library which is the reason we did not need Windows-specific
hacks earlier). Happily, it is very easy to provide Windows-specific
emulations for the pthread calls we use.
[JES: fixed commit message]
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Diffstat (limited to 'libvncclient/tls_openssl.c')
-rw-r--r-- | libvncclient/tls_openssl.c | 49 |
1 files changed, 36 insertions, 13 deletions
diff --git a/libvncclient/tls_openssl.c b/libvncclient/tls_openssl.c index a1b771e..669913d 100644 --- a/libvncclient/tls_openssl.c +++ b/libvncclient/tls_openssl.c @@ -27,7 +27,30 @@ #include <openssl/rand.h> #include <openssl/x509.h> +#ifdef _MSC_VER +typedef CRITICAL_SECTION MUTEX_TYPE; +#define MUTEX_INIT(mutex) InitializeCriticalSection(&mutex) +#define MUTEX_FREE(mutex) DeleteCriticalSection(&mutex) +#define MUTEX_LOCK(mutex) EnterCriticalSection(&mutex) +#define MUTEX_UNLOCK(mutex) LeaveCriticalSection(&mutex) +#define CURRENT_THREAD_ID GetCurrentThreadId() +#else +typedef pthread_mutex_t MUTEX_TYPE; +#define MUTEX_INIT(mutex) {\ + pthread_mutexattr_t mutexAttr;\ + pthread_mutexattr_init(&mutexAttr);\ + pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE);\ + pthread_mutex_init(&mutex, &mutexAttr);\ +} +#define MUTEX_FREE(mutex) pthread_mutex_destroy(&mutex) +#define MUTEX_LOCK(mutex) pthread_mutex_lock(&mutex) +#define MUTEX_UNLOCK(mutex) pthread_mutex_unlock(&mutex) +#define CURRENT_THREAD_ID pthread_self() +#endif + +#ifndef _MSC_VER #include <pthread.h> +#endif #include "tls.h" @@ -38,23 +61,23 @@ typedef SSIZE_T ssize_t; #endif static rfbBool rfbTLSInitialized = FALSE; -static pthread_mutex_t *mutex_buf = NULL; +static MUTEX_TYPE *mutex_buf = NULL; struct CRYPTO_dynlock_value { - pthread_mutex_t mutex; + MUTEX_TYPE mutex; }; static void locking_function(int mode, int n, const char *file, int line) { if (mode & CRYPTO_LOCK) - pthread_mutex_lock(&mutex_buf[n]); + MUTEX_LOCK(mutex_buf[n]); else - pthread_mutex_unlock(&mutex_buf[n]); + MUTEX_UNLOCK(mutex_buf[n]); } static unsigned long id_function(void) { - return ((unsigned long) pthread_self()); + return ((unsigned long) CURRENT_THREAD_ID); } static struct CRYPTO_dynlock_value *dyn_create_function(const char *file, int line) @@ -65,7 +88,7 @@ static struct CRYPTO_dynlock_value *dyn_create_function(const char *file, int li malloc(sizeof(struct CRYPTO_dynlock_value)); if (!value) goto err; - pthread_mutex_init(&value->mutex, NULL); + MUTEX_INIT(value->mutex); return value; @@ -76,16 +99,16 @@ err: static void dyn_lock_function (int mode, struct CRYPTO_dynlock_value *l, const char *file, int line) { if (mode & CRYPTO_LOCK) - pthread_mutex_lock(&l->mutex); + MUTEX_LOCK(l->mutex); else - pthread_mutex_unlock(&l->mutex); + MUTEX_UNLOCK(l->mutex); } static void dyn_destroy_function(struct CRYPTO_dynlock_value *l, const char *file, int line) { - pthread_mutex_destroy(&l->mutex); + MUTEX_FREE(l->mutex); free(l); } @@ -123,14 +146,14 @@ InitializeTLS(void) if (rfbTLSInitialized) return TRUE; - mutex_buf = malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t)); + mutex_buf = malloc(CRYPTO_num_locks() * sizeof(MUTEX_TYPE)); if (mutex_buf == NULL) { rfbClientLog("Failed to initialized OpenSSL: memory.\n"); return (-1); } for (i = 0; i < CRYPTO_num_locks(); i++) - pthread_mutex_init(&mutex_buf[i], NULL); + MUTEX_INIT(mutex_buf[i]); CRYPTO_set_locking_callback(locking_function); CRYPTO_set_id_callback(id_function); @@ -199,7 +222,7 @@ static int sock_read_ready(SSL *ssl, uint32_t ms) FD_SET(SSL_get_fd(ssl), &fds); tv.tv_sec = ms / 1000; - tv.tv_usec = (ms % 1000) * 1000; + tv.tv_usec = (ms % 1000) * ms; r = select (SSL_get_fd(ssl) + 1, &fds, NULL, NULL, &tv); @@ -580,7 +603,7 @@ void FreeTLS(rfbClient* client) CRYPTO_set_id_callback(NULL); for (i = 0; i < CRYPTO_num_locks(); i++) - pthread_mutex_destroy(&mutex_buf[i]); + MUTEX_FREE(mutex_buf[i]); free(mutex_buf); mutex_buf = NULL; } |