# HG changeset patch # User Alain Mazy # Date 1701868833 -3600 # Node ID 099d45f49fe14f0a5e267cb1adadaabcb24e9a1e # Parent 9ffd6d18daf3e3be1a65072f2fce68a12166f617# Parent b750777c36a2f5a2f32a8db88352ebd646a42cfd merge default -> pg-transactions diff -r 9ffd6d18daf3 -r 099d45f49fe1 NEWS --- a/NEWS Tue Dec 05 16:26:35 2023 +0100 +++ b/NEWS Wed Dec 06 14:20:33 2023 +0100 @@ -65,6 +65,7 @@ * Plugins are now allowed to modify/delete private metadata/attachments (i.e. whose identifiers are < 1024) +* Added "OrthancPluginSetCurrentThreadName()" in the plugin SDK. Maintenance diff -r 9ffd6d18daf3 -r 099d45f49fe1 OrthancFramework/Sources/Logging.cpp --- a/OrthancFramework/Sources/Logging.cpp Tue Dec 05 16:26:35 2023 +0100 +++ b/OrthancFramework/Sources/Logging.cpp Wed Dec 06 14:20:33 2023 +0100 @@ -539,6 +539,7 @@ static boost::mutex loggingStreamsMutex_; static Orthanc::Logging::NullStream nullStream_; static OrthancPluginContext* pluginContext_ = NULL; +static boost::recursive_mutex threadNamesMutex_; static std::map threadNames_; static bool enableThreadNames_ = true; @@ -624,7 +625,8 @@ void SetCurrentThreadNameInternal(const boost::thread::id& id, const std::string& name) { - // this method assumes that the loggingStreamsMutex is already locked + boost::recursive_mutex::scoped_lock lock(threadNamesMutex_); + if (name.size() > 16) { throw OrthancException(ErrorCode_InternalError, std::string("Thread name can not exceed 16 characters: ") + name); @@ -635,7 +637,7 @@ void SetCurrentThreadName(const std::string& name) { - boost::mutex::scoped_lock lock(loggingStreamsMutex_); + boost::recursive_mutex::scoped_lock lock(threadNamesMutex_); SetCurrentThreadNameInternal(boost::this_thread::get_id(), name); } @@ -649,9 +651,10 @@ static std::string GetCurrentThreadName() { - // this method assumes that the loggingStreamsMutex is already locked boost::thread::id threadId = boost::this_thread::get_id(); + boost::recursive_mutex::scoped_lock lock(threadNamesMutex_); + if (threadNames_.find(threadId) == threadNames_.end()) { // set the threadId as the thread name diff -r 9ffd6d18daf3 -r 099d45f49fe1 OrthancServer/Plugins/Engine/OrthancPlugins.cpp --- a/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Tue Dec 05 16:26:35 2023 +0100 +++ b/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Wed Dec 06 14:20:33 2023 +0100 @@ -5530,6 +5530,12 @@ return true; } + case _OrthancPluginService_SetCurrentThreadName: + { + Logging::SetCurrentThreadName(std::string(reinterpret_cast(parameters))); + return true; + } + default: return false; } diff -r 9ffd6d18daf3 -r 099d45f49fe1 OrthancServer/Plugins/Include/orthanc/OrthancCPlugin.h --- a/OrthancServer/Plugins/Include/orthanc/OrthancCPlugin.h Tue Dec 05 16:26:35 2023 +0100 +++ b/OrthancServer/Plugins/Include/orthanc/OrthancCPlugin.h Wed Dec 06 14:20:33 2023 +0100 @@ -449,6 +449,8 @@ _OrthancPluginService_CreateDicom2 = 41, /* New in Orthanc 1.9.0 */ _OrthancPluginService_GetDatabaseServerIdentifier = 42, /* New in Orthanc 1.11.1 */ _OrthancPluginService_SetMetricsIntegerValue = 43, /* New in Orthanc 1.12.1 */ + _OrthancPluginService_SetCurrentThreadName = 44, /* New in Orthanc 1.12.2 */ + /* Registration of callbacks */ _OrthancPluginService_RegisterRestCallback = 1000, @@ -9356,6 +9358,25 @@ } +/** + * @brief Sets the name of the current thread + * + * This function sets the name of the thread that is calling it. + * This name is used in the logs. This function shall be called only from threads that + * the plugin has created itself. + * + * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). + * @param threadName The name of the current thread. A Thread name can not be larger than 16 characters. + * @return 0 if success, other value if error. + * @ingroup Toolbox + **/ + ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode OrthancPluginSetCurrentThreadName( + OrthancPluginContext* context, + const char* threadName) + { + return context->InvokeService(context, _OrthancPluginService_SetCurrentThreadName, threadName); + } + #ifdef __cplusplus } #endif diff -r 9ffd6d18daf3 -r 099d45f49fe1 OrthancServer/Plugins/Samples/DelayedDeletion/Plugin.cpp --- a/OrthancServer/Plugins/Samples/DelayedDeletion/Plugin.cpp Tue Dec 05 16:26:35 2023 +0100 +++ b/OrthancServer/Plugins/Samples/DelayedDeletion/Plugin.cpp Wed Dec 06 14:20:33 2023 +0100 @@ -186,6 +186,8 @@ static void DeletionWorker() { + OrthancPluginSetCurrentThreadName(OrthancPlugins::GetGlobalContext(), "DELETION"); + static const unsigned int GRANULARITY = 100; // In milliseconds while (continue_) diff -r 9ffd6d18daf3 -r 099d45f49fe1 OrthancServer/Plugins/Samples/Housekeeper/Plugin.cpp --- a/OrthancServer/Plugins/Samples/Housekeeper/Plugin.cpp Tue Dec 05 16:26:35 2023 +0100 +++ b/OrthancServer/Plugins/Samples/Housekeeper/Plugin.cpp Wed Dec 06 14:20:33 2023 +0100 @@ -599,6 +599,8 @@ static void WorkerThread() { + OrthancPluginSetCurrentThreadName(OrthancPlugins::GetGlobalContext(), "HOUSEKEEPER"); + DbConfiguration currentDbConfiguration; OrthancPluginLogWarning(OrthancPlugins::GetGlobalContext(), "Starting Housekeeper worker thread"); diff -r 9ffd6d18daf3 -r 099d45f49fe1 OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp --- a/OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp Tue Dec 05 16:26:35 2023 +0100 +++ b/OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp Wed Dec 06 14:20:33 2023 +0100 @@ -243,8 +243,9 @@ void ThreadedSetOfInstancesJob::InstanceWorkerThread(ThreadedSetOfInstancesJob* that) { - static uint8_t threadCounter = 0; + static uint16_t threadCounter = 0; Logging::SetCurrentThreadName(std::string("JOB-INS-WORK-") + boost::lexical_cast(threadCounter++)); + threadCounter %= 1000; while (true) {