diff --git a/configure.ac b/configure.ac index 66437ff7e..854d6b1d4 100644 --- a/configure.ac +++ b/configure.ac @@ -836,8 +836,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) diff --git a/doc/release-notes-15849.md b/doc/release-notes-15849.md new file mode 100644 index 000000000..a1df31f25 --- /dev/null +++ b/doc/release-notes-15849.md @@ -0,0 +1,6 @@ +Thread names in logs +-------------------- + +On platforms supporting `thread_local`, log lines can be prefixed with the name +of the thread that caused the log. To enable this behavior, use +`-logthreadnames=1`. diff --git a/src/Makefile.am b/src/Makefile.am index 4fe589882..ed5cab7f0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -209,6 +209,7 @@ BITCOIN_CORE_H = \ util/memory.h \ util/moneystr.h \ util/rbf.h \ + util/threadnames.h \ util/time.h \ util/url.h \ util/validation.h \ @@ -489,6 +490,7 @@ libbitcoin_util_a_SOURCES = \ util/system.cpp \ util/moneystr.cpp \ util/rbf.cpp \ + util/threadnames.cpp \ util/strencodings.cpp \ util/time.cpp \ util/url.cpp \ diff --git a/src/Makefile.test.include b/src/Makefile.test.include index 692f8d97b..1144ca8a7 100644 --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -138,6 +138,7 @@ BITCOIN_TESTS =\ test/skiplist_tests.cpp \ test/streams_tests.cpp \ test/sync_tests.cpp \ + test/util_threadnames_tests.cpp \ test/timedata_tests.cpp \ test/torcontrol_tests.cpp \ test/transaction_tests.cpp \ diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index dde75c1b1..b31f86cdd 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -64,6 +65,8 @@ static bool AppInit(int argc, char* argv[]) bool fRet = false; + util::ThreadRename("init"); + // // Parameters // diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 5d9c3d2c1..63639fa3e 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -17,7 +18,7 @@ #include #include #include -#include +#include #include #include @@ -284,7 +285,7 @@ static void http_reject_request_cb(struct evhttp_request* req, void*) /** Event dispatcher thread */ static bool ThreadHTTP(struct event_base* base) { - RenameThread("bitcoin-http"); + util::ThreadRename("http"); LogPrint(BCLog::HTTP, "Entering http event loop\n"); event_base_dispatch(base); // Event loop will be interrupted by InterruptHTTPServer() @@ -335,9 +336,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"); + util::ThreadRename(strprintf("httpworker.%i", worker_num)); queue->Run(); } @@ -430,7 +431,7 @@ void StartHTTPServer() threadHTTP = std::thread(ThreadHTTP, eventBase); for (int i = 0; i < rpcThreads; i++) { - g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue); + g_thread_http_workers.emplace_back(HTTPWorkQueueRun, workQueue, i); } } diff --git a/src/init.cpp b/src/init.cpp index 8f0be3ebe..2272624f8 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -41,6 +41,7 @@ #include