diff options
author | Johnathan Corgan <johnathan@corganlabs.com> | 2014-04-06 03:11:38 -0700 |
---|---|---|
committer | Johnathan Corgan <johnathan@corganlabs.com> | 2014-04-06 03:11:38 -0700 |
commit | cd06fdc9372bb2489cc7a455b75cef445343296d (patch) | |
tree | 265ab672168b8a7a676e5d5c56cb1898cd1fe60b | |
parent | 38ec48293ca2fc424f171d3273eeb3f4b7384af5 (diff) | |
parent | 12a63ecea7b7f6f5c6fe5e3d9c15a50fb5737ec6 (diff) |
Merge remote-tracking branch 'balint/3.7-2/thread_naming'
-rw-r--r-- | gnuradio-runtime/include/gnuradio/thread/thread.h | 3 | ||||
-rw-r--r-- | gnuradio-runtime/lib/thread/thread.cc | 71 | ||||
-rw-r--r-- | gnuradio-runtime/lib/tpb_thread_body.cc | 2 |
3 files changed, 76 insertions, 0 deletions
diff --git a/gnuradio-runtime/include/gnuradio/thread/thread.h b/gnuradio-runtime/include/gnuradio/thread/thread.h index a0c9e4f09d..6cd84ae7e5 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 5e5874ec0f..53eb23b2f0 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, ¶m); } + + 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 7cdee6a097..79abd0e61d 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; |