Skip to content

Commit 06ab1e9

Browse files
committed
setThreadName: abbreviate thread name if too long
1 parent eda81a5 commit 06ab1e9

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

‎Foundation/src/Thread_POSIX.cpp‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#if POCO_OS == POCO_OS_LINUX
3939
#include <sys/syscall.h>
4040
#endif
41+
#include <cstring>
42+
4143

4244
//
4345
// Block SIGPIPE in main thread.
@@ -69,18 +71,26 @@ static SignalBlocker signalBlocker;
6971

7072

7173
namespace {
72-
void setThreadName(pthread_t thread, const char* threadName)
74+
void setThreadName(pthread_t thread, const std::string& threadName)
7375
{
74-
# if (POCO_OS == POCO_OS_MAC_OS_X)
75-
pthread_setname_np(threadName); // __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2)
76-
# else
77-
pthread_setname_np(thread, threadName);
78-
# endif
76+
#if (POCO_OS == POCO_OS_MAC_OS_X)
77+
pthread_setname_np(threadName.c_str()); // __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2)
78+
#else
79+
if (pthread_setname_np(thread, threadName.c_str()))
80+
{
81+
char truncName[16] = {0};
82+
std::size_t suffixIndex = threadName.length() - 7;
83+
std::memcpy(truncName, &threadName[0], 7);
84+
truncName[7] = '~';
85+
memcpy(&truncName[8], &threadName[suffixIndex], 7);
86+
pthread_setname_np(thread, truncName);
87+
}
88+
#endif
7989
}
8090
}
8191

8292

83-
#endif
93+
#endif // POCO_POSIX_DEBUGGER_THREAD_NAMES
8494

8595

8696
namespace Poco {
@@ -448,7 +458,7 @@ void* ThreadImpl::runnableEntry(void* pThread)
448458

449459
ThreadImpl* pThreadImpl = reinterpret_cast<ThreadImpl*>(pThread);
450460
#if defined(POCO_POSIX_DEBUGGER_THREAD_NAMES)
451-
setThreadName(pThreadImpl->_pData->thread, reinterpret_cast<Thread*>(pThread)->getName().c_str());
461+
setThreadName(pThreadImpl->_pData->thread, reinterpret_cast<Thread*>(pThread)->getName());
452462
#endif
453463
#if POCO_OS == POCO_OS_LINUX
454464
pThreadImpl->_pData->tid = static_cast<TIDImpl>( syscall (SYS_gettid) );

0 commit comments

Comments
 (0)