# HG changeset patch # User Alain Mazy # Date 1573230894 -3600 # Node ID ee508761d753e9891e2b17a0c48cfe1a10e105c0 # Parent 2999a6e9456b824acdded0190a798eeb5cd39284# Parent 4812825e69fc65fff8ef73ac6e56057e498553b6 merge diff -r 2999a6e9456b -r ee508761d753 Core/Cache/MemoryObjectCache.cpp --- a/Core/Cache/MemoryObjectCache.cpp Thu Nov 07 17:02:19 2019 +0100 +++ b/Core/Cache/MemoryObjectCache.cpp Fri Nov 08 17:34:54 2019 +0100 @@ -208,31 +208,52 @@ } - MemoryObjectCache::Reader::Reader(MemoryObjectCache& cache, - const std::string& key) : -#if !defined(__EMSCRIPTEN__) - contentLock_(cache.contentMutex_), - cacheLock_(cache.cacheMutex_), -#endif + MemoryObjectCache::Accessor::Accessor(MemoryObjectCache& cache, + const std::string& key, + bool unique) : item_(NULL) { +#if !defined(__EMSCRIPTEN__) + if (unique) + { + writerLock_ = WriterLock(cache.contentMutex_); + } + else + { + readerLock_ = ReaderLock(cache.contentMutex_); + } + + // Lock the global structure of the cache, must be *after* the + // reader/writer lock + cacheLock_ = boost::mutex::scoped_lock(cache.cacheMutex_); +#endif + if (cache.content_.Contains(key, item_)) { cache.content_.MakeMostRecent(key); } - + #if !defined(__EMSCRIPTEN__) cacheLock_.unlock(); if (item_ == NULL) { - contentLock_.unlock(); + // This item does not exist in the cache, we can release the + // reader/writer lock + if (unique) + { + writerLock_.unlock(); + } + else + { + readerLock_.unlock(); + } } #endif } - ICacheable& MemoryObjectCache::Reader::GetValue() const + ICacheable& MemoryObjectCache::Accessor::GetValue() const { if (IsValid()) { @@ -245,7 +266,7 @@ } - const boost::posix_time::ptime& MemoryObjectCache::Reader::GetTime() const + const boost::posix_time::ptime& MemoryObjectCache::Accessor::GetTime() const { if (IsValid()) { diff -r 2999a6e9456b -r ee508761d753 Core/Cache/MemoryObjectCache.h --- a/Core/Cache/MemoryObjectCache.h Thu Nov 07 17:02:19 2019 +0100 +++ b/Core/Cache/MemoryObjectCache.h Fri Nov 08 17:34:54 2019 +0100 @@ -83,19 +83,21 @@ void Invalidate(const std::string& key); - class Reader : public boost::noncopyable + class Accessor : public boost::noncopyable { private: #if !defined(__EMSCRIPTEN__) - ReaderLock contentLock_; + ReaderLock readerLock_; + WriterLock writerLock_; boost::mutex::scoped_lock cacheLock_; #endif - Item* item_; + Item* item_; public: - Reader(MemoryObjectCache& cache, - const std::string& key); + Accessor(MemoryObjectCache& cache, + const std::string& key, + bool unique); bool IsValid() const { diff -r 2999a6e9456b -r ee508761d753 Core/Cache/MemoryStringCache.cpp --- a/Core/Cache/MemoryStringCache.cpp Thu Nov 07 17:02:19 2019 +0100 +++ b/Core/Cache/MemoryStringCache.cpp Fri Nov 08 17:34:54 2019 +0100 @@ -69,7 +69,7 @@ bool MemoryStringCache::Fetch(std::string& value, const std::string& key) { - MemoryObjectCache::Reader reader(cache_, key); + MemoryObjectCache::Accessor reader(cache_, key, false /* multiple readers are allowed */); if (reader.IsValid()) {