Skip to content

Commit

Permalink
Merge pull request #2691 from neobrain/refactor_scoped_signal_mask
Browse files Browse the repository at this point in the history
ScopedSignalMask: Clean up API and use std::unique_lock/shared_lock
  • Loading branch information
Sonicadvance1 committed Nov 19, 2023
2 parents 9b64674 + d33b0cb commit 8726c8f
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 488 deletions.
6 changes: 3 additions & 3 deletions FEXCore/Source/Interface/Context/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include <FEXCore/Core/SignalDelegator.h>
#include <FEXCore/Debug/InternalThreadState.h>
#include <FEXCore/Utils/CompilerDefs.h>
#include <FEXCore/Utils/DeferredSignalMutex.h>
#include <FEXCore/Utils/Event.h>
#include <FEXCore/Utils/SignalScopeGuards.h>
#include <FEXCore/fextl/memory.h>
#include <FEXCore/fextl/set.h>
#include <FEXCore/fextl/string.h>
Expand Down Expand Up @@ -295,7 +295,7 @@ namespace FEXCore::Context {
template<auto Fn>
static uint64_t ThreadExitFunctionLink(FEXCore::Core::CpuStateFrame *Frame, uint64_t *record) {
auto Thread = Frame->Thread;
ScopedDeferredSignalWithForkableSharedLock lk(static_cast<ContextImpl*>(Thread->CTX)->CodeInvalidationMutex, Thread);
auto lk = GuardSignalDeferringSection<std::shared_lock>(static_cast<ContextImpl*>(Thread->CTX)->CodeInvalidationMutex, Thread);

return Fn(Frame, record);
}
Expand All @@ -306,7 +306,7 @@ namespace FEXCore::Context {
auto Thread = Frame->Thread;

LogMan::Throw::AFmt(Thread->ThreadManager.GetTID() == FHU::Syscalls::gettid(), "Must be called from owning thread {}, not {}", Thread->ThreadManager.GetTID(), FHU::Syscalls::gettid());
ScopedDeferredSignalWithForkableUniqueLock lk(static_cast<ContextImpl*>(Thread->CTX)->CodeInvalidationMutex, Thread);
auto lk = GuardSignalDeferringSection(static_cast<ContextImpl*>(Thread->CTX)->CodeInvalidationMutex, Thread);

ThreadRemoveCodeEntry(Thread, GuestRIP);
}
Expand Down
10 changes: 5 additions & 5 deletions FEXCore/Source/Interface/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ desc: Glues Frontend, OpDispatcher and IR Opts & Compilation, LookupCache, Dispa
*/

#include <cstdint>
#include "FEXCore/Utils/DeferredSignalMutex.h"
#include "Interface/Context/Context.h"
#include "Interface/Core/ArchHelpers//Arm64Emitter.h"
#include "Interface/Core/LookupCache.h"
Expand Down Expand Up @@ -46,6 +45,7 @@ desc: Glues Frontend, OpDispatcher and IR Opts & Compilation, LookupCache, Dispa
#include <FEXCore/Utils/Event.h>
#include <FEXCore/Utils/File.h>
#include <FEXCore/Utils/LogManager.h>
#include "FEXCore/Utils/SignalScopeGuards.h"
#include <FEXCore/Utils/Threads.h>
#include <FEXCore/Utils/Profiler.h>
#include <FEXCore/fextl/fmt.h>
Expand Down Expand Up @@ -1112,7 +1112,7 @@ namespace FEXCore::Context {
auto Thread = Frame->Thread;

// Invalidate might take a unique lock on this, to guarantee that during invalidation no code gets compiled
ScopedDeferredSignalWithForkableSharedLock lk(CodeInvalidationMutex, Thread);
auto lk = GuardSignalDeferringSection<std::shared_lock>(CodeInvalidationMutex, Thread);

// Is the code in the cache?
// The backends only check L1 and L2, not L3
Expand Down Expand Up @@ -1295,7 +1295,7 @@ namespace FEXCore::Context {
// Potential deferred since Thread might not be valid.
// Thread object isn't valid very early in frontend's initialization.
// To be more optimal the frontend should provide this code with a valid Thread object earlier.
ScopedPotentialDeferredSignalWithForkableUniqueLock lk(CodeInvalidationMutex, Thread);
auto lk = GuardSignalDeferringSectionWithFallback(CodeInvalidationMutex, Thread);

InvalidateGuestCodeRangeInternal(this, Start, Length);
}
Expand All @@ -1304,7 +1304,7 @@ namespace FEXCore::Context {
// Potential deferred since Thread might not be valid.
// Thread object isn't valid very early in frontend's initialization.
// To be more optimal the frontend should provide this code with a valid Thread object earlier.
ScopedPotentialDeferredSignalWithForkableUniqueLock lk(CodeInvalidationMutex, Thread);
auto lk = GuardSignalDeferringSectionWithFallback(CodeInvalidationMutex, Thread);

InvalidateGuestCodeRangeInternal(this, Start, Length);
CallAfter(Start, Length);
Expand Down Expand Up @@ -1332,7 +1332,7 @@ namespace FEXCore::Context {
}

void ContextImpl::ThreadAddBlockLink(FEXCore::Core::InternalThreadState *Thread, uint64_t GuestDestination, uintptr_t HostLink, const std::function<void()> &delinker) {
ScopedDeferredSignalWithForkableSharedLock lk(static_cast<ContextImpl*>(Thread->CTX)->CodeInvalidationMutex, Thread);
auto lk = GuardSignalDeferringSection<std::shared_lock>(static_cast<ContextImpl*>(Thread->CTX)->CodeInvalidationMutex, Thread);

Thread->LookupCache->AddBlockLink(GuestDestination, HostLink, delinker);
}
Expand Down
8 changes: 4 additions & 4 deletions FEXCore/Source/Utils/Allocator/64BitAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <FEXCore/Utils/Allocator.h>
#include <FEXCore/Utils/LogManager.h>
#include <FEXCore/Utils/MathUtils.h>
#include <FEXCore/Utils/SignalScopeGuards.h>
#include <FEXCore/fextl/sstream.h>
#include <FEXCore/Utils/DeferredSignalMutex.h>
#include <FEXHeaderUtils/Syscalls.h>
#include <FEXHeaderUtils/TypeDefines.h>
#include <FEXCore/fextl/memory.h>
Expand Down Expand Up @@ -272,7 +272,7 @@ void *OSAllocator_64Bit::Mmap(void *addr, size_t length, int prot, int flags, in
size_t NumberOfPages = length / FHU::FEX_PAGE_SIZE;

// This needs a mutex to be thread safe
FEXCore::ScopedPotentialDeferredSignalWithForkableMutex lk(AllocationMutex, TLSThread);
auto lk = FEXCore::GuardSignalDeferringSectionWithFallback(AllocationMutex, TLSThread);

uint64_t AllocatedOffset{};
LiveVMARegion *LiveRegion{};
Expand Down Expand Up @@ -460,7 +460,7 @@ int OSAllocator_64Bit::Munmap(void *addr, size_t length) {
}

// This needs a mutex to be thread safe
FEXCore::ScopedPotentialDeferredSignalWithForkableMutex lk(AllocationMutex, TLSThread);
auto lk = FEXCore::GuardSignalDeferringSectionWithFallback(AllocationMutex, TLSThread);

length = FEXCore::AlignUp(length, FHU::FEX_PAGE_SIZE);

Expand Down Expand Up @@ -585,7 +585,7 @@ OSAllocator_64Bit::OSAllocator_64Bit() {

OSAllocator_64Bit::~OSAllocator_64Bit() {
// This needs a mutex to be thread safe
FEXCore::ScopedPotentialDeferredSignalWithForkableMutex lk(AllocationMutex, TLSThread);
auto lk = FEXCore::GuardSignalDeferringSectionWithFallback(AllocationMutex, TLSThread);

// Walk the pages and deallocate
// First walk the live regions
Expand Down

0 comments on commit 8726c8f

Please sign in to comment.