Merge pull request #500 from paulfd/loading-mutex

Add a mutex on the loading jobs to protect them in the case of freewheeling
This commit is contained in:
Paul Ferrand 2020-10-15 18:44:24 +02:00 committed by GitHub
commit 078fcf5118
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View file

@ -476,6 +476,8 @@ void sfz::FilePool::dispatchingJob() noexcept
continue;
}
std::lock_guard<std::mutex> guard { loadingJobsMutex };
if (filesToLoad.try_pop(queuedData)) {
loadingJobs.push_back(
threadPool.enqueue([this](const QueuedFileData& data) { loadingJob(data); }, queuedData));
@ -483,7 +485,7 @@ void sfz::FilePool::dispatchingJob() noexcept
// Clear finished jobs
swapAndPopAll(loadingJobs, [](std::future<void>& future) {
return future.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
return is_ready(future);
});
}
}
@ -515,8 +517,11 @@ void sfz::FilePool::emptyFileLoadingQueues() noexcept
void sfz::FilePool::waitForBackgroundLoading() noexcept
{
std::lock_guard<std::mutex> guard { loadingJobsMutex };
for (auto& job : loadingJobs)
job.wait();
loadingJobs.clear();
}

View file

@ -354,6 +354,7 @@ private:
void dispatchingJob() noexcept;
void garbageJob() noexcept;
void loadingJob(QueuedFileData data) noexcept;
std::mutex loadingJobsMutex;
std::vector<std::future<void>> loadingJobs;
std::thread dispatchThread { &FilePool::dispatchingJob, this };
std::thread garbageThread { &FilePool::garbageJob, this };