Skip to content

Commit

Permalink
Merge pull request #255 from dmitttri/fix-filecache-concurrent-access
Browse files Browse the repository at this point in the history
fix: file cache operations concurrent access ensured with a mutex
  • Loading branch information
dasgarner authored Mar 26, 2022
2 parents 1fdfaac + b20f806 commit eaa6747
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions player/common/storage/FileCacheImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,12 @@ void FileCacheImpl::addToCache(const std::string& filename, const Md5Hash& hash,
node.put(Md5Attr, hash);
node.put(ValidAttr, hash == target);

/* critical section, access and modify file cache buffer */
fileCacheMutex_.lock();
fileCache_.put_child(fullPath(filename), node);

saveFileHashes(cacheFile_);
fileCacheMutex_.unlock();
}

void FileCacheImpl::addToCache(const std::string& filename, const Md5Hash& hash, const DateTime& lastUpdate)
Expand All @@ -157,9 +160,12 @@ void FileCacheImpl::addToCache(const std::string& filename, const Md5Hash& hash,
node.put(LastUpdateAttr, lastUpdate.timestamp());
node.put(ValidAttr, true);

/* critical section, access and modify file cache buffer */
fileCacheMutex_.lock();
fileCache_.put_child(fullPath(filename), node);

saveFileHashes(cacheFile_);
fileCacheMutex_.unlock();
}

void FileCacheImpl::loadFileHashes(const FilePath& path)
Expand Down
2 changes: 2 additions & 0 deletions player/common/storage/FileCacheImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "common/storage/FileCache.hpp"

#include <boost/noncopyable.hpp>
#include <boost/thread/mutex.hpp>

class FileCacheImpl : public FileCache, public XmlDefaultFileHandler, private boost::noncopyable
{
Expand Down Expand Up @@ -31,6 +32,7 @@ class FileCacheImpl : public FileCache, public XmlDefaultFileHandler, private bo
void saveFileHashes(const FilePath& path);

private:
boost::mutex fileCacheMutex_;
XmlNode fileCache_;
FilePath cacheFile_;
};

0 comments on commit eaa6747

Please sign in to comment.