changeset 356:03d33290cba1

fix concurrency issue
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 20 Dec 2024 14:56:08 +0100
parents a307ec25a008
children 3fc2ef580095
files Framework/Inputs/DecodedPyramidCache.cpp
diffstat 1 files changed, 27 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Inputs/DecodedPyramidCache.cpp	Fri Dec 20 13:48:34 2024 +0100
+++ b/Framework/Inputs/DecodedPyramidCache.cpp	Fri Dec 20 14:56:08 2024 +0100
@@ -97,23 +97,41 @@
 
 
   DecodedPyramidCache::CachedPyramid * DecodedPyramidCache::Store(FrameIdentifier identifier,
-                                                                      DecodedTiledPyramid *pyramid)
+                                                                  DecodedTiledPyramid *pyramid)
   {
     // Mutex must be locked
 
     std::unique_ptr<CachedPyramid> payload(new CachedPyramid(pyramid));
-    CachedPyramid* result = payload.get();
+    CachedPyramid* result;
 
-    MakeRoom(payload->GetMemoryUsage());
+    if (cache_.Contains(identifier, result))
+    {
+      // This element has already been cached by another thread
+      if (result == NULL)
+      {
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+      }
 
-    memoryUsage_ += payload->GetMemoryUsage();
+      // Tag the series as the most recently used
+      cache_.MakeMostRecent(identifier);
 
-    // Add a new element to the cache and make it the most
-    // recently used entry
-    cache_.Add(identifier, payload.release());
+      return result;
+    }
+    else
+    {
+      result = payload.get();
+
+      MakeRoom(payload->GetMemoryUsage());
 
-    assert(SanityCheck());
-    return result;
+      memoryUsage_ += payload->GetMemoryUsage();
+
+      // Add a new element to the cache and make it the most
+      // recently used entry
+      cache_.Add(identifier, payload.release());
+
+      assert(SanityCheck());
+      return result;
+    }
   }