diff options
-rw-r--r-- | src/music/win32_m.cpp | 3 | ||||
-rw-r--r-- | src/os/windows/win32.cpp | 33 | ||||
-rw-r--r-- | src/os/windows/win32.h | 6 | ||||
-rw-r--r-- | src/sound/win32_s.cpp | 3 | ||||
-rw-r--r-- | src/thread/thread_win32.cpp | 13 | ||||
-rw-r--r-- | src/video/dedicated_v.cpp | 3 | ||||
-rw-r--r-- | src/video/win32_v.cpp | 2 |
7 files changed, 59 insertions, 4 deletions
diff --git a/src/music/win32_m.cpp b/src/music/win32_m.cpp index d3a7019a0..fff0376a0 100644 --- a/src/music/win32_m.cpp +++ b/src/music/win32_m.cpp @@ -14,6 +14,7 @@ #include "win32_m.h" #include <windows.h> #include <mmsystem.h> +#include "../os/windows/win32.h" #include "../safeguards.h" @@ -105,6 +106,8 @@ static bool MidiIntIsSongPlaying() static DWORD WINAPI MidiThread(LPVOID arg) { + SetWin32ThreadName(-1, "ottd:win-midi"); + do { char *s; int vol; diff --git a/src/os/windows/win32.cpp b/src/os/windows/win32.cpp index 344d24314..3fd1777fa 100644 --- a/src/os/windows/win32.cpp +++ b/src/os/windows/win32.cpp @@ -785,3 +785,36 @@ uint GetCPUCoreCount() GetSystemInfo(&info); 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 diff --git a/src/os/windows/win32.h b/src/os/windows/win32.h index 4e53879cf..c3ded817a 100644 --- a/src/os/windows/win32.h +++ b/src/os/windows/win32.h @@ -39,4 +39,10 @@ HRESULT OTTDSHGetFolderPath(HWND, int, HANDLE, DWORD, LPTSTR); #define SHGFP_TYPE_CURRENT 0 #endif /* __MINGW32__ */ +#ifdef _MSC_VER +void SetWin32ThreadName(DWORD dwThreadID, const char* threadName); +#else +void SetWin32ThreadName(DWORD dwThreadID, const char* threadName) {} +#endif + #endif /* WIN32_H */ diff --git a/src/sound/win32_s.cpp b/src/sound/win32_s.cpp index 45f88172a..c9c1a8afd 100644 --- a/src/sound/win32_s.cpp +++ b/src/sound/win32_s.cpp @@ -19,6 +19,7 @@ #include "win32_s.h" #include <windows.h> #include <mmsystem.h> +#include "../os/windows/win32.h" #include "../safeguards.h" @@ -41,6 +42,8 @@ static void PrepareHeader(WAVEHDR *hdr) static DWORD WINAPI SoundThread(LPVOID arg) { + SetWin32ThreadName(-1, "ottd:win-sound"); + do { for (WAVEHDR *hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) { if ((hdr->dwFlags & WHDR_INQUEUE) != 0) continue; diff --git a/src/thread/thread_win32.cpp b/src/thread/thread_win32.cpp index 81a721253..a01ea8e10 100644 --- a/src/thread/thread_win32.cpp +++ b/src/thread/thread_win32.cpp @@ -16,6 +16,7 @@ #include <stdlib.h> #include <windows.h> #include <process.h> +#include "../os/windows/win32.h" #include "../safeguards.h" @@ -29,17 +30,19 @@ private: OTTDThreadFunc proc; ///< External thread procedure. void *param; ///< Parameter for the external thread procedure. bool self_destruct; ///< Free ourselves when done? + const char *name; ///< Thread name. public: /** * 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), id(0), proc(proc), param(param), - self_destruct(self_destruct) + self_destruct(self_destruct), + name(name) { this->thread = (HANDLE)_beginthreadex(NULL, 0, &stThreadProc, this, CREATE_SUSPENDED, &this->id); if (this->thread == NULL) return; @@ -85,6 +88,10 @@ private: */ 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 { this->proc(this->param); } catch (OTTDThreadExitSignal) { @@ -98,7 +105,7 @@ private: /* 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; return true; } diff --git a/src/video/dedicated_v.cpp b/src/video/dedicated_v.cpp index e038df975..5e2be481c 100644 --- a/src/video/dedicated_v.cpp +++ b/src/video/dedicated_v.cpp @@ -84,6 +84,7 @@ static void DedicatedSignalHandler(int sig) # endif # include <time.h> # include <tchar.h> +# include "../os/windows/win32.h" static HANDLE _hInputReady, _hWaitForInputHandling; static HANDLE _hThread; // Thread to close static char _win_console_thread_buffer[200]; @@ -95,6 +96,8 @@ static void WINAPI CheckForConsoleInput() /* WinCE doesn't support console stuff */ return; #else + SetWin32ThreadName(-1, "ottd:win-console"); + DWORD nb; HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); for (;;) { diff --git a/src/video/win32_v.cpp b/src/video/win32_v.cpp index 4dcc9eb70..c37ebd7dd 100644 --- a/src/video/win32_v.cpp +++ b/src/video/win32_v.cpp @@ -1199,7 +1199,7 @@ void VideoDriver_Win32::MainLoop() _draw_threaded = false; } else { _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. */ if (!_draw_threaded) { |