commit 9cf335c9a5fe6767e90cc5cfdc1f5c95465edb10 Author: Nick Mathewson nickm@torproject.org Date: Thu Jun 28 09:14:42 2018 -0400
Extract threading code into a new library.
Note that the workqueue code does *not* go here: it is logically at a higher level, since it needs to use libevent and the networking stack. --- .gitignore | 2 ++ Makefile.am | 2 ++ src/common/compat.h | 2 +- src/common/include.am | 10 --------- src/common/workqueue.c | 2 +- src/include.am | 1 + src/lib/thread/.may_include | 6 ++++++ src/{common => lib/thread}/compat_pthreads.c | 11 ++++++---- src/{common => lib/thread}/compat_threads.c | 5 ++--- src/{common => lib/thread}/compat_winthreads.c | 5 +++-- src/lib/thread/include.am | 25 ++++++++++++++++++++++ .../compat_threads.h => lib/thread/threads.h} | 2 ++ src/rust/build.rs | 1 + src/test/test_threads.c | 2 +- src/test/test_workqueue.c | 2 +- 15 files changed, 55 insertions(+), 23 deletions(-)
diff --git a/.gitignore b/.gitignore index 5198daf4f..65c9fb83f 100644 --- a/.gitignore +++ b/.gitignore @@ -195,6 +195,8 @@ uptime-*.json /src/lib/libtor-string-testing.a /src/lib/libtor-smartlist-core.a /src/lib/libtor-smartlist-core-testing.a +/src/lib/libtor-thread.a +/src/lib/libtor-thread-testing.a /src/lib/libtor-tls.a /src/lib/libtor-tls-testing.a /src/lib/libtor-trace.a diff --git a/Makefile.am b/Makefile.am index c016310a6..be645194a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -45,6 +45,7 @@ TOR_UTIL_LIBS = \ src/lib/libtor-sandbox.a \ src/lib/libtor-container.a \ src/lib/libtor-net.a \ + src/lib/libtor-thread.a \ src/lib/libtor-log.a \ src/lib/libtor-lock.a \ src/lib/libtor-fdio.a \ @@ -65,6 +66,7 @@ TOR_UTIL_TESTING_LIBS = \ src/lib/libtor-sandbox-testing.a \ src/lib/libtor-container-testing.a \ src/lib/libtor-net-testing.a \ + src/lib/libtor-thread-testing.a \ src/lib/libtor-log-testing.a \ src/lib/libtor-lock-testing.a \ src/lib/libtor-fdio-testing.a \ diff --git a/src/common/compat.h b/src/common/compat.h index 95447d3b8..574dc5720 100644 --- a/src/common/compat.h +++ b/src/common/compat.h @@ -190,6 +190,6 @@ int tor_mlockall(void); ssize_t tor_getpass(const char *prompt, char *output, size_t buflen);
/* This needs some of the declarations above so we include it here. */ -#include "common/compat_threads.h" +#include "lib/thread/threads.h"
#endif /* !defined(TOR_COMPAT_H) */ diff --git a/src/common/include.am b/src/common/include.am index 14e7dcb9f..9ed5b30d3 100644 --- a/src/common/include.am +++ b/src/common/include.am @@ -17,13 +17,6 @@ else libor_extra_source= endif
-if THREADS_PTHREADS -threads_impl_source=src/common/compat_pthreads.c -endif -if THREADS_WIN32 -threads_impl_source=src/common/compat_winthreads.c -endif - if BUILD_READPASSPHRASE_C readpassphrase_source=src/ext/readpassphrase.c else @@ -34,7 +27,6 @@ LIBOR_A_SRC = \ src/common/address_set.c \ src/common/buffers.c \ src/common/compat.c \ - src/common/compat_threads.c \ src/common/compat_time.c \ src/common/conffile.c \ src/common/memarea.c \ @@ -44,7 +36,6 @@ LIBOR_A_SRC = \ src/common/token_bucket.c \ src/common/workqueue.c \ $(libor_extra_source) \ - $(threads_impl_source) \ $(readpassphrase_source)
src/common/src_common_libor_testing_a-log.$(OBJEXT) \ @@ -77,7 +68,6 @@ COMMONHEADERS = \ src/common/buffers.h \ src/common/compat.h \ src/common/compat_libevent.h \ - src/common/compat_threads.h \ src/common/compat_time.h \ src/common/conffile.h \ src/common/handles.h \ diff --git a/src/common/workqueue.c b/src/common/workqueue.c index fd31ccd7f..4735aadd7 100644 --- a/src/common/workqueue.c +++ b/src/common/workqueue.c @@ -26,7 +26,7 @@ #include "orconfig.h" #include "common/compat.h" #include "common/compat_libevent.h" -#include "common/compat_threads.h" +#include "lib/thread/threads.h" #include "lib/crypt_ops/crypto_rand.h" #include "common/util.h" #include "common/workqueue.h" diff --git a/src/include.am b/src/include.am index c83ec0d57..0a8af38b0 100644 --- a/src/include.am +++ b/src/include.am @@ -20,6 +20,7 @@ include src/lib/sandbox/include.am include src/lib/string/include.am include src/lib/smartlist_core/include.am include src/lib/testsupport/include.am +include src/lib/thread/include.am include src/lib/tls/include.am include src/lib/trace/include.am include src/lib/wallclock/include.am diff --git a/src/lib/thread/.may_include b/src/lib/thread/.may_include new file mode 100644 index 000000000..93ad0cd73 --- /dev/null +++ b/src/lib/thread/.may_include @@ -0,0 +1,6 @@ +orconfig.h +lib/cc/*.h +lib/lock/*.h +lib/log/*.h +lib/testsupport/*.h +lib/thread/*.h diff --git a/src/common/compat_pthreads.c b/src/lib/thread/compat_pthreads.c similarity index 98% rename from src/common/compat_pthreads.c rename to src/lib/thread/compat_pthreads.c index e345cfef0..246c6254b 100644 --- a/src/common/compat_pthreads.c +++ b/src/lib/thread/compat_pthreads.c @@ -11,13 +11,16 @@ */
#include "orconfig.h" +#include "lib/thread/threads.h" +#include "lib/log/torlog.h" +#include "lib/log/util_bug.h" + +#include <sys/time.h> #include <pthread.h> #include <signal.h> #include <time.h> - -#include "common/compat.h" -#include "lib/log/torlog.h" -#include "common/util.h" +#include <errno.h> +#include <string.h>
/** Wraps a void (*)(void*) function and its argument so we can * invoke them in a way pthreads would expect. diff --git a/src/common/compat_threads.c b/src/lib/thread/compat_threads.c similarity index 96% rename from src/common/compat_threads.c rename to src/lib/thread/compat_threads.c index 87833c6cf..1d685b2c3 100644 --- a/src/common/compat_threads.c +++ b/src/lib/thread/compat_threads.c @@ -13,11 +13,10 @@
#include "orconfig.h" #include <stdlib.h> -#include "common/compat.h" -#include "common/compat_threads.h" +#include "lib/thread/threads.h"
-#include "common/util.h" #include "lib/log/torlog.h" +#include "lib/log/util_bug.h"
/** Allocate and return a new condition variable. */ tor_cond_t * diff --git a/src/common/compat_winthreads.c b/src/lib/thread/compat_winthreads.c similarity index 98% rename from src/common/compat_winthreads.c rename to src/lib/thread/compat_winthreads.c index d082af31c..7f9877d21 100644 --- a/src/common/compat_winthreads.c +++ b/src/lib/thread/compat_winthreads.c @@ -12,11 +12,12 @@
#ifdef _WIN32
-#include "common/compat.h" #include <windows.h> #include <process.h> -#include "common/util.h" +#include "lib/thread/threads.h" #include "lib/log/torlog.h" +#include "lib/log/util_bug.h" +#include "lib/log/win32err.h"
/* This value is more or less total cargo-cult */ #define SPIN_COUNT 2000 diff --git a/src/lib/thread/include.am b/src/lib/thread/include.am new file mode 100644 index 000000000..0eac7edee --- /dev/null +++ b/src/lib/thread/include.am @@ -0,0 +1,25 @@ + +noinst_LIBRARIES += src/lib/libtor-thread.a + +if UNITTESTS_ENABLED +noinst_LIBRARIES += src/lib/libtor-thread-testing.a +endif + +if THREADS_PTHREADS +threads_impl_source=src/lib/thread/compat_pthreads.c +endif +if THREADS_WIN32 +threads_impl_source=src/lib/thread/compat_winthreads.c +endif + +src_lib_libtor_thread_a_SOURCES = \ + src/lib/thread/compat_threads.c \ + $(threads_impl_source) + +src_lib_libtor_thread_testing_a_SOURCES = \ + $(src_lib_libtor_thread_a_SOURCES) +src_lib_libtor_thread_testing_a_CPPFLAGS = $(AM_CPPFLAGS) $(TEST_CPPFLAGS) +src_lib_libtor_thread_testing_a_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS) + +noinst_HEADERS += \ + src/lib/thread/threads.h diff --git a/src/common/compat_threads.h b/src/lib/thread/threads.h similarity index 99% rename from src/common/compat_threads.h rename to src/lib/thread/threads.h index 69350421b..fcccc643d 100644 --- a/src/common/compat_threads.h +++ b/src/lib/thread/threads.h @@ -15,6 +15,8 @@ #include <stdatomic.h> #endif
+struct timeval; + int spawn_func(void (*func)(void *), void *data); void spawn_exit(void) ATTR_NORETURN;
diff --git a/src/rust/build.rs b/src/rust/build.rs index 36f847d2e..e17f9e5df 100644 --- a/src/rust/build.rs +++ b/src/rust/build.rs @@ -154,6 +154,7 @@ pub fn main() { cfg.component("tor-sandbox"); cfg.component("tor-encoding-testing"); cfg.component("tor-net"); + cfg.component("tor-thread-testing"); cfg.component("tor-log"); cfg.component("tor-lock"); cfg.component("tor-fdio"); diff --git a/src/test/test_threads.c b/src/test/test_threads.c index 11f41a044..e698e4f61 100644 --- a/src/test/test_threads.c +++ b/src/test/test_threads.c @@ -5,7 +5,7 @@
#include "orconfig.h" #include "or/or.h" -#include "common/compat_threads.h" +#include "lib/thread/threads.h" #include "test/test.h"
/** mutex for thread test to stop the threads hitting data at the same time. */ diff --git a/src/test/test_workqueue.c b/src/test/test_workqueue.c index 7c525aa28..5cda99b58 100644 --- a/src/test/test_workqueue.c +++ b/src/test/test_workqueue.c @@ -4,7 +4,7 @@ /* See LICENSE for licensing information */
#include "or/or.h" -#include "common/compat_threads.h" +#include "lib/thread/threads.h" #include "or/onion.h" #include "common/workqueue.h" #include "lib/crypt_ops/crypto_curve25519.h"