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/CriticalSection.h | 1 + src/OSSupport/IsThread.cpp | 46 +++++++++++++++++++++++++++++++++++++++-- src/OSSupport/IsThread.h | 5 +---- 3 files changed, 46 insertions(+), 6 deletions(-) (limited to 'src/OSSupport') diff --git a/src/OSSupport/CriticalSection.h b/src/OSSupport/CriticalSection.h index 19e4f78af..17fcdfc12 100644 --- a/src/OSSupport/CriticalSection.h +++ b/src/OSSupport/CriticalSection.h @@ -1,6 +1,7 @@ #pragma once #include +#include 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; } } diff --git a/src/OSSupport/IsThread.h b/src/OSSupport/IsThread.h index 6aadaf447..131c6950e 100644 --- a/src/OSSupport/IsThread.h +++ b/src/OSSupport/IsThread.h @@ -16,8 +16,7 @@ In the descending class' constructor call the Start() method to start the thread #pragma once -#ifndef CISTHREAD_H_INCLUDED -#define CISTHREAD_H_INCLUDED +#include @@ -56,5 +55,3 @@ protected: - -#endif // CISTHREAD_H_INCLUDED -- cgit v1.2.3