changeset 6695:4dfff5043aef

reformatting ThreadedInstancesLoader
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 13 Apr 2026 15:11:41 +0200
parents 1fff016b86e8
children ad0e8b7517d4
files OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp OrthancServer/Sources/OrthancConfiguration.cpp OrthancServer/Sources/OrthancGetRequestHandler.cpp OrthancServer/Sources/OrthancMoveRequestHandler.cpp OrthancServer/Sources/OutgoingDicomInstance.h OrthancServer/Sources/ServerContext.cpp OrthancServer/Sources/ServerJobs/ArchiveJob.cpp OrthancServer/Sources/ServerJobs/DicomModalityStoreJob.cpp OrthancServer/Sources/ServerJobs/DicomModalityStoreJob.h OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.cpp OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.h OrthancServer/Sources/ServerJobs/ResourceModificationJob.cpp OrthancServer/Sources/ServerJobs/StoreJob.cpp OrthancServer/Sources/ServerJobs/StoreJob.h OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.h OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp TODO
diffstat 18 files changed, 150 insertions(+), 113 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp	Mon Apr 13 15:11:41 2026 +0200
@@ -1215,8 +1215,8 @@
     bool isIdentical;
 
     std::unique_ptr<DatabaseDicomTagConstraint> dbConstraint(c.ConvertToDatabaseConstraint(isIdentical, level, DicomTagType_Identifier)); // first try to look in the identifier table (that is faster !)
-    
-    if (!isIdentical) // if an exact match can not be ensured because some values contained characters that are removed from the DicomIdentifier table during normalization, 
+
+    if (!isIdentical) // if an exact match can not be ensured because some values contained characters that are removed from the DicomIdentifier table during normalization,
                       // search in the MainDicomTags table instead.
     {
       dbConstraint.reset(c.ConvertToDatabaseConstraint(isIdentical, level, DicomTagType_Main));
--- a/OrthancServer/Sources/OrthancConfiguration.cpp	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/OrthancConfiguration.cpp	Mon Apr 13 15:11:41 2026 +0200
@@ -1354,13 +1354,14 @@
   {
     // from 1.10.0 to 1.12.10, only CONFIG_ZIP_LOADER_THREADS was available -> read from it if CONFIG_LOADER_THREADS is not specified.
     unsigned int loaderThreads = GetUnsignedIntegerParameter(CONFIG_LOADER_THREADS, GetUnsignedIntegerParameter(CONFIG_ZIP_LOADER_THREADS, 1));
-    
+
     if (loaderThreads <= 1)
     {
       return 1; // 0 is not a valid internal value anymore
     }
-
-    return loaderThreads;
+    else
+    {
+      return loaderThreads;
+    }
   }
-
 }
--- a/OrthancServer/Sources/OrthancGetRequestHandler.cpp	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/OrthancGetRequestHandler.cpp	Mon Apr 13 15:11:41 2026 +0200
@@ -73,7 +73,7 @@
     const std::string& id = instancesIds_[position_++];
 
     std::string dicom;
-    instancesLoader_->GetDicom(dicom, id);
+    instancesLoader_->WaitDicomInstance(dicom, id);
     
     if (dicom.empty())
     {
@@ -592,7 +592,7 @@
 
     for (size_t i = 0; i < instancesIds_.size(); ++i)
     {
-      instancesLoader_->PrepareDicom(instancesIds_[i], filesInfo[i]);
+      instancesLoader_->PreloadDicomInstance(instancesIds_[i], filesInfo[i]);
     }
 
     failedUIDs_.clear();
--- a/OrthancServer/Sources/OrthancMoveRequestHandler.cpp	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/OrthancMoveRequestHandler.cpp	Mon Apr 13 15:11:41 2026 +0200
@@ -92,7 +92,7 @@
 
         for (size_t i = 0; i < instancesIds_.size(); ++i)
         {
-          instancesLoader_->PrepareDicom(instancesIds_[i], filesInfo[i]);
+          instancesLoader_->PreloadDicomInstance(instancesIds_[i], filesInfo[i]);
         }
       }
 
