From 061007ee29a65bc6178f5cbb148669597fb36947 Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Tue, 1 Apr 2014 16:56:11 -0400 Subject: [PATCH] In debug builds, set the thread name in the debugger to match the name given to fc::thread ctor --- src/thread/thread.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/thread/thread.cpp b/src/thread/thread.cpp index 3ab54f6..3bb7444 100644 --- a/src/thread/thread.cpp +++ b/src/thread/thread.cpp @@ -4,6 +4,55 @@ #include #include "thread_d.hpp" +#if defined(_MSC_VER) && !defined(NDEBUG) +# include +const DWORD MS_VC_EXCEPTION=0x406D1388; + +#pragma pack(push,8) +typedef struct tagTHREADNAME_INFO +{ + DWORD dwType; // Must be 0x1000. + LPCSTR szName; // Pointer to name (in user addr space). + DWORD dwThreadID; // Thread ID (-1=caller thread). + DWORD dwFlags; // Reserved for future use, must be zero. +} THREADNAME_INFO; +#pragma pack(pop) + +static void set_thread_name(const char* threadName) +{ + THREADNAME_INFO info; + info.dwType = 0x1000; + info.szName = threadName; + info.dwThreadID = -1; + info.dwFlags = 0; + + __try + { + RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info); + } + __except(EXCEPTION_EXECUTE_HANDLER) + { + } +} +#elif defined(__linux__) && !defined(NDEBUG) +# include +static void set_thread_name(const char* threadName) +{ + pthread_setname_np(pthread_self(), threadName); +} +#elif defined(__APPLE__) && !defined(NDEBUG) +# include +static void set_thread_name(const char* threadName) +{ + pthread_setname_np(threadName); +} +#else +static void set_thread_name(const char* threadName) +{ + // do nothing in release mode +} +#endif + namespace fc { const char* thread_name() { return thread::current().name().c_str(); @@ -25,6 +74,7 @@ namespace fc { promise::ptr p(new promise()); boost::thread* t = new boost::thread( [this,p,name]() { try { + set_thread_name(name); // set thread's name for the debugger to display this->my = new thread_d(*this); current_thread() = this; p->set_value();