keep _cacheFile alive

This commit is contained in:
SamGondelman 2017-12-15 16:23:53 -08:00
parent a75010fb94
commit 52c4aef721
2 changed files with 7 additions and 27 deletions

View file

@ -324,9 +324,8 @@ public:
protected:
std::shared_ptr<storage::FileStorage> maybeOpenFile() const;
mutable std::mutex _cacheFileCreateMutex;
mutable std::mutex _cacheFileWriteMutex;
mutable std::weak_ptr<storage::FileStorage> _cacheFile;
mutable std::mutex _cacheFileMutex;
mutable std::shared_ptr<storage::FileStorage> _cacheFile;
std::string _filename;
cache::FilePointer _cacheEntry;

View file

@ -188,35 +188,17 @@ KtxStorage::KtxStorage(const std::string& filename) : _filename(filename) {
}
std::shared_ptr<storage::FileStorage> KtxStorage::maybeOpenFile() const {
// 1. Try to get the shared ptr
// 2. If it doesn't exist, grab the mutex around its creation
// 3. If it was created before we got the mutex, return it
// 4. Otherwise, create it
std::shared_ptr<storage::FileStorage> file = _cacheFile.lock();
if (file) {
return file;
if (!_cacheFile) {
_cacheFile = std::make_shared<storage::FileStorage>(_filename.c_str());
}
{
std::lock_guard<std::mutex> lock{ _cacheFileCreateMutex };
file = _cacheFile.lock();
if (file) {
return file;
}
file = std::make_shared<storage::FileStorage>(_filename.c_str());
_cacheFile = file;
}
return file;
return _cacheFile;
}
PixelsPointer KtxStorage::getMipFace(uint16 level, uint8 face) const {
auto faceOffset = _ktxDescriptor->getMipFaceTexelsOffset(level, face);
auto faceSize = _ktxDescriptor->getMipFaceTexelsSize(level, face);
if (faceSize != 0 && faceOffset != 0) {
std::lock_guard<std::mutex> lock(_cacheFileMutex);
auto file = maybeOpenFile();
if (file) {
auto storageView = file->createView(faceSize, faceOffset);
@ -262,6 +244,7 @@ void KtxStorage::assignMipData(uint16 level, const storage::StoragePointer& stor
return;
}
std::lock_guard<std::mutex> lock(_cacheFileMutex);
auto file = maybeOpenFile();
if (!file) {
qWarning() << "Failed to open file to assign mip data " << QString::fromStdString(_filename);
@ -279,8 +262,6 @@ void KtxStorage::assignMipData(uint16 level, const storage::StoragePointer& stor
imageData += ktx::IMAGE_SIZE_WIDTH;
{
std::lock_guard<std::mutex> lock { _cacheFileWriteMutex };
if (level != _minMipLevelAvailable - 1) {
qWarning() << "Invalid level to be stored";
return;