@@ -111,7 +111,7 @@
         const std::string& id = instancesIds_[position_++];
 
         std::string dicom;
-        instancesLoader_->GetDicom(dicom, id);
+        instancesLoader_->WaitDicomInstance(dicom, id);
 
         if (connection_.get() == NULL)
         {
--- a/OrthancServer/Sources/OutgoingDicomInstance.h	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/OutgoingDicomInstance.h	Mon Apr 13 15:11:41 2026 +0200
@@ -37,12 +37,14 @@
 
   class OutgoingDicomInstance : public boost::noncopyable
   {
+  private:
     DicomInstanceDestination            destination_;
     std::unique_ptr<ParsedDicomFile>    dicom_;
 
     explicit OutgoingDicomInstance(ParsedDicomFile* dicom) :
       dicom_(dicom)
-    {}
+    {
+    }
 
   public:
     // WARNING: The source in the factory methods is *not* copied and
@@ -60,8 +62,8 @@
     const DicomInstanceDestination& GetDestination() const
     {
       return destination_;
-    } 
-    
+    }
+
     void GetSimplifiedJson(Json::Value& dicomAsJson) const;
   };
 }
--- a/OrthancServer/Sources/ServerContext.cpp	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/ServerContext.cpp	Mon Apr 13 15:11:41 2026 +0200
@@ -176,7 +176,7 @@
           }
           catch (OrthancException& e)
           {
-            LOG(ERROR) << "Error in the " << it->GetDescription() 
+            LOG(ERROR) << "Error in the " << it->GetDescription()
                        << " callback while signaling a change: " << e.What()
                        << " (code " << e.GetErrorCode() << ")";
           }
@@ -220,7 +220,7 @@
           }
           catch (OrthancException& e)
           {
-            LOG(ERROR) << "Error in the " << it->GetDescription() 
+            LOG(ERROR) << "Error in the " << it->GetDescription()
                        << " callback while signaling a job event: " << e.What()
                        << " (code " << e.GetErrorCode() << ")";
           }
@@ -763,7 +763,7 @@
           }
           catch (OrthancException& e)
           {
-            LOG(ERROR) << "Error in the " << it->GetDescription() 
+            LOG(ERROR) << "Error in the " << it->GetDescription()
                        << " callback while receiving an instance: " << e.What()
                        << " (code " << e.GetErrorCode() << ")";
             throw;
@@ -905,7 +905,7 @@
             }
             catch (OrthancException& e)
             {
-              LOG(ERROR) << "Error in the " << it->GetDescription() 
+              LOG(ERROR) << "Error in the " << it->GetDescription()
                         << " callback while receiving an instance: " << e.What()
                         << " (code " << e.GetErrorCode() << ")";
             }
