Branch data Line data Source code
# 1 : : // Copyright (c) 2018-2019 The Bitcoin Core developers # 2 : : // Distributed under the MIT software license, see the accompanying # 3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php. # 4 : : # 5 : : #if defined(HAVE_CONFIG_H) # 6 : : #include <config/bitcoin-config.h> # 7 : : #endif # 8 : : # 9 : : #include <thread> # 10 : : # 11 : : #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) # 12 : : #include <pthread.h> # 13 : : #include <pthread_np.h> # 14 : : #endif # 15 : : # 16 : : #include <util/threadnames.h> # 17 : : # 18 : : #ifdef HAVE_SYS_PRCTL_H # 19 : : #include <sys/prctl.h> // For prctl, PR_SET_NAME, PR_GET_NAME # 20 : : #endif # 21 : : # 22 : : //! Set the thread's name at the process level. Does not affect the # 23 : : //! internal name. # 24 : : static void SetThreadName(const char* name) # 25 : 13725 : { # 26 : : #if defined(PR_SET_NAME) # 27 : : // Only the first 15 characters are used (16 - NUL terminator) # 28 : : ::prctl(PR_SET_NAME, name, 0, 0, 0); # 29 : : #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) # 30 : 13725 : pthread_set_name_np(pthread_self(), name); # 31 : : #elif defined(MAC_OSX) # 32 : : pthread_setname_np(name); # 33 : : #else # 34 : : // Prevent warnings for unused parameters... # 35 : : (void)name; # 36 : : #endif # 37 : 13725 : } # 38 : : # 39 : : // If we have thread_local, just keep thread ID and name in a thread_local # 40 : : // global. # 41 : : #if defined(HAVE_THREAD_LOCAL) # 42 : : # 43 : : static thread_local std::string g_thread_name; # 44 : : const std::string& util::ThreadGetInternalName() { return g_thread_name; } # 45 : : //! Set the in-memory internal name for this thread. Does not affect the process # 46 : : //! name. # 47 : : static void SetInternalName(std::string name) { g_thread_name = std::move(name); } # 48 : : # 49 : : // Without thread_local available, don't handle internal name at all. # 50 : : #else # 51 : : # 52 : : static const std::string empty_string; # 53 : 116480563 : const std::string& util::ThreadGetInternalName() { return empty_string; } # 54 : 14413 : static void SetInternalName(std::string name) { } # 55 : : #endif # 56 : : # 57 : : void util::ThreadRename(std::string&& name) # 58 : 13724 : { # 59 : 13724 : SetThreadName(("b-" + name).c_str()); # 60 : 13724 : SetInternalName(std::move(name)); # 61 : 13724 : } # 62 : : # 63 : : void util::ThreadSetInternalName(std::string&& name) # 64 : 688 : { # 65 : 688 : SetInternalName(std::move(name)); # 66 : 688 : }