diff --git a/src/sfizz/FilePool.cpp b/src/sfizz/FilePool.cpp index 0fac5591..981485b4 100644 --- a/src/sfizz/FilePool.cpp +++ b/src/sfizz/FilePool.cpp @@ -106,6 +106,10 @@ sfz::FilePool::FilePool(sfz::Logger& logger) sfz::FilePool::~FilePool() { quitThread = true; + + for (unsigned i = 0; i < threadPool.size(); ++i) + workerBarrier.post(); + for (auto& thread: threadPool) thread.join(); } @@ -281,8 +285,9 @@ sfz::FilePromisePtr sfz::FilePool::getFilePromise(const std::string& filename) n DBG("[sfizz] Could not enqueue the promise for " << filename << " (queue capacity " << promiseQueue.capacity() << ")"); return {}; } - + workerBarrier.post(); emptyPromises.pop_back(); + return promise; } @@ -330,8 +335,9 @@ void sfz::FilePool::loadingThread() noexcept continue; } + workerBarrier.wait(); + if (!promiseQueue.try_pop(promise)) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); continue; } @@ -440,6 +446,8 @@ uint32_t sfz::FilePool::getPreloadSize() const noexcept void sfz::FilePool::emptyFileLoadingQueues() noexcept { emptyQueue = true; + workerBarrier.post(); + while (emptyQueue) std::this_thread::sleep_for(std::chrono::milliseconds(1)); } diff --git a/src/sfizz/FilePool.h b/src/sfizz/FilePool.h index 50939dbb..187cb182 100644 --- a/src/sfizz/FilePool.h +++ b/src/sfizz/FilePool.h @@ -27,6 +27,7 @@ #include "Config.h" #include "Defaults.h" #include "LeakDetector.h" +#include "RTSemaphore.h" #include "AudioBuffer.h" #include "AudioSpan.h" #include "SIMDHelpers.h" @@ -260,6 +261,7 @@ private: bool quitThread { false }; bool emptyQueue { false }; std::atomic threadsLoading { 0 }; + RTSemaphore workerBarrier; // File promises data structures along with their guards. std::vector emptyPromises;