Skip to content

Commit

Permalink
Merge pull request #1504 from alicevision/mug/imageCacheRefineMutex
Browse files Browse the repository at this point in the history
[image] ImageCache: refine mutex usage
  • Loading branch information
cbentejac authored Sep 5, 2023
2 parents 34cd9cd + d02eb4c commit 5f5c5cb
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/aliceVision/image/ImageCache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,19 @@ class ImageCache
/**
* @brief Load a new image corresponding to the given key and add it as a new entry in the cache.
* @param[in] key the key used to identify the entry in the cache
* @param[in] lockPeek lock on the peeking mutex, will be released
*/
template<typename TPix>
void load(const CacheKey& key);
void load(const CacheKey& key, std::unique_lock<std::mutex>& lockPeek);

CacheInfo _info;
ImageReadOptions _options;
std::unordered_map<CacheKey, CacheValue, CacheKeyHasher> _imagePtrs;
/// ordered from LRU (Least Recently Used) to MRU (Most Recently Used)
std::list<CacheKey> _keys;
mutable std::mutex _mutex;

mutable std::mutex _mutexGeneral;
mutable std::mutex _mutexPeek;

};

Expand All @@ -289,7 +292,7 @@ std::shared_ptr<Image<TPix>> ImageCache::get(const std::string& filename, int do
<< "request was made with downscale level " << downscaleLevel);
}

const std::lock_guard<std::mutex> lock(_mutex);
std::unique_lock<std::mutex> lockPeek(_mutexPeek);

ALICEVISION_LOG_TRACE("[image] ImageCache: reading " << filename
<< " with downscale level " << downscaleLevel
Expand Down Expand Up @@ -320,6 +323,8 @@ std::shared_ptr<Image<TPix>> ImageCache::get(const std::string& filename, int do
}
}

const std::scoped_lock<std::mutex> lockGeneral(_mutexGeneral);

// retrieve image size
int width, height;
readImageSize(filename, width, height);
Expand All @@ -328,7 +333,7 @@ std::shared_ptr<Image<TPix>> ImageCache::get(const std::string& filename, int do
// add image to cache if it fits in capacity
if (memSize + _info.contentSize <= _info.capacity)
{
load<TPix>(keyReq);
load<TPix>(keyReq, lockPeek);

ALICEVISION_LOG_TRACE("[image] ImageCache: " << toString());
return _imagePtrs.at(keyReq).get<TPix>();
Expand All @@ -355,7 +360,7 @@ std::shared_ptr<Image<TPix>> ImageCache::get(const std::string& filename, int do

_info.nbRemoveUnused++;

load<TPix>(keyReq);
load<TPix>(keyReq, lockPeek);

ALICEVISION_LOG_TRACE("[image] ImageCache: " << toString());
return _imagePtrs.at(keyReq).get<TPix>();
Expand Down Expand Up @@ -394,7 +399,7 @@ std::shared_ptr<Image<TPix>> ImageCache::get(const std::string& filename, int do
// add image to cache if it fits in maxSize
if (memSize + _info.contentSize <= _info.maxSize)
{
load<TPix>(keyReq);
load<TPix>(keyReq, lockPeek);

ALICEVISION_LOG_TRACE("[image] ImageCache: " << toString());
return _imagePtrs.at(keyReq).get<TPix>();
Expand All @@ -406,8 +411,10 @@ std::shared_ptr<Image<TPix>> ImageCache::get(const std::string& filename, int do
}

template<typename TPix>
void ImageCache::load(const CacheKey& key)
void ImageCache::load(const CacheKey& key, std::unique_lock<std::mutex>& lockPeek)
{
lockPeek.unlock();

auto img = std::make_shared<Image<TPix>>();

// load image from disk
Expand All @@ -419,6 +426,8 @@ void ImageCache::load(const CacheKey& key)
imageAlgo::resizeImage(key.downscaleLevel, *img);
}

lockPeek.lock();

_info.nbLoadFromDisk++;

// create wrapper around shared pointer
Expand All @@ -442,7 +451,7 @@ bool ImageCache::contains(const std::string& filename, int downscaleLevel) const
<< "request was made with downscale level " << downscaleLevel);
}

const std::lock_guard<std::mutex> lock(_mutex);
const std::scoped_lock<std::mutex> lockPeek(_mutexPeek);

using TInfo = ColorTypeInfo<TPix>;

Expand Down

0 comments on commit 5f5c5cb

Please sign in to comment.