[+] 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 class IAuroraThread
{ {
public: 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 bool Run() = 0;
virtual void Exit() = 0; virtual void Exit() = 0;
@ -60,11 +56,19 @@ namespace Aurora::Threading::Threads
virtual AuSPtr<TLSView> GetTlsView() = 0; virtual AuSPtr<TLSView> GetTlsView() = 0;
virtual void ExecuteInDeadThread(std::function<void()> callback) = 0;
virtual AuSPtr<IWaitable> AsWaitable() = 0; virtual AuSPtr<IWaitable> AsWaitable() = 0;
virtual AuSPtr<Loop::ILoopSource> AsLoopSource() = 0; virtual AuSPtr<Loop::ILoopSource> AsLoopSource() = 0;
virtual AuSPtr<IWaitable> GetShutdownSignalWaitable() = 0; virtual AuSPtr<IWaitable> GetShutdownSignalWaitable() = 0;
virtual AuSPtr<Loop::ILoopSource> GetShutdownSignalLS() = 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_) if (contextUsed_)
{ {
Exit(); if (!detached_)
WaitFor(terminated_.get()); {
Exit();
WaitFor(terminated_.get());
}
} }
terminated_.reset(); terminated_.reset();
@ -95,6 +98,11 @@ namespace Aurora::Threading::Threads
AuTryInsert(threadFeatures_, feature); AuTryInsert(threadFeatures_, feature);
} }
void OSThread::Detach()
{
detached_ = true;
}
AuSPtr<IWaitable> OSThread::AsWaitable() AuSPtr<IWaitable> OSThread::AsWaitable()
{ {
return terminated_; return terminated_;
@ -338,7 +346,7 @@ namespace Aurora::Threading::Threads
void OSThread::_ThreadEP() void OSThread::_ThreadEP()
{ {
Attach(); OSAttach();
task_(); task_();
Exit(true); Exit(true);
} }
@ -355,17 +363,17 @@ namespace Aurora::Threading::Threads
if (old) if (old)
{ {
static_cast<OSThread *>(old)->Deattach(); static_cast<OSThread *>(old)->OSDeatach();
} }
this->Attach(); this->OSAttach();
callback(); callback();
if (old) if (old)
{ {
HandleRegister(old); HandleRegister(old);
static_cast<OSThread *>(old)->Attach(); static_cast<OSThread *>(old)->OSDeatach();
} }
else [[unlikely]] else [[unlikely]]
{ {
@ -420,7 +428,7 @@ namespace Aurora::Threading::Threads
} }
void OSThread::Attach() void OSThread::OSAttach()
{ {
HandleRegister(this); HandleRegister(this);
#if defined(AURORA_IS_LINUX_DERIVED) #if defined(AURORA_IS_LINUX_DERIVED)
@ -546,7 +554,7 @@ namespace Aurora::Threading::Threads
#endif #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 AddLastHopeTlsHook(const AuSPtr<Threading::Threads::IThreadFeature> &feature) override;
void Detach() override;
void _ThreadEP(); void _ThreadEP();
AuSPtr<Loop::ILoopSource> AsLoopSource() override; AuSPtr<Loop::ILoopSource> AsLoopSource() override;
AuSPtr<IWaitable> GetShutdownSignalWaitable() override; AuSPtr<IWaitable> GetShutdownSignalWaitable() override;
@ -48,8 +50,8 @@ namespace Aurora::Threading::Threads
void UpdatePrio(EThreadPrio prio); void UpdatePrio(EThreadPrio prio);
void UpdateAffinity(AuUInt32 mask); void UpdateAffinity(AuUInt32 mask);
void UpdateName(); void UpdateName();
void Attach(); void OSAttach();
void Deattach(); void OSDeatach();
bool InternalKill(bool locked); bool InternalKill(bool locked);
void TeminateOSContext(bool calledFromThis); void TeminateOSContext(bool calledFromThis);
void FreeOSContext(); void FreeOSContext();
@ -85,6 +87,7 @@ namespace Aurora::Threading::Threads
#endif #endif
AuUInt64 unixThreadId_ = 0; AuUInt64 unixThreadId_ = 0;
bool detached_ {};
}; };
} }