@@ -2065,7 +2065,7 @@
         }
         catch (OrthancException& e)
         {
-          LOG(ERROR) << "Error in the " << it->GetDescription() 
+          LOG(ERROR) << "Error in the " << it->GetDescription()
                       << " callback while sending an instance: " << e.What()
                       << " (code " << e.GetErrorCode() << ")";
           throw;
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Mon Apr 13 15:11:41 2026 +0200
@@ -539,7 +539,7 @@
             try
             {
               LOG(INFO) << "Adding instance " << instanceId_ << " in zip";
-              instancesLoader.GetDicom(content, instanceId_);
+              instancesLoader.WaitDicomInstance(content, instanceId_);
             }
             catch (OrthancException& e)
             {
@@ -658,7 +658,7 @@
                           const std::string& instanceId,
                           const FileInfo& fileInfo)
     {
-      instancesLoader_.PrepareDicom(instanceId, fileInfo);
+      instancesLoader_.PreloadDicomInstance(instanceId, fileInfo);
       commands_.push_back(new Command(Type_WriteInstance, filename, instanceId, fileInfo));
       instancesCount_ ++;
       uncompressedSize_ += fileInfo.GetUncompressedSize();
--- a/OrthancServer/Sources/ServerJobs/DicomModalityStoreJob.cpp	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/DicomModalityStoreJob.cpp	Mon Apr 13 15:11:41 2026 +0200
@@ -55,7 +55,7 @@
 
     try
     {
-      instancesLoader_->GetDicom(dicom, instance);
+      instancesLoader_->WaitDicomInstance(dicom, instance);
     }
     catch (OrthancException& e)
     {
--- a/OrthancServer/Sources/ServerJobs/DicomModalityStoreJob.h	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/DicomModalityStoreJob.h	Mon Apr 13 15:11:41 2026 +0200
@@ -56,6 +56,11 @@
     
     virtual bool HandleTrailingStep() ORTHANC_OVERRIDE;
 
+    virtual const char* GetLoaderPrefix() const ORTHANC_OVERRIDE
+    {
+      return "CSTO";
+    }
+
   public:
     explicit DicomModalityStoreJob(ServerContext& context);
 
@@ -92,11 +97,6 @@
       target = "DicomModalityStore";
     }
 
-    virtual const char* GetLoaderPrefix() const ORTHANC_OVERRIDE
-    {
-      return "CSTO";
-    }
-
     virtual void GetPublicContent(Json::Value& value) const ORTHANC_OVERRIDE;
 
     virtual bool Serialize(Json::Value& target) const ORTHANC_OVERRIDE;
--- a/OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.cpp	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.cpp	Mon Apr 13 15:11:41 2026 +0200
@@ -61,7 +61,7 @@
       if (transcode_)
       {
         std::string dicom;
-        instancesLoader_->GetDicom(dicom, instance);
+        instancesLoader_->WaitDicomInstance(dicom, instance);
 
         std::set<DicomTransferSyntax> syntaxes;
         syntaxes.insert(transferSyntax_);
--- a/OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.h	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.h	Mon Apr 13 15:11:41 2026 +0200
@@ -49,6 +49,11 @@
     
     virtual bool HandleTrailingStep() ORTHANC_OVERRIDE;
 
+    virtual const char* GetLoaderPrefix() const ORTHANC_OVERRIDE
+    {
+      return "PSTO";
+    }
+
   public:
     explicit OrthancPeerStoreJob(ServerContext& context) :
       StoreJob(context),
@@ -96,11 +101,6 @@
       target = "OrthancPeerStore";
     }
 
-    virtual const char* GetLoaderPrefix() const ORTHANC_OVERRIDE
-    {
-      return "PSTO";
-    }
-
     virtual void GetPublicContent(Json::Value& value) const ORTHANC_OVERRIDE;
 
     virtual bool Serialize(Json::Value& target) const ORTHANC_OVERRIDE;
--- a/OrthancServer/Sources/ServerJobs/ResourceModificationJob.cpp	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/ResourceModificationJob.cpp	Mon Apr 13 15:11:41 2026 +0200
@@ -811,13 +811,13 @@
                 {
                   if (!modification_->IsReplaced(*mainPatientTag))
                   {
-                    throw OrthancException(ErrorCode_BadRequest, std::string("Trying to change patient tags in a study.  " 
+                    throw OrthancException(ErrorCode_BadRequest, std::string("Trying to change patient tags in a study.  "
                       "The Patient already exists and has other studies.  All the 'Replace' tags should match the existing patient main dicom tags "
                       "and you should specify all Patient MainDicomTags in your query.  Try using /patients/../modify instead to modify the patient. Missing tag in the 'Replace' tags: ") + mainPatientTag->Format());
                   }
                   else
                   {
-                    throw OrthancException(ErrorCode_BadRequest, std::string("Trying to change patient tags in a study.  " 
+                    throw OrthancException(ErrorCode_BadRequest, std::string("Trying to change patient tags in a study.  "
                       "The Patient already exists and has other studies.  All the 'Replace' tags should match the existing patient main dicom tags "
                       "and you should specify all Patient MainDicomTags in your query.  Try using /patients/../modify instead to modify the patient. Failing tag: ") + mainPatientTag->Format());
                   }
--- a/OrthancServer/Sources/ServerJobs/StoreJob.cpp	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/StoreJob.cpp	Mon Apr 13 15:11:41 2026 +0200
@@ -39,7 +39,8 @@
     context_(context)
   {
   }
-  
+
+
   StoreJob::StoreJob(ServerContext& context,
                      const Json::Value& serialized) :
     SetOfInstancesJob(serialized),
@@ -59,8 +60,8 @@
 
       filesInfo_.push_back(fileInfo);
     }
+  }
 
-  }
 
   void StoreJob::AddInstances(const std::vector<std::string>& instancesIds,
                               const std::vector<FileInfo>& filesInfo)
@@ -73,13 +74,14 @@
     std::copy(instancesIds.begin(), instancesIds.end(), std::back_inserter(instancesIds_));
     std::copy(filesInfo.begin(), filesInfo.end(), std::back_inserter(filesInfo_));
 
-    // create the commands (they must be in the same order as the instancesIds_ such that the instancesLoader loads them in the right consume order)
+    // create the commands (they must be in the same order as the instancesIds_ so that the instancesLoader loads them in the right consume order)
     for (size_t i = 0; i < instancesIds.size(); ++i)
     {
       AddInstance(instancesIds[i]);
     }
   }
 
+
   void StoreJob::Start()
   {
     size_t loaderThreads = 1;
@@ -87,17 +89,18 @@
       OrthancConfiguration::ReaderLock lock;
       loaderThreads = lock.GetConfiguration().GetLoaderThreads();
     }
-    
-    instancesLoader_.reset(new ThreadedInstancesLoader(context_, loaderThreads, false, DicomTransferSyntax_LittleEndianImplicit /* dummy value not used*/, 0, GetLoaderPrefix()));    
-  
+
+    instancesLoader_.reset(new ThreadedInstancesLoader(context_, loaderThreads, false, DicomTransferSyntax_LittleEndianImplicit /* dummy value not used*/, 0, GetLoaderPrefix()));
+
     for (size_t i = 0; i < instancesIds_.size(); ++i)
     {
-      instancesLoader_->PrepareDicom(instancesIds_[i], filesInfo_[i]);
+      instancesLoader_->PreloadDicomInstance(instancesIds_[i], filesInfo_[i]);
     }
 
     SetOfInstancesJob::Start();
   }
 
+
   void StoreJob::Stop(JobStopReason reason)   // For pausing jobs
   {
     if (reason == JobStopReason_Canceled ||
@@ -112,7 +115,4 @@
       }
     }
   }
-  
-
-
 }
