Skip to content

Commit

Permalink
Merge pull request #3326 from Sonicadvance1/thread_frontend_ownership…
Browse files Browse the repository at this point in the history
…_part3

FEXLoader: Moves thread management to the frontend
  • Loading branch information
lioncash committed Dec 20, 2023
2 parents b4b8e81 + 58f2693 commit 5e26b77
Show file tree
Hide file tree
Showing 23 changed files with 407 additions and 492 deletions.
8 changes: 0 additions & 8 deletions FEXCore/Source/Interface/Context/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ namespace FEXCore::Context {
return CustomExitHandler;
}

void FEXCore::Context::ContextImpl::Stop() {
Stop(false);
}

void FEXCore::Context::ContextImpl::CompileRIP(FEXCore::Core::InternalThreadState *Thread, uint64_t GuestRIP) {
CompileBlock(Thread->CurrentFrame, GuestRIP);
}
Expand All @@ -46,10 +42,6 @@ namespace FEXCore::Context {
CompileBlock(Thread->CurrentFrame, GuestRIP, MaxInst);
}

bool FEXCore::Context::ContextImpl::IsDone() const {
return IsPaused();
}

void FEXCore::Context::ContextImpl::SetCustomCPUBackendFactory(CustomCPUFactoryType Factory) {
CustomCPUFactory = std::move(Factory);
}
Expand Down
56 changes: 5 additions & 51 deletions FEXCore/Source/Interface/Context/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,13 @@ namespace FEXCore::Context {
void SetExitHandler(ExitHandler handler) override;
ExitHandler GetExitHandler() const override;

void Pause() override;
void Run() override;
void Stop() override;
void Step() override;

ExitReason RunUntilExit(FEXCore::Core::InternalThreadState *Thread) override;

void ExecuteThread(FEXCore::Core::InternalThreadState *Thread) override;

void CompileRIP(FEXCore::Core::InternalThreadState *Thread, uint64_t GuestRIP) override;
void CompileRIPCount(FEXCore::Core::InternalThreadState *Thread, uint64_t GuestRIP, uint64_t MaxInst) override;

bool IsDone() const override;

void SetCustomCPUBackendFactory(CustomCPUFactoryType Factory) override;

HostFeatures GetHostFeatures() const override;
Expand Down Expand Up @@ -128,17 +121,11 @@ namespace FEXCore::Context {
* - HandleCallback(Thread, RIP);
*/

FEXCore::Core::InternalThreadState* CreateThread(uint64_t InitialRIP, uint64_t StackPointer, ManagedBy WhoManages, FEXCore::Core::CPUState *NewThreadState, uint64_t ParentTID) override;
FEXCore::Core::InternalThreadState* CreateThread(uint64_t InitialRIP, uint64_t StackPointer, FEXCore::Core::CPUState *NewThreadState, uint64_t ParentTID) override;

// Public for threading
void ExecutionThread(FEXCore::Core::InternalThreadState *Thread) override;
/**
* @brief Starts the OS thread object to start executing guest code
*
* @param Thread The internal FEX thread state object
*/
void RunThread(FEXCore::Core::InternalThreadState *Thread) override;
void StopThread(FEXCore::Core::InternalThreadState *Thread) override;

/**
* @brief Destroys this FEX thread object and stops tracking it internally
*
Expand Down Expand Up @@ -176,6 +163,8 @@ namespace FEXCore::Context {
void WriteFilesWithCode(AOTIRCodeFileWriterFn Writer) override {
IRCaptureCache.WriteFilesWithCode(Writer);
}

void ClearCodeCache(FEXCore::Core::InternalThreadState *Thread) override;
void InvalidateGuestCodeRange(FEXCore::Core::InternalThreadState *Thread, uint64_t Start, uint64_t Length) override;
void InvalidateGuestCodeRange(FEXCore::Core::InternalThreadState *Thread, uint64_t Start, uint64_t Length, CodeRangeInvalidationFn callback) override;
void MarkMemoryShared(FEXCore::Core::InternalThreadState *Thread) override;
Expand Down Expand Up @@ -233,17 +222,8 @@ namespace FEXCore::Context {
FEX_CONFIG_OPT(DisableVixlIndirectCalls, DISABLE_VIXL_INDIRECT_RUNTIME_CALLS);
} Config;

std::mutex ThreadCreationMutex;
FEXCore::Core::InternalThreadState* ParentThread{};
fextl::vector<FEXCore::Core::InternalThreadState*> Threads;
std::atomic_bool CoreShuttingDown{false};

std::mutex IdleWaitMutex;
std::condition_variable IdleWaitCV;
std::atomic<uint32_t> IdleWaitRefCount{};

Event PauseWait;
bool Running{};
std::atomic_bool CoreShuttingDown{false};

FEXCore::ForkableSharedMutex CodeInvalidationMutex;

Expand All @@ -268,12 +248,6 @@ namespace FEXCore::Context {
ContextImpl();
~ContextImpl();

bool IsPaused() const { return !Running; }
void WaitForThreadsToRun() override;
void Stop(bool IgnoreCurrentThread);
void WaitForIdle() override;
void SignalThread(FEXCore::Core::InternalThreadState *Thread, FEXCore::Core::SignalEvent Event);

static void ThreadRemoveCodeEntry(FEXCore::Core::InternalThreadState *Thread, uint64_t GuestRIP);
static void ThreadAddBlockLink(FEXCore::Core::InternalThreadState *Thread, uint64_t GuestDestination, uintptr_t HostLink, const std::function<void()> &delinker);

Expand Down Expand Up @@ -349,10 +323,6 @@ namespace FEXCore::Context {
}
}

void IncrementIdleRefCount() override {
++IdleWaitRefCount;
}

FEXCore::Utils::PooledAllocatorVirtual OpDispatcherAllocator;
FEXCore::Utils::PooledAllocatorVirtual FrontendAllocator;

Expand Down Expand Up @@ -382,18 +352,9 @@ namespace FEXCore::Context {

bool ExitOnHLTEnabled() const { return ExitOnHLT; }

ThreadsState GetThreads() override {
return ThreadsState {
.ParentThread = ParentThread,
.Threads = &Threads,
};
}

FEXCore::CPU::CPUBackendFeatures BackendFeatures;

protected:
void ClearCodeCache(FEXCore::Core::InternalThreadState *Thread);

void UpdateAtomicTSOEmulationConfig() {
if (SupportsHardwareTSO) {
// If the hardware supports TSO then we don't need to emulate it through atomics.
Expand All @@ -415,15 +376,8 @@ namespace FEXCore::Context {
*/
void InitializeCompiler(FEXCore::Core::InternalThreadState* Thread);

void WaitForIdleWithTimeout();

void NotifyPause();

void AddBlockMapping(FEXCore::Core::InternalThreadState *Thread, uint64_t Address, void *Ptr);

// Entry Cache
std::mutex ExitMutex;

IR::AOTIRCaptureCache IRCaptureCache;
fextl::unique_ptr<FEXCore::CodeSerialize::CodeObjectSerializeService> CodeObjectCacheService;

Expand Down

0 comments on commit 5e26b77

Please sign in to comment.