From d89c8412989f89881382125aaa5680502279c41e Mon Sep 17 00:00:00 2001 From: Guenter Obiltschnig Date: Mon, 28 Sep 2015 22:49:06 +0200 Subject: [PATCH] setThreadName: abbreviate thread name if too long --- Foundation/src/Thread_POSIX.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/Foundation/src/Thread_POSIX.cpp b/Foundation/src/Thread_POSIX.cpp index b31532699..a3eac3b36 100644 --- a/Foundation/src/Thread_POSIX.cpp +++ b/Foundation/src/Thread_POSIX.cpp @@ -59,16 +59,23 @@ namespace #if defined(POCO_POSIX_DEBUGGER_THREAD_NAMES) -namespace +namespace { +void setThreadName(pthread_t thread, const std::string& threadName) { - void setThreadName(pthread_t thread, const char* threadName) - { -# if (POCO_OS == POCO_OS_MAC_OS_X) - pthread_setname_np(threadName); // __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2) -# else - pthread_setname_np(thread, threadName); -# endif - } +#if (POCO_OS == POCO_OS_MAC_OS_X) + pthread_setname_np(threadName.c_str()); // __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2) +#else + if (pthread_setname_np(thread, threadName.c_str())) + { + char truncName[16] = {0}; + std::size_t suffixIndex = threadName.length() - 7; + std::memcpy(truncName, &threadName[0], 7); + truncName[7] = '~'; + memcpy(&truncName[8], &threadName[suffixIndex], 7); + pthread_setname_np(thread, truncName); + } +#endif +} } @@ -336,7 +343,7 @@ void* ThreadImpl::runnableEntry(void* pThread) ThreadImpl* pThreadImpl = reinterpret_cast(pThread); #if defined(POCO_POSIX_DEBUGGER_THREAD_NAMES) - setThreadName(pThreadImpl->_pData->thread, reinterpret_cast(pThread)->getName().c_str()); + setThreadName(pThreadImpl->_pData->thread, reinterpret_cast(pThread)->getName()); #endif AutoPtr pData = pThreadImpl->_pData; try