Skip to content

Commit

Permalink
Fix psc race
Browse files Browse the repository at this point in the history
Fixes #46, #58
  • Loading branch information
averne committed Nov 4, 2023
1 parent 919988f commit 0140a29
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
23 changes: 7 additions & 16 deletions sysmodule/src/profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Profile Profile::interpolate(float factor, bool from_day) {
void ProfileManager::transition_thread_func([[maybe_unused]] void *args) {
while (!Man::stop.stop_requested()) {
std::this_thread::sleep_for(50ms);

std::scoped_lock lk(Man::mmio_mutex);
if (!Man::is_active || !Man::should_poll_mmio)
continue;

Expand Down Expand Up @@ -109,23 +111,12 @@ void ProfileManager::transition_thread_func([[maybe_unused]] void *args) {
void ProfileManager::psc_thread_func([[maybe_unused]] void *args) {
while (!Man::stop.stop_requested()) {
if (R_SUCCEEDED(eventWait(&Man::psc_module.event, UINT64_MAX))) {
std::scoped_lock lk(Man::mmio_mutex);

PscPmState state;
ON_SCOPE_EXIT { pscPmModuleAcknowledge(&Man::psc_module, state); };
if (R_SUCCEEDED(pscPmModuleGetRequest(&Man::psc_module, &state, nullptr))) {
switch (state) {
case PscPmState_ReadyAwaken:
case PscPmState_ReadyAwakenCritical:
case PscPmState_Awake:
Man::should_poll_mmio = true;
break;
default:
case PscPmState_ReadySleep:
case PscPmState_ReadySleepCritical:
case PscPmState_ReadyShutdown:
Man::should_poll_mmio = false;
break;
}
}
if (R_SUCCEEDED(pscPmModuleGetRequest(&Man::psc_module, &state, nullptr)))
Man::should_poll_mmio = state == PscPmState_Awake;
}
}
}
Expand All @@ -148,7 +139,7 @@ ams::Result ProfileManager::initialize() {
std::uint64_t size;
R_TRY(svcQueryIoMapping(reinterpret_cast<std::uint64_t *>(&g_disp_va_base), &size, DISP_IO_BASE, DISP_IO_SIZE));

std::array deps = {u32(PscPmModuleId_Display)};
std::array deps = {u32(PscPmModuleId_Display), u32(PscPmModuleId_Omm)};
R_TRY(pscmGetPmModule(&Man::psc_module, PscPmModuleId(125), deps.data(), deps.size(), true));

R_TRY(Man::transition_thread.Initialize(Man::transition_thread_func, nullptr, 0x3f));
Expand Down
2 changes: 1 addition & 1 deletion sysmodule/src/profile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class ProfileManager {
static inline std::array<Profile, static_cast<std::size_t>(ProfileId::Total)> profiles;
static inline std::array<std::uint16_t, 9> saved_internal_csc, saved_external_csc;

static inline ams::os::Mutex commit_mutex;
static inline ams::os::Mutex commit_mutex, mmio_mutex;
};

} // namespace fz

0 comments on commit 0140a29

Please sign in to comment.