Merge pull request #521 from paulfd/clearing-concurrency-issues

Clearing concurrency issues
This commit is contained in:
JP Cimalando 2020-10-23 20:19:56 +02:00 committed by GitHub
commit 60f895a9ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 11 deletions

View file

@ -428,14 +428,17 @@ void sfz::FilePool::loadingJob(QueuedFileData data) noexcept
data.data->status = FileData::Status::Done;
std::lock_guard<SpinMutex> guard { lastUsedMutex };
std::lock_guard<SpinMutex> guard { garbageAndLastUsedMutex };
if (absl::c_find(lastUsedFiles, *id) == lastUsedFiles.end())
lastUsedFiles.push_back(*id);
}
void sfz::FilePool::clear()
{
std::lock_guard<SpinMutex> guard { garbageAndLastUsedMutex };
emptyFileLoadingQueues();
garbageToCollect.clear();
lastUsedFiles.clear();
preloadedFiles.clear();
}
@ -514,10 +517,7 @@ void sfz::FilePool::dispatchingJob() noexcept
void sfz::FilePool::garbageJob() noexcept
{
while (semGarbageBarrier.wait(), garbageFlag) {
std::lock_guard<SpinMutex> guard { garbageMutex };
for (auto& g: garbageToCollect)
g.reset();
std::lock_guard<SpinMutex> guard { garbageAndLastUsedMutex };
garbageToCollect.clear();
}
}
@ -587,9 +587,8 @@ void sfz::FilePool::setRamLoading(bool loadInRam) noexcept
void sfz::FilePool::triggerGarbageCollection() noexcept
{
const std::unique_lock<SpinMutex> lastUsedLock { lastUsedMutex, std::try_to_lock };
const std::unique_lock<SpinMutex> garbageLock { garbageMutex, std::try_to_lock };
if (!lastUsedLock.owns_lock() || !garbageLock.owns_lock())
const std::unique_lock<SpinMutex> guard { garbageAndLastUsedMutex, std::try_to_lock };
if (!guard.owns_lock())
return;
const auto now = std::chrono::high_resolution_clock::now();
@ -597,7 +596,15 @@ void sfz::FilePool::triggerGarbageCollection() noexcept
if (garbageToCollect.size() == garbageToCollect.capacity())
return false;
auto& data = preloadedFiles[id];
auto it = preloadedFiles.find(id);
if (it == preloadedFiles.end()) {
// Getting here means that the preloadedFiles got changed (probably cleared)
// while the lastUsedFiles were untouched.
ASSERTFALSE;
return true;
}
sfz::FileData& data = it->second;
if (data.status == FileData::Status::Preloaded)
return true;

View file

@ -362,9 +362,8 @@ private:
std::thread dispatchThread { &FilePool::dispatchingJob, this };
std::thread garbageThread { &FilePool::garbageJob, this };
SpinMutex lastUsedMutex;
SpinMutex garbageAndLastUsedMutex;
std::vector<FileId> lastUsedFiles;
SpinMutex garbageMutex;
std::vector<FileAudioBuffer> garbageToCollect;
std::shared_ptr<ThreadPool> threadPool;

View file

@ -194,6 +194,9 @@ void sfz::Synth::clear()
{
const std::lock_guard<SpinMutex> disableCallback { callbackGuard };
// Clear the background queues before removing everyone
resources.filePool.waitForBackgroundLoading();
for (auto& voice : voices)
voice->reset();
for (auto& list : noteActivationLists)