From c15def27cff7f32963d6acb15fc14dfdd00c1e4a Mon Sep 17 00:00:00 2001 From: paulfd Date: Sat, 28 Sep 2019 15:57:03 +0200 Subject: [PATCH 1/3] Clean up the activation lists on clearing the synth --- sfizz/Synth.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sfizz/Synth.cpp b/sfizz/Synth.cpp index a2e61aba..cc3c2b04 100644 --- a/sfizz/Synth.cpp +++ b/sfizz/Synth.cpp @@ -108,6 +108,14 @@ void sfz::Synth::buildRegion(const std::vector& regionOpcodes) void sfz::Synth::clear() { + for (auto &voice: voices) + voice->reset(); + for (auto& list: noteActivationLists) + list.clear(); + for (auto& list: ccActivationLists) + list.clear(); + regions.clear(); + filePool.clear(); hasGlobal = false; hasControl = false; numGroups = 0; @@ -121,8 +129,6 @@ void sfz::Synth::clear() globalOpcodes.clear(); masterOpcodes.clear(); groupOpcodes.clear(); - regions.clear(); - filePool.clear(); } void sfz::Synth::handleGlobalOpcodes(const std::vector& members) From ebdf7587139f8c64a3bdfef675c3345c09df92df Mon Sep 17 00:00:00 2001 From: paulfd Date: Sat, 28 Sep 2019 15:57:19 +0200 Subject: [PATCH 2/3] Change the initialization order for threads and mutexes in FilePool --- sfizz/FilePool.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sfizz/FilePool.h b/sfizz/FilePool.h index 5c0c41db..eee11e7a 100644 --- a/sfizz/FilePool.h +++ b/sfizz/FilePool.h @@ -72,11 +72,11 @@ private: void loadingThread() noexcept; void garbageThread() noexcept; bool quitThread { false }; + std::mutex fileHandleMutex; + std::vector>> fileHandles; + absl::flat_hash_map>> preloadedData; std::thread fileLoadingThread { &FilePool::loadingThread, this }; std::thread garbageCollectionThread { &FilePool::garbageThread, this }; - std::vector>> fileHandles; - std::mutex fileHandleMutex; - absl::flat_hash_map>> preloadedData; LEAK_DETECTOR(FilePool); }; } From 9f24c2636ee40e453d4e840359ab40d49654dab5 Mon Sep 17 00:00:00 2001 From: paulfd Date: Sat, 28 Sep 2019 15:57:35 +0200 Subject: [PATCH 3/3] Finer lock/unlock for the garbage collection mutex --- sfizz/FilePool.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sfizz/FilePool.cpp b/sfizz/FilePool.cpp index e10377f3..9acec5e7 100644 --- a/sfizz/FilePool.cpp +++ b/sfizz/FilePool.cpp @@ -145,16 +145,17 @@ void sfz::FilePool::loadingThread() noexcept void sfz::FilePool::garbageThread() noexcept { while (!quitThread) { + fileHandleMutex.lock(); for (auto handle = fileHandles.begin(); handle < fileHandles.end();) { if (handle->use_count() == 1) { handle->reset(); - std::lock_guard guard { fileHandleMutex }; std::iter_swap(handle, fileHandles.end() - 1); fileHandles.pop_back(); } else { handle++; } } + fileHandleMutex.unlock(); std::this_thread::sleep_for(200ms); } }