Use mutex for synchronizing the UI with the emulator thread

This commit is contained in:
wheremyfoodat 2023-10-07 22:10:10 +03:00
parent ab2ff18290
commit 6ae8d084b4
2 changed files with 41 additions and 14 deletions

View file

@ -70,13 +70,17 @@ MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent)
void MainWindow::emuThreadMainLoop() {
while (appRunning) {
if (needToLoadROM.load()) {
bool success = emu->loadROM(romToLoad);
if (!success) {
printf("Failed to load ROM");
}
{
std::unique_lock lock(messageQueueMutex);
needToLoadROM.store(false, std::memory_order::seq_cst);
if (needToLoadROM) {
needToLoadROM = false;
bool success = emu->loadROM(romToLoad);
if (!success) {
printf("Failed to load ROM");
}
}
}
emu->runFrame();
@ -99,17 +103,22 @@ void MainWindow::swapEmuBuffer() {
void MainWindow::selectROM() {
// Are we already waiting for a ROM to be loaded? Then complain about it!
if (needToLoadROM.load()) {
QMessageBox::warning(this, tr("Already loading ROM"), tr("Panda3DS is already busy loading a ROM, please wait"));
return;
{
std::unique_lock lock(messageQueueMutex);
if (needToLoadROM) {
QMessageBox::warning(this, tr("Already loading ROM"), tr("Panda3DS is already busy loading a ROM, please wait"));
return;
}
}
auto path =
QFileDialog::getOpenFileName(this, tr("Select 3DS ROM to load"), "", tr("Nintendo 3DS ROMs (*.3ds *.cci *.cxi *.app *.3dsx *.elf *.axf)"));
if (!path.isEmpty()) {
std::unique_lock lock(messageQueueMutex);
romToLoad = path.toStdU16String();
needToLoadROM.store(true, std::memory_order_seq_cst);
needToLoadROM = true;
}
}
@ -184,7 +193,6 @@ void MainWindow::setTheme(Theme theme) {
}
void MainWindow::dumpRomFS() {
// TODO: LOCK FILE MUTEX HERE
auto folder = QFileDialog::getExistingDirectory(
this, tr("Select folder to dump RomFS files to"), "", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks
);
@ -192,7 +200,23 @@ void MainWindow::dumpRomFS() {
if (folder.isEmpty()) {
return;
}
std::filesystem::path path(folder.toStdU16String());
// TODO: This might break if the game accesses RomFS while we're dumping, we should move it to the emulator thread when we've got a message queue going
messageQueueMutex.lock();
RomFS::DumpingResult res = emu->dumpRomFS(path);
messageQueueMutex.unlock();
switch (res) {
case RomFS::DumpingResult::Success: break; // Yay!
case RomFS::DumpingResult::InvalidFormat:
QMessageBox::warning(
this, tr("Invalid format for RomFS dumping"), tr("The currently loaded app is not in a format that supports RomFS!")
);
break;
case RomFS::DumpingResult::NoRomFS:
QMessageBox::warning(this, tr("No RomFS found"), tr("No RomFS partition was found in the loaded app"));
break;
}
}