Skip to content

Commit

Permalink
Merge pull request #12616 from mitaclaw/dvd-interface-cpu-thread-guard
Browse files Browse the repository at this point in the history
DVDInterface: Modernize With CPUThreadGuard
  • Loading branch information
AdmiralCurtiss committed Mar 17, 2024
2 parents 0bfa64b + fe61efc commit c964d55
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 32 deletions.
3 changes: 2 additions & 1 deletion Source/Android/jni/MainAndroid.cpp
Expand Up @@ -635,7 +635,8 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_ChangeDisc(J
HostThreadLock guard;
const std::string path = GetJString(env, jFile);
__android_log_print(ANDROID_LOG_INFO, DOLPHIN_TAG, "Change Disc: %s", path.c_str());
Core::RunAsCPUThread([&path] { Core::System::GetInstance().GetDVDInterface().ChangeDisc(path); });
auto& system = Core::System::GetInstance();
system.GetDVDInterface().ChangeDisc(Core::CPUThreadGuard{system}, path);
}

JNIEXPORT jobject JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_GetLogTypeNames(JNIEnv* env,
Expand Down
20 changes: 11 additions & 9 deletions Source/Core/Core/HW/DVD/DVDInterface.cpp
Expand Up @@ -23,6 +23,7 @@
#include "Core/AchievementManager.h"
#include "Core/Config/MainSettings.h"
#include "Core/Config/SessionSettings.h"
#include "Core/Core.h"
#include "Core/CoreTiming.h"
#include "Core/DolphinAnalytics.h"
#include "Core/HW/AudioInterface.h"
Expand Down Expand Up @@ -419,7 +420,7 @@ bool DVDInterface::IsDiscInside() const

void DVDInterface::AutoChangeDiscCallback(Core::System& system, u64 userdata, s64 cyclesLate)
{
system.GetDVDInterface().AutoChangeDisc();
system.GetDVDInterface().AutoChangeDisc(Core::CPUThreadGuard{system});
}

void DVDInterface::EjectDiscCallback(Core::System& system, u64 userdata, s64 cyclesLate)
Expand All @@ -441,15 +442,16 @@ void DVDInterface::InsertDiscCallback(Core::System& system, u64 userdata, s64 cy
}

// Must only be called on the CPU thread
void DVDInterface::EjectDisc(EjectCause cause)
void DVDInterface::EjectDisc(const Core::CPUThreadGuard& guard, EjectCause cause)
{
m_system.GetCoreTiming().ScheduleEvent(0, m_eject_disc);
if (cause == EjectCause::User)
ExpansionInterface::g_rtc_flags[ExpansionInterface::RTCFlag::EjectButton] = true;
}

// Must only be called on the CPU thread
void DVDInterface::ChangeDisc(const std::vector<std::string>& paths)
void DVDInterface::ChangeDisc(const Core::CPUThreadGuard& guard,
const std::vector<std::string>& paths)
{
ASSERT_MSG(DISCIO, !paths.empty(), "Trying to insert an empty list of discs");

Expand All @@ -459,19 +461,19 @@ void DVDInterface::ChangeDisc(const std::vector<std::string>& paths)
m_auto_disc_change_index = 0;
}

ChangeDisc(paths[0]);
ChangeDisc(guard, paths[0]);
}

