aboutsummaryrefslogtreecommitdiff
path: root/src/util/threadnames.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/threadnames.cpp')
-rw-r--r--src/util/threadnames.cpp42
1 files changed, 20 insertions, 22 deletions
diff --git a/src/util/threadnames.cpp b/src/util/threadnames.cpp
index 91883fe4ff..0249de37e3 100644
--- a/src/util/threadnames.cpp
+++ b/src/util/threadnames.cpp
@@ -2,10 +2,9 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-#if defined(HAVE_CONFIG_H)
-#include <config/bitcoin-config.h>
-#endif
+#include <config/bitcoin-config.h> // IWYU pragma: keep
+#include <cstring>
#include <string>
#include <thread>
#include <utility>
@@ -38,31 +37,30 @@ static void SetThreadName(const char* name)
#endif
}
-// If we have thread_local, just keep thread ID and name in a thread_local
-// global.
-#if defined(HAVE_THREAD_LOCAL)
-
-static thread_local std::string g_thread_name;
-const std::string& util::ThreadGetInternalName() { return g_thread_name; }
+/**
+ * The name of the thread. We use char array instead of std::string to avoid
+ * complications with running a destructor when the thread exits. Avoid adding
+ * other thread_local variables.
+ * @see https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=278701
+ */
+static thread_local char g_thread_name[128]{'\0'};
+std::string 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); }
-
-// 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; }
-static void SetInternalName(std::string name) { }
-#endif
+static void SetInternalName(const std::string& name)
+{
+ const size_t copy_bytes{std::min(sizeof(g_thread_name) - 1, name.length())};
+ std::memcpy(g_thread_name, name.data(), copy_bytes);
+ g_thread_name[copy_bytes] = '\0';
+}
-void util::ThreadRename(std::string&& name)
+void util::ThreadRename(const std::string& name)
{
SetThreadName(("b-" + name).c_str());
- SetInternalName(std::move(name));
+ SetInternalName(name);
}
-void util::ThreadSetInternalName(std::string&& name)
+void util::ThreadSetInternalName(const std::string& name)
{
- SetInternalName(std::move(name));
+ SetInternalName(name);
}