--- a/OrthancServer/Sources/ServerJobs/StoreJob.h	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/StoreJob.h	Mon Apr 13 15:11:41 2026 +0200
@@ -33,7 +33,7 @@
 namespace Orthanc
 {
   class ServerContext;
-  
+
   class ORTHANC_PUBLIC StoreJob : public SetOfInstancesJob
   {
   protected:
@@ -42,6 +42,8 @@
     std::vector<std::string>                   instancesIds_;
     std::vector<FileInfo>                      filesInfo_;
 
+    virtual const char* GetLoaderPrefix() const = 0;
+
   public:
     explicit StoreJob(ServerContext& context);
 
@@ -54,7 +56,5 @@
 
     void AddInstances(const std::vector<std::string>& instancesIds,
                       const std::vector<FileInfo>& filesInfo);
-
-    virtual const char* GetLoaderPrefix() const = 0;
   };
 }
--- a/OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp	Mon Apr 13 15:11:41 2026 +0200
@@ -40,31 +40,39 @@
     FileInfo    fileInfo_;
 
   public:
-    explicit InstanceToPreload(const std::string& id, const FileInfo& fileInfo) : 
+    InstanceToPreload(const std::string& id,
+                      const FileInfo& fileInfo) :
       id_(id),
       fileInfo_(fileInfo)
     {
     }
 
-    virtual ~InstanceToPreload() ORTHANC_OVERRIDE
+    const std::string& GetId() const
     {
+      return id_;
     }
 
