Skip to content

Commit f2d1ae2

Browse files
committed
Merge pull request pocoproject#846 from matejk/develop-thread-os-id
Posix OS thread ID, added %O (thread ID) to logger
2 parents 4ac92e5 + e26bb65 commit f2d1ae2

6 files changed

Lines changed: 55 additions & 3 deletions

File tree

‎Foundation/include/Poco/Message.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ class Foundation_API Message
128128
long getTid() const;
129129
/// Returns the numeric thread identifier for the message.
130130

131+
long getOsTid() const;
132+
/// Returns the numeric OS thread identifier for the message.
133+
131134
void setPid(long pid);
132135
/// Sets the process identifier for the message.
133136

@@ -196,6 +199,7 @@ class Foundation_API Message
196199
Priority _prio;
197200
Timestamp _time;
198201
int _tid;
202+
int _ostid;
199203
std::string _thread;
200204
long _pid;
201205
const char* _file;
@@ -243,6 +247,12 @@ inline long Message::getTid() const
243247
}
244248

245249

250+
inline long Message::getOsTid() const
251+
{
252+
return _ostid;
253+
}
254+
255+
246256
inline long Message::getPid() const
247257
{
248258
return _pid;

‎Foundation/include/Poco/PatternFormatter.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Foundation_API PatternFormatter: public Formatter
4545
/// * %P - message process identifier
4646
/// * %T - message thread name
4747
/// * %I - message thread identifier (numeric)
48+
/// * %O - message thread OS identifier (numeric)
4849
/// * %N - node or host name
4950
/// * %U - message source file path (empty string if not set)
5051
/// * %u - message source line number (0 if not set)

‎Foundation/include/Poco/Thread_POSIX.h‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,18 @@ namespace Poco {
4545
class Foundation_API ThreadImpl
4646
{
4747
public:
48+
49+
#if POCO_OS == POCO_OS_LINUX
50+
// OS kernel thread ID
51+
typedef pid_t TIDImpl;
52+
#elif POCO_OS == POCO_OS_MAC_OS_X
53+
// OS kernel thread ID
54+
typedef mach_port_t TIDImpl;
55+
#else
56+
// Default: pthread id
4857
typedef pthread_t TIDImpl;
58+
#endif
59+
4960
typedef void (*Callable)(void*);
5061

5162
enum Priority
@@ -136,6 +147,7 @@ class Foundation_API ThreadImpl
136147

137148
SharedPtr<Runnable> pRunnableTarget;
138149
pthread_t thread;
150+
TIDImpl tid;
139151
int prio;
140152
int osPrio;
141153
int policy;
@@ -191,7 +203,7 @@ inline int ThreadImpl::getStackSizeImpl() const
191203

192204
inline ThreadImpl::TIDImpl ThreadImpl::tidImpl() const
193205
{
194-
return _pData->thread;
206+
return _pData->tid;
195207
}
196208

197209

‎Foundation/src/Message.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace Poco {
2929
Message::Message():
3030
_prio(PRIO_FATAL),
3131
_tid(0),
32+
_ostid(0),
3233
_pid(0),
3334
_file(0),
3435
_line(0),
@@ -43,6 +44,7 @@ Message::Message(const std::string& source, const std::string& text, Priority pr
4344
_text(text),
4445
_prio(prio),
4546
_tid(0),
47+
_ostid(0),
4648
_pid(0),
4749
_file(0),
4850
_line(0),
@@ -57,6 +59,7 @@ Message::Message(const std::string& source, const std::string& text, Priority pr
5759
_text(text),
5860
_prio(prio),
5961
_tid(0),
62+
_ostid(0),
6063
_pid(0),
6164
_file(file),
6265
_line(line),
@@ -72,6 +75,7 @@ Message::Message(const Message& msg):
7275
_prio(msg._prio),
7376
_time(msg._time),
7477
_tid(msg._tid),
78+
_ostid(msg._ostid),
7579
_thread(msg._thread),
7680
_pid(msg._pid),
7781
_file(msg._file),
@@ -90,6 +94,7 @@ Message::Message(const Message& msg, const std::string& text):
9094
_prio(msg._prio),
9195
_time(msg._time),
9296
_tid(msg._tid),
97+
_ostid(msg._ostid),
9398
_thread(msg._thread),
9499
_pid(msg._pid),
95100
_file(msg._file),
@@ -113,6 +118,7 @@ void Message::init()
113118
#if !defined(POCO_VXWORKS)
114119
_pid = Process::id();
115120
#endif
121+
_ostid = Thread::currentTid();
116122
Thread* pThread = Thread::current();
117123
if (pThread)
118124
{

‎Foundation/src/PatternFormatter.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ void PatternFormatter::format(const Message& msg, std::string& text)
7575
case 'P': NumberFormatter::append(text, msg.getPid()); break;
7676
case 'T': text.append(msg.getThread()); break;
7777
case 'I': NumberFormatter::append(text, msg.getTid()); break;
78+
case 'O': NumberFormatter::append(text, msg.getOsTid()); break;
7879
case 'N': text.append(Environment::nodeName()); break;
7980
case 'U': text.append(msg.getSourceFile() ? msg.getSourceFile() : ""); break;
8081
case 'u': NumberFormatter::append(text, msg.getSourceLine()); break;

‎Foundation/src/Thread_POSIX.cpp‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@
2828
#endif
2929
#if POCO_OS == POCO_OS_LINUX || POCO_OS == POCO_OS_MAC_OS_X || POCO_OS == POCO_OS_QNX
3030
# include <time.h>
31-
# include <unistd.h>
31+
# include <unistd.h>
3232
#endif
3333
#if POCO_OS == POCO_OS_MAC_OS_X
3434
# include <mach/mach.h>
3535
# include <mach/task.h>
3636
# include <mach/thread_policy.h>
3737
#endif
38-
38+
#if POCO_OS == POCO_OS_LINUX
39+
#include <sys/syscall.h>
40+
#endif
3941

4042
//
4143
// Block SIGPIPE in main thread.
@@ -296,6 +298,15 @@ void ThreadImpl::startImpl(SharedPtr<Runnable> pTarget)
296298
pthread_attr_destroy(&attributes);
297299
throw SystemException("cannot start thread");
298300
}
301+
#if POCO_OS == POCO_OS_LINUX
302+
// On Linux the TID is acquired from the running thread using syscall
303+
_pData->tid = 0;
304+
#elif POCO_OS == POCO_OS_MAC_OS_X
305+
_pData->tid = static_cast<TIDImpl>( pthread_mach_thread_np(_pData->thread) );
306+
#else
307+
_pData->tid = _pData->thread;
308+
#endif
309+
299310
_pData->started = true;
300311
pthread_attr_destroy(&attributes);
301312

@@ -353,7 +364,13 @@ ThreadImpl* ThreadImpl::currentImpl()
353364

354365
ThreadImpl::TIDImpl ThreadImpl::currentTidImpl()
355366
{
367+
#if POCO_OS == POCO_OS_LINUX
368+
return static_cast<TIDImpl>( syscall (SYS_gettid) );
369+
#elif POCO_OS == POCO_OS_MAC_OS_X
370+
return static_cast<TIDImpl>( pthread_mach_thread_np(pthread_self()) );
371+
#else
356372
return pthread_self();
373+
#endif
357374
}
358375

359376

@@ -430,7 +447,12 @@ void* ThreadImpl::runnableEntry(void* pThread)
430447
#if defined(POCO_POSIX_DEBUGGER_THREAD_NAMES)
431448
setThreadName(pThreadImpl->_pData->thread, reinterpret_cast<Thread*>(pThread)->getName().c_str());
432449
#endif
450+
#if POCO_OS == POCO_OS_LINUX
451+
pThreadImpl->_pData->tid = static_cast<TIDImpl>( syscall (SYS_gettid) );
452+
#endif
453+
433454
AutoPtr<ThreadData> pData = pThreadImpl->_pData;
455+
434456
try
435457
{
436458
pData->pRunnableTarget->run();

0 commit comments

Comments
 (0)