From a372d40c48918a7b5cd3ef725c5e5dc52895dd31 Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Fri, 23 Oct 2020 14:46:18 +0200 Subject: [PATCH] Use a single mutex for both the garbage and last used file data structures They're more or less jointly updated anyway by the garbage collection thread --- src/sfizz/FilePool.cpp | 12 ++++-------- src/sfizz/FilePool.h | 3 +-- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/sfizz/FilePool.cpp b/src/sfizz/FilePool.cpp index db1a1dfb..3177018b 100644 --- a/src/sfizz/FilePool.cpp +++ b/src/sfizz/FilePool.cpp @@ -428,7 +428,7 @@ void sfz::FilePool::loadingJob(QueuedFileData data) noexcept data.data->status = FileData::Status::Done; - std::lock_guard guard { lastUsedMutex }; + std::lock_guard guard { garbageAndLastUsedMutex }; if (absl::c_find(lastUsedFiles, *id) == lastUsedFiles.end()) lastUsedFiles.push_back(*id); } @@ -514,10 +514,7 @@ void sfz::FilePool::dispatchingJob() noexcept void sfz::FilePool::garbageJob() noexcept { while (semGarbageBarrier.wait(), garbageFlag) { - std::lock_guard guard { garbageMutex }; - for (auto& g: garbageToCollect) - g.reset(); - + std::lock_guard guard { garbageAndLastUsedMutex }; garbageToCollect.clear(); } } @@ -587,9 +584,8 @@ void sfz::FilePool::setRamLoading(bool loadInRam) noexcept void sfz::FilePool::triggerGarbageCollection() noexcept { - const std::unique_lock lastUsedLock { lastUsedMutex, std::try_to_lock }; - const std::unique_lock garbageLock { garbageMutex, std::try_to_lock }; - if (!lastUsedLock.owns_lock() || !garbageLock.owns_lock()) + const std::unique_lock guard { garbageAndLastUsedMutex, std::try_to_lock }; + if (!guard.owns_lock()) return; const auto now = std::chrono::high_resolution_clock::now(); diff --git a/src/sfizz/FilePool.h b/src/sfizz/FilePool.h index 8c14f1ce..41068598 100644 --- a/src/sfizz/FilePool.h +++ b/src/sfizz/FilePool.h @@ -362,9 +362,8 @@ private: std::thread dispatchThread { &FilePool::dispatchingJob, this }; std::thread garbageThread { &FilePool::garbageJob, this }; - SpinMutex lastUsedMutex; + SpinMutex garbageAndLastUsedMutex; std::vector lastUsedFiles; - SpinMutex garbageMutex; std::vector garbageToCollect; std::shared_ptr threadPool;