[Base] Cleanup set thread name.
This commit is contained in:
parent
ce34e1cbb9
commit
3dc69c6033
|
@ -2,7 +2,7 @@
|
|||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2014 Ben Vanik. All rights reserved. *
|
||||
* Copyright 2020 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
@ -67,9 +67,10 @@ uint32_t current_thread_id();
|
|||
void set_current_thread_id(uint32_t id);
|
||||
|
||||
// Sets the current thread name.
|
||||
void set_name(const std::string& name);
|
||||
void set_name(const std::string_view name);
|
||||
// Sets the target thread name.
|
||||
void set_name(std::thread::native_handle_type handle, const std::string& name);
|
||||
void set_name(std::thread::native_handle_type handle,
|
||||
const std::string_view name);
|
||||
|
||||
// Yields the current thread to the scheduler. Maybe.
|
||||
void MaybeYield();
|
||||
|
@ -358,7 +359,7 @@ class Thread : public WaitHandle {
|
|||
virtual uint32_t system_id() const = 0;
|
||||
|
||||
// Returns the current name of the thread, if previously specified.
|
||||
std::string name() const { return name_; }
|
||||
const std::string& name() const { return name_; }
|
||||
|
||||
// Sets the name of the thread, used in debugging and logging.
|
||||
virtual void set_name(std::string name) { name_ = std::move(name); }
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2014 Ben Vanik. All rights reserved. *
|
||||
* Copyright 2020 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
@ -32,13 +32,12 @@ uint32_t current_thread_system_id() {
|
|||
return static_cast<uint32_t>(syscall(SYS_gettid));
|
||||
}
|
||||
|
||||
void set_name(const std::string& name) {
|
||||
pthread_setname_np(pthread_self(), name.c_str());
|
||||
void set_name(std::thread::native_handle_type handle,
|
||||
const std::string_view name) {
|
||||
pthread_setname_np(handle, std::string(name).c_str());
|
||||
}
|
||||
|
||||
void set_name(std::thread::native_handle_type handle, const std::string& name) {
|
||||
pthread_setname_np(handle, name.c_str());
|
||||
}
|
||||
void set_name(const std::string_view name) { set_name(pthread_self(), name); }
|
||||
|
||||
void MaybeYield() {
|
||||
pthread_yield();
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2014 Ben Vanik. All rights reserved. *
|
||||
* Copyright 2020 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
@ -41,25 +41,10 @@ struct THREADNAME_INFO {
|
|||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
void set_name(HANDLE thread, const std::string& name) {
|
||||
auto kern = GetModuleHandleW(L"kernel32.dll");
|
||||
if (kern) {
|
||||
auto set_thread_description =
|
||||
(SetThreadDescriptionFn)GetProcAddress(kern, "SetThreadDescription");
|
||||
if (set_thread_description) {
|
||||
int len = MultiByteToWideChar(CP_ACP, 0, name.c_str(), -1, NULL, 0);
|
||||
auto str = (LPWSTR)alloca(len * sizeof(WCHAR));
|
||||
if (str) {
|
||||
MultiByteToWideChar(CP_ACP, 0, name.c_str(), -1, str, len);
|
||||
set_thread_description(thread, str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void raise_thread_name_exception(HANDLE thread, const std::string& name) {
|
||||
if (!IsDebuggerPresent()) {
|
||||
return;
|
||||
}
|
||||
|
||||
THREADNAME_INFO info;
|
||||
info.dwType = 0x1000;
|
||||
info.szName = name.c_str();
|
||||
|
@ -72,7 +57,21 @@ void set_name(HANDLE thread, const std::string& name) {
|
|||
}
|
||||
}
|
||||
|
||||
void set_name(const std::string& name) { set_name(GetCurrentThread(), name); }
|
||||
void set_name(HANDLE thread, const std::string_view name) {
|
||||
auto kernel = GetModuleHandleW(L"kernel32.dll");
|
||||
if (kernel) {
|
||||
auto func =
|
||||
(SetThreadDescriptionFn)GetProcAddress(kernel, "SetThreadDescription");
|
||||
if (func) {
|
||||
func(thread, reinterpret_cast<PCWSTR>(xe::to_utf16(name).c_str()));
|
||||
}
|
||||
}
|
||||
raise_thread_name_exception(thread, std::string(name));
|
||||
}
|
||||
|
||||
void set_name(const std::string_view name) {
|
||||
set_name(GetCurrentThread(), name);
|
||||
}
|
||||
|
||||
void MaybeYield() {
|
||||
SwitchToThread();
|
||||
|
|
Loading…
Reference in New Issue