From 99d6a1e86564d7b316a7de1d3565980811e650ce Mon Sep 17 00:00:00 2001 From: James O'Beirne Date: Mon, 21 May 2018 13:44:23 -0400 Subject: [PATCH 1/5] disable thread_local on unreliable platforms See discussions here: - https://github.com/bitcoin/bitcoin/pull/11722#pullrequestreview-79322658 - https://github.com/bitcoin/bitcoin/pull/13168#issuecomment-387181155 --- configure.ac | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index 490b638ee1047..5138b8066e443 100644 --- a/configure.ac +++ b/configure.ac @@ -775,8 +775,23 @@ AC_LINK_IFELSE([AC_LANG_SOURCE([ } ])], [ - AC_DEFINE(HAVE_THREAD_LOCAL,1,[Define if thread_local is supported.]) - AC_MSG_RESULT(yes) + case $host in + *mingw*) + # mingw32's implementation of thread_local has also been shown to behave + # erroneously under concurrent usage; see: + # https://gist.github.com/jamesob/fe9a872051a88b2025b1aa37bfa98605 + AC_MSG_RESULT(no) + ;; + *darwin*) + # TODO enable thread_local on later versions of Darwin where it is + # supported (per https://stackoverflow.com/a/29929949) + AC_MSG_RESULT(no) + ;; + *) + AC_DEFINE(HAVE_THREAD_LOCAL,1,[Define if thread_local is supported.]) + AC_MSG_RESULT(yes) + ;; + esac ], [ AC_MSG_RESULT(no) From 6e12f01b8480a93dcd97c08618e1c7c827ea72b7 Mon Sep 17 00:00:00 2001 From: James O'Beirne Date: Wed, 13 Jun 2018 14:50:59 -0400 Subject: [PATCH 2/5] threads: introduce threadutil, refactor thread naming This work is prerequisite to attaching thread names to log lines and deadlock debug utilities. threadutil allows setting of an "internal" threadname per thread on platforms where thread_local is available. This commit also moves RenameThread() out of util and adds a numeric suffix to disambiguate between threads with the same name. It explicitly names a few main threads using the new Rename() utility. --- src/Makefile.am | 2 ++ src/bitcoind.cpp | 3 ++ src/httpserver.cpp | 11 +++--- src/init.cpp | 7 ++-- src/qt/bitcoin.cpp | 3 ++ src/test/test_bitcoin.cpp | 2 +- src/threadutil.cpp | 76 +++++++++++++++++++++++++++++++++++++++ src/threadutil.h | 26 ++++++++++++++ src/util.cpp | 20 ----------- src/util.h | 6 ++-- src/validation.cpp | 5 +-- src/validation.h | 8 ++--- 12 files changed, 130 insertions(+), 39 deletions(-) create mode 100644 src/threadutil.cpp create mode 100644 src/threadutil.h diff --git a/src/Makefile.am b/src/Makefile.am index 458721293ebde..a0ab13e6e7cbc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -169,6 +169,7 @@ BITCOIN_CORE_H = \ support/events.h \ support/lockedpool.h \ sync.h \ + threadutil.h \ threadsafety.h \ threadinterrupt.h \ timedata.h \ @@ -409,6 +410,7 @@ libbitcoin_util_a_SOURCES = \ rpc/protocol.cpp \ support/cleanse.cpp \ sync.cpp \ + threadutil.cpp \ threadinterrupt.cpp \ util.cpp \ utilmoneystr.cpp \ diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 494a925a79ac8..bf0268997cbfc 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -58,6 +59,8 @@ static bool AppInit(int argc, char* argv[]) { bool fRet = false; + thread_util::Rename("init"); + // // Parameters // diff --git a/src/httpserver.cpp b/src/httpserver.cpp index bd08b04c0f4af..229ff860ad1af 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -16,7 +17,7 @@ #include #include #include -#include +#include #include #include @@ -281,7 +282,7 @@ static void http_reject_request_cb(struct evhttp_request* req, void*) /** Event dispatcher thread */ static bool ThreadHTTP(struct event_base* base, struct evhttp* http) { - RenameThread("bitcoin-http"); + thread_util::Rename("http"); LogPrint(BCLog::HTTP, "Entering http event loop\n"); event_base_dispatch(base); // Event loop will be interrupted by InterruptHTTPServer() @@ -328,9 +329,9 @@ static bool HTTPBindAddresses(struct evhttp* http) } /** Simple wrapper to set thread name and run work queue */ -static void HTTPWorkQueueRun(WorkQueue* queue) +static void HTTPWorkQueueRun(WorkQueue* queue, int worker_num) { - RenameThread("bitcoin-httpworker"); + thread_util::Rename(strprintf("httpworker.%i", worker_num)); queue->Run(); } @@ -433,7 +434,7 @@ bool StartHTTPServer() threadHTTP = std::thread(std::move(task), eventBase, eventHTTP); for (int i = 0; i < rpcThreads; i++) { - g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue); + g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue, i); } return true; } diff --git a/src/init.cpp b/src/init.cpp index 66b0b65eb4bc6..58253da7d6819 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -36,6 +36,7 @@ #include