changeset 3563:4812825e69fc

accessor to the cache can now require to be unique
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 07 Nov 2019 15:35:18 +0100
parents f47149cdc048
children 9a4db48ff5b1 ee508761d753
files Core/Cache/MemoryObjectCache.cpp Core/Cache/MemoryObjectCache.h Core/Cache/MemoryStringCache.cpp
diffstat 3 files changed, 39 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Cache/MemoryObjectCache.cpp	Thu Nov 07 10:50:50 2019 +0100
+++ b/Core/Cache/MemoryObjectCache.cpp	Thu Nov 07 15:35:18 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())
     {
--- a/Core/Cache/MemoryObjectCache.h	Thu Nov 07 10:50:50 2019 +0100
+++ b/Core/Cache/MemoryObjectCache.h	Thu Nov 07 15:35:18 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
       {
--- a/Core/Cache/MemoryStringCache.cpp	Thu Nov 07 10:50:50 2019 +0100
+++ b/Core/Cache/MemoryStringCache.cpp	Thu Nov 07 15:35:18 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())
     {