From 29bd8dca0cd6727e90828290b87059ea6704354c Mon Sep 17 00:00:00 2001 From: Reece Date: Tue, 18 Jan 2022 16:02:24 +0000 Subject: [PATCH] [*] Continue to perfer beginthreadex, use CreateThread when the OS loader is locked on Win32 --- Aurora.json | 5 ++-- Source/Debug/ExceptionWatcher.Win32.cpp | 6 ++--- Source/Entrypoint.cpp | 2 ++ Source/Threading/Threads/OSThread.cpp | 35 ++++++++++++++++++++++++- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/Aurora.json b/Aurora.json index b803ebda..97dfe17a 100644 --- a/Aurora.json +++ b/Aurora.json @@ -13,10 +13,9 @@ "linkSources": "Source/Alloc.cpp", "actions": [ { - "if_": "win32", - "filter": { "platforms": "win32"} , + "filter": {"platforms": "win32"}, "then": { - "links": ["Bcrypt.lib", "UxTheme.lib"] + "links": ["Bcrypt.lib", "UxTheme.lib", "Aux_ulib.lib"] } } ] diff --git a/Source/Debug/ExceptionWatcher.Win32.cpp b/Source/Debug/ExceptionWatcher.Win32.cpp index ba195175..8f6579ca 100644 --- a/Source/Debug/ExceptionWatcher.Win32.cpp +++ b/Source/Debug/ExceptionWatcher.Win32.cpp @@ -228,9 +228,9 @@ namespace Aurora::Debug if ((ExceptionInfo->ExceptionRecord->ExceptionCode == EH_EXCEPTION_NUMBER) && (ExceptionInfo->ExceptionRecord->NumberParameters >= 4) && - ((cxxThrow) || - (cxxThrowPure)) - ) + ((cxxThrow) || + (cxxThrowPure)) + ) { HMODULE handle {}; auto *throwInfo = reinterpret_cast(ExceptionInfo->ExceptionRecord->ExceptionInformation[2]); diff --git a/Source/Entrypoint.cpp b/Source/Entrypoint.cpp index defb6d46..0eb44581 100644 --- a/Source/Entrypoint.cpp +++ b/Source/Entrypoint.cpp @@ -19,6 +19,7 @@ #include "Debug/Debug.hpp" #include "Async/Async.hpp" #include "HWInfo/HWInfo.hpp" +#include "Threading/Threads/OSThread.hpp" #if defined(AURORA_PLATFORM_WIN32) #include "Extensions/Win32/DarkTheme.hpp" #endif @@ -30,6 +31,7 @@ static void Init() #endif Crypto::InitCrypto(); + Aurora::Threading::Threads::InitThreading(); Aurora::Console::Init(); Aurora::IO::FS::InitResources(); Aurora::Console::Init2(); diff --git a/Source/Threading/Threads/OSThread.cpp b/Source/Threading/Threads/OSThread.cpp index 423cdcf3..3a85a29f 100644 --- a/Source/Threading/Threads/OSThread.cpp +++ b/Source/Threading/Threads/OSThread.cpp @@ -22,6 +22,7 @@ #if defined(AURORA_PLATFORM_WIN32) #include +#include #endif @@ -315,13 +316,40 @@ namespace Aurora::Threading::Threads // I think I switched it from CreateThread to _beginthreadex at somepoint and i don't remember why #if defined(AURORA_IS_MODERNNT_DERIVED) - unsigned(WINAPI *OSEP_f)(void *) = [](void *that) -> unsigned + unsigned(WINAPI * OSEP_f)(void *) = [](void *that) -> unsigned { auto thiz = reinterpret_cast(that); thiz->_ThreadEP(); return 0; }; + DWORD(WINAPI * OSCreateThreadEP_f)(void *) = [](void *that) -> DWORD + { + auto thiz = reinterpret_cast(that); + thiz->_ThreadEP(); + return 0; + }; + + + #if defined(AURORA_PLATFORM_WIN32) + BOOL a {}; + if (AuxUlibIsDLLSynchronizationHeld(&a)) + { + if (a) + { + handle_ = CreateThread(NULL, info_.stackSize, OSCreateThreadEP_f, reinterpret_cast(this), NULL, NULL); + if (!handle_) + { + handle_ = INVALID_HANDLE_VALUE; + SysPushErrorGen("Couldn't create locked thread: {}", GetName()); + return false; + } + + return true; + } + } + #endif + auto ok = _beginthreadex(nullptr, info_.stackSize, OSEP_f, this, 0, 0); if (ok == -1L) { @@ -712,4 +740,9 @@ namespace Aurora::Threading::Threads { return this->terminateSignalLs_; } + + void InitThreading() + { + AuxUlibInitialize(); + } }