Skip to content

Commit

Permalink
Merge pull request #3349 from Sonicadvance1/revert_frontend_ownership
Browse files Browse the repository at this point in the history
Revert "FEXLoader: Moves thread management to the frontend"
  • Loading branch information
Sonicadvance1 committed Jan 3, 2024
2 parents 04a88ed + 5358af7 commit db7d7a6
Show file tree
Hide file tree
Showing 22 changed files with 490 additions and 406 deletions.
8 changes: 8 additions & 0 deletions FEXCore/Source/Interface/Context/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ 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 @@ -38,6 +42,10 @@ 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: 51 additions & 5 deletions FEXCore/Source/Interface/Context/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,20 @@ 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 @@ -121,11 +128,17 @@ namespace FEXCore::Context {
* - HandleCallback(Thread, RIP);
*/

FEXCore::Core::InternalThreadState* CreateThread(uint64_t InitialRIP, uint64_t StackPointer, FEXCore::Core::CPUState *NewThreadState, uint64_t ParentTID) override;
FEXCore::Core::InternalThreadState* CreateThread(uint64_t InitialRIP, uint64_t StackPointer, ManagedBy WhoManages, 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 @@ -163,8 +176,6 @@ 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 @@ -221,9 +232,18 @@ 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{};

FEXCore::ForkableSharedMutex CodeInvalidationMutex;

FEXCore::HostFeatures HostFeatures;
Expand All @@ -247,6 +267,12 @@ 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 @@ -322,6 +348,10 @@ namespace FEXCore::Context {
}
}

void IncrementIdleRefCount() override {
++IdleWaitRefCount;
}

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

Expand Down Expand Up @@ -351,9 +381,18 @@ 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 @@ -375,8 +414,15 @@ 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 db7d7a6

Please sign in to comment.