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

pull/8/head
michi_cc 8 years ago
parent a08df612be
commit 36cbb54a9d

@ -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;

@ -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

@ -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 */

@ -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;

@ -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;
}

@ -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 (;;) {

@ -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) {

Loading…
Cancel
Save