Mercurial > hg > orthanc
changeset 6750:2998db9f7a3a
created a thread pool primitive with callables and futures
| author | Sebastien Jodogne <s.jodogne@gmail.com> |
|---|---|
| date | Sun, 10 May 2026 16:49:50 +0200 |
| parents | 3e56913120ab |
| children | 3119f9d18393 a53e19a4ec84 |
| files | OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake OrthancFramework/Sources/MultiThreading/Future.cpp OrthancFramework/Sources/MultiThreading/Future.h OrthancFramework/Sources/MultiThreading/FutureState.cpp OrthancFramework/Sources/MultiThreading/FutureState.h OrthancFramework/Sources/MultiThreading/ICallable.h OrthancFramework/Sources/MultiThreading/IExecutorService.h OrthancFramework/Sources/MultiThreading/ThreadPool.cpp OrthancFramework/Sources/MultiThreading/ThreadPool.h |
| diffstat | 9 files changed, 626 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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
--- /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 + * <http://www.gnu.org/licenses/>. + **/ + + +#include "../PrecompiledHeaders.h" +#include "Future.h" + +#include "FutureState.h" +#include "../OrthancException.h" + +namespace Orthanc +{ + Future::Future(boost::shared_ptr<Internals::FutureState>& state) : + state_(state) + { + if (state.get() == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + } + + Future::~Future() + { + state_->Cancel(); + } + + IDynamicObject* Future::ReleaseResult() + { + return state_->ReleaseResult(); + } +}
--- /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 + * <http://www.gnu.org/licenses/>. + **/ + + +#pragma once + +#include "../IDynamicObject.h" + +#include <boost/shared_ptr.hpp> + +namespace Orthanc +{ + namespace Internals + { + class FutureState; + } + + class ORTHANC_PUBLIC Future : public boost::noncopyable + { + friend class ThreadPool; + + private: + boost::shared_ptr<Internals::FutureState> state_; + + explicit Future(boost::shared_ptr<Internals::FutureState>& state); + + public: + ~Future(); + + IDynamicObject* ReleaseResult(); + }; +}
--- /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 + * <http://www.gnu.org/licenses/>. + **/ + + +#include "../PrecompiledHeaders.h" +#include "FutureState.h" + +#include <cassert> + + +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<IDynamicObject> 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 + } + } + } +}
--- /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 + * <http://www.gnu.org/licenses/>. + **/ + + +#pragma once + +#include "../Compatibility.h" +#include "../IDynamicObject.h" +#include "../OrthancException.h" + +#include <boost/thread/mutex.hpp> +#include <boost/thread/condition_variable.hpp> + +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<IDynamicObject> result_; + std::unique_ptr<OrthancException> 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); + }; + } +}
--- /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 + * <http://www.gnu.org/licenses/>. + **/ + + +#pragma once + +#include "../IDynamicObject.h" + +namespace Orthanc +{ + class ORTHANC_PUBLIC ICallable : public boost::noncopyable + { + public: + virtual ~ICallable() + { + } + + virtual IDynamicObject* Call() = 0; + }; +}
--- /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 + * <http://www.gnu.org/licenses/>. + **/ + + +#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; + }; +}
--- /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 + * <http://www.gnu.org/licenses/>. + **/ + + +#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<ICallable> callable_; + boost::weak_ptr<Internals::FutureState> state_; + + public: + Task(ICallable* callable, + boost::shared_ptr<Internals::FutureState>& state) : + callable_(callable), + state_(state) + { + assert(callable != NULL); + } + + void Execute() + { + boost::shared_ptr<Internals::FutureState> 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<IDynamicObject> task(queue_.Dequeue(DEQUEUE_TIMEOUT_MS)); + + if (task.get() == NULL) + { + boost::mutex::scoped_lock lock(shutdownMutex_); + if (shutdown_) + { + return; + } + } + else + { + dynamic_cast<Task&>(*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<ICallable> protection(callable); + + if (callable == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + + boost::shared_ptr<Internals::FutureState> state(boost::make_shared<Internals::FutureState>()); + + queue_.Enqueue(new Task(protection.release(), state)); + + return new Future(state); + } +}
--- /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 + * <http://www.gnu.org/licenses/>. + **/ + + +#pragma once + +#include "../Compatibility.h" +#include "IExecutorService.h" +#include "SharedMessageQueue.h" + +#include <boost/thread.hpp> + +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; + }; +}
