mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-14 11:46:34 +02:00
weak_ptr _cacheFile
This commit is contained in:
parent
a39eea5a34
commit
c39da5094b
2 changed files with 18 additions and 6 deletions
|
@ -328,7 +328,7 @@ public:
|
|||
std::shared_ptr<storage::FileStorage> maybeOpenFile() const;
|
||||
|
||||
mutable std::shared_ptr<std::mutex> _cacheFileMutex { std::make_shared<std::mutex>() };
|
||||
mutable std::shared_ptr<storage::FileStorage> _cacheFile;
|
||||
mutable std::weak_ptr<storage::FileStorage> _cacheFile;
|
||||
|
||||
static std::vector<std::pair<std::shared_ptr<storage::FileStorage>, std::shared_ptr<std::mutex>>> _cachedKtxFiles;
|
||||
static std::mutex _cachedKtxFilesMutex;
|
||||
|
|
|
@ -190,13 +190,25 @@ KtxStorage::KtxStorage(const std::string& filename) : _filename(filename) {
|
|||
}
|
||||
}
|
||||
|
||||
// maybeOpenFile should be called with _cacheFileMutex already held to avoid modifying the file from multiple threads
|
||||
std::shared_ptr<storage::FileStorage> KtxStorage::maybeOpenFile() const {
|
||||
if (!_cacheFile) {
|
||||
_cacheFile = std::make_shared<storage::FileStorage>(_filename.c_str());
|
||||
std::lock_guard<std::mutex> lock(_cachedKtxFilesMutex);
|
||||
_cachedKtxFiles.emplace_back(_cacheFile, _cacheFileMutex);
|
||||
// Try to get the shared_ptr
|
||||
std::shared_ptr<storage::FileStorage> file = _cacheFile.lock();
|
||||
if (file) {
|
||||
return file;
|
||||
}
|
||||
return _cacheFile;
|
||||
|
||||
// If the file isn't open, create it and save a weak_ptr to it
|
||||
file = std::make_shared<storage::FileStorage>(_filename.c_str());
|
||||
_cacheFile = file;
|
||||
|
||||
{
|
||||
// Add the shared_ptr to the global list of open KTX files, to be released at the beginning of the next present thread frame
|
||||
std::lock_guard<std::mutex> lock(_cachedKtxFilesMutex);
|
||||
_cachedKtxFiles.emplace_back(file, _cacheFileMutex);
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
void KtxStorage::releaseOpenKtxFiles() {
|
||||
|
|
Loading…
Reference in a new issue