From 58d53c92c10c85a6ff11af9999a9ffdd4c5ccf39 Mon Sep 17 00:00:00 2001 From: J Reece Wilson Date: Sun, 14 Aug 2022 23:59:48 +0100 Subject: [PATCH] [*] Move setjmp to beginning of task invoker function for Linux thread kills --- Source/Entrypoint.cpp | 2 +- Source/Threading/Threads/OSThread.cpp | 61 +++++++++++++++++---------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/Source/Entrypoint.cpp b/Source/Entrypoint.cpp index b9e2f409..3d7b1535 100644 --- a/Source/Entrypoint.cpp +++ b/Source/Entrypoint.cpp @@ -84,7 +84,7 @@ static void RuntimeLateClean(); static void Deinit() { - tlsHackIsMainThread = true; + //tlsHackIsMainThread = true; gRuntimeRunLevel = 3; Aurora::Exit::PostLevel(AuThreads::GetThread(), Aurora::Exit::ETriggerLevel::eSafeTermination); gRuntimeRunLevel = 4; diff --git a/Source/Threading/Threads/OSThread.cpp b/Source/Threading/Threads/OSThread.cpp index 8a0d4c5d..06b129d2 100644 --- a/Source/Threading/Threads/OSThread.cpp +++ b/Source/Threading/Threads/OSThread.cpp @@ -14,6 +14,7 @@ #if defined(AURORA_IS_LINUX_DERIVED) #include + #include #endif #if defined(AURORA_HAS_PTHREADS) @@ -97,6 +98,13 @@ namespace Aurora::Threading::Threads bool bDetached {}; bool bDetachedSuccess {}; + #if defined(AURORA_IS_POSIX_DERIVED) + if (getpid() == gettid()) + { + RuntimeLateClean(); + } + #endif + if (this->bNotOwned) { return; @@ -113,7 +121,6 @@ namespace Aurora::Threading::Threads return; } #endif - if (this->contextUsed_) { if (this->detached_) @@ -122,39 +129,38 @@ namespace Aurora::Threading::Threads } else { - if (gRuntimeRunLevel < 3) + if (gRuntimeRunLevel <= 3) { Exit(); } else if (gRuntimeRunLevel >= 5) { + // Application is dead } else { - if (RuntimeIsMainThread()) - { - HookReleaseThreadResources(); - return; - } // Kill the current OS thread instance TeminateOSContext(false); + if (this->exitOnlyOnce_) { this->exitOnlyOnce_->Unlock(); } - } - - if (gRuntimeRunLevel < 4) // minimum: async deinit level - { - WaitFor(this->terminated_.get()); + + #if defined(AURORA_IS_POSIX_DERIVED) + if (this->terminated_) + { + WaitFor(this->terminated_.get()); + } + #endif } } } this->terminated_.reset(); FreeOSContext(); - + if (bDetached) { if (this->tlsReferenceThread_) @@ -446,6 +452,15 @@ namespace Aurora::Threading::Threads bool bFailing {}; OSAttach(); + + #if defined(AURORA_IS_POSIX_DERIVED) + this->bLongJmpOnce = false; + if (setjmp(env) != 0) + { + Exit(true); + } + #endif + try { if (task_) @@ -461,6 +476,12 @@ namespace Aurora::Threading::Threads } Exit(true); #else + #if defined(AURORA_COMPILER_GCC) + catch (abi::__forced_unwind&) + { + throw; + } + #endif catch (...) { bFailing = true; @@ -474,7 +495,10 @@ namespace Aurora::Threading::Threads if (this->terminated_) { - this->exitOnlyOnce_->Unlock(); + if (this->exitOnlyOnce_) + { + this->exitOnlyOnce_->Unlock(); + } this->terminated_->Set(); } @@ -624,13 +648,6 @@ namespace Aurora::Threading::Threads this->unixThreadId_ = 0; // !!!! #endif - #if defined(AURORA_IS_POSIX_DERIVED) - if (setjmp(env) != 0) - { - pthread_exit(nullptr); - } - #endif - UpdatePrio(this->throttle_, this->prio_); SetAffinity(this->mask_); UpdateName(); @@ -1077,7 +1094,7 @@ namespace Aurora::Threading::Threads { } - //::pthread_exit(nullptr); + ::pthread_exit(nullptr); } else {