Replace the clearing thread busy wait with semaphore

This commit is contained in:
Jean Pierre Cimalando 2020-06-23 03:56:56 +02:00
parent 792b62cc78
commit f91ee9cdfa
2 changed files with 15 additions and 5 deletions

View file

@ -125,11 +125,16 @@ sfz::FilePool::~FilePool()
{
quitThread = true;
std::error_code ec;
for (unsigned i = 0; i < threadPool.size(); ++i) {
std::error_code ec;
ec = std::error_code();
workerBarrier.post(ec);
}
ec = std::error_code();
semClearingRequest.post(ec);
for (auto& thread: threadPool)
thread.join();
}
@ -360,10 +365,13 @@ void sfz::FilePool::tryToClearPromises()
void sfz::FilePool::clearingThread()
{
while (!quitThread) {
RTSemaphore& request = semClearingRequest;
do {
request.wait();
if (quitThread)
return;
tryToClearPromises();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
} while (1);
}
void sfz::FilePool::loadingThread() noexcept
@ -443,7 +451,8 @@ void sfz::FilePool::cleanupPromises() noexcept
auto promiseUsedOnce = [](FilePromisePtr& p) { return p.use_count() == 1; };
auto moveToClear = [&](FilePromisePtr& p) { return promisesToClear.push_back(p); };
swapAndPopAll(temporaryFilePromises, promiseUsedOnce, moveToClear);
if (swapAndPopAll(temporaryFilePromises, promiseUsedOnce, moveToClear) > 0)
semClearingRequest.post();
}
void sfz::FilePool::setOversamplingFactor(sfz::Oversampling factor) noexcept

View file

@ -275,6 +275,7 @@ private:
volatile bool emptyQueue { false };
std::atomic<int> threadsLoading { 0 };
RTSemaphore workerBarrier;
RTSemaphore semClearingRequest;
// File promises data structures along with their guards.
std::vector<FilePromisePtr> emptyPromises;