From 49ced3fcc6bd3355f77e6f4cc201615c442bdde9 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Thu, 24 Aug 2023 00:03:05 +0100 Subject: [PATCH] [*] Always attach to the main thread context on init --- Source/Threading/Threads/AuOSThread.cpp | 36 ++++++++++++++++++++++--- Source/Threading/Threads/AuOSThread.hpp | 6 +++-- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Source/Threading/Threads/AuOSThread.cpp b/Source/Threading/Threads/AuOSThread.cpp index 5674eb00..8009760b 100644 --- a/Source/Threading/Threads/AuOSThread.cpp +++ b/Source/Threading/Threads/AuOSThread.cpp @@ -94,6 +94,7 @@ namespace Aurora::Threading::Threads this->terminateSignal_ = AuThreadPrimitives::EventShared(true, false, true); SysAssert(this->terminated_ && this->terminateSignal_); + this->bNotOwned = true; } OSThread::OSThread(AuUInt64 id) : info_(gDummyThreadInfo) @@ -174,8 +175,19 @@ namespace Aurora::Threading::Threads } } } + else + { + if (auto pTerminated = AuExchange(this->terminatedSignalLs_, {})) + { + pTerminated->Set(); + } + + if (auto pTerminated = AuExchange(this->terminated_, {})) + { + pTerminated->Set(); + } + } - this->terminated_.reset(); FreeOSContext(); if (bDetached) @@ -728,10 +740,15 @@ namespace Aurora::Threading::Threads #endif } + void OSThread::MakeMain() + { + this->bNotOwned = true; + this->name_ = "Main System Thread"; + this->OSAttach(); + } + void OSThread::OSAttach() { - this->bSupportsAltKill = true; - HandleRegister(this); #if defined(AURORA_IS_LINUX_DERIVED) this->unixThreadId_ = gettid(); @@ -759,11 +776,17 @@ namespace Aurora::Threading::Threads { SetThreadKey(this->tls_); } + UpdatePrio(this->throttle_, this->prio_); SetAffinity(this->mask_); AffinityPrioThrottleTickAmendECores(); UpdateName(); - AttachSignalKiller(); + + if (!this->bNotOwned) + { + this->bSupportsAltKill = true; + AttachSignalKiller(); + } } static AuHashMap kNiceMap @@ -1417,5 +1440,10 @@ namespace Aurora::Threading::Threads #endif AttachSignalKiller(); + + if (auto pThread = GetThread()) + { + AuStaticCast(pThread)->MakeMain(); + } } } \ No newline at end of file diff --git a/Source/Threading/Threads/AuOSThread.hpp b/Source/Threading/Threads/AuOSThread.hpp index bcfbe75e..2675db27 100644 --- a/Source/Threading/Threads/AuOSThread.hpp +++ b/Source/Threading/Threads/AuOSThread.hpp @@ -56,6 +56,9 @@ namespace Aurora::Threading::Threads bool InternalKill(bool locked); void AffinityPrioThrottleTickAmendECores(); + + // !protected! + void MakeMain(); protected: bool Exit(bool willReturnToOS, bool isEOL = false); @@ -84,7 +87,6 @@ namespace Aurora::Threading::Threads HWInfo::CpuBitId throttleMask_ = HWInfo::CpuBitId().Not(); EThreadPriority prio_ = EThreadPriority::ePrioNormal; EThreadThrottle throttle_ = EThreadThrottle::eNormal; - bool bNotOwned {}; bool bSupportsAltKill {}; bool userManagingAffinity_ {}; bool exiting_{}; @@ -95,8 +97,8 @@ namespace Aurora::Threading::Threads AuSPtr terminatedSignalLs_; bool bLongJmpOnce {}; AuUInt64 qwExitTimeout { AuSToMS(15) }; + bool bNotOwned {}; Primitives::CriticalSection exitOnlyOnce_; - AuList> threadFeatures_; AuFunction task_;