-    const std::string& GetId() const {return id_;};
-    const FileInfo& GetFileInfo() const {return fileInfo_;};
+    const FileInfo& GetFileInfo() const
+    {
+      return fileInfo_;
+    }
   };
 
 
-  ThreadedInstancesLoader::ThreadedInstancesLoader(ServerContext& context, size_t threadCount, bool transcode, DicomTransferSyntax transferSyntax, unsigned int lossyQuality, const std::string& nameForLogs4Char)
-  : availableInstancesSemaphore_(3*threadCount),
-    instancesToPreload_(0), // no limit on the message queue, the flow control is performed by the availableInstancesSemaphore_
-    loadersShouldStop_(false),
-    nameForLogs4Char_(nameForLogs4Char),
+  ThreadedInstancesLoader::ThreadedInstancesLoader(ServerContext& context,
+                                                   size_t threadCount,
+                                                   bool transcode,
+                                                   DicomTransferSyntax transferSyntax,
+                                                   unsigned int lossyQuality,
+                                                   const std::string& nameForLogs4Char) :
     context_(context),
     transcode_(transcode),
     transferSyntax_(transferSyntax),
-    lossyQuality_(lossyQuality)
-
+    lossyQuality_(lossyQuality),
+    nameForLogs4Char_(nameForLogs4Char),
+    availableInstancesSemaphore_(3 * threadCount),
+    instancesToPreload_(0), // no limit on the message queue, the flow control is performed by the availableInstancesSemaphore_
+    loadersShouldStop_(false)
   {
     assert(nameForLogs4Char_.size() <= 4);
 
@@ -79,16 +87,26 @@
     }
   }
 
+
   ThreadedInstancesLoader::~ThreadedInstancesLoader()
   {
-    ThreadedInstancesLoader::Clear(false);
+    try
+    {
+      ThreadedInstancesLoader::Clear(false);
+    }
+    catch (const OrthancException& e)
+    {
+      // Don't throw exceptions in destructors
+      LOG(ERROR) << "Exception: " << e.What();
+    }
   }
 
+
   void ThreadedInstancesLoader::Clear(bool isAbort)
   {
     if (threads_.size() > 0)
     {
-      loadersShouldStop_ = true; // not need to protect this by a mutex.  This is the only "writer" and all loaders are "readers"
+      loadersShouldStop_ = true; // no need to protect this by a mutex. This is the only "writer" and all loaders are "readers"
 
       if (isAbort)
       {
@@ -125,11 +143,13 @@
     }
   }
 
+
   void ThreadedInstancesLoader::PreloaderWorkerThread(ThreadedInstancesLoader* that)
   {
     {
       boost::mutex::scoped_lock lock(loaderThreadsCounterMutex);
-      Logging::SetCurrentThreadName(that->nameForLogs4Char_ + std::string("-LOAD-") + boost::lexical_cast<std::string>(loaderThreadsCounter++));
+      Logging::SetCurrentThreadName(that->nameForLogs4Char_ + std::string("-LOAD-") +
+                                    boost::lexical_cast<std::string>(loaderThreadsCounter++));
       loaderThreadsCounter %= 1000000;
     }
 
@@ -147,7 +167,7 @@
       }
 
       const std::string& instanceId = instanceToPreload->GetId();
-      
+
       try
       {
         boost::shared_ptr<std::string> dicomContent(new std::string());
@@ -173,7 +193,7 @@
         LOG(ERROR) << "Failed to load instance " << instanceId << " error: " << e.GetDetails();
         boost::mutex::scoped_lock lock(that->availableInstancesMutex_);
         // store a NULL result to notify that we could not read the instance
-        that->availableInstances_[instanceId] = boost::shared_ptr<std::string>(); 
+        that->availableInstances_[instanceId] = boost::shared_ptr<std::string>();
         that->condInstanceAvailable_.notify_one();
       }
       catch (...)
@@ -181,47 +201,50 @@
         LOG(ERROR) << "Failed to load instance " << instanceId << " unknown error";
         boost::mutex::scoped_lock lock(that->availableInstancesMutex_);
         // store a NULL result to notify that we could not read the instance
-        that->availableInstances_[instanceId] = boost::shared_ptr<std::string>(); 
+        that->availableInstances_[instanceId] = boost::shared_ptr<std::string>();
         that->condInstanceAvailable_.notify_one();
       }
     }
   }
 
