commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] [gnuradio] 01/02: runtime: added ability to name threa


From: git
Subject: [Commit-gnuradio] [gnuradio] 01/02: runtime: added ability to name threads, TPB scheduler automatically names each block's thread
Date: Sun, 6 Apr 2014 10:26:52 +0000 (UTC)

This is an automated email from the git hooks/post-receive script.

jcorgan pushed a commit to branch master
in repository gnuradio.

commit 12a63ecea7b7f6f5c6fe5e3d9c15a50fb5737ec6
Author: Balint Seeber <address@hidden>
Date:   Fri Apr 4 09:30:47 2014 -0700

    runtime: added ability to name threads, TPB scheduler automatically names 
each block's thread
---
 gnuradio-runtime/include/gnuradio/thread/thread.h |  3 +
 gnuradio-runtime/lib/thread/thread.cc             | 71 +++++++++++++++++++++++
 gnuradio-runtime/lib/tpb_thread_body.cc           |  2 +
 3 files changed, 76 insertions(+)

diff --git a/gnuradio-runtime/include/gnuradio/thread/thread.h 
b/gnuradio-runtime/include/gnuradio/thread/thread.h
index a0c9e4f..6cd84ae 100644
--- a/gnuradio-runtime/include/gnuradio/thread/thread.h
+++ b/gnuradio-runtime/include/gnuradio/thread/thread.h
@@ -149,6 +149,9 @@ namespace gr {
      * Note: this does not work on OSX
      */
     GR_RUNTIME_API int set_thread_priority(gr_thread_t thread, int priority);
+    
+    GR_RUNTIME_API void set_thread_name(gr_thread_t thread,
+                                        std::string name);
 
   } /* namespace thread */
 } /* namespace gr */
diff --git a/gnuradio-runtime/lib/thread/thread.cc 
b/gnuradio-runtime/lib/thread/thread.cc
index 5e5874e..53eb23b 100644
--- a/gnuradio-runtime/lib/thread/thread.cc
+++ b/gnuradio-runtime/lib/thread/thread.cc
@@ -24,6 +24,7 @@
 #endif
 
 #include <gnuradio/thread/thread.h>
+#include <boost/format.hpp>
 
 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
 
@@ -110,6 +111,41 @@ namespace gr {
       // Not implemented on Windows
       return -1;
     }
+#pragma pack(push,8)
+    typedef struct tagTHREADNAME_INFO
+    {
+      DWORD dwType;     // Must be 0x1000
+      LPCSTR szName;    // Pointer to name (in user addr space)
+      DWORD dwThreadID; // Thread ID (-1 = caller thread)
+      DWORD dwFlags;    // Reserved for future use, must be zero
+    } THREADNAME_INFO;
+#pragma pack(pop)
+    void
+    set_thread_name(gr_thread_t thread, std::string name)
+    {
+      const DWORD SET_THREAD_NAME_EXCEPTION = 0x406D1388;
+      
+      DWORD dwThreadId = GetThreadId(thread);
+      if (dwThreadId == 0)
+        return;
+      
+      if (name.empty())
+        name = boost::str(boost::format("thread %lu") % dwThreadId);
+      
+      THREADNAME_INFO info;
+      info.dwType = 0x1000;
+      info.szName = name.c_str();
+      info.dwThreadID = dwThreadId;
+      info.dwFlags = 0;
+      
+      __try
+      {
+        RaiseException(SET_THREAD_NAME_EXCEPTION, 0, 
sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
+      }
+      __except(EXCEPTION_EXECUTE_HANDLER)
+      {
+      }
+    }
 
   } /* namespace thread */
 } /* namespace gr */
@@ -177,6 +213,12 @@ namespace gr {
       // Not implemented on OSX
       return -1;
     }
+    
+    void
+    set_thread_name(gr_thread_t thread, std::string name)
+    {
+      // Not implemented on OSX
+    }
 
   } /* namespace thread */
 } /* namespace gr */
@@ -186,6 +228,7 @@ namespace gr {
 #include <sstream>
 #include <stdexcept>
 #include <pthread.h>
+#include <sys/prctl.h>
 
 namespace gr {
   namespace thread {
@@ -283,6 +326,34 @@ namespace gr {
       param.sched_priority = priority;
       return pthread_setschedparam(thread, policy, &param);
     }
+    
+    void
+    set_thread_name(gr_thread_t thread, std::string name)
+    {
+      if (thread != pthread_self()) // Naming another thread is not supported
+        return;
+      
+      if (name.empty())
+        name = boost::str(boost::format("thread %llu") % ((unsigned long 
long)thread));
+      
+      const int max_len = 16; // Maximum accepted by PR_SET_NAME
+      
+      if ((int)name.size() > max_len) // Shorten the name if necessary by 
taking as many characters from the front
+      {                               // so that the unique_id can still fit 
on the end
+        int i = name.size() - 1;
+        for (; i >= 0; --i)
+        {
+          std::string s = name.substr(i, 1);
+          int n = atoi(s.c_str());
+          if ((n == 0) && (s != "0"))
+            break;
+        }
+        
+        name = name.substr(0, std::max(0, max_len - ((int)name.size() - (i + 
1)))) + name.substr(i + 1);
+      }
+      
+      prctl(PR_SET_NAME, name.c_str(), 0, 0, 0);
+    }
 
   } /* namespace thread */
 } /* namespace gr */
diff --git a/gnuradio-runtime/lib/tpb_thread_body.cc 
b/gnuradio-runtime/lib/tpb_thread_body.cc
index 7cdee6a..79abd0e 100644
--- a/gnuradio-runtime/lib/tpb_thread_body.cc
+++ b/gnuradio-runtime/lib/tpb_thread_body.cc
@@ -36,6 +36,8 @@ namespace gr {
     : d_exec(block, max_noutput_items)
   {
     //std::cerr << "tpb_thread_body: " << block << std::endl;
+    
+    thread::set_thread_name(pthread_self(), boost::str(boost::format("%s%d") % 
block->name() % block->unique_id()));
 
     block_detail *d = block->detail().get();
     block_executor::state s;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]