| The Gaudi Framework v27r1

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