libzypp: zypp-core/ng/base/threaddata.cc Source File

libzypp 17.38.15

threaddata.cc
Go to the documentation of this file.
1#include "private/threaddata_p.h"
2#include "private/eventdispatcher_glib_p.h"
3#include <zypp-core/base/Logger.h>
4#include <ostream> //for std::endl
5#include <sstream>
6#include <pthread.h>
7
8namespace zyppng
9{
10 ThreadData::ThreadData()
11 : _threadId( std::this_thread::get_id() ),
12 _nativeHandle( pthread_self() )
13 {
14 }
15
16 ThreadData &ThreadData::current()
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( GMainContext *ctx )
33 {
34 auto sp = _dispatcher.lock();
35 if (!sp) {
36 MIL << "Creating the Event Dispatcher for thread: " << name() << "("<<_threadId<<")" << std::endl;
37 _dispatcher = sp = EventDispatcherPrivate::create( ctx );
38 } else {
39 if ( ctx && ctx != sp->glibContext() ) MIL << "Ignoring passed GMainContext, because a Zypp Event Dispatcher was created before. This might be a bug!" << std::endl;
40 }
41 return sp;
42 }
43
44 void ThreadData::setDispatcher( const std::shared_ptr<EventDispatcher> &disp )
45 {
46 if ( _dispatcher.lock() ) {
47 WAR << "Dispatcher was already created for the current thread" << std::endl;
48 return;
49 }
50 _dispatcher = disp;
51 }
52
53 void ThreadData::syncNativeName()
54 {
55 // length is restricted to 16 characters, including the terminating null byte ('\0')
56 pthread_setname_np( _nativeHandle, name().substr(0,15).c_str() );
57 }
58
59 std::shared_ptr<EventDispatcher> ThreadData::dispatcher() {
60 auto sp = _dispatcher.lock();
61 if (!sp) {
62 MIL << "Requested Event Dispatcher for thread: " << name() << "("<<_threadId<<") but none was created." << std::endl;
63 }
64 return sp;
65 }
66} // namespace zyppng
Logger.h
MIL
#define MIL
Definition Logger.h:130
WAR
#define WAR
Definition Logger.h:131
zyppng::EventDispatcherPrivate::create
static std::shared_ptr< EventDispatcher > create(GMainContext *ctx=0)
Definition eventdispatcher_glib.cc:341
GMainContext
struct _GMainContext GMainContext
Definition eventdispatcher.h:24
eventdispatcher_glib_p.h
std
Definition ansi.h:855
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::ThreadData::dispatcher
std::shared_ptr< EventDispatcher > dispatcher()
Definition threaddata.cc:59
zyppng::ThreadData::ThreadData
ThreadData()
Definition threaddata.cc:10
zyppng::ThreadData::syncNativeName
void syncNativeName()
Definition threaddata.cc:53
zyppng::ThreadData::_dispatcher
std::weak_ptr< EventDispatcher > _dispatcher
Definition threaddata_p.h:40
zyppng::ThreadData::current
static ZYPP_API ThreadData & current()
Definition threaddata.cc:16
zyppng::ThreadData::name
const std::string & name() const
Definition threaddata.cc:22
zyppng::ThreadData::_threadName
std::string _threadName
lazy initialized to _threadId if unset
Definition threaddata_p.h:38
zyppng::ThreadData::setDispatcher
void setDispatcher(const std::shared_ptr< EventDispatcher > &disp)
Definition threaddata.cc:44
zyppng::ThreadData::_nativeHandle
std::thread::native_handle_type _nativeHandle
Definition threaddata_p.h:39
zyppng::ThreadData::_threadId
std::thread::id _threadId
Definition threaddata_p.h:37
zyppng::ThreadData::ensureDispatcher
std::shared_ptr< EventDispatcher > ensureDispatcher(GMainContext *ctx=0)
Definition threaddata.cc:32
threaddata_p.h