2021-06-27 21:25:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
File: AuOSThread.hpp
|
2021-06-27 21:25:29 +00:00
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#pragma once
|
|
|
|
|
2022-08-06 19:50:40 +00:00
|
|
|
#if defined(AURORA_IS_POSIX_DERIVED)
|
|
|
|
#include <setjmp.h>
|
|
|
|
#endif
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
namespace Aurora::Threading::Threads
|
|
|
|
{
|
2022-06-11 23:52:46 +00:00
|
|
|
struct OSThread : IAuroraThread
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
OSThread();
|
2021-09-06 10:58:08 +00:00
|
|
|
OSThread(const ThreadInfo &info);
|
2021-06-27 21:25:29 +00:00
|
|
|
OSThread(AuUInt64);
|
|
|
|
~OSThread();
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
bool Run() override;
|
|
|
|
void Exit() override;
|
|
|
|
bool Exiting() override;
|
|
|
|
void SendExitSignal() override;
|
2022-03-21 07:16:12 +00:00
|
|
|
|
2022-08-13 23:32:39 +00:00
|
|
|
void UnsafeForceTerminateSignal() override;
|
|
|
|
|
2022-03-21 07:16:12 +00:00
|
|
|
void SetPriority(EThreadPriority prio) override;
|
|
|
|
void SetThrottle(EThreadThrottle prio) override;
|
2022-03-16 17:12:08 +00:00
|
|
|
void SetAffinity(const HWInfo::CpuBitId &mask) override;
|
2021-09-06 10:58:08 +00:00
|
|
|
void SetName(const AuString &name) override;
|
2022-03-21 07:16:12 +00:00
|
|
|
|
|
|
|
EThreadPriority GetPriority() override;
|
|
|
|
EThreadThrottle GetThrottle() override;
|
2022-03-16 17:12:08 +00:00
|
|
|
HWInfo::CpuBitId GetMask() override;
|
2021-09-06 10:58:08 +00:00
|
|
|
AuString GetName() override;
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2022-01-19 17:08:13 +00:00
|
|
|
void ExecuteInDeadThread(AuFunction<void()> callback) override;
|
2021-09-06 10:58:08 +00:00
|
|
|
AuSPtr<IWaitable> AsWaitable() override;
|
|
|
|
|
|
|
|
void AddLastHopeTlsHook(const AuSPtr<Threading::Threads::IThreadFeature> &feature) override;
|
2023-08-23 16:01:56 +00:00
|
|
|
AuUInt64 SetNoUnwindTerminateExitWatchDogTimeoutInMS(AuUInt64 uMS) override;
|
|
|
|
|
2021-10-25 18:19:49 +00:00
|
|
|
void Detach() override;
|
|
|
|
|
2023-10-13 20:43:59 +00:00
|
|
|
AuUInt64 GetThreadCreationTime(Time::EClock eClock) override;
|
|
|
|
|
|
|
|
void InitThreadCreateTime();
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
void _ThreadEP();
|
2022-06-11 23:52:46 +00:00
|
|
|
AuSPtr<AuLoop::ILoopSource> AsLoopSource() override;
|
2021-10-25 17:08:28 +00:00
|
|
|
AuSPtr<IWaitable> GetShutdownSignalWaitable() override;
|
2022-06-11 23:52:46 +00:00
|
|
|
AuSPtr<AuLoop::ILoopSource> GetShutdownSignalLS() override;
|
2022-08-06 19:50:40 +00:00
|
|
|
|
|
|
|
bool InternalKillForceNtfy();
|
2023-10-01 13:12:22 +00:00
|
|
|
void SignalDeath();
|
2022-08-14 13:41:19 +00:00
|
|
|
bool InternalKill(bool locked);
|
2023-01-23 15:23:43 +00:00
|
|
|
|
|
|
|
void AffinityPrioThrottleTickAmendECores();
|
2023-08-23 23:03:05 +00:00
|
|
|
|
|
|
|
// !protected!
|
|
|
|
void MakeMain();
|
2021-06-27 21:25:29 +00:00
|
|
|
protected:
|
|
|
|
|
2022-04-02 00:48:29 +00:00
|
|
|
bool Exit(bool willReturnToOS, bool isEOL = false);
|
2022-01-19 17:08:13 +00:00
|
|
|
bool ExecuteNewOSContext(AuFunction<void()> task);
|
2022-03-21 07:16:12 +00:00
|
|
|
void UpdatePrio(EThreadThrottle throttle, EThreadPriority prio);
|
2022-03-16 17:12:08 +00:00
|
|
|
void UpdateAffinity(const HWInfo::CpuBitId &mask);
|
2021-06-27 21:25:29 +00:00
|
|
|
void UpdateName();
|
2021-10-25 18:19:49 +00:00
|
|
|
void OSAttach();
|
|
|
|
void OSDeatach();
|
2021-06-27 21:25:29 +00:00
|
|
|
void TeminateOSContext(bool calledFromThis);
|
|
|
|
void FreeOSContext();
|
|
|
|
|
|
|
|
void HookReleaseThreadResources();
|
|
|
|
void HookOnExit();
|
|
|
|
|
|
|
|
private:
|
2022-08-06 19:50:40 +00:00
|
|
|
#if defined(AURORA_IS_POSIX_DERIVED)
|
|
|
|
jmp_buf env;
|
|
|
|
#endif
|
2023-04-30 06:54:28 +00:00
|
|
|
Primitives::Mutex tlsLock_;
|
|
|
|
AuUInt32 tls_ {};
|
2021-11-05 17:34:23 +00:00
|
|
|
OSThread * tlsReferenceThread_ {};
|
2021-06-27 21:25:29 +00:00
|
|
|
AuString name_;
|
2021-09-06 10:58:08 +00:00
|
|
|
ThreadInfo info_;
|
2022-03-16 17:12:08 +00:00
|
|
|
HWInfo::CpuBitId mask_ = HWInfo::CpuBitId().Not();
|
2022-03-21 07:16:12 +00:00
|
|
|
HWInfo::CpuBitId throttleMask_ = HWInfo::CpuBitId().Not();
|
|
|
|
EThreadPriority prio_ = EThreadPriority::ePrioNormal;
|
|
|
|
EThreadThrottle throttle_ = EThreadThrottle::eNormal;
|
2022-08-02 04:52:17 +00:00
|
|
|
bool bSupportsAltKill {};
|
2022-03-21 07:16:12 +00:00
|
|
|
bool userManagingAffinity_ {};
|
2021-06-27 21:25:29 +00:00
|
|
|
bool exiting_{};
|
|
|
|
bool contextUsed_{}; // can this thread instance execute code again?
|
2021-09-06 10:58:08 +00:00
|
|
|
Primitives::EventShared_t terminated_;
|
2021-10-25 17:08:28 +00:00
|
|
|
Primitives::EventShared_t terminateSignal_;
|
2023-09-23 19:32:01 +00:00
|
|
|
struct Lock
|
|
|
|
{
|
|
|
|
bool bLock {};
|
|
|
|
AuMutex mutex;
|
|
|
|
};
|
|
|
|
AuSPtr<Lock> pFlag = AuMakeSharedPanic<Lock>();
|
2023-04-30 06:54:28 +00:00
|
|
|
AuSPtr<AuLoop::ILSEvent> terminateSignalLs_;
|
|
|
|
AuSPtr<AuLoop::ILSEvent> terminatedSignalLs_;
|
2022-08-14 13:41:19 +00:00
|
|
|
bool bLongJmpOnce {};
|
2023-08-23 16:01:56 +00:00
|
|
|
AuUInt64 qwExitTimeout { AuSToMS<AuUInt64>(15) };
|
2023-08-23 23:03:05 +00:00
|
|
|
bool bNotOwned {};
|
2023-04-30 06:54:28 +00:00
|
|
|
Primitives::CriticalSection exitOnlyOnce_;
|
2021-09-06 10:58:08 +00:00
|
|
|
AuList<AuSPtr<IThreadFeature>> threadFeatures_;
|
2023-09-23 19:32:01 +00:00
|
|
|
AuBinarySemaphore epExecEvent;
|
2023-10-13 20:57:38 +00:00
|
|
|
AuUInt64 uClockCreationTime[3] {};
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-19 17:08:13 +00:00
|
|
|
AuFunction<void()> task_;
|
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
|
|
|
HANDLE handle_ = INVALID_HANDLE_VALUE;
|
|
|
|
#elif defined(AURORA_HAS_PTHREADS)
|
2022-01-24 18:37:06 +00:00
|
|
|
pthread_t handle_ {};
|
2021-06-27 21:25:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
AuUInt64 unixThreadId_ = 0;
|
2021-10-25 18:19:49 +00:00
|
|
|
bool detached_ {};
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
};
|
2022-01-18 20:49:45 +00:00
|
|
|
|
|
|
|
void InitThreading();
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|