diff options
author | Tiger Wang <ziwei.tiger@hotmail.co.uk> | 2014-10-24 00:58:01 +0200 |
---|---|---|
committer | Tiger Wang <ziwei.tiger@hotmail.co.uk> | 2014-10-24 00:58:01 +0200 |
commit | 51fa6b4090ee930d03592550613592b1087fb788 (patch) | |
tree | a41682a171b54a4cbb721542a6135bcaca2bfef6 /src/OSSupport | |
parent | Added FastRandom.* back to CMakeLists.txt. (diff) | |
download | cuberite-51fa6b4090ee930d03592550613592b1087fb788.tar cuberite-51fa6b4090ee930d03592550613592b1087fb788.tar.gz cuberite-51fa6b4090ee930d03592550613592b1087fb788.tar.bz2 cuberite-51fa6b4090ee930d03592550613592b1087fb788.tar.lz cuberite-51fa6b4090ee930d03592550613592b1087fb788.tar.xz cuberite-51fa6b4090ee930d03592550613592b1087fb788.tar.zst cuberite-51fa6b4090ee930d03592550613592b1087fb788.zip |
Diffstat (limited to 'src/OSSupport')
-rw-r--r-- | src/OSSupport/CriticalSection.h | 1 | ||||
-rw-r--r-- | src/OSSupport/IsThread.cpp | 46 | ||||
-rw-r--r-- | src/OSSupport/IsThread.h | 5 |
3 files changed, 46 insertions, 6 deletions
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 <mutex> +#include <thread> 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 <thread> @@ -56,5 +55,3 @@ protected: - -#endif // CISTHREAD_H_INCLUDED |