ThreadedFileReader: Fix a low-chance race when closing file

This commit is contained in:
Connor McLaughlin 2022-07-02 21:17:51 +10:00 committed by refractionpcsx2
parent 6e706b3a8c
commit dcb17243f8
1 changed files with 8 additions and 0 deletions

View File

@ -193,6 +193,9 @@ bool ThreadedFileReader::Decompress(void* target, u64 begin, u32 size)
u64 off = begin; u64 off = begin;
while (remaining) while (remaining)
{ {
if (m_requestCancelled.load(std::memory_order_relaxed))
return false;
Chunk chunk = ChunkForOffset(off); Chunk chunk = ChunkForOffset(off);
if (m_internalBlockSize || chunk.offset != off || chunk.length > remaining) if (m_internalBlockSize || chunk.offset != off || chunk.length > remaining)
{ {
@ -304,6 +307,11 @@ void ThreadedFileReader::CancelAndWaitUntilStopped(void)
{ {
m_requestCancelled.store(true, std::memory_order_relaxed); m_requestCancelled.store(true, std::memory_order_relaxed);
std::unique_lock<std::mutex> lock(m_mtx); std::unique_lock<std::mutex> lock(m_mtx);
// Prevent the last request being picked up, if there was one.
// m_requestCancelled just stops the current decompress.
m_requestSize = 0;
while (m_running) while (m_running)
m_condition.wait(lock); m_condition.wait(lock);
} }