# HG changeset patch # User Alain Mazy # Date 1701793356 -3600 # Node ID 8345267e8de5e3b073ce8ecf8ae50a7809001557 # Parent b19bfb9393238d142a6d6bc3ee59e84be91f7830 Added OrthancPluginSetCurrentThreadName() in the plugin SDK diff -r b19bfb939323 -r 8345267e8de5 NEWS --- a/NEWS Tue Dec 05 16:28:16 2023 +0100 +++ b/NEWS Tue Dec 05 17:22:36 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 b19bfb939323 -r 8345267e8de5 OrthancFramework/Sources/Logging.cpp --- a/OrthancFramework/Sources/Logging.cpp Tue Dec 05 16:28:16 2023 +0100 +++ b/OrthancFramework/Sources/Logging.cpp Tue Dec 05 17:22:36 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 b19bfb939323 -r 8345267e8de5 OrthancServer/Plugins/Engine/OrthancPlugins.cpp --- a/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Tue Dec 05 16:28:16 2023 +0100 +++ b/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Tue Dec 05 17:22:36 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 b19bfb939323 -r 8345267e8de5 OrthancServer/Plugins/Include/orthanc/OrthancCPlugin.h --- a/OrthancServer/Plugins/Include/orthanc/OrthancCPlugin.h Tue Dec 05 16:28:16 2023 +0100 +++ b/OrthancServer/Plugins/Include/orthanc/OrthancCPlugin.h Tue Dec 05 17:22:36 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 b19bfb939323 -r 8345267e8de5 OrthancServer/Plugins/Samples/DelayedDeletion/Plugin.cpp --- a/OrthancServer/Plugins/Samples/DelayedDeletion/Plugin.cpp Tue Dec 05 16:28:16 2023 +0100 +++ b/OrthancServer/Plugins/Samples/DelayedDeletion/Plugin.cpp Tue Dec 05 17:22:36 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 b19bfb939323 -r 8345267e8de5 OrthancServer/Plugins/Samples/Housekeeper/Plugin.cpp --- a/OrthancServer/Plugins/Samples/Housekeeper/Plugin.cpp Tue Dec 05 16:28:16 2023 +0100 +++ b/OrthancServer/Plugins/Samples/Housekeeper/Plugin.cpp Tue Dec 05 17:22:36 2023 +0100 @@ -599,6 +599,8 @@ static void WorkerThread() { + OrthancPluginSetCurrentThreadName(OrthancPlugins::GetGlobalContext(), "HOUSEKEEPER"); + DbConfiguration currentDbConfiguration; OrthancPluginLogWarning(OrthancPlugins::GetGlobalContext(), "Starting Housekeeper worker thread");