diff options
Diffstat (limited to 'Source/Threading/thread_name.cpp')
-rw-r--r-- | Source/Threading/thread_name.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/Source/Threading/thread_name.cpp b/Source/Threading/thread_name.cpp new file mode 100644 index 00000000..226dd8a6 --- /dev/null +++ b/Source/Threading/thread_name.cpp @@ -0,0 +1,64 @@ +//----------------------------------------------------------------------------- +// Name: thread_name.cpp +// Developer: Wolfire Games LLC +// Description: +// License: Read below +//----------------------------------------------------------------------------- +// +// Copyright 2022 Wolfire Games LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//----------------------------------------------------------------------------- +#include <Threading/thread_name.h> + +#ifdef _MSC_VER +// Adapted from https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx +#define NOMINMAX +#include <windows.h> +const DWORD MS_VC_EXCEPTION = 0x406D1388; +#pragma pack(push,8) +typedef struct tagTHREADNAME_INFO +{ + DWORD dwType; // Must be 0x1000. + LPCSTR szName; // Pointer to name (in user addr space). + DWORD dwThreadID; // Thread ID (-1=caller thread). + DWORD dwFlags; // Reserved for future use, must be zero. +} THREADNAME_INFO; +#pragma pack(pop) +void NameCurrentThread(const char* threadName) { + THREADNAME_INFO info; + info.dwType = 0x1000; + info.szName = threadName; + info.dwThreadID = GetCurrentThreadId(); + info.dwFlags = 0; +#pragma warning(push) +#pragma warning(disable: 6320 6322) + __try{ + RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info); + } + __except (EXCEPTION_EXECUTE_HANDLER){ + } +#pragma warning(pop) +} +#elif PLATFORM_LINUX +#include <sys/prctl.h> +void NameCurrentThread(const char* name) +{ + prctl(PR_SET_NAME,(long unsigned)name,0,0,0); +} +#else // _MSC_VER +void NameCurrentThread(const char* name) +{ +} +#endif // _MSC_VER |