From f53697ce76f193345133bce89c393e5e654b7040 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Fri, 12 Mar 2021 17:30:40 +0000 Subject: [PATCH] Windows: Call SetThreadStackGuarantee for all threads, not just main thread --- src/openttd.cpp | 1 + src/os/os2/os2.cpp | 2 ++ src/os/unix/unix.cpp | 5 +++++ src/os/windows/crashlog_win.cpp | 6 ------ src/os/windows/win32.cpp | 16 ++++++++++++++++ src/thread.h | 11 +++++++++++ 6 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/openttd.cpp b/src/openttd.cpp index aa590122a7..67b845ea7f 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -685,6 +685,7 @@ static const OptionData _options[] = { int openttd_main(int argc, char *argv[]) { SetSelfAsMainThread(); + PerThreadSetup(); std::string musicdriver; std::string sounddriver; std::string videodriver; diff --git a/src/os/os2/os2.cpp b/src/os/os2/os2.cpp index 5ea0b88b3d..2b86dd939c 100644 --- a/src/os/os2/os2.cpp +++ b/src/os/os2/os2.cpp @@ -219,6 +219,8 @@ void SetCurrentThreadName(const char *) int GetCurrentThreadName(char *str, const char *last) { return 0; } void SetSelfAsMainThread() { } +void PerThreadSetup() { } +void PerThreadSetupInit() { } bool IsMainThread() { return false; } bool IsNonMainThread() { return false; } diff --git a/src/os/unix/unix.cpp b/src/os/unix/unix.cpp index bca9220f41..548968ee9a 100644 --- a/src/os/unix/unix.cpp +++ b/src/os/unix/unix.cpp @@ -256,6 +256,7 @@ int CDECL main(int argc, char *argv[]) argc = 1; } #endif + PerThreadSetupInit(); CrashLog::InitialiseCrashLog(); SetRandomSeed(time(nullptr)); @@ -348,6 +349,10 @@ void SetSelfAsMainThread() #endif } +void PerThreadSetup() { } + +void PerThreadSetupInit() { } + bool IsMainThread() { #if !defined(NO_THREADS) diff --git a/src/os/windows/crashlog_win.cpp b/src/os/windows/crashlog_win.cpp index 72edf042e4..4a2aaf48a0 100644 --- a/src/os/windows/crashlog_win.cpp +++ b/src/os/windows/crashlog_win.cpp @@ -725,12 +725,6 @@ static void CDECL CustomAbort(int signal) if (LoadLibraryList((Function*)&AddVectoredExceptionHandler, "kernel32.dll\0AddVectoredExceptionHandler\0\0")) { AddVectoredExceptionHandler(1, VectoredExceptionHandler); } - - BOOL (WINAPI *SetThreadStackGuarantee)(PULONG); - if (LoadLibraryList((Function*)&SetThreadStackGuarantee, "kernel32.dll\0SetThreadStackGuarantee\0\0")) { - ULONG stacksize = 65536; - SetThreadStackGuarantee(&stacksize); - } } /* static */ void CrashLog::DesyncCrashLog(const std::string *log_in, std::string *log_out, const DesyncExtraInfo &info) diff --git a/src/os/windows/win32.cpp b/src/os/windows/win32.cpp index 71791fe89c..0da0b49bf7 100644 --- a/src/os/windows/win32.cpp +++ b/src/os/windows/win32.cpp @@ -436,6 +436,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi /* Set system timer resolution to 1ms. */ timeBeginPeriod(1); + PerThreadSetupInit(); CrashLog::InitialiseCrashLog(); /* Convert the command line to UTF-8. We need a dedicated buffer @@ -739,6 +740,21 @@ void SetSelfAsMainThread() main_thread_id = GetCurrentThreadId(); } +static BOOL (WINAPI *_SetThreadStackGuarantee)(PULONG) = nullptr; + +void PerThreadSetup() +{ + if (_SetThreadStackGuarantee != nullptr) { + ULONG stacksize = 65536; + _SetThreadStackGuarantee(&stacksize); + } +} + +void PerThreadSetupInit() +{ + LoadLibraryList((Function*)&_SetThreadStackGuarantee, "kernel32.dll\0SetThreadStackGuarantee\0\0"); +} + bool IsMainThread() { return main_thread_id == GetCurrentThreadId(); diff --git a/src/thread.h b/src/thread.h index 137acff931..35e3bf0a6f 100644 --- a/src/thread.h +++ b/src/thread.h @@ -49,6 +49,16 @@ int GetCurrentThreadName(char *str, const char *last); */ void SetSelfAsMainThread(); +/** + * Perform per-thread setup + */ +void PerThreadSetup(); + +/** + * Setup thread functionality required for later calls to PerThreadSetup + */ +void PerThreadSetupInit(); + /** * @return true if the current thread definitely the "main" thread. If in doubt returns false. */ @@ -77,6 +87,7 @@ inline bool StartNewThread(std::thread *thr, const char *name, TFn&& _Fx, TArgs& try { std::thread t([] (const char *name, TFn&& F, TArgs&&... A) { SetCurrentThreadName(name); + PerThreadSetup(); try { /* Call user function with the given arguments. */ F(A...);