# HG changeset patch # User Sebastien Jodogne # Date 1792587048 -7200 # Node ID f099b6169d0b87ae6cc08ce3a4dcfa4838cf5145 # Parent 2c0758ba1ab68ed16d60f67345beb15021e73ef3 moving PeriodicRunnableThread from stone diff -r 2c0758ba1ab6 -r f099b6169d0b OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake --- 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 diff -r 2c0758ba1ab6 -r f099b6169d0b OrthancFramework/Sources/MultiThreading/PeriodicRunnableThread.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 + * . + **/ + + +#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); + } + } +} diff -r 2c0758ba1ab6 -r f099b6169d0b OrthancFramework/Sources/MultiThreading/PeriodicRunnableThread.h --- /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 + * . + **/ + + +#pragma once + +#include "IRunnable.h" + +#include + + +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 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_; + } + }; +}