-  void ThreadedInstancesLoader::PrepareDicom(const std::string& instanceId, const FileInfo& fileInfo)
+
+  void ThreadedInstancesLoader::PreloadDicomInstance(const std::string& instanceId,
+                                                     const FileInfo& fileInfo)
   {
     instancesToPreload_.Enqueue(new InstanceToPreload(instanceId, fileInfo));
   }
 
-  void ThreadedInstancesLoader::GetDicom(std::string& dicom, const std::string& instanceId)
+
+  void ThreadedInstancesLoader::WaitDicomInstance(std::string& dicom,
+                                                  const std::string& instanceId)
   {
     boost::mutex::scoped_lock lock(availableInstancesMutex_);
 
-    while (true)
+    // wait for this instance to be available but this might not be the one we are waiting for !
+    while (availableInstances_.find(instanceId) == availableInstances_.end())
     {
-      // wait for this instance to be available but this might not be the one we are waiting for !
-      while (availableInstances_.find(instanceId) == availableInstances_.end())
-      {
-        condInstanceAvailable_.wait(lock);
-      }
+      condInstanceAvailable_.wait(lock);
+    }
+
+    boost::shared_ptr<std::string> dicomContent;
 
-      boost::shared_ptr<std::string> dicomContent;
+    // this is the instance we were waiting for
+    dicomContent = availableInstances_[instanceId];
+    availableInstances_.erase(instanceId);
+    availableInstancesSemaphore_.Release(1);
 
-      // this is the instance we were waiting for
-      dicomContent = availableInstances_[instanceId];
-      availableInstances_.erase(instanceId);
-      availableInstancesSemaphore_.Release(1);
+    if (dicomContent.get() == NULL)  // there has been an error while reading the file
+    {
+      throw OrthancException(ErrorCode_InexistentItem);
+    }
 
-      if (dicomContent.get() == NULL)  // there has been an error while reading the file
-      {
-        throw OrthancException(ErrorCode_InexistentItem);
-      }
-      dicom.swap(*dicomContent);
+    dicom.swap(*dicomContent);
+  }
+
 
-      return;
-    }
-  };
-
-  bool ThreadedInstancesLoader::TranscodeDicom(std::string& transcodedBuffer, const std::string& sourceBuffer, const std::string& instanceId)
+  bool ThreadedInstancesLoader::TranscodeDicom(std::string& transcodedBuffer,
+                                               const std::string& sourceBuffer,
+                                               const std::string& instanceId)
   {
     if (transcode_)
     {
@@ -245,5 +268,4 @@
 
     return false;
   }
-
-}
\ No newline at end of file
+}
--- a/OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.h	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.h	Mon Apr 13 15:11:41 2026 +0200
@@ -22,21 +22,31 @@
 
 #pragma once
 
+#include "../../../OrthancFramework/Sources/Compatibility.h"
+#include "../../../OrthancFramework/Sources/Enumerations.h"
+#include "../../../OrthancFramework/Sources/FileStorage/FileInfo.h"
+#include "../../../OrthancFramework/Sources/MultiThreading/BlockingSharedMessageQueue.h"
+#include "../../../OrthancFramework/Sources/MultiThreading/Semaphore.h"
+
 #include <string>
 #include <boost/noncopyable.hpp>
