diff options
author | Nick Mathewson <nickm@torproject.org> | 2009-05-29 10:18:50 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2009-05-30 18:16:24 -0400 |
commit | fd992deeea769721dc95bacb40ea360ef42f76dd (patch) | |
tree | 70e77957df737e808fd295696fb5b7ddce14fd11 /src/common/compat.c | |
parent | d66c3797650698778bd098bbaf9d3bbeebfa9fcf (diff) | |
download | tor-fd992deeea769721dc95bacb40ea360ef42f76dd.tar.gz tor-fd992deeea769721dc95bacb40ea360ef42f76dd.zip |
Don't attempt to log messages to a controller from a worker thread.
This patch adds a function to determine whether we're in the main
thread, and changes control_event_logmsg() to return immediately if
we're in a subthread. This is necessary because otherwise we will
call connection_write_to_buf, which modifies non-locked data
structures.
Bugfix on 0.2.0.x; fix for at least one of the things currently
called "bug 977".
Diffstat (limited to 'src/common/compat.c')
-rw-r--r-- | src/common/compat.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/common/compat.c b/src/common/compat.c index 35bb3a9ad3..d62b1ce1f4 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -2076,6 +2076,7 @@ tor_threads_init(void) pthread_mutexattr_init(&attr_reentrant); pthread_mutexattr_settype(&attr_reentrant, PTHREAD_MUTEX_RECURSIVE); threads_initialized = 1; + set_main_thread(); } } #elif defined(USE_WIN32_THREADS) @@ -2168,9 +2169,27 @@ tor_threads_init(void) #if 0 cond_event_tls_index = TlsAlloc(); #endif + set_main_thread(); } #endif +/** Identity of the "main" thread */ +static unsigned long main_thread_id = -1; + +/** Start considering the current thread to be the 'main thread'. This has + * no effect on anything besides in_main_thread(). */ +void +set_main_thread(void) +{ + main_thread_id = tor_get_thread_id(); +} +/** Return true iff called from the main thread. */ +int +in_main_thread(void) +{ + return main_thread_id == tor_get_thread_id(); +} + /** * On Windows, WSAEWOULDBLOCK is not always correct: when you see it, * you need to ask the socket for its actual errno. Also, you need to |