diff --git a/thread/threadutil.cpp b/thread/threadutil.cpp index 24ff853c1b..6dcaa4369d 100644 --- a/thread/threadutil.cpp +++ b/thread/threadutil.cpp @@ -1,16 +1,24 @@ #ifdef _WIN32 #include +#define TLS_SUPPORTED +#elif defined(ANDROID) +#define TLS_SUPPORTED #endif + +#include "base/basictypes.h" +#include "base/logging.h" #include "thread/threadutil.h" -void setCurrentThreadName(const char* szThreadName) -{ -#ifdef _WIN32 - static const DWORD MS_VC_EXCEPTION = 0x406D1388; +#ifdef TLS_SUPPORTED +static __THREAD const char *curThreadName; +#endif +void setCurrentThreadName(const char* threadName) { +#ifdef _WIN32 + // Set the debugger-visible threadname through an unholy magic hack + static const DWORD MS_VC_EXCEPTION = 0x406D1388; #pragma pack(push,8) - struct THREADNAME_INFO - { + struct THREADNAME_INFO { DWORD dwType; // must be 0x1000 LPCSTR szName; // pointer to name (in user addr space) DWORD dwThreadID; // thread ID (-1=caller thread) @@ -19,7 +27,7 @@ void setCurrentThreadName(const char* szThreadName) #pragma pack(pop) info.dwType = 0x1000; - info.szName = szThreadName; + info.szName = threadName; info.dwThreadID = -1; //dwThreadID; info.dwFlags = 0; @@ -31,5 +39,17 @@ void setCurrentThreadName(const char* szThreadName) {} #else // Do nothing +#endif + // Set the locally known threadname using a thread local variable. +#ifdef TLS_SUPPORTED + curThreadName = threadName; +#endif +} + +void AssertCurrentThreadName(const char *threadName) { +#ifdef TLS_SUPPORTED + if (strcmp(curThreadName, threadName) != 0) { + ELOG("Thread name assert failed: Expected %s, was %s", threadName, curThreadName); + } #endif } diff --git a/thread/threadutil.h b/thread/threadutil.h index 347562a26c..2c80ef7736 100644 --- a/thread/threadutil.h +++ b/thread/threadutil.h @@ -1,56 +1,6 @@ #pragma once -#include - -#ifdef _WIN32 -#define NOMINMAX -#define WIN32_LEAN_AND_MEAN -#include -#include -#else - -#include - -#ifndef _POSIX_THREADS -#error unsupported platform (no pthreads?) -#endif - -#include - -#endif - -void setCurrentThreadName(const char *name); - -/* -class thread { -private: -#ifdef _WIN32 - typedef HANDLE thread_; -#else - typedef pthread_t thread_; -#endif - -public: - //void run(std::function threadFunc) { - // func_ = - //} - - void wait() { - - } -};*/ - - -#ifdef _WIN32 -#define THREAD_HANDLE HANDLE -#else -#define THREAD_HANDLE pthread_t -#endif - - -// TODO: replace this abomination with std::thread - -class thread { -public: - virtual void Run(); -}; \ No newline at end of file +// Note that name must be a global string that lives until the end of the process, +// for assertThreadName to work. +void setCurrentThreadName(const char *threadName); +void AssertCurrentThreadName(const char *threadName);