libzypp 17.37.17
threaddata.cc
Go to the documentation of this file.
4#include <ostream> //for std::endl
5#include <sstream>
6#include <pthread.h>
7
8namespace zyppng
9{
11 : _threadId( std::this_thread::get_id() ),
12 _nativeHandle( pthread_self() )
13 {
14 }
15
17 {
18 static thread_local ThreadData data;
19 return data;
20 }
21
22 const std::string &ThreadData::name() const
23 {
24 if ( _threadName.empty() ) {
25 std::stringstream strStr;
26 strStr << _threadId;
27 _threadName = strStr.str();
28 }
29 return _threadName;
30 }
31
32 std::shared_ptr<EventDispatcher> ThreadData::ensureDispatcher()
33 {
34 auto sp = _dispatcher.lock();
35 if (!sp) {
36 MIL << "Creating the Event Dispatcher for thread: " << name() << "("<<_threadId<<")" << std::endl;
38 }
39 return sp;
40 }
41
42 void ThreadData::setDispatcher( const std::shared_ptr<EventDispatcher> &disp )
43 {
44 if ( _dispatcher.lock() ) {
45 WAR << "Dispatcher was already created for the current thread" << std::endl;
46 return;
47 }
48 _dispatcher = disp;
49 }
50
52 {
53 // length is restricted to 16 characters, including the terminating null byte ('\0')
54 pthread_setname_np( _nativeHandle, name().substr(0,15).c_str() );
55 }
56
57 std::shared_ptr<EventDispatcher> ThreadData::dispatcher() {
58 auto sp = _dispatcher.lock();
59 if (!sp) {
60 MIL << "Requested Event Dispatcher for thread: " << name() << "("<<_threadId<<") but none was created." << std::endl;
61 }
62 return sp;
63 }
64} // namespace zyppng
static std::shared_ptr< EventDispatcher > create()
Definition Arch.h:364
std::shared_ptr< EventDispatcher > dispatcher()
Definition threaddata.cc:57
std::weak_ptr< EventDispatcher > _dispatcher
static ZYPP_API ThreadData & current()
Definition threaddata.cc:16
const std::string & name() const
Definition threaddata.cc:22
std::string _threadName
lazy initialized to _threadId if unset
void setDispatcher(const std::shared_ptr< EventDispatcher > &disp)
Definition threaddata.cc:42
std::thread::native_handle_type _nativeHandle
std::shared_ptr< EventDispatcher > ensureDispatcher()
Definition threaddata.cc:32
std::thread::id _threadId
#define MIL
Definition Logger.h:100
#define WAR
Definition Logger.h:101