Skip to content

Commit

Permalink
util: avoid using thread_local variable that has a destructor
Browse files Browse the repository at this point in the history
Store the thread name in a `thread_local` variable of type `char[]`
instead of `std::string`. This avoids calling the destructor when
the thread exits. This is a workaround for
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=278701

For type-safety, return `std::string_view` from
`util::ThreadGetInternalName()` instead of `char[]`.

As a side effect of this change, we no longer store a reference
to a `thread_local` variable in `CLockLocation`. This was
dangerous because if the thread quits while the reference still
exists (in the global variable `lock_data`, see inside `GetLockData()`)
then the reference will become dangling.
  • Loading branch information
vasild committed May 13, 2024
1 parent b940619 commit 13f438a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
5 changes: 0 additions & 5 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1049,11 +1049,6 @@ if test "$use_thread_local" = "yes" || test "$use_thread_local" = "auto"; then
dnl https://gist.github.com/jamesob/fe9a872051a88b2025b1aa37bfa98605
AC_MSG_RESULT([no])
;;
*freebsd*)
dnl FreeBSD's implementation of thread_local is also buggy (per
dnl https://groups.google.com/d/msg/bsdmailinglist/22ncTZAbDp4/Dii_pII5AwAJ)
AC_MSG_RESULT([no])
;;
*)
AC_DEFINE([HAVE_THREAD_LOCAL], [1], [Define if thread_local is supported.])
AC_MSG_RESULT([yes])
Expand Down
4 changes: 2 additions & 2 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& loggi
}

if (m_log_threadnames && m_started_new_line) {
const auto& threadname = util::ThreadGetInternalName();
str_prefixed.insert(0, "[" + (threadname.empty() ? "unknown" : threadname) + "] ");
const auto threadname = util::ThreadGetInternalName();
str_prefixed.insert(0, "[" + (threadname.empty() ? "unknown" : std::string{threadname}) + "] ");
}

str_prefixed = LogTimestampStr(str_prefixed);
Expand Down
4 changes: 2 additions & 2 deletions src/sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct CLockLocation {
const char* pszFile,
int nLine,
bool fTryIn,
const std::string& thread_name)
std::string_view thread_name)
: fTry(fTryIn),
mutexName(pszName),
sourceFile(pszFile),
Expand All @@ -60,7 +60,7 @@ struct CLockLocation {
bool fTry;
std::string mutexName;
std::string sourceFile;
const std::string& m_thread_name;
const std::string m_thread_name;
int sourceLine;
};

Expand Down
2 changes: 1 addition & 1 deletion src/test/util_threadnames_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ std::set<std::string> RenameEnMasse(int num_threads)
auto RenameThisThread = [&](int i) {
util::ThreadRename(TEST_THREAD_NAME_BASE + ToString(i));
std::lock_guard<std::mutex> guard(lock);
names.insert(util::ThreadGetInternalName());
names.emplace(util::ThreadGetInternalName());
};

threads.reserve(num_threads);
Expand Down
14 changes: 9 additions & 5 deletions src/util/threadnames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <config/bitcoin-config.h> // IWYU pragma: keep

#include <cstring>
#include <string>
#include <thread>
#include <utility>
Expand Down Expand Up @@ -40,17 +41,20 @@ static void SetThreadName(const char* name)
// global.
#if defined(HAVE_THREAD_LOCAL)

static thread_local std::string g_thread_name;
const std::string& util::ThreadGetInternalName() { return g_thread_name; }
static thread_local char g_thread_name[128]{'\0'};
std::string_view util::ThreadGetInternalName() { return g_thread_name; }
//! Set the in-memory internal name for this thread. Does not affect the process
//! name.
static void SetInternalName(std::string name) { g_thread_name = std::move(name); }
static void SetInternalName(std::string name)
{
std::memcpy(g_thread_name, name.c_str(), std::min(sizeof(g_thread_name), name.length() + 1));
g_thread_name[sizeof(g_thread_name) - 1] = '\0';
}

// Without thread_local available, don't handle internal name at all.
#else

static const std::string empty_string;
const std::string& util::ThreadGetInternalName() { return empty_string; }
std::string_view util::ThreadGetInternalName() { return std::string_view{}; }
static void SetInternalName(std::string name) { }
#endif

Expand Down
3 changes: 2 additions & 1 deletion src/util/threadnames.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define BITCOIN_UTIL_THREADNAMES_H

#include <string>
#include <string_view>

namespace util {
//! Rename a thread both in terms of an internal (in-memory) name as well
Expand All @@ -19,7 +20,7 @@ void ThreadSetInternalName(std::string&&);

//! Get the thread's internal (in-memory) name; used e.g. for identification in
//! logging.
const std::string& ThreadGetInternalName();
std::string_view ThreadGetInternalName();

} // namespace util

Expand Down

0 comments on commit 13f438a

Please sign in to comment.