summaryrefslogtreecommitdiff
path: root/src/common/compat_threads.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2013-09-22 21:30:46 -0400
committerNick Mathewson <nickm@torproject.org>2015-01-14 10:41:53 -0500
commitc2f0d52b7fb937f3f66ef8b2b74b0fdf239b0e9b (patch)
treec0deccd74112c5d135d018099f616842431e81ee /src/common/compat_threads.c
parent518b0b3c5fefba2458181c7f733bc684e5466cf9 (diff)
downloadtor-c2f0d52b7fb937f3f66ef8b2b74b0fdf239b0e9b.tar.gz
tor-c2f0d52b7fb937f3f66ef8b2b74b0fdf239b0e9b.zip
Split threading-related code out of compat.c
Also, re-enable the #if'd out condition-variable code. Work queues are going to make us hack on all of this stuff a bit more closely, so it might not be a terrible idea to make it easier to hack.
Diffstat (limited to 'src/common/compat_threads.c')
-rw-r--r--src/common/compat_threads.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/common/compat_threads.c b/src/common/compat_threads.c
new file mode 100644
index 0000000000..84a8a21fe2
--- /dev/null
+++ b/src/common/compat_threads.c
@@ -0,0 +1,42 @@
+/* Copyright (c) 2003-2004, Roger Dingledine
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2015, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "compat.h"
+#include "util.h"
+
+/** Return a newly allocated, ready-for-use mutex. */
+tor_mutex_t *
+tor_mutex_new(void)
+{
+ tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t));
+ tor_mutex_init(m);
+ return m;
+}
+/** Release all storage and system resources held by <b>m</b>. */
+void
+tor_mutex_free(tor_mutex_t *m)
+{
+ if (!m)
+ return;
+ tor_mutex_uninit(m);
+ tor_free(m);
+}
+
+/** 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();
+}