2021-06-27 21:25:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: OSThread.cpp
|
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2021-06-27 21:25:29 +00:00
|
|
|
#include "Threads.hpp"
|
|
|
|
#include "OSThread.hpp"
|
|
|
|
#include "../WaitFor.hpp"
|
|
|
|
#include "ThreadHandles.hpp"
|
|
|
|
#include "TLSView.hpp"
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
#if defined(AURORA_IS_LINUX_DERIVED)
|
2022-01-24 18:37:06 +00:00
|
|
|
#include <sys/resource.h>
|
2021-06-27 21:25:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(AURORA_HAS_PTHREADS)
|
2022-01-24 18:37:06 +00:00
|
|
|
#include <sched.h>
|
2021-06-27 21:25:29 +00:00
|
|
|
#endif
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
#if defined(AURORA_PLATFORM_WIN32)
|
2022-01-24 18:37:06 +00:00
|
|
|
#include <Aux_ulib.h>
|
2021-09-06 10:58:08 +00:00
|
|
|
#endif
|
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
#include "SpawnThread.hpp"
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
namespace Aurora::Threading::Threads
|
|
|
|
{
|
2021-10-24 10:19:47 +00:00
|
|
|
static ThreadInfo gDummyThreadInfo;
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2021-10-23 18:25:43 +00:00
|
|
|
OSThread::OSThread(const ThreadInfo &info) : info_(info)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
name_ = info.name.value_or("Aurora Thread");
|
|
|
|
terminated_ = Primitives::EventShared(true, false);
|
2021-10-25 17:08:28 +00:00
|
|
|
|
|
|
|
// maybe we should atomic exchange compare these when needed frogthink
|
|
|
|
terminateSignal_ = Primitives::EventShared(true, false, true);
|
|
|
|
terminatedSignalLs_ = Loop::NewLSEvent(true, false);
|
|
|
|
terminateSignalLs_ = Loop::NewLSEvent(true, false, true);
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
exitOnlyOnce_ = Primitives::CriticalSectionUnique();
|
|
|
|
SysAssert(terminated_ ? true : false, "out of memory");
|
|
|
|
}
|
2021-09-06 10:58:08 +00:00
|
|
|
|
|
|
|
OSThread::OSThread() : info_(gDummyThreadInfo)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
name_ = "Main Thread";
|
2021-09-06 10:58:08 +00:00
|
|
|
terminated_ = Primitives::EventShared(true, false);
|
2021-06-27 21:25:29 +00:00
|
|
|
exitOnlyOnce_ = Primitives::CriticalSectionUnique();
|
|
|
|
}
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
OSThread::OSThread(AuUInt64 id) : info_(gDummyThreadInfo)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
name_ = "System Thread";
|
|
|
|
handle_ = reinterpret_cast<decltype(handle_)>(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
OSThread::~OSThread()
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
bool bDetached {};
|
|
|
|
bool bDetachedSuccess {};
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
if (contextUsed_)
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
if (detached_)
|
|
|
|
{
|
|
|
|
bDetached = true;
|
|
|
|
}
|
|
|
|
else
|
2021-10-25 18:19:49 +00:00
|
|
|
{
|
|
|
|
Exit();
|
|
|
|
WaitFor(terminated_.get());
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
terminated_.reset();
|
|
|
|
FreeOSContext();
|
2021-11-05 17:34:23 +00:00
|
|
|
|
|
|
|
if (bDetached)
|
|
|
|
{
|
|
|
|
if (this->tlsReferenceThread_)
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(this->tlsLock_);
|
|
|
|
AU_LOCK_GUARD(this->tlsReferenceThread_->tlsLock_);
|
|
|
|
|
2022-01-19 17:08:13 +00:00
|
|
|
AuExchange(this->tlsReferenceThread_->tls_, this->tls_);
|
|
|
|
AuExchange(this->tlsReferenceThread_->threadFeatures_, this->threadFeatures_);
|
2021-11-05 17:34:23 +00:00
|
|
|
|
|
|
|
bDetachedSuccess = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bDetachedSuccess || !bDetached)
|
|
|
|
{
|
|
|
|
HookReleaseThreadResources();
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called whenever an aurora thread is exiting
|
|
|
|
void OSThread::HookOnExit()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called whenever a thread object is released, or on thread termination
|
|
|
|
// Whichever is soonest
|
|
|
|
// It is possible a user may wish to keep a thread handle open
|
|
|
|
// Is is expected for a system thread to release on dtor unlike our aurora threads
|
|
|
|
void OSThread::HookReleaseThreadResources()
|
|
|
|
{
|
|
|
|
tls_.reset();
|
2021-09-06 10:58:08 +00:00
|
|
|
|
|
|
|
for (const auto &feature : threadFeatures_)
|
|
|
|
{
|
|
|
|
feature->Cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
void OSThread::AddLastHopeTlsHook(const AuSPtr<AuThreads::IThreadFeature> &feature)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
|
|
|
// TODO: mutex
|
2021-11-05 17:34:23 +00:00
|
|
|
if (!feature)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-09-06 10:58:08 +00:00
|
|
|
AuTryInsert(threadFeatures_, feature);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 18:19:49 +00:00
|
|
|
void OSThread::Detach()
|
|
|
|
{
|
|
|
|
detached_ = true;
|
|
|
|
}
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
AuSPtr<IWaitable> OSThread::AsWaitable()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
return terminated_;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OSThread::SendExitSignal()
|
|
|
|
{
|
|
|
|
exiting_ = true;
|
2021-10-25 17:08:28 +00:00
|
|
|
|
|
|
|
if (this->terminateSignalLs_)
|
|
|
|
{
|
|
|
|
this->terminateSignalLs_->Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->terminateSignal_)
|
|
|
|
{
|
|
|
|
this->terminateSignal_->Set();
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
bool OSThread::Run()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
if (!terminated_)
|
|
|
|
{
|
|
|
|
SysPanic("::Run called on system thread");
|
|
|
|
}
|
|
|
|
|
2022-01-19 17:08:13 +00:00
|
|
|
if (AuExchange(contextUsed_, true))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
return false;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
terminated_->Reset();
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
return ExecuteNewOSContext([=]()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-10-23 18:25:43 +00:00
|
|
|
// this functional backends are being deprecated
|
2021-10-24 10:19:47 +00:00
|
|
|
if (info_.callbacks)
|
2021-10-23 18:25:43 +00:00
|
|
|
{
|
|
|
|
info_.callbacks->OnEntry(this);
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Debug::PrintError();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSThread::Exit()
|
|
|
|
{
|
|
|
|
while ((!terminated_) || (!terminated_->TryLock()))
|
|
|
|
{
|
|
|
|
if (Exit(false)) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OSThread::Exit(bool willReturnToOS)
|
|
|
|
{
|
|
|
|
if (GetThread() == this)
|
|
|
|
{
|
|
|
|
if (!this->InternalKill(false))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// release handle + sigterm
|
|
|
|
if (!willReturnToOS)
|
|
|
|
{
|
|
|
|
TeminateOSContext(true);
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
YieldToOtherThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// exit signal
|
|
|
|
exiting_ = true;
|
|
|
|
|
|
|
|
if (!terminated_)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// attempt to join with the thread once it has exited, or timeout
|
|
|
|
if (WaitFor(terminated_.get(), 15 * 1000))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not force terminate if we're marked as dead and still running
|
|
|
|
// The thread must've requested suicide and got stuck in a lengthy clean up effort
|
|
|
|
if (!exitOnlyOnce_->TryLock())
|
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
AuLogWarn("Watchdog error - OS thread context didn't finish in 15 seconds, but he should exiting now.");
|
2021-06-27 21:25:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
AuLogWarn("Watchdog error - OS thread context didn't finish in 15 seconds, forcefully terminating without a watchdog overlooking onExit");
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
// Kill the current OS thread instance
|
|
|
|
TeminateOSContext(false);
|
|
|
|
|
|
|
|
// Dispatch kill callback from within an emulated thread context (no switch, just just change thread tls)
|
|
|
|
ExecuteInDeadThread([=]()
|
|
|
|
{
|
|
|
|
this->InternalKill(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
exitOnlyOnce_->Unlock();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OSThread::Exiting()
|
|
|
|
{
|
|
|
|
return exiting_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSThread::SetName(const AuString &name)
|
|
|
|
{
|
|
|
|
name_ = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
EThreadPrio OSThread::GetPrio()
|
|
|
|
{
|
|
|
|
return prio_;
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
AuUInt64 OSThread::GetMask()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
return affinityProcessMask_;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuString OSThread::GetName()
|
|
|
|
{
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
void OSThread::SetAffinity(AuUInt64 mask)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
if (mask == 0) mask = 0xFFFFFFFFFFFFFFFF;
|
2021-06-27 21:25:29 +00:00
|
|
|
affinityProcessMask_ = mask;
|
|
|
|
UpdateAffinity(mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSThread::SetPrio(EThreadPrio prio)
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
if (!EThreadPrioIsValid(prio)) return;
|
2021-06-27 21:25:29 +00:00
|
|
|
UpdatePrio(prio);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<TLSView> OSThread::GetTlsView()
|
|
|
|
{
|
|
|
|
if (!tls_)
|
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
tls_ = AuMakeShared<TLSViewImpl>();
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tls_;
|
|
|
|
}
|
|
|
|
|
2022-01-19 17:08:13 +00:00
|
|
|
bool OSThread::ExecuteNewOSContext(AuFunction<void()> task)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
task_ = task;
|
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
auto ret = SpawnThread([this]()
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
this->_ThreadEP();
|
|
|
|
}, GetName(), info_.stackSize);
|
|
|
|
|
|
|
|
if (ret.first)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
handle_ = (decltype(handle_))ret.second;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
return ret.first;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OSThread::_ThreadEP()
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
// Poke TLS reference thread entity
|
|
|
|
// TODO: we need an internal OSThread *TryPokeTLSThread()
|
|
|
|
auto osThread = static_cast<OSThread *>(GetThread());
|
|
|
|
this->tlsReferenceThread_ = osThread;
|
|
|
|
|
2021-10-25 18:19:49 +00:00
|
|
|
OSAttach();
|
2022-01-21 16:26:35 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
task_();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
SysPushErrorHAL("OS Thread Aborted");
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
Exit(true);
|
|
|
|
}
|
|
|
|
|
2022-01-19 17:08:13 +00:00
|
|
|
void OSThread::ExecuteInDeadThread(AuFunction<void()> callback)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
auto old = HandleCurrent();
|
|
|
|
|
|
|
|
if (this == old) [[unlikely]]
|
|
|
|
{
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old)
|
|
|
|
{
|
2021-10-25 18:19:49 +00:00
|
|
|
static_cast<OSThread *>(old)->OSDeatach();
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 18:19:49 +00:00
|
|
|
this->OSAttach();
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
callback();
|
|
|
|
|
|
|
|
if (old)
|
|
|
|
{
|
|
|
|
HandleRegister(old);
|
2021-10-25 18:19:49 +00:00
|
|
|
static_cast<OSThread *>(old)->OSDeatach();
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
else [[unlikely]]
|
|
|
|
{
|
|
|
|
HandleRemove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSThread::UpdateName()
|
|
|
|
{
|
|
|
|
#if defined(AURORA_PLATFORM_WIN32)
|
|
|
|
static const DWORD kMSVCExceptionSetName = 0x406D1388;
|
|
|
|
|
|
|
|
#pragma pack(push,8)
|
|
|
|
typedef struct tagTHREADNAME_INFO
|
|
|
|
{
|
|
|
|
DWORD dwType;
|
|
|
|
LPCSTR szName;
|
|
|
|
DWORD dwThreadID;
|
|
|
|
DWORD dwFlags;
|
|
|
|
} THREADNAME_INFO;
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
if (handle_ == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsDebuggerPresent())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
THREADNAME_INFO info;
|
|
|
|
info.dwType = 0x1000;
|
|
|
|
info.szName = name_.c_str();
|
|
|
|
info.dwThreadID = ::GetThreadId(handle_);
|
|
|
|
info.dwFlags = 0;
|
|
|
|
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
RaiseException(kMSVCExceptionSetName, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info);
|
|
|
|
}
|
|
|
|
__except (EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(AURORA_HAS_PTHREADS)
|
|
|
|
|
2021-10-02 16:07:33 +00:00
|
|
|
pthread_setname_np(handle_, name_.c_str());
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-25 18:19:49 +00:00
|
|
|
void OSThread::OSAttach()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
HandleRegister(this);
|
2021-09-06 10:58:08 +00:00
|
|
|
#if defined(AURORA_IS_LINUX_DERIVED)
|
2021-06-27 21:25:29 +00:00
|
|
|
this->unixThreadId_ = gettid();
|
2021-09-06 10:58:08 +00:00
|
|
|
#elif defined(AURORA_IS_XNU_DERIVED)
|
2021-06-27 21:25:29 +00:00
|
|
|
this->unixThreadId_ = pthread_getthreadid_np();
|
2021-09-06 10:58:08 +00:00
|
|
|
#elif defined(AURORA_IS_BSD_DERIVED)
|
|
|
|
#if __FreeBSD_version < 900031
|
|
|
|
long lwpid;
|
|
|
|
thr_self(&lwpid);
|
|
|
|
this->unixThreadId_ = lwpid;
|
|
|
|
#elif __FreeBSD_version > 900030
|
|
|
|
this->unixThreadId_ = pthread_getthreadid_np();
|
|
|
|
#else
|
|
|
|
static_assert(false);
|
|
|
|
#endif
|
2021-06-27 21:25:29 +00:00
|
|
|
#elif defined(AURORA_HAS_PTHREADS)
|
|
|
|
this->unixThreadId_ = 0; // !!!!
|
|
|
|
#endif
|
|
|
|
UpdatePrio(prio_);
|
2021-11-05 17:34:23 +00:00
|
|
|
SetAffinity(affinityProcessMask_);
|
2021-06-27 21:25:29 +00:00
|
|
|
UpdateName();
|
|
|
|
}
|
|
|
|
|
|
|
|
static AuHashMap<EThreadPrio, int> kNiceMap
|
|
|
|
{
|
|
|
|
{
|
|
|
|
EThreadPrio::ePrioRT, -19
|
|
|
|
},
|
|
|
|
{
|
|
|
|
EThreadPrio::ePrioAboveHigh, -19
|
|
|
|
},
|
|
|
|
{
|
|
|
|
EThreadPrio::ePrioHigh, -11
|
|
|
|
},
|
|
|
|
{
|
|
|
|
EThreadPrio::ePrioNormal, -2
|
|
|
|
},
|
|
|
|
{
|
|
|
|
EThreadPrio::ePrioSub, 5
|
|
|
|
}
|
|
|
|
};
|
2021-09-06 10:58:08 +00:00
|
|
|
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2021-06-27 21:25:29 +00:00
|
|
|
static const AuHashMap<EThreadPrio, int> kWin32Map
|
|
|
|
{
|
|
|
|
{
|
|
|
|
EThreadPrio::ePrioRT, THREAD_PRIORITY_TIME_CRITICAL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
EThreadPrio::ePrioAboveHigh, THREAD_PRIORITY_HIGHEST
|
|
|
|
},
|
|
|
|
{
|
|
|
|
EThreadPrio::ePrioHigh, THREAD_PRIORITY_ABOVE_NORMAL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
EThreadPrio::ePrioNormal, THREAD_PRIORITY_NORMAL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
EThreadPrio::ePrioSub, THREAD_PRIORITY_LOWEST
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void OSThread::UpdatePrio(EThreadPrio prio)
|
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
if (handle_ == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int *val;
|
2021-09-06 10:58:08 +00:00
|
|
|
if (!AuTryFind(kWin32Map, prio, val))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetThreadPriority(handle_, *val);
|
|
|
|
|
|
|
|
#elif defined(AURORA_HAS_PTHREADS)
|
|
|
|
|
|
|
|
if (!handle_)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:11:12 +00:00
|
|
|
if (prio == EThreadPrio::ePrioRT)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
sched_param param {};
|
|
|
|
param.sched_priority = sched_get_priority_min(SCHED_RR);
|
|
|
|
|
|
|
|
if (pthread_setschedparam(handle_, SCHED_RR, ¶m) == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fall through on error
|
|
|
|
}
|
2021-11-05 17:34:23 +00:00
|
|
|
else if (prio_ == EThreadPrio::ePrioRT)
|
|
|
|
{
|
|
|
|
int policyNonRT =
|
|
|
|
#if defined(AURORA_IS_XNU_DERIVED)
|
|
|
|
SCHED_FIFO;
|
|
|
|
#else
|
|
|
|
SCHED_OTHER;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sched_param param {};
|
|
|
|
param.sched_priority = sched_get_priority_min(policyNonRT);
|
|
|
|
pthread_setschedparam(handle_, policyNonRT, ¶m);
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
const int *val;
|
2021-09-06 10:58:08 +00:00
|
|
|
if (!AuTryFind(kNiceMap, prio, val))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto tid = unixThreadId_)
|
|
|
|
{
|
2021-10-02 16:07:33 +00:00
|
|
|
setpriority(PRIO_PROCESS, tid, *val);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2021-11-05 17:34:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
prio_ = prio;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
void OSThread::UpdateAffinity(AuUInt64 mask)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2021-06-27 21:25:29 +00:00
|
|
|
if (handle_ == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SetThreadAffinityMask(handle_, static_cast<DWORD_PTR>(mask));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-10-25 18:19:49 +00:00
|
|
|
void OSThread::OSDeatach()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OSThread::InternalKill(bool locked)
|
|
|
|
{
|
|
|
|
if (terminated_)
|
|
|
|
{
|
|
|
|
if (!locked)
|
|
|
|
{
|
|
|
|
if (!exitOnlyOnce_->TryLock())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HookOnExit();
|
|
|
|
|
2021-10-23 18:25:43 +00:00
|
|
|
try
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-10-23 18:25:43 +00:00
|
|
|
// dispatch kill callback
|
2021-10-24 10:19:47 +00:00
|
|
|
if (info_.callbacks)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-10-23 18:25:43 +00:00
|
|
|
info_.callbacks->OnExit(this);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-23 18:25:43 +00:00
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Debug::PrintError();
|
2022-01-24 18:37:06 +00:00
|
|
|
AuLogWarn("Couldn't deinitialize thread");
|
|
|
|
AuLogWarn("The smart thing to do at this point would be to panic");
|
|
|
|
AuLogWarn("...but we could continue");
|
|
|
|
AuLogWarn("Carrying on despite the potential for data integrity loss and memory leaks");
|
2021-10-23 18:25:43 +00:00
|
|
|
Telemetry::Mayday();
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
HookReleaseThreadResources();
|
|
|
|
|
|
|
|
if (terminated_)
|
|
|
|
{
|
|
|
|
exitOnlyOnce_->Unlock();
|
|
|
|
terminated_->Set();
|
2021-10-25 17:08:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (terminatedSignalLs_)
|
|
|
|
{
|
|
|
|
terminatedSignalLs_->Set();
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2021-10-25 17:08:28 +00:00
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSThread::TeminateOSContext(bool calledFromThis)
|
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
if (handle_ == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (calledFromThis)
|
|
|
|
{
|
|
|
|
ExitThread(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TerminateThread(handle_, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(AURORA_HAS_PTHREADS)
|
|
|
|
|
|
|
|
pthread_kill(handle_, SIGTERM);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
SysPanic("Not implemented");
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSThread::FreeOSContext()
|
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2022-01-19 17:08:13 +00:00
|
|
|
if (auto handle = AuExchange(handle_, {}))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
CloseHandle(handle_);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2021-10-25 17:08:28 +00:00
|
|
|
|
|
|
|
AuSPtr<Loop::ILoopSource> OSThread::AsLoopSource()
|
|
|
|
{
|
|
|
|
return this->terminateSignalLs_;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IWaitable> OSThread::GetShutdownSignalWaitable()
|
|
|
|
{
|
|
|
|
return this->terminateSignal_;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<Loop::ILoopSource> OSThread::GetShutdownSignalLS()
|
|
|
|
{
|
|
|
|
return this->terminateSignalLs_;
|
|
|
|
}
|
2022-01-18 16:02:24 +00:00
|
|
|
|
|
|
|
void InitThreading()
|
|
|
|
{
|
|
|
|
AuxUlibInitialize();
|
|
|
|
}
|
2021-10-02 16:07:33 +00:00
|
|
|
}
|