Replace the queue emptying busy wait with semaphore

This commit is contained in:
Jean Pierre Cimalando 2020-06-23 03:59:20 +02:00
parent f91ee9cdfa
commit f25dd03db0
2 changed files with 6 additions and 11 deletions

View file

@ -378,19 +378,17 @@ void sfz::FilePool::loadingThread() noexcept
{ {
FilePromisePtr promise; FilePromisePtr promise;
while (!quitThread) { while (!quitThread) {
workerBarrier.wait();
if (emptyQueue) { if (emptyQueue) {
while(promiseQueue.try_pop(promise)) { while (promiseQueue.try_pop(promise)) {
// We're just dequeuing // We're just dequeuing
} }
emptyQueue = false; emptyQueue = false;
semEmptyQueueFinished.post();
continue; continue;
} }
std::error_code ec;
workerBarrier.wait(ec);
ASSERT(!ec);
if (!promiseQueue.try_pop(promise)) { if (!promiseQueue.try_pop(promise)) {
continue; continue;
} }
@ -483,12 +481,8 @@ uint32_t sfz::FilePool::getPreloadSize() const noexcept
void sfz::FilePool::emptyFileLoadingQueues() noexcept void sfz::FilePool::emptyFileLoadingQueues() noexcept
{ {
emptyQueue = true; emptyQueue = true;
std::error_code ec; workerBarrier.post();
workerBarrier.post(ec); semEmptyQueueFinished.wait();
ASSERT(!ec);
while (emptyQueue)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
} }
void sfz::FilePool::waitForBackgroundLoading() noexcept void sfz::FilePool::waitForBackgroundLoading() noexcept

View file

@ -273,6 +273,7 @@ private:
// Signals // Signals
volatile bool quitThread { false }; volatile bool quitThread { false };
volatile bool emptyQueue { false }; volatile bool emptyQueue { false };
RTSemaphore semEmptyQueueFinished;
std::atomic<int> threadsLoading { 0 }; std::atomic<int> threadsLoading { 0 };
RTSemaphore workerBarrier; RTSemaphore workerBarrier;
RTSemaphore semClearingRequest; RTSemaphore semClearingRequest;