/Users/eugenesiegel/btc/bitcoin/src/util/threadnames.cpp
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | // Copyright (c) 2018-2022 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 |  | #include <cstring> | 
| 6 |  | #include <string> | 
| 7 |  | #include <thread> | 
| 8 |  | #include <utility> | 
| 9 |  |  | 
| 10 |  | #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) | 
| 11 |  | #include <pthread.h> | 
| 12 |  | #include <pthread_np.h> | 
| 13 |  | #endif | 
| 14 |  |  | 
| 15 |  | #include <util/threadnames.h> | 
| 16 |  |  | 
| 17 |  | #if __has_include(<sys/prctl.h>) | 
| 18 |  | #include <sys/prctl.h> | 
| 19 |  | #endif | 
| 20 |  |  | 
| 21 |  | //! Set the thread's name at the process level. Does not affect the | 
| 22 |  | //! internal name. | 
| 23 |  | static void SetThreadName(const char* name) | 
| 24 | 51.2k | { | 
| 25 |  | #if defined(PR_SET_NAME) | 
| 26 |  |     // Only the first 15 characters are used (16 - NUL terminator) | 
| 27 |  |     ::prctl(PR_SET_NAME, name, 0, 0, 0); | 
| 28 |  | #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) | 
| 29 |  |     pthread_set_name_np(pthread_self(), name); | 
| 30 |  | #elif defined(__APPLE__) | 
| 31 |  |     pthread_setname_np(name); | 
| 32 |  | #else | 
| 33 |  |     // Prevent warnings for unused parameters... | 
| 34 |  |     (void)name; | 
| 35 |  | #endif | 
| 36 | 51.2k | } | 
| 37 |  |  | 
| 38 |  | /** | 
| 39 |  |  * The name of the thread. We use char array instead of std::string to avoid | 
| 40 |  |  * complications with running a destructor when the thread exits. Avoid adding | 
| 41 |  |  * other thread_local variables. | 
| 42 |  |  * @see https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=278701 | 
| 43 |  |  */ | 
| 44 |  | static thread_local char g_thread_name[128]{'\0'}; | 
| 45 | 102k | std::string util::ThreadGetInternalName() { return g_thread_name; } | 
| 46 |  | //! Set the in-memory internal name for this thread. Does not affect the process | 
| 47 |  | //! name. | 
| 48 |  | static void SetInternalName(const std::string& name) | 
| 49 | 51.2k | { | 
| 50 | 51.2k |     const size_t copy_bytes{std::min(sizeof(g_thread_name) - 1, name.length())}; | 
| 51 | 51.2k |     std::memcpy(g_thread_name, name.data(), copy_bytes); | 
| 52 | 51.2k |     g_thread_name[copy_bytes] = '\0'; | 
| 53 | 51.2k | } | 
| 54 |  |  | 
| 55 |  | void util::ThreadRename(const std::string& name) | 
| 56 | 51.2k | { | 
| 57 | 51.2k |     SetThreadName(("b-" + name).c_str()); | 
| 58 | 51.2k |     SetInternalName(name); | 
| 59 | 51.2k | } | 
| 60 |  |  | 
| 61 |  | void util::ThreadSetInternalName(const std::string& name) | 
| 62 | 0 | { | 
| 63 | 0 |     SetInternalName(name); | 
| 64 | 0 | } |