#include "log.h"
#include "util.h"
+#ifdef TOR_IS_MULTITHREADED
+#include <openssl/crypto.h>
+#endif
+
/* Inline the strl functions if the platform doesn't have them. */
#ifndef HAVE_STRLCPY
#include "strlcpy.c"
log_fn(LOG_WARN, "Failed to release mutex: %d", GetLastError());
}
}
+unsigned long
+tor_get_thread_id(void)
+{
+ return (unsigned long)GetCurrentThreadId();
+}
#elif defined(USE_PTHREADS)
struct tor_mutex_t {
pthread_mutex_t mutex;
pthread_mutex_destroy(&m->mutex);
tor_free(m);
}
+unsigned long
+tor_get_thread_id(void)
+{
+ return (unsigned long)pthread_self();
+}
#else
struct tor_mutex_t {
int _unused;
void tor_mutex_acquire(tor_mutex_t *m) { }
void tor_mutex_release(tor_mutex_t *m) { }
void tor_mutex_free(tor_mutex_t *m) { }
+unsigned long tor_get_thread_id(void) { return 1; }
#endif
/**
void tor_mutex_acquire(tor_mutex_t *m);
void tor_mutex_release(tor_mutex_t *m);
void tor_mutex_free(tor_mutex_t *m);
+unsigned long tor_get_thread_id(void);
#if defined(MS_WINDOWS)
#define USE_WIN32_THREADS
#include "aes.h"
#include "util.h"
#include "container.h"
+#include "compat.h"
#if OPENSSL_VERSION_NUMBER < 0x00905000l
#error "We require openssl >= 0.9.5"
EVP_PKEY *_crypto_pk_env_get_evp_pkey(crypto_pk_env_t *env, int private);
DH *_crypto_dh_env_get_dh(crypto_dh_env_t *dh);
+static int setup_openssl_threading(void);
+
/** Return the number of bytes added by padding method <b>padding</b>.
*/
static INLINE int
if (!_crypto_global_initialized) {
ERR_load_crypto_strings();
_crypto_global_initialized = 1;
+ setup_openssl_threading();
}
return 0;
}
crypto_free_digest_env(d);
}
+#ifdef TOR_IS_MULTITHREADED
+static tor_mutex_t **_openssl_mutexes = NULL;
+static void
+_openssl_locking_cb(int mode, int n, const char *file, int line)
+{
+ if (mode & CRYPTO_LOCK)
+ tor_mutex_acquire(_openssl_mutexes[n]);
+ else
+ tor_mutex_release(_openssl_mutexes[n]);
+}
+static int
+setup_openssl_threading(void) {
+ int i;
+ int n = CRYPTO_num_locks();
+ _openssl_mutexes = tor_malloc(n*sizeof(tor_mutex_t *));
+ for (i=0; i <n; ++i)
+ _openssl_mutexes[i] = tor_mutex_new();
+ CRYPTO_set_locking_callback(_openssl_locking_cb);
+ CRYPTO_set_id_callback(tor_get_thread_id);
+ return 0;
+}
+#else
+static int setup_openssl_threading(void) { return 0; }
+#endif