mirror of
https://github.com/RetroDECK/Duckstation.git
synced 2025-03-06 14:27:44 +00:00
Qt: Fix emulation thread using 100% CPU when idle
This commit is contained in:
parent
c52040434a
commit
1c2c4c8489
src/duckstation-qt
|
@ -13,6 +13,7 @@
|
||||||
#include "qtutils.h"
|
#include "qtutils.h"
|
||||||
#include <QtCore/QCoreApplication>
|
#include <QtCore/QCoreApplication>
|
||||||
#include <QtCore/QDebug>
|
#include <QtCore/QDebug>
|
||||||
|
#include <QtCore/QEventLoop>
|
||||||
#include <QtWidgets/QMessageBox>
|
#include <QtWidgets/QMessageBox>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
Log_SetChannel(QtHostInterface);
|
Log_SetChannel(QtHostInterface);
|
||||||
|
@ -374,6 +375,8 @@ void QtHostInterface::pauseSystem(bool paused)
|
||||||
|
|
||||||
m_paused = paused;
|
m_paused = paused;
|
||||||
m_audio_stream->PauseOutput(paused);
|
m_audio_stream->PauseOutput(paused);
|
||||||
|
if (!paused)
|
||||||
|
wakeThread();
|
||||||
emit emulationPaused(paused);
|
emit emulationPaused(paused);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -436,6 +439,7 @@ void QtHostInterface::doBootSystem(QString initial_filename, QString initial_sav
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wakeThread();
|
||||||
m_audio_stream->PauseOutput(false);
|
m_audio_stream->PauseOutput(false);
|
||||||
emit emulationStarted();
|
emit emulationStarted();
|
||||||
}
|
}
|
||||||
|
@ -478,12 +482,13 @@ void QtHostInterface::doStopThread()
|
||||||
|
|
||||||
void QtHostInterface::threadEntryPoint()
|
void QtHostInterface::threadEntryPoint()
|
||||||
{
|
{
|
||||||
|
m_worker_thread_event_loop = new QEventLoop();
|
||||||
while (!m_shutdown_flag.load())
|
while (!m_shutdown_flag.load())
|
||||||
{
|
{
|
||||||
if (!m_system)
|
if (!m_system || m_paused)
|
||||||
{
|
{
|
||||||
// wait until we have a system before running
|
// wait until we have a system before running
|
||||||
QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
|
m_worker_thread_event_loop->exec();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -516,15 +521,25 @@ void QtHostInterface::threadEntryPoint()
|
||||||
UpdatePerformanceCounters();
|
UpdatePerformanceCounters();
|
||||||
}
|
}
|
||||||
|
|
||||||
QCoreApplication::processEvents(QEventLoop::AllEvents, m_paused ? 16 : 0);
|
m_worker_thread_event_loop->processEvents(QEventLoop::AllEvents);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_system.reset();
|
m_system.reset();
|
||||||
|
delete m_worker_thread_event_loop;
|
||||||
|
m_worker_thread_event_loop = nullptr;
|
||||||
|
|
||||||
// move back to UI thread
|
// move back to UI thread
|
||||||
moveToThread(m_original_thread);
|
moveToThread(m_original_thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QtHostInterface::wakeThread()
|
||||||
|
{
|
||||||
|
if (isOnWorkerThread())
|
||||||
|
m_worker_thread_event_loop->quit();
|
||||||
|
else
|
||||||
|
QMetaObject::invokeMethod(m_worker_thread_event_loop, "quit", Qt::QueuedConnection);
|
||||||
|
}
|
||||||
|
|
||||||
QtHostInterface::Thread::Thread(QtHostInterface* parent) : QThread(parent), m_parent(parent) {}
|
QtHostInterface::Thread::Thread(QtHostInterface* parent) : QThread(parent), m_parent(parent) {}
|
||||||
|
|
||||||
QtHostInterface::Thread::~Thread() = default;
|
QtHostInterface::Thread::~Thread() = default;
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
|
|
||||||
class ByteStream;
|
class ByteStream;
|
||||||
|
|
||||||
|
class QEventLoop;
|
||||||
class QWidget;
|
class QWidget;
|
||||||
|
|
||||||
class GameList;
|
class GameList;
|
||||||
|
@ -116,6 +117,7 @@ private:
|
||||||
void createThread();
|
void createThread();
|
||||||
void stopThread();
|
void stopThread();
|
||||||
void threadEntryPoint();
|
void threadEntryPoint();
|
||||||
|
void wakeThread();
|
||||||
|
|
||||||
QSettings m_qsettings;
|
QSettings m_qsettings;
|
||||||
|
|
||||||
|
@ -124,6 +126,7 @@ private:
|
||||||
QtDisplayWindow* m_display_window = nullptr;
|
QtDisplayWindow* m_display_window = nullptr;
|
||||||
QThread* m_original_thread = nullptr;
|
QThread* m_original_thread = nullptr;
|
||||||
Thread* m_worker_thread = nullptr;
|
Thread* m_worker_thread = nullptr;
|
||||||
|
QEventLoop* m_worker_thread_event_loop = nullptr;
|
||||||
|
|
||||||
std::atomic_bool m_shutdown_flag{false};
|
std::atomic_bool m_shutdown_flag{false};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue