From a5647ab33943302c4f99bd9cd61e5745510bd362 Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Thu, 15 Oct 2020 14:30:15 +0100 Subject: [PATCH 1/4] Add a mutex on the loading jobs to protect them in the case of freewheeling --- src/sfizz/FilePool.cpp | 4 ++++ src/sfizz/FilePool.h | 1 + 2 files changed, 5 insertions(+) diff --git a/src/sfizz/FilePool.cpp b/src/sfizz/FilePool.cpp index 6c3a08df..ca437d52 100644 --- a/src/sfizz/FilePool.cpp +++ b/src/sfizz/FilePool.cpp @@ -482,6 +482,7 @@ void sfz::FilePool::dispatchingJob() noexcept } // Clear finished jobs + std::lock_guard guard { loadingJobsMutex }; swapAndPopAll(loadingJobs, [](std::future& future) { return future.wait_for(std::chrono::seconds(0)) == std::future_status::ready; }); @@ -515,8 +516,11 @@ void sfz::FilePool::emptyFileLoadingQueues() noexcept void sfz::FilePool::waitForBackgroundLoading() noexcept { + std::lock_guard guard { loadingJobsMutex }; + for (auto& job : loadingJobs) job.wait(); + loadingJobs.clear(); } diff --git a/src/sfizz/FilePool.h b/src/sfizz/FilePool.h index 109394b4..94f2fb2d 100644 --- a/src/sfizz/FilePool.h +++ b/src/sfizz/FilePool.h @@ -354,6 +354,7 @@ private: void dispatchingJob() noexcept; void garbageJob() noexcept; void loadingJob(QueuedFileData data) noexcept; + SpinMutex loadingJobsMutex; std::vector> loadingJobs; std::thread dispatchThread { &FilePool::dispatchingJob, this }; std::thread garbageThread { &FilePool::garbageJob, this }; From 67af16f0e2c7a349217edbb36145feb6617c25ee Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Thu, 15 Oct 2020 15:58:08 +0200 Subject: [PATCH 2/4] Move the lock guard --- src/sfizz/FilePool.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sfizz/FilePool.cpp b/src/sfizz/FilePool.cpp index ca437d52..5be67c6e 100644 --- a/src/sfizz/FilePool.cpp +++ b/src/sfizz/FilePool.cpp @@ -476,13 +476,14 @@ void sfz::FilePool::dispatchingJob() noexcept continue; } + std::lock_guard guard { loadingJobsMutex }; + if (filesToLoad.try_pop(queuedData)) { loadingJobs.push_back( threadPool.enqueue([this](const QueuedFileData& data) { loadingJob(data); }, queuedData)); } // Clear finished jobs - std::lock_guard guard { loadingJobsMutex }; swapAndPopAll(loadingJobs, [](std::future& future) { return future.wait_for(std::chrono::seconds(0)) == std::future_status::ready; }); From f4b6323050da7620972e01b32ebc102e9b7a0aba Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Thu, 15 Oct 2020 16:44:08 +0100 Subject: [PATCH 3/4] Use std::mutex --- src/sfizz/FilePool.cpp | 4 ++-- src/sfizz/FilePool.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sfizz/FilePool.cpp b/src/sfizz/FilePool.cpp index 5be67c6e..cb7faa47 100644 --- a/src/sfizz/FilePool.cpp +++ b/src/sfizz/FilePool.cpp @@ -476,7 +476,7 @@ void sfz::FilePool::dispatchingJob() noexcept continue; } - std::lock_guard guard { loadingJobsMutex }; + std::lock_guard guard { loadingJobsMutex }; if (filesToLoad.try_pop(queuedData)) { loadingJobs.push_back( @@ -517,7 +517,7 @@ void sfz::FilePool::emptyFileLoadingQueues() noexcept void sfz::FilePool::waitForBackgroundLoading() noexcept { - std::lock_guard guard { loadingJobsMutex }; + std::lock_guard guard { loadingJobsMutex }; for (auto& job : loadingJobs) job.wait(); diff --git a/src/sfizz/FilePool.h b/src/sfizz/FilePool.h index 94f2fb2d..a44ce948 100644 --- a/src/sfizz/FilePool.h +++ b/src/sfizz/FilePool.h @@ -354,7 +354,7 @@ private: void dispatchingJob() noexcept; void garbageJob() noexcept; void loadingJob(QueuedFileData data) noexcept; - SpinMutex loadingJobsMutex; + std::mutex loadingJobsMutex; std::vector> loadingJobs; std::thread dispatchThread { &FilePool::dispatchingJob, this }; std::thread garbageThread { &FilePool::garbageJob, this }; From bd4057e9272f888456d9b4ecb1bbf7433f891529 Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Thu, 15 Oct 2020 16:51:25 +0100 Subject: [PATCH 4/4] Use the future helper --- src/sfizz/FilePool.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sfizz/FilePool.cpp b/src/sfizz/FilePool.cpp index cb7faa47..056c76d6 100644 --- a/src/sfizz/FilePool.cpp +++ b/src/sfizz/FilePool.cpp @@ -485,7 +485,7 @@ void sfz::FilePool::dispatchingJob() noexcept // Clear finished jobs swapAndPopAll(loadingJobs, [](std::future& future) { - return future.wait_for(std::chrono::seconds(0)) == std::future_status::ready; + return is_ready(future); }); } }