summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2009-05-31 19:17:22 -0400
committerNick Mathewson <nickm@torproject.org>2009-05-31 19:17:22 -0400
commit77ffd6b2a7bb8450c033763e2eb4d7f0ed7a5757 (patch)
treeb7578a5b8c4c86909b9f63546b6d557a2782823a /src/common
parente84ddead349e5af8c183042d3de27ecb4b6d4e87 (diff)
parentc4c7dcd453b62b3d3bcc8e78df8455a77645e62a (diff)
downloadtor-77ffd6b2a7bb8450c033763e2eb4d7f0ed7a5757.tar.gz
tor-77ffd6b2a7bb8450c033763e2eb4d7f0ed7a5757.zip
Merge commit 'origin/maint-0.2.1'
Diffstat (limited to 'src/common')
-rw-r--r--src/common/compat.c19
-rw-r--r--src/common/compat.h3
-rw-r--r--src/common/util.c2
3 files changed, 24 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
diff --git a/src/common/compat.h b/src/common/compat.h
index edc38faf64..4d5a016cf2 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -522,6 +522,9 @@ void tor_threads_init(void);
#define tor_threads_init() STMT_NIL
#endif
+void set_main_thread(void);
+int in_main_thread(void);
+
#ifdef TOR_IS_MULTITHREADED
#if 0
typedef struct tor_cond_t tor_cond_t;
diff --git a/src/common/util.c b/src/common/util.c
index a3338b1d18..7b9e5eb562 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -2480,6 +2480,8 @@ start_daemon(void)
if (fork() != 0) {
exit(0);
}
+ set_main_thread(); /* We are now the main thread. */
+
return;
}
}