libzypp  17.34.1
threaddata.cc
Go to the documentation of this file.
1 #include "private/threaddata_p.h"
4 #include <ostream> //for std::endl
5 #include <sstream>
6 #include <pthread.h>
7 
8 namespace 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) {
37  }
38  return sp;
39  }
40 
41  void ThreadData::setDispatcher( const std::shared_ptr<EventDispatcher> &disp )
42  {
43  if ( _dispatcher.lock() ) {
44  WAR << "Dispatcher was already created for the current thread" << std::endl;
45  return;
46  }
47  _dispatcher = disp;
48  }
49 
51  {
52  // length is restricted to 16 characters, including the terminating null byte ('\0')
53  pthread_setname_np( _nativeHandle, name().substr(0,15).c_str() );
54  }
55 }
std::thread::id _threadId
Definition: threaddata_p.h:35
static std::shared_ptr< EventDispatcher > create()
std::string _threadName
lazy initialized to _threadId if unset
Definition: threaddata_p.h:36
const std::string & name() const
Definition: threaddata.cc:22
Definition: Arch.h:363
std::thread::native_handle_type _nativeHandle
Definition: threaddata_p.h:37
#define WAR
Definition: Logger.h:99
std::weak_ptr< EventDispatcher > _dispatcher
Definition: threaddata_p.h:38
static ThreadData & current()
Definition: threaddata.cc:16
std::shared_ptr< EventDispatcher > ensureDispatcher()
Definition: threaddata.cc:32
void setDispatcher(const std::shared_ptr< EventDispatcher > &disp)
Definition: threaddata.cc:41