aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-04-22 15:59:59 +0000
committerNick Mathewson <nickm@torproject.org>2008-04-22 15:59:59 +0000
commit299014b2c784c0bb72188f4db55d099b69a4e7f4 (patch)
tree5e79a3296656046eb9a3017fa675e5528ef6060c /src/common
parent0cbecbe050cfae1ae3919615a5eafdfea738d63b (diff)
downloadtor-299014b2c784c0bb72188f4db55d099b69a4e7f4.tar
tor-299014b2c784c0bb72188f4db55d099b69a4e7f4.tar.gz
r15251@tombo: nickm | 2008-04-22 11:59:46 -0400
On platforms using pthreads, allow a thread to acquire a lock it already holds. This is crucial for logging: otherwise any log message thrown from inside the logging process (especially from control.c) will deadlock. Win32 CriticalSections are already recursive. Bug spotted by nwf. Bugfix on 0.2.0.16-alpha. Backport candidate. I hope this is portable. svn:r14406
Diffstat (limited to 'src/common')
-rw-r--r--src/common/compat.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index 34b4ffae1..b92fa48de 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -1735,13 +1735,14 @@ tor_get_thread_id(void)
struct tor_mutex_t {
pthread_mutex_t mutex;
};
+static pthread_mutexattr_t attr_reentrant;
/** Allocate and return new lock. */
tor_mutex_t *
tor_mutex_new(void)
{
int err;
tor_mutex_t *mutex = tor_malloc_zero(sizeof(tor_mutex_t));
- err = pthread_mutex_init(&mutex->mutex, NULL);
+ err = pthread_mutex_init(&mutex->mutex, &attr_reentrant);
if (PREDICT_UNLIKELY(err)) {
log_err(LD_GENERAL, "Error %d creating a mutex.", err);
tor_fragile_assert();
@@ -1857,6 +1858,10 @@ tor_cond_signal_all(tor_cond_t *cond)
void
tor_threads_init(void)
{
+#ifdef USE_PTHREADS
+ pthread_mutexattr_init(&attr_reentrant);
+ pthread_mutexattr_settype(&attr_reentrant, PTHREAD_MUTEX_RECURSIVE);
+#endif
}
#elif defined(USE_WIN32_THREADS)
#if 0