Add method to get name of current thread

This commit is contained in:
Jonathan G Rennison
2018-08-25 23:39:10 +01:00
parent 312595b737
commit dde88887ae
6 changed files with 59 additions and 0 deletions

View File

@@ -133,4 +133,12 @@ void SetSelfAsMainThread();
*/ */
bool IsMainThread(); bool IsMainThread();
/**
* Get the name of the current thread, if any.
* @param str The start of the buffer.
* @param last The last char of the buffer.
* @return Number of chars written to str.
*/
int GetThreadName(char *str, const char *last);
#endif /* THREAD_H */ #endif /* THREAD_H */

View File

@@ -199,3 +199,5 @@ private:
void SetSelfAsMainThread() { } void SetSelfAsMainThread() { }
bool IsMainThread() { return false; } bool IsMainThread() { return false; }
int GetThreadName(char *str, const char *last) { return 0; }

View File

@@ -37,3 +37,5 @@ public:
void SetSelfAsMainThread() { } void SetSelfAsMainThread() { }
bool IsMainThread() { return true; } bool IsMainThread() { return true; }
int GetThreadName(char *str, const char *last) { return 0; }

View File

@@ -149,3 +149,5 @@ public:
void SetSelfAsMainThread() { } void SetSelfAsMainThread() { }
bool IsMainThread() { return false; } bool IsMainThread() { return false; }
int GetThreadName(char *str, const char *last) { return 0; }

View File

@@ -11,6 +11,7 @@
#include "../stdafx.h" #include "../stdafx.h"
#include "thread.h" #include "thread.h"
#include "../string_func.h"
#include <pthread.h> #include <pthread.h>
#include <errno.h> #include <errno.h>
@@ -201,3 +202,17 @@ bool IsMainThread()
{ {
return main_thread == pthread_self(); return main_thread == pthread_self();
} }
int GetThreadName(char *str, const char *last)
{
#if defined(__GLIBC__)
#if __GLIBC_PREREQ(2, 12)
char buffer[16];
int result = pthread_getname_np(pthread_self(), buffer, sizeof(buffer));
if (result == 0) {
return seprintf(str, last, "%s", buffer);
}
#endif
#endif
return 0;
}

View File

@@ -13,13 +13,19 @@
#include "thread.h" #include "thread.h"
#include "../debug.h" #include "../debug.h"
#include "../core/alloc_func.hpp" #include "../core/alloc_func.hpp"
#include "../scope.h"
#include "../string_func.h"
#include <stdlib.h> #include <stdlib.h>
#include <windows.h> #include <windows.h>
#include <process.h> #include <process.h>
#include "../os/windows/win32.h" #include "../os/windows/win32.h"
#include <map>
#include <string>
#include "../safeguards.h" #include "../safeguards.h"
static void Win32SetThreadName(uint id, const char *name);
/** /**
* Win32 thread version for ThreadObject. * Win32 thread version for ThreadObject.
*/ */
@@ -46,6 +52,7 @@ public:
{ {
this->thread = (HANDLE)_beginthreadex(NULL, 0, &stThreadProc, this, CREATE_SUSPENDED, &this->id); this->thread = (HANDLE)_beginthreadex(NULL, 0, &stThreadProc, this, CREATE_SUSPENDED, &this->id);
if (this->thread == NULL) return; if (this->thread == NULL) return;
Win32SetThreadName(this->id, name);
ResumeThread(this->thread); ResumeThread(this->thread);
} }
@@ -177,3 +184,26 @@ bool IsMainThread()
{ {
return main_thread_id == GetCurrentThreadId(); return main_thread_id == GetCurrentThreadId();
} }
static std::map<uint, std::string> _thread_name_map;
static ThreadMutex_Win32 _thread_name_map_mutex;
static void Win32SetThreadName(uint id, const char *name)
{
_thread_name_map_mutex.BeginCritical();
_thread_name_map[id] = name;
_thread_name_map_mutex.EndCritical();
}
int GetThreadName(char *str, const char *last)
{
_thread_name_map_mutex.BeginCritical();
auto guard = scope_guard([&]() {
_thread_name_map_mutex.EndCritical();
});
auto iter = _thread_name_map.find(GetCurrentThreadId());
if (iter != _thread_name_map.end()) {
return seprintf(str, last, "%s", iter->second.c_str());
}
return 0;
}