Use RTSemaphore instead of sleeplocking the background threads

This commit is contained in:
Paul Fd 2020-04-07 15:45:38 +02:00 committed by Jean Pierre Cimalando
parent 5726fd92d1
commit 5f9bbbede5
2 changed files with 12 additions and 2 deletions

View file

@ -106,6 +106,10 @@ sfz::FilePool::FilePool(sfz::Logger& logger)
sfz::FilePool::~FilePool() sfz::FilePool::~FilePool()
{ {
quitThread = true; quitThread = true;
for (unsigned i = 0; i < threadPool.size(); ++i)
workerBarrier.post();
for (auto& thread: threadPool) for (auto& thread: threadPool)
thread.join(); 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() << ")"); DBG("[sfizz] Could not enqueue the promise for " << filename << " (queue capacity " << promiseQueue.capacity() << ")");
return {}; return {};
} }
workerBarrier.post();
emptyPromises.pop_back(); emptyPromises.pop_back();
return promise; return promise;
} }
@ -330,8 +335,9 @@ void sfz::FilePool::loadingThread() noexcept
continue; continue;
} }
workerBarrier.wait();
if (!promiseQueue.try_pop(promise)) { if (!promiseQueue.try_pop(promise)) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue; continue;
} }
@ -440,6 +446,8 @@ uint32_t sfz::FilePool::getPreloadSize() const noexcept
void sfz::FilePool::emptyFileLoadingQueues() noexcept void sfz::FilePool::emptyFileLoadingQueues() noexcept
{ {
emptyQueue = true; emptyQueue = true;
workerBarrier.post();
while (emptyQueue) while (emptyQueue)
std::this_thread::sleep_for(std::chrono::milliseconds(1)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} }

View file

@ -27,6 +27,7 @@
#include "Config.h" #include "Config.h"
#include "Defaults.h" #include "Defaults.h"
#include "LeakDetector.h" #include "LeakDetector.h"
#include "RTSemaphore.h"
#include "AudioBuffer.h" #include "AudioBuffer.h"
#include "AudioSpan.h" #include "AudioSpan.h"
#include "SIMDHelpers.h" #include "SIMDHelpers.h"
@ -260,6 +261,7 @@ private:
bool quitThread { false }; bool quitThread { false };
bool emptyQueue { false }; bool emptyQueue { false };
std::atomic<int> threadsLoading { 0 }; std::atomic<int> threadsLoading { 0 };
RTSemaphore workerBarrier;
// File promises data structures along with their guards. // File promises data structures along with their guards.
std::vector<FilePromisePtr> emptyPromises; std::vector<FilePromisePtr> emptyPromises;