From d6d7eb73dfd3d6a0b1aabbf5e1eb2fa33ce39fe6 Mon Sep 17 00:00:00 2001 From: paulfd Date: Fri, 20 Sep 2019 09:47:34 +0200 Subject: [PATCH] Corrected a bug where the loading thread would quit on creation due to the order of class member initialization --- sfizz/FilePool.cpp | 4 +++- sfizz/FilePool.h | 12 ++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/sfizz/FilePool.cpp b/sfizz/FilePool.cpp index 38034a8d..9b20a6b1 100644 --- a/sfizz/FilePool.cpp +++ b/sfizz/FilePool.cpp @@ -44,7 +44,7 @@ std::unique_ptr> readFromFile(SndfileHandle& sndFile, int numFram sndFile.readf(tempReadBuffer->channelWriter(0), numFrames); ::readInterleaved(tempReadBuffer->getSpan(0), returnedBuffer->getSpan(0), returnedBuffer->getSpan(1)); } - return std::move(returnedBuffer); + return returnedBuffer; } std::optional sfz::FilePool::getFileInformation(std::string_view filename, uint32_t offset) noexcept @@ -142,8 +142,10 @@ void sfz::FilePool::garbageThread() noexcept for (auto handle = fileHandles.begin(); handle < fileHandles.end();) { if (handle->use_count() == 1) { std::lock_guard guard { fileHandleMutex }; + handle->reset(); std::iter_swap(handle, fileHandles.end() - 1); fileHandles.pop_back(); + DBG("Popped a background file... " << fileHandles.size() << " remaining") } else { handle++; } diff --git a/sfizz/FilePool.h b/sfizz/FilePool.h index c8041afa..453f7145 100644 --- a/sfizz/FilePool.h +++ b/sfizz/FilePool.h @@ -38,11 +38,7 @@ namespace sfz { class FilePool { public: - FilePool() - : fileLoadingThread(std::thread(&FilePool::loadingThread, this)) - , garbageCollectionThread(std::thread(&FilePool::garbageThread, this)) - { - } + FilePool() { } ~FilePool() { @@ -75,11 +71,11 @@ private: moodycamel::BlockingReaderWriterQueue loadingQueue { config::numVoices }; void loadingThread() noexcept; void garbageThread() noexcept; - std::thread fileLoadingThread; - std::thread garbageCollectionThread; + bool quitThread { false }; + std::thread fileLoadingThread { &FilePool::loadingThread, this }; + std::thread garbageCollectionThread { &FilePool::garbageThread, this }; std::vector>> fileHandles; std::mutex fileHandleMutex; - bool quitThread { false }; absl::flat_hash_map>> preloadedData; LEAK_DETECTOR(FilePool); };