changeset 6775:55669d3f63bc

created class IRunnable
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 12 May 2026 18:10:45 +0200
parents 7cb0789238ee
children a5d1aa442f75
files OrthancFramework/Sources/Cache/SharedObjectCache.cpp OrthancFramework/Sources/Cache/SharedObjectCache.h OrthancFramework/Sources/MultiThreading/IExecutorService.h OrthancFramework/Sources/MultiThreading/ThreadPool.cpp OrthancFramework/Sources/MultiThreading/ThreadPool.h
diffstat 5 files changed, 126 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/Cache/SharedObjectCache.cpp	Tue May 12 07:39:56 2026 +0200
+++ b/OrthancFramework/Sources/Cache/SharedObjectCache.cpp	Tue May 12 18:10:45 2026 +0200
@@ -59,16 +59,17 @@
   {
     assert(newObjectSize != 0);
 
-    while (currentSize_ + newObjectSize > capacity_)
+    while (!content_.empty() &&
+           currentSize_ + newObjectSize > capacity_)
     {
       std::string oldest = lru_.RemoveOldest();
       assert(!lru_.Contains(oldest));
 
       Content::iterator found = content_.find(oldest);
       assert(found != content_.end());
-      assert(found->second == NULL);
+      assert(found->second != NULL);
 
-      assert(currentSize_ < found->second->GetSize());
+      assert(currentSize_ >= found->second->GetSize());
       currentSize_ -= found->second->GetSize();
 
       delete found->second;
@@ -133,11 +134,17 @@
       throw OrthancException(ErrorCode_NullPointer);
     }
 
-    assert(provider_.get() == NULL);
+    assert(provider_.get() != NULL);
     size_t size = provider_->GetSize(*value) + sizeof(Item);
 
+    // Caching items with zero size would break the logic of "MakeRoom()", hence "sizeof(Item)"
     assert(size != 0);
 
+    if (size > capacity_)
+    {
+      return;  // This value is too large to be cached
+    }
+    else
     {
       Mutex::ScopedLock lock(mutex_);
 
@@ -148,8 +155,8 @@
       if (found == content_.end())
       {
         assert(!lru_.Contains(id));
+        lru_.Add(id);
 
-        lru_.Add(id);
         content_[id] = new Item(value, size);
       }
       else
@@ -164,6 +171,8 @@
       }
 
       assert(lru_.Contains(id));
+
+      currentSize_ += size;
     }
   }
 
--- a/OrthancFramework/Sources/Cache/SharedObjectCache.h	Tue May 12 07:39:56 2026 +0200
+++ b/OrthancFramework/Sources/Cache/SharedObjectCache.h	Tue May 12 18:10:45 2026 +0200
@@ -44,13 +44,13 @@
       {
       }
 
-      virtual size_t GetSize(const IDynamicObject& object) = 0;
+      virtual size_t GetSize(const IDynamicObject& object) const = 0;
     };
 
     class StringSizeProvider : public ISizeProvider
     {
     public:
-      virtual size_t GetSize(const IDynamicObject& object) ORTHANC_OVERRIDE
+      virtual size_t GetSize(const IDynamicObject& object) const ORTHANC_OVERRIDE
       {
         return dynamic_cast<const SingleValueObject<std::string>&>(object).GetValue().size();
       }
--- a/OrthancFramework/Sources/MultiThreading/IExecutorService.h	Tue May 12 07:39:56 2026 +0200
+++ b/OrthancFramework/Sources/MultiThreading/IExecutorService.h	Tue May 12 18:10:45 2026 +0200
@@ -26,6 +26,8 @@
 
 #include "Future.h"
 #include "ICallable.h"
+#include "IRunnable.h"
+
 
 namespace Orthanc
 {
@@ -37,5 +39,9 @@
     }
 
     virtual Future* Submit(ICallable* callable /* takes ownership */) = 0;
+
+    virtual void Submit(IRunnable* runnable /* takes ownership */) = 0;
+
+    virtual void Stop() = 0;
   };
 }
--- a/OrthancFramework/Sources/MultiThreading/ThreadPool.cpp	Tue May 12 07:39:56 2026 +0200
+++ b/OrthancFramework/Sources/MultiThreading/ThreadPool.cpp	Tue May 12 18:10:45 2026 +0200
@@ -29,20 +29,36 @@
 #include "../OrthancException.h"
 #include "FutureState.h"
 
