src/ex/thread_name.cpp

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