// Must only be called on the CPU thread
void DVDInterface::ChangeDisc(const std::string& new_path)
void DVDInterface::ChangeDisc(const Core::CPUThreadGuard& guard, const std::string& new_path)
{
if (!m_disc_path_to_insert.empty())
{
PanicAlertFmtT("A disc is already about to be inserted.");
return;
}

EjectDisc(EjectCause::User);
EjectDisc(guard, EjectCause::User);

m_disc_path_to_insert = new_path;
m_system.GetCoreTiming().ScheduleEvent(m_system.GetSystemTimers().GetTicksPerSecond(),
Expand All @@ -491,13 +493,13 @@ void DVDInterface::ChangeDisc(const std::string& new_path)
}

// Must only be called on the CPU thread
bool DVDInterface::AutoChangeDisc()
bool DVDInterface::AutoChangeDisc(const Core::CPUThreadGuard& guard)
{
if (m_auto_disc_change_paths.empty())
return false;

m_auto_disc_change_index = (m_auto_disc_change_index + 1) % m_auto_disc_change_paths.size();
ChangeDisc(m_auto_disc_change_paths[m_auto_disc_change_index]);
ChangeDisc(guard, m_auto_disc_change_paths[m_auto_disc_change_index]);
return true;
}

Expand Down Expand Up @@ -1096,7 +1098,7 @@ void DVDInterface::ExecuteCommand(ReplyType reply_type)
}
else if (force_eject)
{
EjectDisc(EjectCause::Software);
EjectDisc(Core::CPUThreadGuard{m_system}, EjectCause::Software);
}
break;
}
Expand Down
11 changes: 6 additions & 5 deletions Source/Core/Core/HW/DVD/DVDInterface.h
Expand Up @@ -17,8 +17,9 @@
class PointerWrap;
namespace Core
{
class CPUThreadGuard;
class System;
}
} // namespace Core
namespace CoreTiming
{
struct EventType;
Expand Down Expand Up @@ -140,10 +141,10 @@ class DVDInterface
void SetDisc(std::unique_ptr<DiscIO::VolumeDisc> disc,
std::optional<std::vector<std::string>> auto_disc_change_paths);
bool IsDiscInside() const;
void EjectDisc(EjectCause cause); // Must only be called on the CPU thread
void ChangeDisc(const std::vector<std::string>& paths); // Must only be called on the CPU thread
void ChangeDisc(const std::string& new_path); // Must only be called on the CPU thread
bool AutoChangeDisc(); // Must only be called on the CPU thread
void EjectDisc(const Core::CPUThreadGuard& guard, EjectCause cause);
void ChangeDisc(const Core::CPUThreadGuard& guard, const std::vector<std::string>& paths);
void ChangeDisc(const Core::CPUThreadGuard& guard, const std::string& new_path);
bool AutoChangeDisc(const Core::CPUThreadGuard& guard);

// This function returns true and calls SConfig::SetRunningGameMetadata(Volume&, Partition&)
// if both of the following conditions are true:
Expand Down
7 changes: 5 additions & 2 deletions Source/Core/Core/HW/WII_IPC.cpp
Expand Up @@ -6,6 +6,7 @@
#include "Common/ChunkFile.h"
#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
#include "Core/Core.h"
#include "Core/CoreTiming.h"
#include "Core/HW/DVD/DVDInterface.h"
#include "Core/HW/MMIO.h"
Expand Down Expand Up @@ -176,7 +177,8 @@ void WiiIPC::RegisterMMIO(MMIO::Mapping* mmio, u32 base)
if (wii_ipc.m_gpio_out[GPIO::DO_EJECT])
{
INFO_LOG_FMT(WII_IPC, "Ejecting disc due to GPIO write");
system.GetDVDInterface().EjectDisc(DVD::EjectCause::Software);
system.GetDVDInterface().EjectDisc(Core::CPUThreadGuard{system},
DVD::EjectCause::Software);
}
// SENSOR_BAR is checked by WiimoteEmu::CameraLogic
// TODO: AVE, SLOT_LED
Expand Down Expand Up @@ -212,7 +214,8 @@ void WiiIPC::RegisterMMIO(MMIO::Mapping* mmio, u32 base)
if (wii_ipc.m_gpio_out[GPIO::DO_EJECT])
{
INFO_LOG_FMT(WII_IPC, "Ejecting disc due to GPIO write");
system.GetDVDInterface().EjectDisc(DVD::EjectCause::Software);
system.GetDVDInterface().EjectDisc(Core::CPUThreadGuard{system},
DVD::EjectCause::Software);
}
// SENSOR_BAR is checked by WiimoteEmu::CameraLogic
// TODO: AVE, SLOT_LED
Expand Down
13 changes: 6 additions & 7 deletions Source/Core/Core/Movie.cpp
Expand Up @@ -1254,13 +1254,12 @@ void MovieManager::PlayController(GCPadStatus* PadStatus, int controllerID)

if (m_pad_state.disc)
{
Core::RunAsCPUThread([this] {
if (!m_system.GetDVDInterface().AutoChangeDisc())
{
m_system.GetCPU().Break();
PanicAlertFmtT("Change the disc to {0}", m_disc_change_filename);
}
});
const Core::CPUThreadGuard guard(m_system);
if (!m_system.GetDVDInterface().AutoChangeDisc(guard))
{
m_system.GetCPU().Break();
PanicAlertFmtT("Change the disc to {0}", m_disc_change_filename);
}
}

if (m_pad_state.reset)
Expand Down
5 changes: 2 additions & 3 deletions Source/Core/DolphinQt/GameList/GameList.cpp
Expand Up @@ -879,9 +879,8 @@ void GameList::ChangeDisc()
if (!game)
return;

Core::RunAsCPUThread([file_path = game->GetFilePath()] {
Core::System::GetInstance().GetDVDInterface().ChangeDisc(file_path);
});
auto& system = Core::System::GetInstance();
system.GetDVDInterface().ChangeDisc(Core::CPUThreadGuard{system}, game->GetFilePath());
}

QAbstractItemView* GameList::GetActiveView() const
Expand Down
12 changes: 7 additions & 5 deletions Source/Core/DolphinQt/MainWindow.cpp
Expand Up @@ -794,15 +794,17 @@ void MainWindow::ChangeDisc()
{
std::vector<std::string> paths = StringListToStdVector(PromptFileNames());

if (!paths.empty())
Core::RunAsCPUThread(
[&paths] { Core::System::GetInstance().GetDVDInterface().ChangeDisc(paths); });
if (paths.empty())
return;

auto& system = Core::System::GetInstance();
system.GetDVDInterface().ChangeDisc(Core::CPUThreadGuard{system}, paths);
}

void MainWindow::EjectDisc()
{
Core::RunAsCPUThread(
[] { Core::System::GetInstance().GetDVDInterface().EjectDisc(DVD::EjectCause::User); });
auto& system = Core::System::GetInstance();
system.GetDVDInterface().EjectDisc(Core::CPUThreadGuard{system}, DVD::EjectCause::User);
}

void MainWindow::OpenUserFolder()
Expand Down

0 comments on commit c964d55

Please sign in to comment.