[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Tell openssl how to use locks and how to find thread ids --...



Update of /home/or/cvsroot/tor/src/common
In directory moria.mit.edu:/tmp/cvs-serv9664/src/common

Modified Files:
	compat.c compat.h crypto.c 
Log Message:
Tell openssl how to use locks and how to find thread ids -- this may prevent race conditions surrounding the error queue.

Index: compat.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/compat.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -d -r1.34 -r1.35
--- compat.c	3 Feb 2005 23:13:36 -0000	1.34
+++ compat.c	13 Feb 2005 22:32:25 -0000	1.35
@@ -72,6 +72,10 @@
 #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"
@@ -774,6 +778,11 @@
     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;
@@ -800,6 +809,11 @@
   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;
@@ -808,6 +822,7 @@
 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
 
 /**

Index: compat.h
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/compat.h,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- compat.h	3 Feb 2005 19:59:10 -0000	1.20
+++ compat.h	13 Feb 2005 22:32:25 -0000	1.21
@@ -199,6 +199,7 @@
 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

Index: crypto.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/crypto.c,v
retrieving revision 1.132
retrieving revision 1.133
diff -u -d -r1.132 -r1.133
--- crypto.c	12 Feb 2005 21:03:37 -0000	1.132
+++ crypto.c	13 Feb 2005 22:32:25 -0000	1.133
@@ -55,6 +55,7 @@
 #include "aes.h"
 #include "util.h"
 #include "container.h"
+#include "compat.h"
 
 #if OPENSSL_VERSION_NUMBER < 0x00905000l
 #error "We require openssl >= 0.9.5"
@@ -100,6 +101,8 @@
 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
@@ -157,6 +160,7 @@
   if (!_crypto_global_initialized) {
       ERR_load_crypto_strings();
       _crypto_global_initialized = 1;
+      setup_openssl_threading();
   }
   return 0;
 }
@@ -1626,3 +1630,27 @@
   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