-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Named threads for better debugging (#98)
* Implement named threads for windows * Add thread names to host_task thread-pool worker * static_assert native thread handle type * Add missing header * Implement named threads for unix * Fix unused variable warning in release builds * Replace string with vector * Implement proper string/wstring conversion * Simplify named threads API * Remove dependency to std::vector and use normal array instead * Use overloading and accept minor code duplication * Add comment about possible memory leak in case of exception * Make use of catch2 string matcher * Add prefix to thread names, and additionally label worker threads with id * Fix wrong cmake if-block for platform specific translation units
- Loading branch information
BlackMark
authored
Mar 8, 2022
1 parent
3c303bb
commit 25d769d
Showing
12 changed files
with
176 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <thread> | ||
|
||
namespace celerity::detail { | ||
|
||
std::thread::native_handle_type get_current_thread_handle(); | ||
|
||
void set_thread_name(const std::thread::native_handle_type thread_handle, const std::string& name); | ||
|
||
std::string get_thread_name(const std::thread::native_handle_type thread_handle); | ||
|
||
} // namespace celerity::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "named_threads.h" | ||
|
||
#include <cassert> | ||
#include <type_traits> | ||
|
||
#include <pthread.h> | ||
|
||
namespace celerity::detail { | ||
|
||
static_assert(std::is_same_v<std::thread::native_handle_type, pthread_t>, "Unexpected native thread handle type"); | ||
|
||
constexpr auto PTHREAD_MAX_THREAD_NAME_LEN = 16; | ||
|
||
std::thread::native_handle_type get_current_thread_handle() { | ||
return pthread_self(); | ||
} | ||
|
||
void set_thread_name(const std::thread::native_handle_type thread_handle, const std::string& name) { | ||
auto truncated_name = name; | ||
truncated_name.resize(PTHREAD_MAX_THREAD_NAME_LEN - 1); // -1 because of null terminator | ||
[[maybe_unused]] const auto res = pthread_setname_np(thread_handle, truncated_name.c_str()); | ||
assert(res == 0 && "Failed to set thread name"); | ||
} | ||
|
||
std::string get_thread_name(const std::thread::native_handle_type thread_handle) { | ||
char name[PTHREAD_MAX_THREAD_NAME_LEN] = {}; | ||
[[maybe_unused]] const auto res = pthread_getname_np(thread_handle, name, PTHREAD_MAX_THREAD_NAME_LEN); | ||
assert(res == 0 && "Failed to get thread name"); | ||
return name; // Automatically strips null terminator | ||
} | ||
|
||
} // namespace celerity::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "named_threads.h" | ||
|
||
#include <cassert> | ||
#include <cwchar> | ||
#include <type_traits> | ||
|
||
#include <windows.h> | ||
|
||
namespace celerity::detail { | ||
|
||
static_assert(std::is_same_v<std::thread::native_handle_type, HANDLE>, "Unexpected native thread handle type"); | ||
|
||
static inline std::string convert_string(const std::wstring& str) { | ||
const auto* src = str.c_str(); | ||
auto mbstate = std::mbstate_t{}; | ||
const auto len = std::wcsrtombs(nullptr, &src, 0, &mbstate); | ||
auto dst = std::string(len, L'\0'); // Automatically includes space for the null terminator | ||
std::wcsrtombs(dst.data(), &src, dst.size(), &mbstate); | ||
return dst; | ||
} | ||
|
||
static inline std::wstring convert_string(const std::string& str) { | ||
const auto* src = str.c_str(); | ||
auto mbstate = std::mbstate_t{}; | ||
const auto len = std::mbsrtowcs(nullptr, &src, 0, &mbstate); | ||
auto dst = std::wstring(len, L'\0'); // Automatically includes space for the null terminator | ||
std::mbsrtowcs(dst.data(), &src, dst.size(), &mbstate); | ||
return dst; | ||
} | ||
|
||
std::thread::native_handle_type get_current_thread_handle() { | ||
return GetCurrentThread(); | ||
} | ||
|
||
void set_thread_name(const std::thread::native_handle_type thread_handle, const std::string& name) { | ||
const auto wname = convert_string(name); | ||
[[maybe_unused]] const auto res = SetThreadDescription(thread_handle, wname.c_str()); | ||
assert(SUCCEEDED(res) && "Failed to set thread name"); | ||
} | ||
|
||
std::string get_thread_name(const std::thread::native_handle_type thread_handle) { | ||
PWSTR wname = nullptr; | ||
const auto res = GetThreadDescription(thread_handle, &wname); | ||
assert(SUCCEEDED(res) && "Failed to get thread name"); | ||
std::string name; | ||
if(SUCCEEDED(res)) { | ||
name = convert_string(wname); | ||
LocalFree(wname); // Will leak if convert_string throws | ||
} | ||
return name; | ||
} | ||
|
||
} // namespace celerity::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters