100.00% Lines (5/5)
100.00% Functions (1/1)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | Code | |||
| 1 | // | 1 | // | |||||
| 2 | // Copyright (c) 2026 Michael Vandeberg | 2 | // Copyright (c) 2026 Michael Vandeberg | |||||
| 3 | // | 3 | // | |||||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||||
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||||
| 6 | // | 6 | // | |||||
| 7 | // Official repository: https://github.com/cppalliance/capy | 7 | // Official repository: https://github.com/cppalliance/capy | |||||
| 8 | // | 8 | // | |||||
| 9 | 9 | |||||||
| 10 | #include <boost/capy/ex/thread_name.hpp> | 10 | #include <boost/capy/ex/thread_name.hpp> | |||||
| 11 | 11 | |||||||
| 12 | #if defined(_WIN32) | 12 | #if defined(_WIN32) | |||||
| 13 | 13 | |||||||
| 14 | #ifndef NOMINMAX | 14 | #ifndef NOMINMAX | |||||
| 15 | #define NOMINMAX | 15 | #define NOMINMAX | |||||
| 16 | #endif | 16 | #endif | |||||
| 17 | #include <windows.h> | 17 | #include <windows.h> | |||||
| 18 | #include <string> | 18 | #include <string> | |||||
| 19 | 19 | |||||||
| 20 | #elif defined(__APPLE__) | 20 | #elif defined(__APPLE__) | |||||
| 21 | 21 | |||||||
| 22 | #include <pthread.h> | 22 | #include <pthread.h> | |||||
| 23 | #include <cstring> | 23 | #include <cstring> | |||||
| 24 | 24 | |||||||
| 25 | #elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) | 25 | #elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) | |||||
| 26 | 26 | |||||||
| 27 | #include <pthread.h> | 27 | #include <pthread.h> | |||||
| 28 | #include <cstring> | 28 | #include <cstring> | |||||
| 29 | 29 | |||||||
| 30 | #endif | 30 | #endif | |||||
| 31 | 31 | |||||||
| 32 | /* | 32 | /* | |||||
| 33 | Platform-specific thread naming implementation. | 33 | Platform-specific thread naming implementation. | |||||
| 34 | 34 | |||||||
| 35 | Each platform has a different API and name length limit: | 35 | Each platform has a different API and name length limit: | |||||
| 36 | - Windows: SetThreadDescription with UTF-8 to UTF-16 conversion (no limit) | 36 | - Windows: SetThreadDescription with UTF-8 to UTF-16 conversion (no limit) | |||||
| 37 | - macOS: pthread_setname_np(name) with 63-char limit | 37 | - macOS: pthread_setname_np(name) with 63-char limit | |||||
| 38 | - Linux/FreeBSD: pthread_setname_np(thread, name) with 15-char limit | 38 | - Linux/FreeBSD: pthread_setname_np(thread, name) with 15-char limit | |||||
| 39 | - NetBSD: pthread_setname_np(thread, format, arg) with a | 39 | - NetBSD: pthread_setname_np(thread, format, arg) with a | |||||
| 40 | PTHREAD_MAX_NAMELEN_NP limit | 40 | PTHREAD_MAX_NAMELEN_NP limit | |||||
| 41 | 41 | |||||||
| 42 | All operations are best-effort and silently fail on error, since thread | 42 | All operations are best-effort and silently fail on error, since thread | |||||
| 43 | naming is purely for debugging visibility and should never affect program | 43 | naming is purely for debugging visibility and should never affect program | |||||
| 44 | correctness. The noexcept guarantee is maintained by catching exceptions | 44 | correctness. The noexcept guarantee is maintained by catching exceptions | |||||
| 45 | from std::wstring allocation on Windows. | 45 | from std::wstring allocation on Windows. | |||||
| 46 | */ | 46 | */ | |||||
| 47 | 47 | |||||||
| 48 | namespace boost { | 48 | namespace boost { | |||||
| 49 | namespace capy { | 49 | namespace capy { | |||||
| 50 | 50 | |||||||
| 51 | void | 51 | void | |||||
| HITCBC | 52 | 500 | set_current_thread_name(char const* name) noexcept | 52 | 505 | set_current_thread_name(char const* name) noexcept | ||
| 53 | { | 53 | { | |||||
| 54 | #if defined(_WIN32) | 54 | #if defined(_WIN32) | |||||
| 55 | // SetThreadDescription requires Windows 10 1607+. Older Windows versions | 55 | // SetThreadDescription requires Windows 10 1607+. Older Windows versions | |||||
| 56 | // are unsupported; the program may fail to link on those systems. | 56 | // are unsupported; the program may fail to link on those systems. | |||||
| 57 | 57 | |||||||
| 58 | // Query required buffer size for UTF-8 to wide conversion. | 58 | // Query required buffer size for UTF-8 to wide conversion. | |||||
| 59 | int required = MultiByteToWideChar(CP_UTF8, 0, name, -1, nullptr, 0); | 59 | int required = MultiByteToWideChar(CP_UTF8, 0, name, -1, nullptr, 0); | |||||
| 60 | if(required <= 0) | 60 | if(required <= 0) | |||||
| 61 | return; | 61 | return; | |||||
| 62 | 62 | |||||||
| 63 | // Allocate and convert; catch exceptions to maintain noexcept. | 63 | // Allocate and convert; catch exceptions to maintain noexcept. | |||||
| 64 | std::wstring wname; | 64 | std::wstring wname; | |||||
| 65 | try | 65 | try | |||||
| 66 | { | 66 | { | |||||
| 67 | wname.resize(static_cast<std::size_t>(required)); | 67 | wname.resize(static_cast<std::size_t>(required)); | |||||
| 68 | } | 68 | } | |||||
| 69 | catch(...) | 69 | catch(...) | |||||
| 70 | { | 70 | { | |||||
| 71 | return; | 71 | return; | |||||
| 72 | } | 72 | } | |||||
| 73 | 73 | |||||||
| 74 | if(MultiByteToWideChar(CP_UTF8, 0, name, -1, wname.data(), required) <= 0) | 74 | if(MultiByteToWideChar(CP_UTF8, 0, name, -1, wname.data(), required) <= 0) | |||||
| 75 | return; | 75 | return; | |||||
| 76 | 76 | |||||||
| 77 | // Ignore return value: thread naming is best-effort for debugging. | 77 | // Ignore return value: thread naming is best-effort for debugging. | |||||
| 78 | (void)SetThreadDescription(GetCurrentThread(), wname.c_str()); | 78 | (void)SetThreadDescription(GetCurrentThread(), wname.c_str()); | |||||
| 79 | #elif defined(__APPLE__) | 79 | #elif defined(__APPLE__) | |||||
| 80 | // macOS pthread_setname_np takes only the name (no thread handle) | 80 | // macOS pthread_setname_np takes only the name (no thread handle) | |||||
| 81 | // and has a 64 char limit (63 + null terminator) | 81 | // and has a 64 char limit (63 + null terminator) | |||||
| 82 | char truncated[64]; | 82 | char truncated[64]; | |||||
| 83 | std::strncpy(truncated, name, 63); | 83 | std::strncpy(truncated, name, 63); | |||||
| 84 | truncated[63] = '\0'; | 84 | truncated[63] = '\0'; | |||||
| 85 | 85 | |||||||
| 86 | // Ignore return value: thread naming is best-effort for debugging. | 86 | // Ignore return value: thread naming is best-effort for debugging. | |||||
| 87 | (void)pthread_setname_np(truncated); | 87 | (void)pthread_setname_np(truncated); | |||||
| 88 | #elif defined(__NetBSD__) | 88 | #elif defined(__NetBSD__) | |||||
| 89 | char truncated[PTHREAD_MAX_NAMELEN_NP]; | 89 | char truncated[PTHREAD_MAX_NAMELEN_NP]; | |||||
| 90 | std::strncpy(truncated, name, sizeof(truncated) - 1); | 90 | std::strncpy(truncated, name, sizeof(truncated) - 1); | |||||
| 91 | truncated[sizeof(truncated) - 1] = '\0'; | 91 | truncated[sizeof(truncated) - 1] = '\0'; | |||||
| 92 | 92 | |||||||
| 93 | // NetBSD takes a printf-style format, so the name is its argument | 93 | // NetBSD takes a printf-style format, so the name is its argument | |||||
| 94 | // rather than the format, which a '%' in it would corrupt. | 94 | // rather than the format, which a '%' in it would corrupt. | |||||
| 95 | (void)pthread_setname_np(pthread_self(), "%s", truncated); | 95 | (void)pthread_setname_np(pthread_self(), "%s", truncated); | |||||
| 96 | #elif defined(__linux__) || defined(__FreeBSD__) | 96 | #elif defined(__linux__) || defined(__FreeBSD__) | |||||
| 97 | // pthread_setname_np has 16 char limit (15 + null terminator) | 97 | // pthread_setname_np has 16 char limit (15 + null terminator) | |||||
| 98 | char truncated[16]; | 98 | char truncated[16]; | |||||
| HITCBC | 99 | 500 | std::strncpy(truncated, name, 15); | 99 | 505 | std::strncpy(truncated, name, 15); | ||
| HITCBC | 100 | 500 | truncated[15] = '\0'; | 100 | 505 | truncated[15] = '\0'; | ||
| 101 | 101 | |||||||
| 102 | // Ignore return value: thread naming is best-effort for debugging. | 102 | // Ignore return value: thread naming is best-effort for debugging. | |||||
| HITCBC | 103 | 500 | (void)pthread_setname_np(pthread_self(), truncated); | 103 | 505 | (void)pthread_setname_np(pthread_self(), truncated); | ||
| 104 | #else | 104 | #else | |||||
| 105 | (void)name; | 105 | (void)name; | |||||
| 106 | #endif | 106 | #endif | |||||
| HITCBC | 107 | 500 | } | 107 | 505 | } | ||
| 108 | 108 | |||||||
| 109 | } // capy | 109 | } // capy | |||||
| 110 | } // boost | 110 | } // boost | |||||