From 51fa6b4090ee930d03592550613592b1087fb788 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 23 Oct 2014 23:58:01 +0100 Subject: Suggestions --- src/OSSupport/IsThread.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) (limited to 'src/OSSupport/IsThread.cpp') diff --git a/src/OSSupport/IsThread.cpp b/src/OSSupport/IsThread.cpp index 94c867d19..02d8a77c7 100644 --- a/src/OSSupport/IsThread.cpp +++ b/src/OSSupport/IsThread.cpp @@ -11,6 +11,40 @@ +#if defined(_MSC_VER) && defined(_DEBUG) + // Code adapted from MSDN: http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx + + const DWORD MS_VC_EXCEPTION = 0x406D1388; + #pragma pack(push, 8) + struct THREADNAME_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. + }; + #pragma pack(pop) + + /** Sets the name of a thread with the specified ID + (When in MSVC, the debugger provides "thread naming" by catching special exceptions) + */ + static void SetThreadName(std::thread * a_Thread, const char * a_ThreadName) + { + THREADNAME_INFO info { 0x1000, a_ThreadName, GetThreadId(a_Thread->native_handle()), 0 }; + __try + { + RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info); + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + } + } +#endif // _MSC_VER && _DEBUG + + + + + //////////////////////////////////////////////////////////////////////////////// // cIsThread: @@ -39,11 +73,19 @@ bool cIsThread::Start(void) try { m_Thread = std::thread(&cIsThread::Execute, this); + + #if defined (_MSC_VER) && defined(_DEBUG) + if (!m_ThreadName.empty()) + { + SetThreadName(&m_Thread, m_ThreadName.c_str()); + } + #endif + return true; } catch (std::system_error & a_Exception) { - LOGERROR("ERROR: Could not create thread \"%s\", error = %s!", m_ThreadName.c_str(), a_Exception.code(), a_Exception.what()); + LOGERROR("cIsThread::Wait (std::thread) error %i: could not construct thread %s; %s", m_ThreadName.c_str(), a_Exception.code().value(), a_Exception.what()); return false; } } @@ -77,7 +119,7 @@ bool cIsThread::Wait(void) } catch (std::system_error & a_Exception) { - LOGERROR("ERROR: Could wait for thread \"%s\" to finish, error = %s!", m_ThreadName.c_str(), a_Exception.code(), a_Exception.what()); + LOGERROR("cIsThread::Wait (std::thread) error %i: could not join thread %s; %s", m_ThreadName.c_str(), a_Exception.code().value(), a_Exception.what()); return false; } } -- cgit v1.2.3