1
0
forked from 0ad/0ad

Fix windows SEH hook when crashing in an std::thread

Follows 107d3d461f and other 'pthread->std::thread' diffs.

Windows uses Structured Exception Handling to allow reporting errors
(both C++ and hardware) nicely. This works by wrapping the code in a
__try __catch block.
The pthread wrapper did this automatically, but we now need to do it
explicitly for std::thread.

Tested by: Stan
Differential Revision: https://code.wildfiregames.com/D3261
This was SVN commit r24530.
This commit is contained in:
wraitii 2021-01-10 08:39:54 +00:00
parent f876db857a
commit 5ee8354e99
25 changed files with 141 additions and 58 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -30,6 +30,7 @@
#include "ps/CLogger.h"
#include "ps/FileIo.h"
#include "ps/Profile.h"
#include "ps/Threading.h"
#include "ps/scripting/JSInterface_VFS.h"
#include "scriptinterface/ScriptContext.h"
#include "scriptinterface/ScriptConversions.h"
@ -81,10 +82,10 @@ void CMapGeneratorWorker::Initialize(const VfsPath& scriptFile, const std::strin
m_Settings = settings;
// Launch the worker thread
m_WorkerThread = std::thread(RunThread, this);
m_WorkerThread = std::thread(Threading::HandleExceptions<RunThread>::Wrapper, this);
}
void* CMapGeneratorWorker::RunThread(CMapGeneratorWorker* self)
void CMapGeneratorWorker::RunThread(CMapGeneratorWorker* self)
{
debug_SetThreadName("MapGenerator");
g_Profiler2.RegisterCurrentThread("MapGenerator");
@ -109,8 +110,6 @@ void* CMapGeneratorWorker::RunThread(CMapGeneratorWorker* self)
// At this point the random map scripts are done running, so the thread has no further purpose
// and can die. The data will be stored in m_MapData already if successful, or m_Progress
// will contain an error value on failure.
return NULL;
}
bool CMapGeneratorWorker::Run()

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -186,7 +186,7 @@ private:
/**
* Perform map generation in an independent thread.
*/
static void* RunThread(CMapGeneratorWorker* self);
static void RunThread(CMapGeneratorWorker* self);
/**
* Perform the map generation.

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2019 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -80,7 +80,7 @@ struct ItemNameGeq
template<typename value_t>
typename CShaderParams<value_t>::SItems* CShaderParams<value_t>::GetInterned(const SItems& items)
{
ENSURE(ThreadUtil::IsMainThread()); // s_InternedItems is not thread-safe
ENSURE(Threading::IsMainThread()); // s_InternedItems is not thread-safe
typename InternedItems_t::iterator it = s_InternedItems.find(items);
if (it != s_InternedItems.end())

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -27,6 +27,7 @@
#include "ps/CLogger.h"
#include "ps/CStr.h"
#include "ps/Profiler2.h"
#include "ps/Threading.h"
#include "ps/XML/Xeromyces.h"
#if CONFIG2_NVTT
@ -298,7 +299,7 @@ CTextureConverter::CTextureConverter(PIVFS vfs, bool highQuality) :
ENSURE(nvtt::version() >= NVTT_VERSION);
#endif
m_WorkerThread = std::thread(RunThread, this);
m_WorkerThread = std::thread(Threading::HandleExceptions<RunThread>::Wrapper, this);
// Maybe we should share some centralised pool of worker threads?
// For now we'll just stick with a single thread for this specific use.

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2010 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -27,6 +27,8 @@
#ifndef INCLUDED_WSEH
#define INCLUDED_WSEH
#include <excpt.h>
struct _EXCEPTION_POINTERS;
extern long __stdcall wseh_ExceptionFilter(_EXCEPTION_POINTERS* ep);

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2015 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -44,6 +44,9 @@ WINIT_REGISTER_EARLY_INIT(wutil_Init);
WINIT_REGISTER_LATE_SHUTDOWN(wutil_Shutdown);
// Defined in ps/Pyrogenesis.h
extern const char* main_window_name;
//-----------------------------------------------------------------------------
// safe allocator
@ -528,7 +531,12 @@ static BOOL CALLBACK FindAppWindowByPid(HWND hWnd, LPARAM UNUSED(lParam))
UNUSED2(tid);
if(pid == GetCurrentProcessId())
{
char windowName[100];
GetWindowTextA(hWnd, windowName, 99);
if (strcmp(windowName, main_window_name) == 0)
hAppWindow = hWnd;
}
return TRUE; // keep calling
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -34,6 +34,7 @@
#include "ps/Game.h"
#include "ps/Loader.h"
#include "ps/Profile.h"
#include "ps/Threading.h"
#include "scriptinterface/ScriptInterface.h"
#include "simulation2/Simulation2.h"
@ -175,7 +176,7 @@ bool CNetClient::SetupConnection(const CStr& server, const u16 port, ENetHost* e
CNetClientSession* session = new CNetClientSession(*this);
bool ok = session->Connect(server, port, m_IsLocalClient, enetClient);
SetAndOwnSession(session);
m_PollingThread = std::thread(CNetClientSession::RunNetLoop, m_Session);
m_PollingThread = std::thread(Threading::HandleExceptions<CNetClientSession::RunNetLoop>::Wrapper, m_Session);
return ok;
}

View File

@ -32,6 +32,7 @@
#include "ps/ConfigDB.h"
#include "ps/GUID.h"
#include "ps/Profile.h"
#include "ps/Threading.h"
#include "scriptinterface/ScriptContext.h"
#include "scriptinterface/ScriptInterface.h"
#include "simulation2/Simulation2.h"
@ -205,11 +206,11 @@ bool CNetServerWorker::SetupConnection(const u16 port)
m_State = SERVER_STATE_PREGAME;
// Launch the worker thread
m_WorkerThread = std::thread(RunThread, this);
m_WorkerThread = std::thread(Threading::HandleExceptions<RunThread>::Wrapper, this);
#if CONFIG2_MINIUPNPC
// Launch the UPnP thread
m_UPnPThread = std::thread(SetupUPnP);
m_UPnPThread = std::thread(Threading::HandleExceptions<SetupUPnP>::Wrapper);
#endif
return true;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2019 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -98,7 +98,7 @@ static CStrInternInternals* GetString(const char* str, size_t len)
// g_Strings is not thread-safe, so complain if anyone is using this
// type in non-main threads. (If that's desired, g_Strings should be changed
// to be thread-safe, preferably without sacrificing performance.)
ENSURE(ThreadUtil::IsMainThread());
ENSURE(Threading::IsMainThread());
std::unordered_map<StringsKey, shared_ptr<CStrInternInternals> >::iterator it = g_Strings.find(str);

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -355,7 +355,7 @@ ErrorReactionInternal psDisplayError(const wchar_t* UNUSED(text), size_t UNUSED(
// displaying the error dialog hangs the desktop since the dialog box is behind the
// fullscreen window. So we just force the game to windowed mode before displaying the dialog.
// (But only if we're in the main thread, and not if we're being reentrant.)
if (ThreadUtil::IsMainThread())
if (Threading::IsMainThread())
{
static bool reentering = false;
if (!reentering)
@ -828,7 +828,7 @@ void EarlyInit()
// If you ever want to catch a particular allocation:
//_CrtSetBreakAlloc(232647);
ThreadUtil::SetMainThread();
Threading::SetMainThread();
debug_SetThreadName("main");
// add all debug_printf "tags" that we are interested in:

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2019 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -438,7 +438,7 @@ static _CRT_ALLOC_HOOK prev_hook;
static int crt_alloc_hook(int allocType, void* userData, size_t size, int blockType,
long requestNumber, const unsigned char* filename, int lineNumber)
{
if (allocType == _HOOK_ALLOC && ThreadUtil::IsMainThread())
if (allocType == _HOOK_ALLOC && Threading::IsMainThread())
++malloc_count;
if (prev_hook)
@ -760,7 +760,7 @@ CProfileSample::CProfileSample(const char* name)
if (CProfileManager::IsInitialised())
{
// The profiler is only safe to use on the main thread
if(ThreadUtil::IsMainThread())
if(Threading::IsMainThread())
g_Profiler.Start(name);
}
}
@ -768,6 +768,6 @@ CProfileSample::CProfileSample(const char* name)
CProfileSample::~CProfileSample()
{
if (CProfileManager::IsInitialised())
if(ThreadUtil::IsMainThread())
if(Threading::IsMainThread())
g_Profiler.Stop();
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -254,7 +254,7 @@ void CProfiler2::Shutdown()
// the destructor is not called for the main thread
// we have to call it manually to avoid memory leaks
ENSURE(ThreadUtil::IsMainThread());
ENSURE(Threading::IsMainThread());
m_Initialised = false;
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -336,7 +336,7 @@ public:
*/
void RecordFrameStart()
{
ENSURE(ThreadUtil::IsMainThread());
ENSURE(Threading::IsMainThread());
GetThreadStorage().RecordFrameStart(GetTime());
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2019 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -25,6 +25,7 @@
#include "lib/svn_revision.h"
const char* engine_version = "0.0.24";
const char* main_window_name = "0 A.D.";
// convert contents of file <in_filename> from char to wchar_t and
// append to <out> file.

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -28,6 +28,7 @@ class Path;
using OsPath = Path;
extern const char* engine_version;
extern const char* main_window_name;
extern void psBundleLogs(FILE* f); // set during InitVfs
extern void psSetLogDir(const OsPath& logDir); // set during InitVfs

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2019 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -18,7 +18,12 @@
#ifndef INCLUDED_THREADUTIL
#define INCLUDED_THREADUTIL
namespace ThreadUtil
/**
* Light-weight threading utilities.
* Implemented in Threading.cpp.
* Split from Threading because this is included (via profilers) in most files.
*/
namespace Threading
{
/**

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2019 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -17,14 +17,14 @@
#include "precompiled.h"
#include <thread>
#include "Threading.h"
#include "ThreadUtil.h"
#include <thread>
static bool g_MainThreadSet;
static std::thread::id g_MainThread;
bool ThreadUtil::IsMainThread()
bool Threading::IsMainThread()
{
// If SetMainThread hasn't been called yet, this is probably being
// called at static initialisation time, so it must be the main thread
@ -34,7 +34,7 @@ bool ThreadUtil::IsMainThread()
return g_MainThread == std::this_thread::get_id();
}
void ThreadUtil::SetMainThread()
void Threading::SetMainThread()
{
g_MainThread = std::this_thread::get_id();
g_MainThreadSet = true;

61
source/ps/Threading.h Normal file
View File

@ -0,0 +1,61 @@
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_THREADING
#define INCLUDED_THREADING
#include "ThreadUtil.h"
#if OS_WIN
#include "lib/sysdep/os/win/wseh.h"
#endif
namespace Threading
{
/**
* Wrap a function to handle exceptions.
* This currently deals with Windows Structured Exception Handling (see wseh.cpp)
*/
template<auto F, class T>
struct HandleExceptionsBase {};
template<auto functionPtr>
struct HandleExceptions : public HandleExceptionsBase<functionPtr, decltype(functionPtr)> {};
template<auto functionPtr, typename... Types>
struct HandleExceptionsBase<functionPtr, void(*)(Types...)>
{
static void Wrapper(Types... args)
{
#if OS_WIN
__try
{
functionPtr(args...);
}
__except(wseh_ExceptionFilter(GetExceptionInformation()))
{
// Nothing particular to do.
}
#else
functionPtr(args...);
#endif
}
};
}
#endif // INCLUDED_THREADING

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2019 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -30,6 +30,7 @@
#include "ps/Filesystem.h"
#include "ps/Profiler2.h"
#include "ps/Pyrogenesis.h"
#include "ps/Threading.h"
#include <condition_variable>
#include <deque>
@ -138,7 +139,7 @@ public:
m_Headers = curl_slist_append(m_Headers, "Accept: ");
curl_easy_setopt(m_Curl, CURLOPT_HTTPHEADER, m_Headers);
m_WorkerThread = std::thread(RunThread, this);
m_WorkerThread = std::thread(Threading::HandleExceptions<RunThread>::Wrapper, this);
}
~CUserReporterWorker()

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -32,6 +32,7 @@
#include "ps/ConfigDB.h"
#include "ps/Filesystem.h"
#include "ps/Game.h"
#include "ps/Pyrogenesis.h"
#include "ps/GameSetup/Config.h"
#include "renderer/Renderer.h"
@ -82,7 +83,7 @@ bool CVideoMode::SetVideoMode(int w, int h, int bpp, bool fullscreen)
flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
m_WindowedX = m_WindowedY = SDL_WINDOWPOS_CENTERED_DISPLAY(m_ConfigDisplay);
m_Window = SDL_CreateWindow("0 A.D.", m_WindowedX, m_WindowedY, w, h, flags);
m_Window = SDL_CreateWindow(main_window_name, m_WindowedX, m_WindowedY, w, h, flags);
if (!m_Window)
{
// If fullscreen fails, try windowed mode

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -38,13 +38,13 @@ void GCSliceCallbackHook(JSContext* UNUSED(cx), JS::GCProgress progress, const J
if (progress == JS::GC_SLICE_BEGIN)
{
if (CProfileManager::IsInitialised() && ThreadUtil::IsMainThread())
if (CProfileManager::IsInitialised() && Threading::IsMainThread())
g_Profiler.Start("GCSlice");
g_Profiler2.RecordRegionEnter("GCSlice");
}
else if (progress == JS::GC_SLICE_END)
{
if (CProfileManager::IsInitialised() && ThreadUtil::IsMainThread())
if (CProfileManager::IsInitialised() && Threading::IsMainThread())
g_Profiler.Stop();
g_Profiler2.RecordRegionLeave();
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -231,7 +231,7 @@ bool ProfileStart(JSContext* cx, uint argc, JS::Value* vp)
name = StringFlyweight(str).get().c_str();
}
if (CProfileManager::IsInitialised() && ThreadUtil::IsMainThread())
if (CProfileManager::IsInitialised() && Threading::IsMainThread())
g_Profiler.StartScript(name);
g_Profiler2.RecordRegionEnter(name);
@ -243,7 +243,7 @@ bool ProfileStart(JSContext* cx, uint argc, JS::Value* vp)
bool ProfileStop(JSContext* UNUSED(cx), uint argc, JS::Value* vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
if (CProfileManager::IsInitialised() && ThreadUtil::IsMainThread())
if (CProfileManager::IsInitialised() && Threading::IsMainThread())
g_Profiler.Stop();
g_Profiler2.RecordRegionLeave();
@ -368,7 +368,7 @@ ScriptInterface::ScriptInterface(const char* nativeScopeName, const char* debugN
m(new ScriptInterface_impl(nativeScopeName, context))
{
// Profiler stats table isn't thread-safe, so only enable this on the main thread
if (ThreadUtil::IsMainThread())
if (Threading::IsMainThread())
{
if (g_ScriptStatsTable)
g_ScriptStatsTable->Add(this, debugName);
@ -381,7 +381,7 @@ ScriptInterface::ScriptInterface(const char* nativeScopeName, const char* debugN
ScriptInterface::~ScriptInterface()
{
if (ThreadUtil::IsMainThread())
if (Threading::IsMainThread())
{
if (g_ScriptStatsTable)
g_ScriptStatsTable->Remove(this);

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -30,6 +30,7 @@
#include "ps/ConfigDB.h"
#include "ps/Filesystem.h"
#include "ps/Profiler2.h"
#include "ps/Threading.h"
#include "ps/XML/Xeromyces.h"
#include <thread>
@ -51,7 +52,7 @@ public:
m_DeadItems = new ItemsList;
m_Shutdown = false;
m_WorkerThread = std::thread(RunThread, this);
m_WorkerThread = std::thread(Threading::HandleExceptions<RunThread>::Wrapper, this);
}
~CSoundManagerWorker()

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -77,7 +77,7 @@ class MiscSetup : public CxxTest::GlobalFixture
setlocale(LC_CTYPE, "UTF-8");
#endif
ThreadUtil::SetMainThread();
Threading::SetMainThread();
g_Profiler2.Initialise();
m_ScriptEngine = new ScriptEngine;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2019 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -142,7 +142,7 @@ bool BeginAtlas(const CmdLineArgs& args, const DllLoader& dll)
// TODO: delete all remaining messages, to avoid memory leak warnings
// Restore main thread
ThreadUtil::SetMainThread();
Threading::SetMainThread();
// Clean up
AtlasView::DestroyViews();