Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions FEXCore/include/FEXCore/Utils/SpinWaitLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ static inline void WaitPred(T* Futex, T ComparisonValue) {
}
}

template<typename T, typename Pred>
static inline void WaitBitMaskPred(T* Futex, T BitMask, T ComparisonValue, Pred Predicate) {
auto AtomicFutex = std::atomic_ref<T>(*Futex);
T Result = AtomicFutex.load();

while (!Predicate(Result & BitMask, ComparisonValue)) {
Result = LoadExclusive(Futex);
if (Predicate(Result & BitMask, ComparisonValue)) {
return;
}

Result = WFELoadAtomic(Futex);
}
}

template<typename T, typename TT>
static inline bool Wait(T* Futex, TT ExpectedValue, const std::chrono::nanoseconds& Timeout) {
auto AtomicFutex = std::atomic_ref<T>(*Futex);
Expand Down Expand Up @@ -213,6 +228,16 @@ static inline void WaitPred(T* Futex, T ComparisonValue) {
}
}

template<typename T, typename Pred>
static inline void WaitBitMaskPred(T* Futex, T BitMask, T ComparisonValue, Pred Predicate) {
auto AtomicFutex = std::atomic_ref<T>(*Futex);
T Result = AtomicFutex.load();

while (!Predicate(Result & BitMask, ComparisonValue)) {
Result = AtomicFutex.load();
}
}

template<typename T, typename TT>
static inline bool Wait(T* Futex, TT ExpectedValue, const std::chrono::nanoseconds& Timeout) {
auto AtomicFutex = std::atomic_ref<T>(*Futex);
Expand Down
7 changes: 5 additions & 2 deletions Source/Windows/WOW64/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ struct TLS {
return reinterpret_cast<std::atomic<uint32_t>&>(TEB->TlsSlots[FEXCore::ToUnderlying(Slot::CONTROL_WORD)]);
}

uint32_t* ControlWordAddress() const {
return reinterpret_cast<uint32_t*>(&TEB->TlsSlots[FEXCore::ToUnderlying(Slot::CONTROL_WORD)]);
}

CONTEXT*& EntryContext() const {
return reinterpret_cast<CONTEXT*&>(TEB->TlsSlots[FEXCore::ToUnderlying(Slot::ENTRY_CONTEXT)]);
}
Expand Down Expand Up @@ -822,8 +826,7 @@ NTSTATUS BTCpuSuspendLocalThread(HANDLE Thread, ULONG* Count) {
}

// Spin until the JIT is interrupted
while (TLS.ControlWord().load() & ControlBits::IN_JIT)
;
FEXCore::Utils::SpinWaitLock::WaitBitMaskPred(TLS.ControlWordAddress(), ControlBits::IN_JIT, 0U, std::equal_to<>());

// The JIT has now been interrupted and the context stored in the thread's CPU area is up-to-date
if (Err = NtSuspendThread(*ThreadDup, Count); Err) {
Expand Down
Loading