# HG changeset patch # User Alain Mazy # Date 1776768565 -7200 # Node ID d691dafe5ca84f584325f55f0fc82b7249d3a85b # Parent c225552d8824af285cbb07759533d312cb2ae3b5 ClearThreadName to reduce the size of threadNames_ map that could contain thousands of loader thread ids or dead thread ids after a /tools/reset diff -r c225552d8824 -r d691dafe5ca8 OrthancFramework/Sources/DicomNetworking/DicomServer.cpp --- a/OrthancFramework/Sources/DicomNetworking/DicomServer.cpp Tue Apr 21 11:40:30 2026 +0200 +++ b/OrthancFramework/Sources/DicomNetworking/DicomServer.cpp Tue Apr 21 12:49:25 2026 +0200 @@ -63,7 +63,7 @@ unsigned int maximumPduLength, bool useDicomTls) { - Logging::SetCurrentThreadName("DICOM-SERVER"); + Logging::ScopedThreadNameSetter setter("DICOM-SERVER"); CLOG(INFO, DICOM) << "DICOM server started"; while (server->continue_) diff -r c225552d8824 -r d691dafe5ca8 OrthancFramework/Sources/JobsEngine/JobsEngine.cpp --- a/OrthancFramework/Sources/JobsEngine/JobsEngine.cpp Tue Apr 21 11:40:30 2026 +0200 +++ b/OrthancFramework/Sources/JobsEngine/JobsEngine.cpp Tue Apr 21 12:49:25 2026 +0200 @@ -109,7 +109,7 @@ void JobsEngine::RetryHandler(JobsEngine* engine) { - Logging::SetCurrentThreadName("JOBS-RETRY"); + Logging::ScopedThreadNameSetter setter("JOBS-RETRY"); assert(engine != NULL); @@ -125,7 +125,7 @@ size_t workerIndex) { assert(engine != NULL); - Logging::SetCurrentThreadName(std::string("JOBS-WORKER-") + boost::lexical_cast(workerIndex)); + Logging::ScopedThreadNameSetter setter(std::string("JOBS-WORKER-") + boost::lexical_cast(workerIndex)); CLOG(INFO, JOBS) << "Worker thread " << workerIndex << " has started"; while (engine->IsRunning()) diff -r c225552d8824 -r d691dafe5ca8 OrthancFramework/Sources/Logging.cpp --- a/OrthancFramework/Sources/Logging.cpp Tue Apr 21 11:40:30 2026 +0200 +++ b/OrthancFramework/Sources/Logging.cpp Tue Apr 21 12:49:25 2026 +0200 @@ -693,6 +693,14 @@ return threadNames_.find(threadId) != threadNames_.end(); } + void ClearCurrentThreadName() + { + boost::thread::id threadId = boost::this_thread::get_id(); + + boost::recursive_mutex::scoped_lock lock(threadNamesMutex_); + threadNames_.erase(threadId); + } + static std::string GetCurrentThreadName() { boost::thread::id threadId = boost::this_thread::get_id(); @@ -708,6 +716,17 @@ return threadNames_[threadId]; } + ScopedThreadNameSetter::ScopedThreadNameSetter(const std::string& threadName) + { + SetCurrentThreadName(threadName); + } + + ScopedThreadNameSetter::~ScopedThreadNameSetter() + { + ClearCurrentThreadName(); + } + + void AddLoggingListener(ILoggingListener* listener) { boost::mutex::scoped_lock lock(loggingListenersMutex_); diff -r c225552d8824 -r d691dafe5ca8 OrthancFramework/Sources/Logging.h --- a/OrthancFramework/Sources/Logging.h Tue Apr 21 11:40:30 2026 +0200 +++ b/OrthancFramework/Sources/Logging.h Tue Apr 21 12:49:25 2026 +0200 @@ -75,6 +75,14 @@ LogCategory_LUA = (1 << 6) }; + class ScopedThreadNameSetter + { + public: + ScopedThreadNameSetter(const std::string& threadName); + + ~ScopedThreadNameSetter(); + }; + class ILoggingListener { public: @@ -110,6 +118,8 @@ ORTHANC_PUBLIC bool HasCurrentThreadName(); + ORTHANC_PUBLIC void ClearCurrentThreadName(); + ORTHANC_PUBLIC void AddLoggingListener(ILoggingListener* listener); ORTHANC_PUBLIC void ClearLoggingListeners(); diff -r c225552d8824 -r d691dafe5ca8 OrthancFramework/Sources/MultiThreading/RunnableWorkersPool.cpp --- a/OrthancFramework/Sources/MultiThreading/RunnableWorkersPool.cpp Tue Apr 21 11:40:30 2026 +0200 +++ b/OrthancFramework/Sources/MultiThreading/RunnableWorkersPool.cpp Tue Apr 21 12:49:25 2026 +0200 @@ -48,7 +48,7 @@ static void WorkerThread(Worker* that) { - Logging::SetCurrentThreadName(that->threadName_); + Logging::ScopedThreadNameSetter setter(that->threadName_); while (that->continue_) { diff -r c225552d8824 -r d691dafe5ca8 OrthancServer/Sources/LuaScripting.cpp --- a/OrthancServer/Sources/LuaScripting.cpp Tue Apr 21 11:40:30 2026 +0200 +++ b/OrthancServer/Sources/LuaScripting.cpp Tue Apr 21 12:49:25 2026 +0200 @@ -856,7 +856,7 @@ void LuaScripting::HeartBeatThread(LuaScripting* that) { - Logging::SetCurrentThreadName("LUA-HEARTBEAT"); + Logging::ScopedThreadNameSetter setter("LUA-HEARTBEAT"); static const unsigned int GRANULARITY = 100; // In milliseconds @@ -894,7 +894,7 @@ void LuaScripting::EventThread(LuaScripting* that) { - Logging::SetCurrentThreadName("LUA-EVENTS"); + Logging::ScopedThreadNameSetter setter("LUA-EVENTS"); for (;;) { diff -r c225552d8824 -r d691dafe5ca8 OrthancServer/Sources/OrthancWebDav.cpp --- a/OrthancServer/Sources/OrthancWebDav.cpp Tue Apr 21 11:40:30 2026 +0200 +++ b/OrthancServer/Sources/OrthancWebDav.cpp Tue Apr 21 12:49:25 2026 +0200 @@ -1163,7 +1163,7 @@ void OrthancWebDav::UploadWorker(OrthancWebDav* that) { - Logging::SetCurrentThreadName("WEBDAV-UPLOAD"); + Logging::ScopedThreadNameSetter setter("WEBDAV-UPLOAD"); assert(that != NULL); diff -r c225552d8824 -r d691dafe5ca8 OrthancServer/Sources/ServerContext.cpp --- a/OrthancServer/Sources/ServerContext.cpp Tue Apr 21 11:40:30 2026 +0200 +++ b/OrthancServer/Sources/ServerContext.cpp Tue Apr 21 12:49:25 2026 +0200 @@ -116,7 +116,7 @@ void ServerContext::MemoryTrimmingThread(ServerContext* that, unsigned int intervalInSeconds) { - Logging::SetCurrentThreadName("MEMORY-TRIM"); + Logging::ScopedThreadNameSetter setter("MEMORY-TRIM"); boost::posix_time::ptime lastExecution = boost::posix_time::second_clock::universal_time(); @@ -146,7 +146,7 @@ void ServerContext::ChangeThread(ServerContext* that, unsigned int sleepDelay) { - Logging::SetCurrentThreadName("CHANGES"); + Logging::ScopedThreadNameSetter setter("CHANGES"); while (!that->done_) { @@ -190,7 +190,7 @@ void ServerContext::JobEventsThread(ServerContext* that, unsigned int sleepDelay) { - Logging::SetCurrentThreadName("JOB-EVENTS"); + Logging::ScopedThreadNameSetter setter("JOB-EVENTS"); while (!that->done_) { @@ -234,7 +234,7 @@ void ServerContext::SaveJobsThread(ServerContext* that, unsigned int sleepDelay) { - Logging::SetCurrentThreadName("SAVE-JOBS"); + Logging::ScopedThreadNameSetter setter("SAVE-JOBS"); static const boost::posix_time::time_duration PERIODICITY = boost::posix_time::seconds(10); diff -r c225552d8824 -r d691dafe5ca8 OrthancServer/Sources/ServerIndex.cpp --- a/OrthancServer/Sources/ServerIndex.cpp Tue Apr 21 11:40:30 2026 +0200 +++ b/OrthancServer/Sources/ServerIndex.cpp Tue Apr 21 12:49:25 2026 +0200 @@ -276,7 +276,7 @@ void ServerIndex::FlushThread(ServerIndex* that, unsigned int threadSleepGranularityMilliseconds) { - Logging::SetCurrentThreadName("DB-FLUSH"); + Logging::ScopedThreadNameSetter setter("DB-FLUSH"); // By default, wait for 10 seconds before flushing static const unsigned int SLEEP_SECONDS = 10; @@ -458,7 +458,7 @@ void ServerIndex::UnstableResourcesMonitorThread(ServerIndex* that, unsigned int threadSleepGranularityMilliseconds) { - Logging::SetCurrentThreadName("UNSTABLE-MON"); + Logging::ScopedThreadNameSetter setter("UNSTABLE-MON"); int stableAge; diff -r c225552d8824 -r d691dafe5ca8 OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp --- a/OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp Tue Apr 21 11:40:30 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp Tue Apr 21 12:49:25 2026 +0200 @@ -30,6 +30,15 @@ static boost::mutex loaderThreadsCounterMutex; static uint32_t loaderThreadsCounter = 0; +static std::string GetLoaderThreadName(const std::string& prefix) +{ + boost::mutex::scoped_lock lock(loaderThreadsCounterMutex); + std::string threadName = prefix + std::string("-LOAD-") + + boost::lexical_cast(loaderThreadsCounter++); + loaderThreadsCounter %= 1000000; + + return threadName; +} namespace Orthanc { @@ -146,12 +155,7 @@ void ThreadedInstancesLoader::PreloaderWorkerThread(ThreadedInstancesLoader* that) { - { - boost::mutex::scoped_lock lock(loaderThreadsCounterMutex); - Logging::SetCurrentThreadName(that->nameForLogs4Char_ + std::string("-LOAD-") + - boost::lexical_cast(loaderThreadsCounter++)); - loaderThreadsCounter %= 1000000; - } + Logging::ScopedThreadNameSetter setter(::GetLoaderThreadName(that->nameForLogs4Char_)); LOG(INFO) << "Loader thread has started"; diff -r c225552d8824 -r d691dafe5ca8 OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp --- a/OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp Tue Apr 21 11:40:30 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp Tue Apr 21 12:49:25 2026 +0200 @@ -34,6 +34,14 @@ static boost::mutex instanceWorkerThreadsCounterMutex; static uint32_t instanceWorkerThreadsCounter = 0; +static std::string GetInstanceWorkerThreadName() +{ + boost::mutex::scoped_lock lock(instanceWorkerThreadsCounterMutex); + std::string threadName = std::string("JOB-INS-WORK-") + boost::lexical_cast(instanceWorkerThreadsCounter++); + instanceWorkerThreadsCounter %= 1000; + + return threadName; +} namespace Orthanc { @@ -248,11 +256,7 @@ void ThreadedSetOfInstancesJob::InstanceWorkerThread(ThreadedSetOfInstancesJob* that) { - { - boost::mutex::scoped_lock lock(instanceWorkerThreadsCounterMutex); - Logging::SetCurrentThreadName(std::string("JOB-INS-WORK-") + boost::lexical_cast(instanceWorkerThreadsCounter++)); - instanceWorkerThreadsCounter %= 1000; - } + Logging::ScopedThreadNameSetter setter(::GetInstanceWorkerThreadName()); while (true) {