-#include "../../../OrthancFramework/Sources/Compatibility.h"
-#include "../../../OrthancFramework/Sources/MultiThreading/BlockingSharedMessageQueue.h"
-#include "../../../OrthancFramework/Sources/Enumerations.h"
-#include "../../../OrthancFramework/Sources/MultiThreading/Semaphore.h"
-#include "../../../OrthancFramework/Sources/FileStorage/FileInfo.h"
 
 
 namespace Orthanc
 {
   class ServerContext;
 
-  class ORTHANC_PUBLIC ThreadedInstancesLoader : public boost::noncopyable
+  class ORTHANC_PUBLIC ThreadedInstancesLoader ORTHANC_FINAL : public boost::noncopyable
   {
+  private:
+    // Parameters from the constructor
+    ServerContext&                      context_;
+    bool                                transcode_;
+    DicomTransferSyntax                 transferSyntax_;
+    unsigned int                        lossyQuality_;
+    std::string                         nameForLogs4Char_;
+
+    // Internal variables
     boost::condition_variable           condInstanceAvailable_;
     std::map<std::string, boost::shared_ptr<std::string> >  availableInstances_;
     boost::mutex                        availableInstancesMutex_;
@@ -44,29 +54,29 @@
     BlockingSharedMessageQueue          instancesToPreload_;
     std::vector<boost::thread*>         threads_;
     bool                                loadersShouldStop_;
-    std::string                         nameForLogs4Char_;
+
+    static void PreloaderWorkerThread(ThreadedInstancesLoader* that);
 
-  protected:
-    ServerContext&                        context_;
-    bool                                  transcode_;
-    DicomTransferSyntax                   transferSyntax_;
-    unsigned int                          lossyQuality_;
+    bool TranscodeDicom(std::string& transcodedBuffer,
+                        const std::string& sourceBuffer,
+                        const std::string& instanceId);
 
   public:
-    ThreadedInstancesLoader(ServerContext& context, size_t threadCount, bool transcode, DicomTransferSyntax transferSyntax, unsigned int lossyQuality, const std::string& nameForLogs4Char);
+    ThreadedInstancesLoader(ServerContext& context,
+                            size_t threadCount,
+                            bool transcode,
+                            DicomTransferSyntax transferSyntax,
+                            unsigned int lossyQuality,
+                            const std::string& nameForLogs4Char);
 
     ~ThreadedInstancesLoader();
 
     void Clear(bool isAbort);
 
-    static void PreloaderWorkerThread(ThreadedInstancesLoader* that);
-
-    void PrepareDicom(const std::string& instanceId, const FileInfo& fileInfo);
+    void PreloadDicomInstance(const std::string& instanceId,
+                              const FileInfo& fileInfo);
 
-    void GetDicom(std::string& dicom, const std::string& instanceId);
-
-  protected:
-    bool TranscodeDicom(std::string& transcodedBuffer, const std::string& sourceBuffer, const std::string& instanceId);
+    void WaitDicomInstance(std::string& dicom,
+                           const std::string& instanceId);
   };
 }
- 
\ No newline at end of file
--- a/OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp	Mon Apr 13 14:05:54 2026 +0200
+++ b/OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp	Mon Apr 13 15:11:41 2026 +0200
@@ -393,7 +393,7 @@
       if (includeParentResourcesField)
       {
         std::set<std::string> keys;
-        for (std::map<std::string, ResourceType>::const_iterator it = parentResources.begin(); it != parentResources.end(); ++it) 
+        for (std::map<std::string, ResourceType>::const_iterator it = parentResources.begin(); it != parentResources.end(); ++it)
         {
           keys.insert(it->first);
         }
@@ -473,7 +473,7 @@
         }
         catch(...)
         {
-          // ignore errors, the resource might have disappear 
+          // ignore errors, the resource might have disappear
         }
       }
     }
--- a/TODO	Mon Apr 13 14:05:54 2026 +0200
+++ b/TODO	Mon Apr 13 15:11:41 2026 +0200
@@ -71,7 +71,9 @@
     "admin": {
       "Password": "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8", 
       "Hashing": "sha1"} }
-  
+* Have a single instance of "ThreadedInstancesLoader" shared by all the jobs
+  in the Orthanc server to prevent resource exhaustion.
+
 
 ============================
 Documentation (Orthanc Book)