diff Core/Cache/MemoryObjectCache.cpp @ 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 4d809b2e1141
children 94f4a18a79cc
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())
     {