diff --git a/src/xenia/base/threading.h b/src/xenia/base/threading.h index 480b76207..fef37dd06 100644 --- a/src/xenia/base/threading.h +++ b/src/xenia/base/threading.h @@ -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); } diff --git a/src/xenia/base/threading_posix.cc b/src/xenia/base/threading_posix.cc index e08074d93..28597e608 100644 --- a/src/xenia/base/threading_posix.cc +++ b/src/xenia/base/threading_posix.cc @@ -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(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(); diff --git a/src/xenia/base/threading_win.cc b/src/xenia/base/threading_win.cc index 2b59d97f0..605c2ccbf 100644 --- a/src/xenia/base/threading_win.cc +++ b/src/xenia/base/threading_win.cc @@ -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(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();