diff --git a/rpcs3/Emu/RSX/D3D12/D3D12GSRender.cpp b/rpcs3/Emu/RSX/D3D12/D3D12GSRender.cpp index 24d279e0c2..81328c58bd 100644 --- a/rpcs3/Emu/RSX/D3D12/D3D12GSRender.cpp +++ b/rpcs3/Emu/RSX/D3D12/D3D12GSRender.cpp @@ -35,21 +35,46 @@ void SetGetD3DGSFrameCallback(GetGSFrameCb2 value) GarbageCollectionThread::GarbageCollectionThread() { + m_isThreadAlive = true; + m_askForTermination = false; m_worker = std::thread([this]() { - while (true) + while (m_isThreadAlive) { std::unique_lock lock(m_mutex); - while (m_queue.empty()) + while (!m_askForTermination) + { + CHECK_EMU_STATUS; + + if (!lock) + { + lock.lock(); + continue; + } + + if (!m_queue.empty()) + { + auto func = std::move(m_queue.front()); + + m_queue.pop(); + + if (lock) lock.unlock(); + + func(); + + continue; + } cv.wait(lock); - m_queue.front()(); - m_queue.pop(); + } } + m_isThreadAlive = false; }); m_worker.detach(); } GarbageCollectionThread::~GarbageCollectionThread() { + m_askForTermination = true; + while (m_isThreadAlive); } void GarbageCollectionThread::pushWork(std::function&& f) diff --git a/rpcs3/Emu/RSX/D3D12/D3D12GSRender.h b/rpcs3/Emu/RSX/D3D12/D3D12GSRender.h index 243f5a4a70..266ed1b7f6 100644 --- a/rpcs3/Emu/RSX/D3D12/D3D12GSRender.h +++ b/rpcs3/Emu/RSX/D3D12/D3D12GSRender.h @@ -215,6 +215,8 @@ struct DataHeap */ struct GarbageCollectionThread { + bool m_isThreadAlive; + bool m_askForTermination; std::mutex m_mutex; std::condition_variable cv; std::queue > m_queue;