All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ThreadGaudi.cpp
Go to the documentation of this file.
1 // -*-C++-*-
2 
4 
5 #include <sstream>
6 
7 #include <iostream>
8 
11 
12 //
13 // getGaudiThreadIDfromName
14 // ------------------------
15 
18  ost << threadSeparator << iCopy << std::ends;
19  return ost.str();
20 }
21 
22 //
23 // getGaudiThreadIDfromName
24 // ------------------------
25 
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 
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 //
74  return (!(getGaudiThreadIDfromName(name).empty()));
75 }
76 
77 //
78 // ThreadGaudi
79 // -----------
80 
82 
84  m_threadMap{ new ThreadMap() }
85 {
86  (*m_threadMap)[0] = "" ;
87 }
88 
89 
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
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 
108  return ThreadGaudi::instance()->m_threadMap.get();
109 }
110 
112  ThreadMap* p_threadMap = getThreadMap() ;
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 }
std::unique_ptr< ThreadMap > m_threadMap
Definition: ThreadGaudi.h:30
static ThreadGaudi * instance()
singleton access
Definition: ThreadGaudi.cpp:90
const std::string & getThreadID()
get Gaudi ID of current pthread
void * ThreadHandle
A Thread handle.
Definition: System.h:110
ThreadGaudi * ThreadGaudiInstance
Definition: ThreadGaudi.cpp:81
T endl(T...args)
std::map< System::ThreadHandle, std::string > ThreadMap
Definition: ThreadGaudi.h:17
std::string getGaudiThreadIDfromID(int iCopy)
helper function to extract Gaudi Thread ID from thread copy number
Definition: ThreadGaudi.cpp:16
T end(T...args)
std::string getGaudiThreadGenericName(const std::string &name)
helper function to extract Gaudi instance name from thread copy name
Definition: ThreadGaudi.cpp:50
STL class.
singleton mapping the pthread ID to the Gaudi thread ID
Definition: ThreadGaudi.h:15
constexpr double second
STL class.
ThreadMap * getThreadMap()
access the whole map
void setThreadID(const std::string &threadID)
associate Gaudi ID to pthread
Definition: ThreadGaudi.cpp:95
const std::string threadSeparator
Definition: ThreadGaudi.cpp:9
const std::string algToolSeparator
Definition: ThreadGaudi.cpp:10
T count(T...args)
T find(T...args)
T length(T...args)
ThreadHandle threadSelf()
thread handle "accessor"
Definition: System.h:112
T ends(T...args)
bool isGaudiThreaded(const std::string &name)
test if current Gaudi object is running /will run in a thread
Definition: ThreadGaudi.cpp:73
T substr(T...args)
std::string getGaudiThreadIDfromName(const std::string &name)
helper function to extract Gaudi Thread ID from thread copy name
Definition: ThreadGaudi.cpp:26