pocoproject / poco

The POCO C++ Libraries are powerful cross-platform C++ libraries for building network- and internet-based applications that run on desktop, server, mobile, IoT, and embedded systems.
https://pocoproject.org
Other
8.32k stars 2.15k forks source link

Extend format patterns %T and %I to support native threads #3323

Closed osmeest closed 2 years ago

osmeest commented 3 years ago

PatternFormatter provides %T and %I to print the thread name and thread id. Those patterns depend on attributes of Message which are set in the init() function, using Thread::current() and calling getName() and getTid() on the thread if any. Since Thread::current() works only for a thread that has been created using Poco::Thread, it does not work for threads that were created in another library (eg. std::thread).

osmeest commented 3 years ago

The simplest way to add support for native threads would be to extend Message::init() and look for native thread attributes when Thread::current() returned nullptr. However, inspecting native thread attributes is going to be OS dependent, just like Thread uses multiple implementations of ThreadImpl depending on the target platform.

A cleaner way would be to split ThreadImpl in 3 parts:

For simplicity sake, NativeThreadImpl could provide stubbed methods for setStackSize and start, since they make no sense for already running threads. Instead of deriving from ThreadImpl, a Thread would contain a NativeThreadImpl (or a ThreadImpl when it is created by Thread constructor).

matejk commented 3 years ago

There is pull request #3017 that also introduces patterns for native threads.

osmeest commented 3 years ago

The proposals made in pull request #3017 add a new pattern for the OS specific thread id. In the meantime, I've developed the simpler solution described above (NativeThreadInfo capable of reading id & name + platform specific implementations). It's implemented and unit tested for POSIX. I also provided blind implementations for Windows and VxWorks. Not sure how to proceed in checking those.

Corresponding pull request #3333

osmeest commented 3 years ago

Given that there are multiple proposals for the OS-native thread id (%O in #3017) and %I in the existing pattern, which direction do you intend to take in r1.12.0 ?

aleks-f commented 2 years ago

%J it is

osmeest commented 2 years ago

%J is ok for the OS thread id (gettid()). But what about the thread name (pthread_getname_np()) ?