# HG changeset patch # User Sebastien Jodogne # Date 1778424590 -7200 # Node ID 2998db9f7a3afb561a87841e070f00e4ec7f63b5 # Parent 3e56913120ab09709c3464c95e07ad879e06a243 created a thread pool primitive with callables and futures diff -r 3e56913120ab -r 2998db9f7a3a OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake --- a/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Sun May 10 12:03:27 2026 +0200 +++ b/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Sun May 10 16:49:50 2026 +0200 @@ -659,9 +659,12 @@ ${CMAKE_CURRENT_LIST_DIR}/../../Sources/FileStorage/FilesystemStorage.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/MetricsRegistry.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/MultiThreading/BlockingSharedMessageQueue.cpp + ${CMAKE_CURRENT_LIST_DIR}/../../Sources/MultiThreading/Future.cpp + ${CMAKE_CURRENT_LIST_DIR}/../../Sources/MultiThreading/FutureState.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/MultiThreading/RunnableWorkersPool.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/MultiThreading/Semaphore.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/MultiThreading/SharedMessageQueue.cpp + ${CMAKE_CURRENT_LIST_DIR}/../../Sources/MultiThreading/ThreadPool.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/SharedLibrary.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/SystemToolbox.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/TemporaryFile.cpp diff -r 3e56913120ab -r 2998db9f7a3a OrthancFramework/Sources/MultiThreading/Future.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/MultiThreading/Future.cpp Sun May 10 16:49:50 2026 +0200 @@ -0,0 +1,51 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + **/ + + +#include "../PrecompiledHeaders.h" +#include "Future.h" + +#include "FutureState.h" +#include "../OrthancException.h" + +namespace Orthanc +{ + Future::Future(boost::shared_ptr& state) : + state_(state) + { + if (state.get() == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + } + + Future::~Future() + { + state_->Cancel(); + } + + IDynamicObject* Future::ReleaseResult() + { + return state_->ReleaseResult(); + } +} diff -r 3e56913120ab -r 2998db9f7a3a OrthancFramework/Sources/MultiThreading/Future.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/MultiThreading/Future.h Sun May 10 16:49:50 2026 +0200 @@ -0,0 +1,52 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + **/ + + +#pragma once + +#include "../IDynamicObject.h" + +#include + +namespace Orthanc +{ + namespace Internals + { + class FutureState; + } + + class ORTHANC_PUBLIC Future : public boost::noncopyable + { + friend class ThreadPool; + + private: + boost::shared_ptr state_; + + explicit Future(boost::shared_ptr& state); + + public: + ~Future(); + + IDynamicObject* ReleaseResult(); + }; +} diff -r 3e56913120ab -r 2998db9f7a3a OrthancFramework/Sources/MultiThreading/FutureState.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/MultiThreading/FutureState.cpp Sun May 10 16:49:50 2026 +0200 @@ -0,0 +1,162 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + **/ + + +#include "../PrecompiledHeaders.h" +#include "FutureState.h" + +#include + + +namespace Orthanc +{ + namespace Internals + { + IDynamicObject* FutureState::ReleaseInternal(bool hasTimeout, + unsigned int millisecondsTimeout) + { + boost::mutex::scoped_lock lock(mutex_); + + for (;;) + { + switch (state_) + { + case State_Pending: + if (hasTimeout) + { + bool success = completed_.timed_wait(lock, boost::posix_time::milliseconds(millisecondsTimeout)); + if (success) + { + if (state_ != State_Success && + state_ != State_Failed) + { + throw OrthancException(ErrorCode_InternalError); // Should never happen + } + } + } + else + { + completed_.wait(lock); + } + break; + + case State_Canceled: + throw OrthancException(ErrorCode_CanceledJob); + + case State_Failed: + if (error_.get() == NULL) + { + throw OrthancException(ErrorCode_InternalError); // Should never happen + } + else + { + throw OrthancException(*error_); + } + + case State_Success: + return result_.release(); + + default: + throw OrthancException(ErrorCode_InternalError); // Should never happen + } + } + } + + + void FutureState::Cancel() ORTHANC_NOEXCEPT + { + boost::mutex::scoped_lock lock(mutex_); + + if (state_ == State_Pending) + { + state_ = State_Canceled; + completed_.notify_all(); + } + } + + + void FutureState::AcquireResult(IDynamicObject* result) + { + std::unique_ptr protection(result); + + if (result == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + else + { + boost::mutex::scoped_lock lock(mutex_); + + switch (state_) + { + case State_Pending: + assert(result_.get() == NULL); + assert(error_.get() == NULL); + + result_.reset(protection.release()); + state_ = State_Success; + completed_.notify_all(); + break; + + case State_Canceled: + break; // Ignore + + case State_Failed: + case State_Success: + throw OrthancException(ErrorCode_BadSequenceOfCalls); + + default: + throw OrthancException(ErrorCode_InternalError); // Should never happen + } + } + } + + + void FutureState::SetError(const OrthancException& error) + { + boost::mutex::scoped_lock lock(mutex_); + + switch (state_) + { + case State_Pending: + assert(result_.get() == NULL); + assert(error_.get() == NULL); + + error_.reset(new OrthancException(error)); + state_ = State_Success; + completed_.notify_all(); + break; + + case State_Canceled: + break; // Ignore + + case State_Failed: + case State_Success: + throw OrthancException(ErrorCode_BadSequenceOfCalls); + + default: + throw OrthancException(ErrorCode_InternalError); // Should never happen + } + } + } +} diff -r 3e56913120ab -r 2998db9f7a3a OrthancFramework/Sources/MultiThreading/FutureState.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/MultiThreading/FutureState.h Sun May 10 16:49:50 2026 +0200 @@ -0,0 +1,82 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + **/ + + +#pragma once + +#include "../Compatibility.h" +#include "../IDynamicObject.h" +#include "../OrthancException.h" + +#include +#include + +namespace Orthanc +{ + namespace Internals + { + class FutureState : public boost::noncopyable + { + public: + enum State + { + State_Pending, + State_Success, + State_Failed, + State_Canceled + }; + + private: + boost::mutex mutex_; + boost::condition_variable completed_; + State state_; + std::unique_ptr result_; + std::unique_ptr error_; + + IDynamicObject* ReleaseInternal(bool hasTimeout, + unsigned int millisecondsTimeout); + + public: + FutureState() : + state_(State_Pending) + { + } + + IDynamicObject* ReleaseResult() + { + return ReleaseInternal(false, 0); + } + + IDynamicObject* ReleaseResult(unsigned int millisecondsTimeout) + { + return ReleaseInternal(true, millisecondsTimeout); + } + + void Cancel() ORTHANC_NOEXCEPT; + + void AcquireResult(IDynamicObject* result /* takes ownership */); + + void SetError(const OrthancException& error); + }; + } +} diff -r 3e56913120ab -r 2998db9f7a3a OrthancFramework/Sources/MultiThreading/ICallable.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/MultiThreading/ICallable.h Sun May 10 16:49:50 2026 +0200 @@ -0,0 +1,40 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + **/ + + +#pragma once + +#include "../IDynamicObject.h" + +namespace Orthanc +{ + class ORTHANC_PUBLIC ICallable : public boost::noncopyable + { + public: + virtual ~ICallable() + { + } + + virtual IDynamicObject* Call() = 0; + }; +} diff -r 3e56913120ab -r 2998db9f7a3a OrthancFramework/Sources/MultiThreading/IExecutorService.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/MultiThreading/IExecutorService.h Sun May 10 16:49:50 2026 +0200 @@ -0,0 +1,41 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + **/ + + +#pragma once + +#include "Future.h" +#include "ICallable.h" + +namespace Orthanc +{ + class ORTHANC_PUBLIC IExecutorService : public boost::noncopyable + { + public: + virtual ~IExecutorService() + { + } + + virtual Future* Submit(ICallable* callable /* takes ownership */) = 0; + }; +} diff -r 3e56913120ab -r 2998db9f7a3a OrthancFramework/Sources/MultiThreading/ThreadPool.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/MultiThreading/ThreadPool.cpp Sun May 10 16:49:50 2026 +0200 @@ -0,0 +1,141 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + **/ + + +#include "../PrecompiledHeaders.h" +#include "ThreadPool.h" + +#include "../OrthancException.h" +#include "FutureState.h" + +static const unsigned int DEQUEUE_TIMEOUT_MS = 100; + + +namespace Orthanc +{ + class ThreadPool::Task : public IDynamicObject + { + private: + std::unique_ptr callable_; + boost::weak_ptr state_; + + public: + Task(ICallable* callable, + boost::shared_ptr& state) : + callable_(callable), + state_(state) + { + assert(callable != NULL); + } + + void Execute() + { + boost::shared_ptr locked = state_.lock(); + + if (locked) + { + try + { + locked->AcquireResult(callable_->Call()); + } + catch (const OrthancException& e) + { + locked->SetError(e); + } + catch (...) + { + locked->SetError(OrthancException(ErrorCode_InternalError, "Unknown exception in ICallable::Call()")); + } + } + else + { + // Nothing to do: The future was canceled before we even started + } + } + }; + + + void ThreadPool::WorkerLoop() + { + while (true) + { + std::unique_ptr task(queue_.Dequeue(DEQUEUE_TIMEOUT_MS)); + + if (task.get() == NULL) + { + boost::mutex::scoped_lock lock(shutdownMutex_); + if (shutdown_) + { + return; + } + } + else + { + dynamic_cast(*task).Execute(); + } + } + } + + + ThreadPool::ThreadPool(unsigned int countThreads) : + shutdown_(false) + { + if (countThreads < 1) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + + for (unsigned int i = 0; i < countThreads; i++) + { + workers_.create_thread(boost::bind(&ThreadPool::WorkerLoop, this)); + } + } + + + ThreadPool::~ThreadPool() + { + { + boost::mutex::scoped_lock lock(shutdownMutex_); + shutdown_ = true; + } + + workers_.join_all(); + } + + + Future* ThreadPool::Submit(ICallable* callable) + { + std::unique_ptr protection(callable); + + if (callable == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + + boost::shared_ptr state(boost::make_shared()); + + queue_.Enqueue(new Task(protection.release(), state)); + + return new Future(state); + } +} diff -r 3e56913120ab -r 2998db9f7a3a OrthancFramework/Sources/MultiThreading/ThreadPool.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/MultiThreading/ThreadPool.h Sun May 10 16:49:50 2026 +0200 @@ -0,0 +1,54 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + **/ + + +#pragma once + +#include "../Compatibility.h" +#include "IExecutorService.h" +#include "SharedMessageQueue.h" + +#include + +namespace Orthanc +{ + class ORTHANC_PUBLIC ThreadPool : public IExecutorService + { + private: + class Task; + + SharedMessageQueue queue_; + boost::thread_group workers_; + boost::mutex shutdownMutex_; + bool shutdown_; + + void WorkerLoop(); + + public: + ThreadPool(unsigned int countThreads); + + ~ThreadPool(); + + virtual Future* Submit(ICallable* callable /* takes ownership */) ORTHANC_OVERRIDE; + }; +}