Unravel Engine C++ Reference
Loading...
Searching...
No Matches
thread.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "config.hpp"
4
5#include <thread>
6
7// An attempt at making a wrapper to deal with many Linuxes as well as Windows. Please edit as needed.
8#if UNRAVEL_PLATFORM_WINDOWS && UNRAVEL_COMPILER_MSVC
9#define WIN32_LEAN_AND_MEAN
10#include <Windows.h>
11
12#ifdef min
13#undef min
14#endif
15
16#ifdef max
17#undef max
18#endif
19
20namespace platform
21{
22#pragma pack(push, 8)
23typedef struct tagTHREADNAME_INFO
24{
25 DWORD dwType; // Must be 0x1000.
26 LPCSTR szName; // Pointer to name (in user addr space).
27 DWORD dwThreadID; // Thread ID (-1=caller thread).
28 DWORD dwFlags; // Reserved for future use, must be zero.
29} THREADNAME_INFO;
30#pragma pack(pop)
31
32inline void set_thread_name(DWORD dwThreadID, const char* threadName)
33{
34 THREADNAME_INFO info;
35 info.dwType = 0x1000;
36 info.szName = threadName;
37 info.dwThreadID = dwThreadID;
38 info.dwFlags = 0;
39
40 static const DWORD MS_VC_EXCEPTION = 0x406D1388;
41
42 // Push an exception handler to ignore all following exceptions
43#pragma warning(push)
44#pragma warning(disable : 6320 6322)
45 __try
46 {
47 RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
48 }
49 __except(EXCEPTION_EXECUTE_HANDLER)
50 {
51 }
52#pragma warning(pop)
53}
54
55inline void set_thread_name(const char* threadName)
56{
57 DWORD threadId = ::GetCurrentThreadId();
58 //DWORD threadId = ::GetThreadId(reinterpret_cast<HANDLE>(thread.native_handle()));
59 set_thread_name(threadId, threadName);
60}
61} // namespace platform
62#elif UNRAVEL_PLATFORM_LINUX
63#include <pthread.h>
64namespace platform
65{
66inline void set_thread_name(const char* threadName)
67{
68 pthread_setname_np(pthread_self(), threadName);
69}
70} // namespace platform
71#elif UNRAVEL_PLATFORM_OSX
72#include <pthread.h>
73namespace platform
74{
75inline void set_thread_name(const char* threadName)
76{
77 pthread_setname_np(threadName);
78}
79} // namespace platform
80#else
81namespace platform
82{
83inline void set_thread_name(const char* threadName)
84{
85}
86} // namespace platform
87#endif
88
void set_thread_name(const char *threadName)
Definition thread.hpp:83