Support 24-bit LAF files
[openal-soft.git] / common / althrd_setname.cpp
blobb92b05d1c91ff6c3c10377db5ea587c308278877
2 #include "config.h"
4 #include "althrd_setname.h"
7 #ifdef _WIN32
8 #define WIN32_LEAN_AND_MEAN
9 #include <windows.h>
11 void althrd_setname(const char *name [[maybe_unused]])
13 #if defined(_MSC_VER) && !defined(_M_ARM)
15 #define MS_VC_EXCEPTION 0x406D1388
16 #pragma pack(push,8)
17 struct InfoStruct {
18 DWORD dwType; // Must be 0x1000.
19 LPCSTR szName; // Pointer to name (in user addr space).
20 DWORD dwThreadID; // Thread ID (-1=caller thread).
21 DWORD dwFlags; // Reserved for future use, must be zero.
23 #pragma pack(pop)
24 InfoStruct info{};
25 info.dwType = 0x1000;
26 info.szName = name;
27 info.dwThreadID = ~DWORD{0};
28 info.dwFlags = 0;
30 /* FIXME: How to do this on MinGW? */
31 __try {
32 RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
34 __except(EXCEPTION_CONTINUE_EXECUTION) {
36 #undef MS_VC_EXCEPTION
37 #endif
40 #else
42 #include <pthread.h>
43 #ifdef HAVE_PTHREAD_NP_H
44 #include <pthread_np.h>
45 #endif
47 namespace {
49 using setname_t1 = int(*)(const char*);
50 using setname_t2 = int(*)(pthread_t, const char*);
51 using setname_t3 = void(*)(pthread_t, const char*);
52 using setname_t4 = int(*)(pthread_t, const char*, void*);
54 [[maybe_unused]] void setname_caller(setname_t1 func, const char *name)
55 { func(name); }
57 [[maybe_unused]] void setname_caller(setname_t2 func, const char *name)
58 { func(pthread_self(), name); }
60 [[maybe_unused]] void setname_caller(setname_t3 func, const char *name)
61 { func(pthread_self(), name); }
63 [[maybe_unused]] void setname_caller(setname_t4 func, const char *name)
64 { func(pthread_self(), "%s", const_cast<char*>(name)); /* NOLINT(*-const-cast) */ }
66 } // namespace
68 void althrd_setname(const char *name [[maybe_unused]])
70 #if defined(HAVE_PTHREAD_SET_NAME_NP)
71 setname_caller(pthread_set_name_np, name);
72 #elif defined(HAVE_PTHREAD_SETNAME_NP)
73 setname_caller(pthread_setname_np, name);
74 #endif
77 #endif