Skip to content

Instantly share code, notes, and snippets.

Created April 13, 2014 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2f6e2440976fcc969f32 to your computer and use it in GitHub Desktop.
Save anonymous/2f6e2440976fcc969f32 to your computer and use it in GitHub Desktop.
@@ -268,10 +268,10 @@ inline u32 getTime(TimePrecision prec)
#if (defined(linux) || defined(__linux))
- #include <sys/prctl.h>
+ #include <pthread.h>
- inline void setThreadName(const char* name) {
- prctl(PR_SET_NAME,name);
+ inline void setThreadName(const char *name) {
+ pthread_setname_np(pthread_self(), name);
}
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
/* BSD doesn't seem to support thread names. If you know about a way
@@ -280,8 +280,47 @@ inline void setThreadName(const char* name) {
*/
inline void setThreadName(const char* name) {}
#elif defined(_WIN32)
- // threadnames are not supported on windows
- inline void setThreadName(const char* name) {}
+ #include <windows.h>
+ #define MS_VC_EXCEPTION 0x406d1388
+
+ typedef struct tagTHREADNAME_INFO
+ {
+ DWORD dwType; // must be 0x1000
+ LPCSTR szName; // pointer to name (in same addr space)
+ DWORD dwThreadID; // thread ID (-1 caller thread)
+ DWORD dwFlags; // reserved for future use, most be zero
+ } THREADNAME_INFO;
+
+ inline void SetThreadName(DWORD dwThreadID, LPCTSTR szThreadName)
+ {
+ THREADNAME_INFO info;
+ info.dwType = 0x1000;
+ info.szName = szThreadName;
+ info.dwThreadID = dwThreadID;
+ info.dwFlags = 0;
+
+ __try
+ {
+ RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(DWORD),
+ (DWORD *)&info);
+ }
+ _except (EXCEPTION_CONTINUE_EXECUTION)
+ {
+ }
+ }
+
+ inline void setThreadName(const char *name) {
+ SetThreadName(-1, name);
+ }
+ #elif defined(__APPLE__)
+ #include <pthread.h>
+
+ inline void setThreadName(const char *name) {
+ pthread_setname_np(name);
+
+ inline void setThreadName(const char *name) {
+ pthread_setname_np(name);
+ }
#else
#warning "Unknown platform for setThreadName support, you wont have threadname support."
inline void setThreadName(const char* name) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment