From 17236412e3f2be9ffd8416d09a96ae1fdb530107 Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Sat, 30 Nov 2019 16:17:10 +0100 Subject: [PATCH 1/2] Added a signal that the filepool should empty the queue --- src/sfizz/FilePool.cpp | 8 +++++++- src/sfizz/FilePool.h | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/sfizz/FilePool.cpp b/src/sfizz/FilePool.cpp index 9acec5e7..200bb285 100644 --- a/src/sfizz/FilePool.cpp +++ b/src/sfizz/FilePool.cpp @@ -112,6 +112,12 @@ void sfz::FilePool::enqueueLoading(Voice* voice, const std::string* sample, int void sfz::FilePool::loadingThread() noexcept { while (!quitThread) { + if (emptyQueue) { + while(loadingQueue.pop()) {} + emptyQueue = false; + continue; + } + FileLoadingInformation fileToLoad {}; if (!loadingQueue.wait_dequeue_timed(fileToLoad, 200ms)) { continue; @@ -135,7 +141,7 @@ void sfz::FilePool::loadingThread() noexcept } SndfileHandle sndFile(reinterpret_cast(file.c_str())); - + std::lock_guard guard { fileHandleMutex }; fileHandles.emplace_back(readFromFile(sndFile, fileToLoad.numFrames)); fileToLoad.voice->setFileData(fileHandles.back(), fileToLoad.ticket); diff --git a/src/sfizz/FilePool.h b/src/sfizz/FilePool.h index 3c088dce..d90ffc54 100644 --- a/src/sfizz/FilePool.h +++ b/src/sfizz/FilePool.h @@ -113,6 +113,8 @@ public: * */ void clear(); + void emptyFileLoadingQueue() noexcept { emptyQueue = true; } + bool shouldEmptyQueue() const noexcept { return emptyQueue; } private: fs::path rootDirectory; struct FileLoadingInformation { @@ -126,6 +128,7 @@ private: void loadingThread() noexcept; void garbageThread() noexcept; bool quitThread { false }; + bool emptyQueue { false }; std::mutex fileHandleMutex; std::vector>> fileHandles; absl::flat_hash_map>> preloadedData; From 8b426fa6e2a9b81d0d0ed7bcbbd707188317c9fa Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Sat, 30 Nov 2019 16:17:27 +0100 Subject: [PATCH 2/2] Signal the filepool to empty the queue and wait for completion --- src/sfizz/Synth.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/sfizz/Synth.cpp b/src/sfizz/Synth.cpp index 8d8cb447..4be10b24 100644 --- a/src/sfizz/Synth.cpp +++ b/src/sfizz/Synth.cpp @@ -533,6 +533,12 @@ void sfz::Synth::resetVoices(int numVoices) std::this_thread::sleep_for(1ms); } + // Empty the file pool loading queue and spinlock on it + filePool.emptyFileLoadingQueue(); + while (filePool.shouldEmptyQueue()) { + std::this_thread::sleep_for(1ms); + } + voices.clear(); for (int i = 0; i < numVoices; ++i) voices.push_back(std::make_unique(midiState));