#ifndef MRT_MUTATOR_INLINE_H
#define MRT_MUTATOR_INLINE_H
#include "MutatorManager.h"
namespace MapleRuntime {
inline void Mutator::DoEnterSaferegion()
{
SetInSaferegion(SAFE_REGION_TRUE);
}
inline bool Mutator::EnterSaferegion(bool updateUnwindContext) noexcept
{
if (LIKELY(!InSaferegion())) {
if (updateUnwindContext && uwContext.GetUnwindContextStatus() != UnwindContextStatus::RISKY) {
UpdateUnwindContext();
}
DoEnterSaferegion();
return true;
}
return false;
}
inline bool Mutator::LeaveSaferegion() noexcept
{
if (LIKELY(InSaferegion())) {
DoLeaveSaferegion();
return true;
}
return false;
}
__attribute__((always_inline)) inline bool Mutator::TransitionGCPhase(bool bySelf)
{
do {
GCPhaseTransitionState state = transitionState.load();
if (state == FINISH_TRANSITION) {
bool result = mutatorPhase.load() == Heap::GetHeap().GetGCPhase();
if (!bySelf && !result) {
LOG(RTLOG_FATAL, "gc transition mutator %p (phase %u) to gc phase %u failed",
this, mutatorPhase.load(), Heap::GetHeap().GetGCPhase());
}
return result;
}
if (state == IN_TRANSITION) {
if (bySelf) {
WaitForPhaseTransition();
return true;
} else {
return false;
}
}
if (!bySelf && state == NO_TRANSITION) {
return true;
}
CHECK(state == NEED_TRANSITION);
if (transitionState.compare_exchange_weak(state, IN_TRANSITION)) {
TransitionToGCPhaseExclusive(Heap::GetHeap().GetGCPhase());
transitionState.store(FINISH_TRANSITION, std::memory_order_release);
return true;
}
} while (true);
}
__attribute__((always_inline)) inline bool Mutator::TransitionToCpuProfile(bool bySelf)
{
do {
CpuProfileState state = cpuProfileState.load();
if (state == FINISH_CPUPROFILE) {
return true;
}
if (state == IN_CPUPROFILING) {
if (bySelf) {
WaitForCpuProfiling();
return true;
} else {
return false;
}
}
if (!bySelf && state == NO_CPUPROFILE) {
return true;
}
CHECK(state == NEED_CPUPROFILE);
if (cpuProfileState.compare_exchange_weak(state, IN_CPUPROFILING)) {
TransitionToCpuProfileExclusive();
cpuProfileState.store(FINISH_CPUPROFILE, std::memory_order_release);
return true;
}
} while (true);
}
}
#endif