1
0
Fork 0

(svn r27673) -Add: [Win32] Thread names for windows debuggers.

release/1.7
michi_cc 2016-10-30 18:22:55 +00:00
parent b2fe2c6e3d
commit c83306391e
7 changed files with 59 additions and 4 deletions

View File

@ -14,6 +14,7 @@
#include "win32_m.h" #include "win32_m.h"
#include <windows.h> #include <windows.h>
#include <mmsystem.h> #include <mmsystem.h>
#include "../os/windows/win32.h"
#include "../safeguards.h" #include "../safeguards.h"
@ -105,6 +106,8 @@ static bool MidiIntIsSongPlaying()
static DWORD WINAPI MidiThread(LPVOID arg) static DWORD WINAPI MidiThread(LPVOID arg)
{ {
SetWin32ThreadName(-1, "ottd:win-midi");
do { do {
char *s; char *s;
int vol; int vol;

View File

@ -785,3 +785,36 @@ uint GetCPUCoreCount()
GetSystemInfo(&info); GetSystemInfo(&info);
return info.dwNumberOfProcessors; return info.dwNumberOfProcessors;
} }
#ifdef _MSC_VER
/* Code from MSDN: https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx */
const DWORD MS_VC_EXCEPTION = 0x406D1388;
#pragma pack(push,8)
typedef struct {
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)
/**
* Signal thread name to any attached debuggers.
*/
void SetWin32ThreadName(DWORD dwThreadID, const char* threadName)
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = threadName;
info.dwThreadID = dwThreadID;
info.dwFlags = 0;
#pragma warning(push)
#pragma warning(disable: 6320 6322)
__try {
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
} __except (EXCEPTION_EXECUTE_HANDLER) {
}
#pragma warning(pop)
}
#endif

View File

@ -39,4 +39,10 @@ HRESULT OTTDSHGetFolderPath(HWND, int, HANDLE, DWORD, LPTSTR);
#define SHGFP_TYPE_CURRENT 0 #define SHGFP_TYPE_CURRENT 0
#endif /* __MINGW32__ */ #endif /* __MINGW32__ */
#ifdef _MSC_VER
void SetWin32ThreadName(DWORD dwThreadID, const char* threadName);
#else
void SetWin32ThreadName(DWORD dwThreadID, const char* threadName) {}
#endif
#endif /* WIN32_H */ #endif /* WIN32_H */

View File

@ -19,6 +19,7 @@
#include "win32_s.h" #include "win32_s.h"
#include <windows.h> #include <windows.h>
#include <mmsystem.h> #include <mmsystem.h>
#include "../os/windows/win32.h"
#include "../safeguards.h" #include "../safeguards.h"
@ -41,6 +42,8 @@ static void PrepareHeader(WAVEHDR *hdr)
static DWORD WINAPI SoundThread(LPVOID arg) static DWORD WINAPI SoundThread(LPVOID arg)
{ {
SetWin32ThreadName(-1, "ottd:win-sound");
do { do {
for (WAVEHDR *hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) { for (WAVEHDR *hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) {
if ((hdr->dwFlags & WHDR_INQUEUE) != 0) continue; if ((hdr->dwFlags & WHDR_INQUEUE) != 0) continue;

View File

@ -16,6 +16,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <windows.h> #include <windows.h>
#include <process.h> #include <process.h>
#include "../os/windows/win32.h"
#include "../safeguards.h" #include "../safeguards.h"
@ -29,17 +30,19 @@ private:
OTTDThreadFunc proc; ///< External thread procedure. OTTDThreadFunc proc; ///< External thread procedure.
void *param; ///< Parameter for the external thread procedure. void *param; ///< Parameter for the external thread procedure.
bool self_destruct; ///< Free ourselves when done? bool self_destruct; ///< Free ourselves when done?
const char *name; ///< Thread name.
public: public:
/** /**
* Create a win32 thread and start it, calling proc(param). * Create a win32 thread and start it, calling proc(param).
*/ */
ThreadObject_Win32(OTTDThreadFunc proc, void *param, bool self_destruct) : ThreadObject_Win32(OTTDThreadFunc proc, void *param, bool self_destruct, const char *name) :
thread(NULL), thread(NULL),
id(0), id(0),
proc(proc), proc(proc),
param(param), param(param),
self_destruct(self_destruct) self_destruct(self_destruct),
name(name)
{ {
this->thread = (HANDLE)_beginthreadex(NULL, 0, &stThreadProc, this, CREATE_SUSPENDED, &this->id); this->thread = (HANDLE)_beginthreadex(NULL, 0, &stThreadProc, this, CREATE_SUSPENDED, &this->id);
if (this->thread == NULL) return; if (this->thread == NULL) return;
@ -85,6 +88,10 @@ private:
*/ */
void ThreadProc() void ThreadProc()
{ {
#ifdef _MSC_VER
/* Set thread name for debuggers. Has to be done from the thread due to a race condition in older MS debuggers. */
SetWin32ThreadName(-1, this->name);
#endif
try { try {
this->proc(this->param); this->proc(this->param);
} catch (OTTDThreadExitSignal) { } catch (OTTDThreadExitSignal) {
@ -98,7 +105,7 @@ private:
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread, const char *name) /* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread, const char *name)
{ {
ThreadObject *to = new ThreadObject_Win32(proc, param, thread == NULL); ThreadObject *to = new ThreadObject_Win32(proc, param, thread == NULL, name);
if (thread != NULL) *thread = to; if (thread != NULL) *thread = to;
return true; return true;
} }

View File

@ -84,6 +84,7 @@ static void DedicatedSignalHandler(int sig)
# endif # endif
# include <time.h> # include <time.h>
# include <tchar.h> # include <tchar.h>
# include "../os/windows/win32.h"
static HANDLE _hInputReady, _hWaitForInputHandling; static HANDLE _hInputReady, _hWaitForInputHandling;
static HANDLE _hThread; // Thread to close static HANDLE _hThread; // Thread to close
static char _win_console_thread_buffer[200]; static char _win_console_thread_buffer[200];
@ -95,6 +96,8 @@ static void WINAPI CheckForConsoleInput()
/* WinCE doesn't support console stuff */ /* WinCE doesn't support console stuff */
return; return;
#else #else
SetWin32ThreadName(-1, "ottd:win-console");
DWORD nb; DWORD nb;
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
for (;;) { for (;;) {

View File

@ -1199,7 +1199,7 @@ void VideoDriver_Win32::MainLoop()
_draw_threaded = false; _draw_threaded = false;
} else { } else {
_draw_continue = true; _draw_continue = true;
_draw_threaded = ThreadObject::New(&PaintWindowThread, NULL, &_draw_thread); _draw_threaded = ThreadObject::New(&PaintWindowThread, NULL, &_draw_thread, "ottd:draw-win32");
/* Free the mutex if we won't be able to use it. */ /* Free the mutex if we won't be able to use it. */
if (!_draw_threaded) { if (!_draw_threaded) {