Corrected a bug where the loading thread would quit on creation due to the order of class member initialization

This commit is contained in:
paulfd 2019-09-20 09:47:34 +02:00
parent 4d70ed5b73
commit d6d7eb73df
2 changed files with 7 additions and 9 deletions

View file

@ -44,7 +44,7 @@ std::unique_ptr<AudioBuffer<T>> readFromFile(SndfileHandle& sndFile, int numFram
sndFile.readf(tempReadBuffer->channelWriter(0), numFrames); sndFile.readf(tempReadBuffer->channelWriter(0), numFrames);
::readInterleaved<float>(tempReadBuffer->getSpan(0), returnedBuffer->getSpan(0), returnedBuffer->getSpan(1)); ::readInterleaved<float>(tempReadBuffer->getSpan(0), returnedBuffer->getSpan(0), returnedBuffer->getSpan(1));
} }
return std::move(returnedBuffer); return returnedBuffer;
} }
std::optional<sfz::FilePool::FileInformation> sfz::FilePool::getFileInformation(std::string_view filename, uint32_t offset) noexcept std::optional<sfz::FilePool::FileInformation> sfz::FilePool::getFileInformation(std::string_view filename, uint32_t offset) noexcept
@ -142,8 +142,10 @@ void sfz::FilePool::garbageThread() noexcept
for (auto handle = fileHandles.begin(); handle < fileHandles.end();) { for (auto handle = fileHandles.begin(); handle < fileHandles.end();) {
if (handle->use_count() == 1) { if (handle->use_count() == 1) {
std::lock_guard guard { fileHandleMutex }; std::lock_guard guard { fileHandleMutex };
handle->reset();
std::iter_swap(handle, fileHandles.end() - 1); std::iter_swap(handle, fileHandles.end() - 1);
fileHandles.pop_back(); fileHandles.pop_back();
DBG("Popped a background file... " << fileHandles.size() << " remaining")
} else { } else {
handle++; handle++;
} }

View file

@ -38,11 +38,7 @@
namespace sfz { namespace sfz {
class FilePool { class FilePool {
public: public:
FilePool() FilePool() { }
: fileLoadingThread(std::thread(&FilePool::loadingThread, this))
, garbageCollectionThread(std::thread(&FilePool::garbageThread, this))
{
}
~FilePool() ~FilePool()
{ {
@ -75,11 +71,11 @@ private:
moodycamel::BlockingReaderWriterQueue<FileLoadingInformation> loadingQueue { config::numVoices }; moodycamel::BlockingReaderWriterQueue<FileLoadingInformation> loadingQueue { config::numVoices };
void loadingThread() noexcept; void loadingThread() noexcept;
void garbageThread() noexcept; void garbageThread() noexcept;
std::thread fileLoadingThread; bool quitThread { false };
std::thread garbageCollectionThread; std::thread fileLoadingThread { &FilePool::loadingThread, this };
std::thread garbageCollectionThread { &FilePool::garbageThread, this };
std::vector<std::shared_ptr<AudioBuffer<float>>> fileHandles; std::vector<std::shared_ptr<AudioBuffer<float>>> fileHandles;
std::mutex fileHandleMutex; std::mutex fileHandleMutex;
bool quitThread { false };
absl::flat_hash_map<std::string_view, std::shared_ptr<AudioBuffer<float>>> preloadedData; absl::flat_hash_map<std::string_view, std::shared_ptr<AudioBuffer<float>>> preloadedData;
LEAK_DETECTOR(FilePool); LEAK_DETECTOR(FilePool);
}; };