changeset 5452:8345267e8de5

Added OrthancPluginSetCurrentThreadName() in the plugin SDK
author Alain Mazy <am@osimis.io>
date Tue, 05 Dec 2023 17:22:36 +0100
parents b19bfb939323
children b750777c36a2
files NEWS OrthancFramework/Sources/Logging.cpp OrthancServer/Plugins/Engine/OrthancPlugins.cpp OrthancServer/Plugins/Include/orthanc/OrthancCPlugin.h OrthancServer/Plugins/Samples/DelayedDeletion/Plugin.cpp OrthancServer/Plugins/Samples/Housekeeper/Plugin.cpp
diffstat 6 files changed, 38 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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<boost::thread::id, std::string> 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
--- 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<const char*>(parameters)));
+        return true;
+      }
+
       default:
         return false;
     }
--- 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
--- 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_)
--- 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");