+#include <boost/lexical_cast.hpp>
+
+
 static const unsigned int DEFAULT_DEQUEUE_TIMEOUT_MS = 100;
 
 
 namespace Orthanc
 {
-  class ThreadPool::Task : public IDynamicObject
+  class ThreadPool::ITask : public IDynamicObject
+  {
+  public:
+    virtual ~ITask()
+    {
+    }
+
+    virtual void Execute() = 0;
+
+    virtual void Cancel() = 0;
+  };
+
+
+  class ThreadPool::CallableTask : public ITask
   {
   private:
     std::unique_ptr<ICallable>               callable_;
     boost::weak_ptr<Internals::FutureState>  state_;
 
   public:
-    Task(ICallable* callable,
-         boost::shared_ptr<Internals::FutureState>& state) :
+    CallableTask(ICallable* callable,
+                 boost::shared_ptr<Internals::FutureState>& state) :
       callable_(callable),
       state_(state)
     {
@@ -90,6 +106,29 @@
   };
 
 
+  class ThreadPool::RunnableTask : public ITask
+  {
+  private:
+    std::unique_ptr<IRunnable>  runnable_;
+
+  public:
+    RunnableTask(IRunnable* runnable) :
+      runnable_(runnable)
+    {
+      assert(runnable != NULL);
+    }
+
+    void Execute()
+    {
+      runnable_->Run();
+    }
+
+    void Cancel()
+    {
+    }
+  };
+
+
   void ThreadPool::StopInternal(bool throws)
   {
     {
@@ -150,7 +189,7 @@
       {
         try
         {
-          dynamic_cast<Task&>(*task).Cancel();
+          dynamic_cast<ITask&>(*task).Cancel();
         }
         catch (OrthancException& e)
         {
@@ -166,8 +205,10 @@
   }
 
 
-  void ThreadPool::WorkerLoop()
+  void ThreadPool::WorkerLoop(std::string threadName)
   {
+    Logging::SetCurrentThreadName(threadName);
+
     while (true)
     {
       unsigned int timeout;
@@ -190,13 +231,14 @@
 
       if (task.get() != NULL)
       {
-        dynamic_cast<Task&>(*task).Execute();
+        dynamic_cast<ITask&>(*task).Execute();
       }
     }
   }
 
 
   ThreadPool::ThreadPool() :
+    loggingThreadName_("POOL"),
     countThreads_(1),
     state_(State_Initialization),
     dequeueTimeoutMilliseconds_(DEFAULT_DEQUEUE_TIMEOUT_MS)
@@ -204,6 +246,21 @@
   }
 
 
+  void ThreadPool::SetLoggingThreadName(const std::string& name)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+
+    if (state_ == State_Initialization)
+    {
+      loggingThreadName_ = name;
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls, "Start() has already been called");
+    }
+  }
+
+
   void ThreadPool::SetCountThreads(unsigned int count)
   {
     if (count < 1)
@@ -277,7 +334,8 @@
 
       for (unsigned int i = 0; i < countThreads_; i++)
       {
-        workers_->create_thread(boost::bind(&ThreadPool::WorkerLoop, this));
+        const std::string threadName = loggingThreadName_ + "-" + boost::lexical_cast<std::string>(i);
+        workers_->create_thread(boost::bind(&ThreadPool::WorkerLoop, this, threadName));
       }
     }
     else
@@ -289,6 +347,13 @@
 
   Future* ThreadPool::Submit(ICallable* callable)
   {
+    std::unique_ptr<ICallable> protection(callable);
+
+    if (callable == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+
     {
       boost::mutex::scoped_lock lock(mutex_);
 
@@ -298,17 +363,32 @@
       }
     }
 
-    std::unique_ptr<ICallable> protection(callable);
+    boost::shared_ptr<Internals::FutureState> state(boost::make_shared<Internals::FutureState>());
+
+    queue_.Enqueue(new CallableTask(protection.release(), state));
+
+    return new Future(state);
+  }
 
-    if (callable == NULL)
+
+  void ThreadPool::Submit(IRunnable* runnable)
+  {
+    std::unique_ptr<IRunnable> protection(runnable);
+
+    if (runnable == NULL)
     {
       throw OrthancException(ErrorCode_NullPointer);
     }
 
-    boost::shared_ptr<Internals::FutureState> state(boost::make_shared<Internals::FutureState>());
+    {
+      boost::mutex::scoped_lock lock(mutex_);
 
-    queue_.Enqueue(new Task(protection.release(), state));
+      if (state_ != State_Running)
+      {
+        throw OrthancException(ErrorCode_BadSequenceOfCalls, "The thread pool is not running");
+      }
+    }
 
-    return new Future(state);
+    queue_.Enqueue(new RunnableTask(protection.release()));
   }
 }
--- a/OrthancFramework/Sources/MultiThreading/ThreadPool.h	Tue May 12 07:39:56 2026 +0200
+++ b/OrthancFramework/Sources/MultiThreading/ThreadPool.h	Tue May 12 18:10:45 2026 +0200
@@ -42,18 +42,21 @@
       State_Finalization
     };
 
-    class Task;
+    class ITask;
+    class CallableTask;
+    class RunnableTask;
 
     SharedMessageQueue                    queue_;
     std::unique_ptr<boost::thread_group>  workers_;
     boost::mutex                          mutex_;
+    std::string                           loggingThreadName_;
     unsigned int                          countThreads_;
     State                                 state_;
     unsigned int                          dequeueTimeoutMilliseconds_;
 
     void StopInternal(bool throws);
 
-    void WorkerLoop();
+    void WorkerLoop(std::string threadName);
 
   public:
     ThreadPool();
@@ -63,6 +66,8 @@
       StopInternal(false /* don't throw in destructor */);
     }
 
+    void SetLoggingThreadName(const std::string& name);
+
     void SetCountThreads(unsigned int count);
 
     unsigned int GetCountThreads();
@@ -73,11 +78,13 @@
 
     void Start();
 
-    void Stop()
+    virtual Future* Submit(ICallable* callable /* takes ownership */) ORTHANC_OVERRIDE;
+
+    virtual void Submit(IRunnable* runnable /* takes ownership */) ORTHANC_OVERRIDE;
+
+    virtual void Stop() ORTHANC_OVERRIDE
     {
       StopInternal(true);
     }
-
-    virtual Future* Submit(ICallable* callable /* takes ownership */) ORTHANC_OVERRIDE;
   };
 }