4 #include "althrd_setname.h"
8 #define WIN32_LEAN_AND_MEAN
11 void althrd_setname(const char *name
[[maybe_unused
]])
13 #if defined(_MSC_VER) && !defined(_M_ARM)
15 #define MS_VC_EXCEPTION 0x406D1388
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.
27 info
.dwThreadID
= ~DWORD
{0};
30 /* FIXME: How to do this on MinGW? */
32 RaiseException(MS_VC_EXCEPTION
, 0, sizeof(info
)/sizeof(ULONG_PTR
), (ULONG_PTR
*)&info
);
34 __except(EXCEPTION_CONTINUE_EXECUTION
) {
36 #undef MS_VC_EXCEPTION
43 #ifdef HAVE_PTHREAD_NP_H
44 #include <pthread_np.h>
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
)
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) */ }
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
);