Mercurial > hg > orthanc
changeset 7175:f099b6169d0b
moving PeriodicRunnableThread from stone
| author | Sebastien Jodogne <s.jodogne@gmail.com> |
|---|---|
| date | Wed, 21 Oct 2026 14:50:48 +0200 |
| parents | 2c0758ba1ab6 |
| children | 240c8c844a7d |
| files | OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake OrthancFramework/Sources/MultiThreading/PeriodicRunnableThread.cpp OrthancFramework/Sources/MultiThreading/PeriodicRunnableThread.h |
| diffstat | 3 files changed, 181 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Tue Oct 20 15:40:30 2026 +0200 +++ b/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Wed Oct 21 14:50:48 2026 +0200 @@ -652,6 +652,7 @@ ${CMAKE_CURRENT_LIST_DIR}/../../Sources/MultiThreading/BlockingSharedMessageQueue.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/MultiThreading/Internals/FutureState.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/MultiThreading/Internals/ThreadPoolFuture.cpp + ${CMAKE_CURRENT_LIST_DIR}/../../Sources/MultiThreading/PeriodicRunnableThread.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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/MultiThreading/PeriodicRunnableThread.cpp Wed Oct 21 14:50:48 2026 +0200 @@ -0,0 +1,105 @@ +/** + * Stone of Orthanc + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., 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 "PeriodicRunnableThread.h" + +#include "../OrthancException.h" + + +namespace Orthanc +{ + void PeriodicRunnableThread::Worker(PeriodicRunnableThread* that) + { + for (;;) + { + { + boost::mutex::scoped_lock lock(that->mutex_); + if (that->state_ != State_Running) + { + return; + } + } + + that->runnable_->Run(); + + if (that->timeResolution_ != 0) + { + boost::this_thread::sleep(boost::posix_time::milliseconds(that->timeResolution_)); + } + } + } + + + void PeriodicRunnableThread::StopInternal(bool throws) + { + { + boost::mutex::scoped_lock lock(mutex_); + + if (state_ != State_Running) + { + if (throws) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + } + else + { + state_ = State_Done; + } + } + + if (thread_.joinable()) + { + thread_.join(); + } + } + + + PeriodicRunnableThread::PeriodicRunnableThread(IRunnable* runnable, + unsigned int timeResolution) : + runnable_(runnable), + timeResolution_(timeResolution), + state_(State_Initialization) + { + if (runnable == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + } + + + void PeriodicRunnableThread::Start() + { + boost::mutex::scoped_lock lock(mutex_); + + if (state_ != State_Initialization) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + state_ = State_Running; + thread_ = boost::thread(Worker, this); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/MultiThreading/PeriodicRunnableThread.h Wed Oct 21 14:50:48 2026 +0200 @@ -0,0 +1,75 @@ +/** + * Stone of Orthanc + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., 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 "IRunnable.h" + +#include <boost/thread.hpp> + + +namespace Orthanc +{ + // This is a thread that periodically executes a runnable + class ORTHANC_PUBLIC PeriodicRunnableThread : public boost::noncopyable + { + private: + enum State + { + State_Initialization, + State_Running, + State_Done + }; + + static void Worker(PeriodicRunnableThread* that); + + void StopInternal(bool throws); + + boost::mutex mutex_; + std::unique_ptr<IRunnable> runnable_; + unsigned int timeResolution_; + volatile State state_; + boost::thread thread_; + + public: + PeriodicRunnableThread(IRunnable* runnable /* takes ownership */, + unsigned int timeResolution); + + ~PeriodicRunnableThread() + { + StopInternal(false /* no exception in destructors */); + } + + void Start(); + + void Stop() + { + StopInternal(true); + } + + IRunnable& GetRunnable() const + { + return *runnable_; + } + }; +}
