diff --git a/Core/FileLoaders/CachingFileLoader.cpp b/Core/FileLoaders/CachingFileLoader.cpp index 61a7459045..f161574916 100644 --- a/Core/FileLoaders/CachingFileLoader.cpp +++ b/Core/FileLoaders/CachingFileLoader.cpp @@ -109,9 +109,11 @@ void CachingFileLoader::ShutdownCache() { // TODO: Maybe add some hint that deletion is coming soon? // We can't delete while the thread is running, so have to wait. // This should only happen from the menu. - while (aheadThread_) { + while (aheadThreadRunning_) { sleep_ms(1); } + if (aheadThread_.joinable()) + aheadThread_.join(); std::lock_guard guard(blocksMutex_); for (auto block : blocks_) { @@ -252,7 +254,7 @@ bool CachingFileLoader::MakeCacheSpaceFor(size_t blocks, bool readingAhead) { void CachingFileLoader::StartReadAhead(s64 pos) { std::lock_guard guard(blocksMutex_); - if (aheadThread_) { + if (aheadThreadRunning_) { // Already going. return; } @@ -261,8 +263,10 @@ void CachingFileLoader::StartReadAhead(s64 pos) { return; } - aheadThread_ = true; - std::thread th([this, pos] { + aheadThreadRunning_ = true; + if (aheadThread_.joinable()) + aheadThread_.join(); + aheadThread_ = std::thread([this, pos] { setCurrentThreadName("FileLoaderReadAhead"); std::unique_lock guard(blocksMutex_); @@ -278,7 +282,6 @@ void CachingFileLoader::StartReadAhead(s64 pos) { } } - aheadThread_ = false; + aheadThreadRunning_ = false; }); - th.detach(); } diff --git a/Core/FileLoaders/CachingFileLoader.h b/Core/FileLoaders/CachingFileLoader.h index 97fe0aa682..e778265cce 100644 --- a/Core/FileLoaders/CachingFileLoader.h +++ b/Core/FileLoaders/CachingFileLoader.h @@ -19,6 +19,7 @@ #include #include +#include #include "Common/CommonTypes.h" #include "Core/Loaders.h" @@ -75,6 +76,7 @@ private: std::map blocks_; std::recursive_mutex blocksMutex_; - bool aheadThread_ = false; + bool aheadThreadRunning_ = false; + std::thread aheadThread_; std::once_flag preparedFlag_; }; diff --git a/Core/FileLoaders/RamCachingFileLoader.cpp b/Core/FileLoaders/RamCachingFileLoader.cpp index 4b934cebdf..9ea0c2e61f 100644 --- a/Core/FileLoaders/RamCachingFileLoader.cpp +++ b/Core/FileLoaders/RamCachingFileLoader.cpp @@ -105,9 +105,11 @@ void RamCachingFileLoader::ShutdownCache() { // We can't delete while the thread is running, so have to wait. // This should only happen from the menu. - while (aheadThread_) { + while (aheadThreadRunning_) { sleep_ms(1); } + if (aheadThread_.joinable()) + aheadThread_.join(); std::lock_guard guard(blocksMutex_); blocks_.clear(); @@ -118,7 +120,7 @@ void RamCachingFileLoader::ShutdownCache() { } void RamCachingFileLoader::Cancel() { - if (aheadThread_) { + if (aheadThreadRunning_) { std::lock_guard guard(blocksMutex_); aheadCancel_ = true; } @@ -213,14 +215,16 @@ void RamCachingFileLoader::StartReadAhead(s64 pos) { std::lock_guard guard(blocksMutex_); aheadPos_ = pos; - if (aheadThread_) { + if (aheadThreadRunning_) { // Already going. return; } - aheadThread_ = true; + aheadThreadRunning_ = true; aheadCancel_ = false; - std::thread th([this] { + if (aheadThread_.joinable()) + aheadThread_.join(); + aheadThread_ = std::thread([this] { setCurrentThreadName("FileLoaderReadAhead"); while (aheadRemaining_ != 0 && !aheadCancel_) { @@ -243,9 +247,8 @@ void RamCachingFileLoader::StartReadAhead(s64 pos) { } } - aheadThread_ = false; + aheadThreadRunning_ = false; }); - th.detach(); } u32 RamCachingFileLoader::NextAheadBlock() { diff --git a/Core/FileLoaders/RamCachingFileLoader.h b/Core/FileLoaders/RamCachingFileLoader.h index 60ca2afc91..44c375d442 100644 --- a/Core/FileLoaders/RamCachingFileLoader.h +++ b/Core/FileLoaders/RamCachingFileLoader.h @@ -19,6 +19,7 @@ #include #include +#include #include "Common/CommonTypes.h" #include "Core/Loaders.h" @@ -65,6 +66,7 @@ private: std::mutex blocksMutex_; u32 aheadRemaining_; s64 aheadPos_; - bool aheadThread_ = false; + std::thread aheadThread_; + bool aheadThreadRunning_ = false; bool aheadCancel_ = false; }; diff --git a/Core/HLE/sceIo.cpp b/Core/HLE/sceIo.cpp index 341b31b8d9..8a04372cd1 100644 --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -601,7 +601,6 @@ void __IoInit() { if (ioManagerThreadEnabled) { Core_ListenLifecycle(&__IoWakeManager); ioManagerThread = new std::thread(&__IoManagerThread); - ioManagerThread->detach(); } __KernelRegisterWaitTypeFuncs(WAITTYPE_ASYNCIO, __IoAsyncBeginCallback, __IoAsyncEndCallback); @@ -651,9 +650,10 @@ void __IoShutdown() { ioManagerThreadEnabled = false; ioManager.SyncThread(); ioManager.FinishEventLoop(); - if (ioManagerThread != NULL) { + if (ioManagerThread != nullptr) { + ioManagerThread->join(); delete ioManagerThread; - ioManagerThread = NULL; + ioManagerThread = nullptr; ioManager.Shutdown(); }