| The Gaudi Framework v29r0 (ff2e7097)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

ThreadGaudi.cpp
Go to the documentation of this file.
1 // -*-C++-*-
2
3 #include "GaudiKernel/ThreadGaudi.h"
4
5 #include <sstream>
6
7 #include <iostream>
8
9 const std::string threadSeparator = "__";
10 const std::string algToolSeparator = ".";
11
12 //
13 // getGaudiThreadIDfromName
14 // ------------------------
15
16 std::string getGaudiThreadIDfromID( int iCopy )
17 {
18 std::ostringstream ost;
19 ost << threadSeparator << iCopy << std::ends;
20 return ost.str();
21 }
22
23 //
24 // getGaudiThreadIDfromName
25 // ------------------------
26
27 std::string getGaudiThreadIDfromName( const std::string& name )
28 {
29 std::string threadAppendix = "";
30
31 // find parent if name of an AlgTool
32 std::string parent_name = name;
33 std::string tool_name = "";
34 size_t pp = name.find( "." );
35 if ( ( pp > 0 ) && ( pp <= name.length() ) ) {
36 parent_name = name.substr( 0, name.find( "." ) - 1 );
37 tool_name = name.substr( name.find( "." ) );
38 }
39
40 // get from (parent_)name thread ID
41 pp = parent_name.find( threadSeparator );
42 if ( ( pp > 0 ) && ( pp <= parent_name.length() ) ) {
43 threadAppendix = parent_name.substr( parent_name.find( threadSeparator ) );
44 }
45 return threadAppendix;
46 }
47
48 //
49 // getGaudiThreadGenericName
50 // -------------------------
51
52 std::string getGaudiThreadGenericName( const std::string& name )
53 {
54 std::string genericName = name;
55
56 // find parent if name of an AlgTool
57 std::string parent_name = name;
58 std::string tool_name = "";
59 size_t pp = name.find( "." );
60 if ( ( pp > 0 ) && ( pp <= name.length() ) ) {
61 parent_name = name.substr( 0, name.find( "." ) - 1 );
62 tool_name = name.substr( name.find( "." ) );
63 }
64
65 // construct gneric name
66 pp = parent_name.find( threadSeparator );
67 if ( ( pp > 0 ) && ( pp <= parent_name.length() ) ) {
68 genericName = parent_name.substr( 0, parent_name.find( threadSeparator ) ) + tool_name;
69 }
70 return genericName;
71 }
72
73 //
74 // isGaudiThreaded
75 //
76 bool isGaudiThreaded( const std::string& name ) { return ( !( getGaudiThreadIDfromName( name ).empty() ) ); }
77
78 //
79 // ThreadGaudi
80 // -----------
81
82 ThreadGaudi* ThreadGaudiInstance = nullptr;
83
84 ThreadGaudi::ThreadGaudi() : m_threadMap{new ThreadMap()} { ( *m_threadMap )[0] = ""; }
85
86 ThreadGaudi* ThreadGaudi::instance()
87 {
88 if ( !ThreadGaudiInstance ) ThreadGaudiInstance = new ThreadGaudi();
89 return ThreadGaudiInstance;
90 }
91
92 void ThreadGaudi::setThreadID( const std::string& threadID )
93 {
94 ThreadMap* p_threadMap = getThreadMap();
95 // get from name thread ID
96 System::ThreadHandle s_pid = System::threadSelf();
97 if ( p_threadMap->count( s_pid ) == 0 ) {
98 ( *p_threadMap )[s_pid] = threadID;
99 #ifdef THREAD_GAUDI__DEBUG
100 std::cout << " *** ThreadGaudi setThreadID *** value set for *** " << threadID << " thread ID : " << s_pid
101 << std::endl;
102 #endif
103 }
104 }
105
106 ThreadGaudi::ThreadMap* ThreadGaudi::getThreadMap() { return ThreadGaudi::instance()->m_threadMap.get(); }
107
108 const std::string& ThreadGaudi::getThreadID()
109 {
110 ThreadMap* p_threadMap = getThreadMap();
111 System::ThreadHandle s_pid = System::threadSelf();
112 if ( p_threadMap->find( s_pid ) != p_threadMap->end() ) {
113 return ( *p_threadMap->find( s_pid ) ).second;
114 } else {
115 return ( *p_threadMap->find( 0 ) ).second;
116 ;
117 }
118 }
ThreadGaudi::m_threadMap
std::unique_ptr< ThreadMap > m_threadMap
Definition: ThreadGaudi.h:29
ThreadGaudi::instance
static ThreadGaudi * instance()
singleton access
Definition: ThreadGaudi.cpp:86
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
ThreadGaudi::getThreadID
const std::string & getThreadID()
get Gaudi ID of current pthread
Definition: ThreadGaudi.cpp:108
System::ThreadHandle
void * ThreadHandle
A Thread handle.
Definition: System.h:109
ThreadGaudiInstance
ThreadGaudi * ThreadGaudiInstance
Definition: ThreadGaudi.cpp:82
std::endl
T endl(T...args)
getGaudiThreadIDfromID
std::string getGaudiThreadIDfromID(int iCopy)
helper function to extract Gaudi Thread ID from thread copy number
Definition: ThreadGaudi.cpp:16
std::map::end
T end(T...args)
getGaudiThreadGenericName
std::string getGaudiThreadGenericName(const std::string &name)
helper function to extract Gaudi instance name from thread copy name
Definition: ThreadGaudi.cpp:52
std::map
STL class.
ThreadGaudi.h
ThreadGaudi
singleton mapping the pthread ID to the Gaudi thread ID
Definition: ThreadGaudi.h:13
Gaudi::Units::second
constexpr double second
Definition: SystemOfUnits.h:123
std::string
STL class.
ThreadGaudi::getThreadMap
ThreadMap * getThreadMap()
access the whole map
Definition: ThreadGaudi.cpp:106
ThreadGaudi::ThreadMap
std::map< System::ThreadHandle, std::string > ThreadMap
Definition: ThreadGaudi.h:16
std::ostringstream
STL class.
std::ostringstream::str
T str(T...args)
ThreadGaudi::setThreadID
void setThreadID(const std::string &threadID)
associate Gaudi ID to pthread
Definition: ThreadGaudi.cpp:92
ThreadGaudi::ThreadGaudi
ThreadGaudi()
Definition: ThreadGaudi.cpp:84
threadSeparator
const std::string threadSeparator
Definition: ThreadGaudi.cpp:9
algToolSeparator
const std::string algToolSeparator
Definition: ThreadGaudi.cpp:10
std::map::count
T count(T...args)
std::string::find
T find(T...args)
std::string::length
T length(T...args)
System::threadSelf
ThreadHandle threadSelf()
thread handle "accessor"
Definition: System.h:111
std::ends
T ends(T...args)
compareOutputFiles.pp
pp
Definition: compareOutputFiles.py:465
isGaudiThreaded
bool isGaudiThreaded(const std::string &name)
test if current Gaudi object is running /will run in a thread
Definition: ThreadGaudi.cpp:76
std::string::substr
T substr(T...args)
getGaudiThreadIDfromName
std::string getGaudiThreadIDfromName(const std::string &name)
helper function to extract Gaudi Thread ID from thread copy name
Definition: ThreadGaudi.cpp:27
std::cout