[+] Added IAuroraThread::Detach

This commit is contained in:
Reece Wilson 2021-10-25 19:19:49 +01:00
parent 9ed6d2ff03
commit 14bde3750c
3 changed files with 30 additions and 15 deletions

View File

@ -35,10 +35,6 @@ namespace Aurora::Threading::Threads
class IAuroraThread
{
public:
// TODO: consider detach support.
// The problem is, if the aurora runtime transfers ownership of an IAuroraThread to you, you have to leak it
// or you have to be forced to use the very comfy dtor mechanic. We need a detach function to overload this
// release-is-always-terminate assumption.
virtual bool Run() = 0;
virtual void Exit() = 0;
@ -60,11 +56,19 @@ namespace Aurora::Threading::Threads
virtual AuSPtr<TLSView> GetTlsView() = 0;
virtual void ExecuteInDeadThread(std::function<void()> callback) = 0;
virtual AuSPtr<IWaitable> AsWaitable() = 0;
virtual AuSPtr<Loop::ILoopSource> AsLoopSource() = 0;
virtual AuSPtr<IWaitable> GetShutdownSignalWaitable() = 0;
virtual AuSPtr<Loop::ILoopSource> GetShutdownSignalLS() = 0;
// TODO: consider detach support.
// The problem is, if the aurora runtime transfers ownership of an IAuroraThread to you, you have to leak it
// or you have to be forced to use the very comfy dtor mechanic. We need a detach function to overload this
// release-is-always-terminate assumption.
// UPDATE: DONE
virtual void Detach() = 0;
virtual void ExecuteInDeadThread(std::function<void()> callback) = 0;
};
}

View File

@ -60,8 +60,11 @@ namespace Aurora::Threading::Threads
{
if (contextUsed_)
{
Exit();
WaitFor(terminated_.get());
if (!detached_)
{
Exit();
WaitFor(terminated_.get());
}
}
terminated_.reset();
@ -95,6 +98,11 @@ namespace Aurora::Threading::Threads
AuTryInsert(threadFeatures_, feature);
}
void OSThread::Detach()
{
detached_ = true;
}
AuSPtr<IWaitable> OSThread::AsWaitable()
{
return terminated_;
@ -338,7 +346,7 @@ namespace Aurora::Threading::Threads
void OSThread::_ThreadEP()
{
Attach();
OSAttach();
task_();
Exit(true);
}
@ -355,17 +363,17 @@ namespace Aurora::Threading::Threads
if (old)
{
static_cast<OSThread *>(old)->Deattach();
static_cast<OSThread *>(old)->OSDeatach();
}
this->Attach();
this->OSAttach();
callback();
if (old)
{
HandleRegister(old);
static_cast<OSThread *>(old)->Attach();
static_cast<OSThread *>(old)->OSDeatach();
}
else [[unlikely]]
{
@ -420,7 +428,7 @@ namespace Aurora::Threading::Threads
}
void OSThread::Attach()
void OSThread::OSAttach()
{
HandleRegister(this);
#if defined(AURORA_IS_LINUX_DERIVED)
@ -546,7 +554,7 @@ namespace Aurora::Threading::Threads
#endif
}
void OSThread::Deattach()
void OSThread::OSDeatach()
{
}

View File

@ -37,6 +37,8 @@ namespace Aurora::Threading::Threads
void AddLastHopeTlsHook(const AuSPtr<Threading::Threads::IThreadFeature> &feature) override;
void Detach() override;
void _ThreadEP();
AuSPtr<Loop::ILoopSource> AsLoopSource() override;
AuSPtr<IWaitable> GetShutdownSignalWaitable() override;
@ -48,8 +50,8 @@ namespace Aurora::Threading::Threads
void UpdatePrio(EThreadPrio prio);
void UpdateAffinity(AuUInt32 mask);
void UpdateName();
void Attach();
void Deattach();
void OSAttach();
void OSDeatach();
bool InternalKill(bool locked);
void TeminateOSContext(bool calledFromThis);
void FreeOSContext();
@ -85,6 +87,7 @@ namespace Aurora::Threading::Threads
#endif
AuUInt64 unixThreadId_ = 0;
bool detached_ {};
};
}