Prevent lost wakupes in Pacer and ComputerManager

This commit is contained in:
Cameron Gutman committed 2026-11-12 20:40:31 -06:00
1 parent aad0090d97
commit fe5a0aea98
3 files changed
+37 -12

No files matched your search

+7 -3
View File
@@ -205,9 +205,13 @@ ComputerManager::~ComputerManager()
// Stop the delayed flush thread before acquiring the lock in write mode
// to avoid deadlocking with a flush that needs the lock in read mode.
{
// Wake the delayed flush thread
m_DelayedFlushThread->requestInterruption();
m_DelayedFlushCondition.wakeOne();
// Acquire the delayed flush mutex before requesting interruption and
// waking the thread so we cannot race with its predicate check and wait().
{
QMutexLocker locker(&m_DelayedFlushMutex);
m_DelayedFlushThread->requestInterruption();
m_DelayedFlushCondition.wakeOne();
}
// Wait for it to terminate (and finish any pending flush)
m_DelayedFlushThread->wait();
@@ -35,6 +35,7 @@ Pacer::Pacer(IFFmpegRenderer* renderer, PVIDEO_STATS videoStats) :
m_VsyncThread(nullptr),
m_DeferredFreeFrame(nullptr),
m_Stopping(false),
m_VsyncPending(false),
m_VsyncSource(nullptr),
m_VsyncRenderer(renderer),
m_MaxVideoFps(0),
@@ -46,12 +47,16 @@ Pacer::Pacer(IFFmpegRenderer* renderer, PVIDEO_STATS videoStats) :
Pacer::~Pacer()
{
m_Stopping = true;
{
QMutexLocker locker(&m_FrameQueueLock);
m_Stopping = true;
m_PacingQueueNotEmpty.wakeAll();
m_VsyncSignalled.wakeAll();
m_RenderQueueNotEmpty.wakeAll();
}
// Stop the V-sync thread
if (m_VsyncThread != nullptr) {
m_PacingQueueNotEmpty.wakeAll();
m_VsyncSignalled.wakeAll();
SDL_WaitThread(m_VsyncThread, nullptr);
}
@@ -61,7 +66,6 @@ Pacer::~Pacer()
// Stop the render thread
if (m_RenderThread != nullptr) {
m_RenderQueueNotEmpty.wakeAll();
SDL_WaitThread(m_RenderThread, nullptr);
}
else {
@@ -116,11 +120,18 @@ int Pacer::vsyncThread(void *context)
#endif
bool async = me->m_VsyncSource->isAsync();
while (!me->m_Stopping) {
for (;;) {
bool stopping;
if (async) {
// Wait for the VSync source to invoke signalVsync() or 100ms to elapse
QMutexLocker locker(&me->m_FrameQueueLock);
me->m_VsyncSignalled.wait(&me->m_FrameQueueLock, 100);
while (!me->m_Stopping && !me->m_VsyncPending) {
if (!me->m_VsyncSignalled.wait(&me->m_FrameQueueLock, 100)) {
break;
}
}
me->m_VsyncPending = false;
stopping = me->m_Stopping;
}
else {
// Let the VSync source wait in the context of our thread
@@ -131,11 +142,18 @@ int Pacer::vsyncThread(void *context)
// this is a synchronous VSync source, but we wait on m_VsyncSignalled
// anyway so that the destructor can immediately wake us on shutdown.
QMutexLocker locker(&me->m_FrameQueueLock);
me->m_VsyncSignalled.wait(&me->m_FrameQueueLock, 1000 / me->m_DisplayFps);
if (!me->m_Stopping) {
me->m_VsyncSignalled.wait(&me->m_FrameQueueLock, 1000 / me->m_DisplayFps);
}
stopping = me->m_Stopping;
}
else {
QMutexLocker locker(&me->m_FrameQueueLock);
stopping = me->m_Stopping;
}
}
if (me->m_Stopping) {
if (stopping) {
break;
}
@@ -155,7 +173,7 @@ int Pacer::renderThread(void* context)
SDL_GetError());
}
while (!me->m_Stopping) {
for (;;) {
// Wait for the renderer to be ready for the next frame
me->m_VsyncRenderer->waitToRender();
@@ -347,6 +365,8 @@ bool Pacer::initialize(SDL_Window* window, int maxVideoFps, bool enablePacing)
void Pacer::signalVsync()
{
QMutexLocker locker(&m_FrameQueueLock);
m_VsyncPending = true;
m_VsyncSignalled.wakeOne();
}
@@ -69,6 +69,7 @@ private:
SDL_Thread* m_VsyncThread;
AVFrame* m_DeferredFreeFrame;
bool m_Stopping;
bool m_VsyncPending;
IVsyncSource* m_VsyncSource;
IFFmpegRenderer* m_VsyncRenderer;