| 35 | namespace { |
| 36 | |
| 37 | void setThreadName(const char* name) { |
| 38 | // Limit the thread name to 15 characters plus null terminator |
| 39 | char tmp_name[16]; |
| 40 | strncpy(tmp_name, name, sizeof(tmp_name) - 1); |
| 41 | tmp_name[sizeof(tmp_name) - 1] = '\0'; // Ensure null termination |
| 42 | |
| 43 | #ifdef _WIN32 |
| 44 | DWORD threadId = GetCurrentThreadId(); |
| 45 | nvtxNameOsThreadA(threadId, tmp_name); |
| 46 | HRESULT hr = SetThreadDescription(GetCurrentThread(), std::wstring(tmp_name, tmp_name + strlen(tmp_name)).c_str()); |
| 47 | if (FAILED(hr)) { |
| 48 | std::cerr << "Failed to set thread description on Windows." << std::endl; |
| 49 | } |
| 50 | #elif defined(__linux__) |
| 51 | nvtxNameOsThreadA(syscall(SYS_gettid), tmp_name); |
| 52 | pthread_setname_np(pthread_self(), tmp_name); |
| 53 | #else |
| 54 | std::cerr << "Setting thread name not supported on this platform." << std::endl; |
| 55 | #endif |
| 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 |
no test coverage detected