From 8cbff50461adf03aae367fe314c22279b77f51b8 Mon Sep 17 00:00:00 2001 From: Jupeyy Date: Tue, 15 Feb 2022 16:27:53 +0100 Subject: [PATCH] Give the condition waits proper conditions --- src/engine/client/backend_sdl.cpp | 12 +++++++----- src/engine/client/backend_sdl.h | 1 + 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/engine/client/backend_sdl.cpp b/src/engine/client/backend_sdl.cpp index 150cb77aa..3beb205da 100644 --- a/src/engine/client/backend_sdl.cpp +++ b/src/engine/client/backend_sdl.cpp @@ -61,6 +61,7 @@ void CGraphicsBackend_Threaded::ThreadFunc(void *pUser) auto *pSelf = (CGraphicsBackend_Threaded *)pUser; std::unique_lock Lock(pSelf->m_BufferSwapMutex); // notify, that the thread started + pSelf->m_Started = true; pSelf->m_BufferDoneCond.notify_all(); while(!pSelf->m_Shutdown) { @@ -75,11 +76,12 @@ void CGraphicsBackend_Threaded::ThreadFunc(void *pUser) pSelf->m_pBuffer = nullptr; pSelf->m_BufferInProcess.store(false, std::memory_order_relaxed); pSelf->m_BufferDoneCond.notify_all(); - } + #if defined(CONF_VIDEORECORDER) - if(IVideo::Current()) - IVideo::Current()->NextVideoFrameThread(); + if(IVideo::Current()) + IVideo::Current()->NextVideoFrameThread(); #endif + } } } @@ -97,7 +99,7 @@ void CGraphicsBackend_Threaded::StartProcessor(ICommandProcessor *pProcessor) std::unique_lock Lock(m_BufferSwapMutex); m_Thread = thread_init(ThreadFunc, this, "Graphics thread"); // wait for the thread to start - m_BufferDoneCond.wait(Lock); + m_BufferDoneCond.wait(Lock, [this]() -> bool { return m_Started; }); } void CGraphicsBackend_Threaded::StopProcessor() @@ -127,7 +129,7 @@ bool CGraphicsBackend_Threaded::IsIdle() const void CGraphicsBackend_Threaded::WaitForIdle() { std::unique_lock Lock(m_BufferSwapMutex); - if(m_pBuffer != nullptr) + while(m_pBuffer != nullptr) m_BufferDoneCond.wait(Lock); } diff --git a/src/engine/client/backend_sdl.h b/src/engine/client/backend_sdl.h index 77d1c59c6..e2f8a8ab7 100644 --- a/src/engine/client/backend_sdl.h +++ b/src/engine/client/backend_sdl.h @@ -72,6 +72,7 @@ private: std::condition_variable m_BufferDoneCond; CCommandBuffer *m_pBuffer; std::atomic_bool m_Shutdown; + bool m_Started = false; std::atomic_bool m_BufferInProcess; void *m_Thread;