# HG changeset patch # User Sebastien Jodogne # Date 1778602245 -7200 # Node ID 55669d3f63bcb8226aef5b19ab8c4075f9fe83cd # Parent 7cb0789238eec0d0ca0da7b261c250f2643636ed created class IRunnable diff -r 7cb0789238ee -r 55669d3f63bc OrthancFramework/Sources/Cache/SharedObjectCache.cpp --- 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; } } diff -r 7cb0789238ee -r 55669d3f63bc OrthancFramework/Sources/Cache/SharedObjectCache.h --- 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&>(object).GetValue().size(); } diff -r 7cb0789238ee -r 55669d3f63bc OrthancFramework/Sources/MultiThreading/IExecutorService.h --- 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; }; } diff -r 7cb0789238ee -r 55669d3f63bc OrthancFramework/Sources/MultiThreading/ThreadPool.cpp --- 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 + + 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 callable_; boost::weak_ptr state_; public: - Task(ICallable* callable, - boost::shared_ptr& state) : + CallableTask(ICallable* callable, + boost::shared_ptr& state) : callable_(callable), state_(state) { @@ -90,6 +106,29 @@ }; + class ThreadPool::RunnableTask : public ITask + { + private: + std::unique_ptr 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).Cancel(); + dynamic_cast(*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).Execute(); + dynamic_cast(*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(i); + workers_->create_thread(boost::bind(&ThreadPool::WorkerLoop, this, threadName)); } } else @@ -289,6 +347,13 @@ Future* ThreadPool::Submit(ICallable* callable) { + std::unique_ptr protection(callable); + + if (callable == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + { boost::mutex::scoped_lock lock(mutex_); @@ -298,17 +363,32 @@ } } - std::unique_ptr protection(callable); + boost::shared_ptr state(boost::make_shared()); + + queue_.Enqueue(new CallableTask(protection.release(), state)); + + return new Future(state); + } - if (callable == NULL) + + void ThreadPool::Submit(IRunnable* runnable) + { + std::unique_ptr protection(runnable); + + if (runnable == NULL) { throw OrthancException(ErrorCode_NullPointer); } - boost::shared_ptr state(boost::make_shared()); + { + 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())); } } diff -r 7cb0789238ee -r 55669d3f63bc OrthancFramework/Sources/MultiThreading/ThreadPool.h --- 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 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; }; }