[*] Move setjmp to beginning of task invoker function for Linux thread kills

This commit is contained in:
Reece Wilson 2022-08-14 23:59:48 +01:00
parent e30e004d04
commit 58d53c92c1
2 changed files with 40 additions and 23 deletions

View File

@ -84,7 +84,7 @@ static void RuntimeLateClean();
static void Deinit() static void Deinit()
{ {
tlsHackIsMainThread = true; //tlsHackIsMainThread = true;
gRuntimeRunLevel = 3; gRuntimeRunLevel = 3;
Aurora::Exit::PostLevel(AuThreads::GetThread(), Aurora::Exit::ETriggerLevel::eSafeTermination); Aurora::Exit::PostLevel(AuThreads::GetThread(), Aurora::Exit::ETriggerLevel::eSafeTermination);
gRuntimeRunLevel = 4; gRuntimeRunLevel = 4;

View File

@ -14,6 +14,7 @@
#if defined(AURORA_IS_LINUX_DERIVED) #if defined(AURORA_IS_LINUX_DERIVED)
#include <sys/resource.h> #include <sys/resource.h>
#include <cxxabi.h>
#endif #endif
#if defined(AURORA_HAS_PTHREADS) #if defined(AURORA_HAS_PTHREADS)
@ -97,6 +98,13 @@ namespace Aurora::Threading::Threads
bool bDetached {}; bool bDetached {};
bool bDetachedSuccess {}; bool bDetachedSuccess {};
#if defined(AURORA_IS_POSIX_DERIVED)
if (getpid() == gettid())
{
RuntimeLateClean();
}
#endif
if (this->bNotOwned) if (this->bNotOwned)
{ {
return; return;
@ -113,7 +121,6 @@ namespace Aurora::Threading::Threads
return; return;
} }
#endif #endif
if (this->contextUsed_) if (this->contextUsed_)
{ {
if (this->detached_) if (this->detached_)
@ -122,33 +129,32 @@ namespace Aurora::Threading::Threads
} }
else else
{ {
if (gRuntimeRunLevel < 3) if (gRuntimeRunLevel <= 3)
{ {
Exit(); Exit();
} }
else if (gRuntimeRunLevel >= 5) else if (gRuntimeRunLevel >= 5)
{ {
// Application is dead // Application is dead
} }
else else
{ {
if (RuntimeIsMainThread())
{
HookReleaseThreadResources();
return;
}
// Kill the current OS thread instance // Kill the current OS thread instance
TeminateOSContext(false); TeminateOSContext(false);
if (this->exitOnlyOnce_) if (this->exitOnlyOnce_)
{ {
this->exitOnlyOnce_->Unlock(); this->exitOnlyOnce_->Unlock();
} }
}
if (gRuntimeRunLevel < 4) // minimum: async deinit level #if defined(AURORA_IS_POSIX_DERIVED)
if (this->terminated_)
{ {
WaitFor(this->terminated_.get()); WaitFor(this->terminated_.get());
} }
#endif
}
} }
} }
@ -446,6 +452,15 @@ namespace Aurora::Threading::Threads
bool bFailing {}; bool bFailing {};
OSAttach(); OSAttach();
#if defined(AURORA_IS_POSIX_DERIVED)
this->bLongJmpOnce = false;
if (setjmp(env) != 0)
{
Exit(true);
}
#endif
try try
{ {
if (task_) if (task_)
@ -461,6 +476,12 @@ namespace Aurora::Threading::Threads
} }
Exit(true); Exit(true);
#else #else
#if defined(AURORA_COMPILER_GCC)
catch (abi::__forced_unwind&)
{
throw;
}
#endif
catch (...) catch (...)
{ {
bFailing = true; bFailing = true;
@ -473,8 +494,11 @@ namespace Aurora::Threading::Threads
this->HookOnExit(); this->HookOnExit();
if (this->terminated_) if (this->terminated_)
{
if (this->exitOnlyOnce_)
{ {
this->exitOnlyOnce_->Unlock(); this->exitOnlyOnce_->Unlock();
}
this->terminated_->Set(); this->terminated_->Set();
} }
@ -624,13 +648,6 @@ namespace Aurora::Threading::Threads
this->unixThreadId_ = 0; // !!!! this->unixThreadId_ = 0; // !!!!
#endif #endif
#if defined(AURORA_IS_POSIX_DERIVED)
if (setjmp(env) != 0)
{
pthread_exit(nullptr);
}
#endif
UpdatePrio(this->throttle_, this->prio_); UpdatePrio(this->throttle_, this->prio_);
SetAffinity(this->mask_); SetAffinity(this->mask_);
UpdateName(); UpdateName();
@ -1077,7 +1094,7 @@ namespace Aurora::Threading::Threads
{ {
} }
//::pthread_exit(nullptr); ::pthread_exit(nullptr);
} }
else else
{ {