All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ThreadGaudi.cpp
Go to the documentation of this file.
1 // -*-C++-*-
2 // $Id: ThreadGaudi.cpp,v 1.4 2006/04/20 16:42:58 hmd Exp $
3 // $Name: $
4 
6 
7 #include <sstream>
8 
9 #include <iostream>
10 
11 const std::string threadSeparator = "__" ;
12 const std::string algToolSeparator = "." ;
13 
14 //
15 // getGaudiThreadIDfromName
16 // ------------------------
17 
18 std::string getGaudiThreadIDfromID(int iCopy) {
19  std::ostringstream ost;
20  ost << threadSeparator << iCopy << std::ends;
21  return ost.str();
22 }
23 
24 //
25 // getGaudiThreadIDfromName
26 // ------------------------
27 
28 std::string getGaudiThreadIDfromName(const std::string& name) {
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  std::string genericName = name ;
54 
55  // find parent if name of an AlgTool
56  std::string parent_name = name ;
57  std::string tool_name = "" ;
58  size_t pp = name.find(".") ;
59  if ( (pp > 0) && (pp <= name.length()) ) {
60  parent_name = name.substr(0,name.find(".")-1) ;
61  tool_name = name.substr(name.find("."));
62  }
63 
64  // construct gneric name
65  pp = parent_name.find(threadSeparator) ;
66  if ( (pp > 0) && (pp <= parent_name.length()) ) {
67  genericName = parent_name.substr(0,parent_name.find(threadSeparator))+tool_name ;
68  }
69  return genericName ;
70 }
71 
72 //
73 // isGaudiThreaded
74 //
75 bool isGaudiThreaded(const std::string& name) {
76  return (!(getGaudiThreadIDfromName(name).empty()));
77 }
78 
79 //
80 // ThreadGaudi
81 // -----------
82 
84 
86  m_threadMap = new ThreadMap();
87  (*m_threadMap)[0] = "" ;
88 }
89 
91  delete m_threadMap;
92 }
93 
95  if ( 0 == ThreadGaudiInstance ) {
96  ThreadGaudiInstance = new ThreadGaudi();
97  }
98  return ThreadGaudiInstance ;
99 }
100 
101 void ThreadGaudi::setThreadID(const std::string& threadID) {
102  ThreadMap* p_threadMap = getThreadMap() ;
103  // get from name thread ID
105  if (p_threadMap->count(s_pid) == 0 ) {
106  (*p_threadMap)[s_pid] = threadID ;
107 #ifdef THREAD_GAUDI__DEBUG
108  std::cout << " *** ThreadGaudi setThreadID *** value set for *** " << threadID << " thread ID : " << s_pid << std::endl ;
109 #endif
110  }
111 }
112 
115 }
116 
117 const std::string& ThreadGaudi::getThreadID() {
118  ThreadMap* p_threadMap = getThreadMap() ;
120  if ( p_threadMap->find(s_pid) != p_threadMap->end() ) {
121  return (*p_threadMap->find(s_pid)).second ;
122  } else {
123  return (*p_threadMap->find(0)).second ; ;
124  }
125 }
static ThreadGaudi * instance()
singleton access
Definition: ThreadGaudi.cpp:94
virtual ~ThreadGaudi()
Definition: ThreadGaudi.cpp:90
const std::string & getThreadID()
get Gaudi ID of current pthread
ThreadMap * m_threadMap
Definition: ThreadGaudi.h:30
void * ThreadHandle
A Thread handle.
Definition: System.h:99
ThreadGaudi * ThreadGaudiInstance
Definition: ThreadGaudi.cpp:83
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:18
std::string getGaudiThreadGenericName(const std::string &name)
helper function to extract Gaudi instance name from thread copy name
Definition: ThreadGaudi.cpp:52
singleton mapping the pthread ID to the Gaudi thread ID
Definition: ThreadGaudi.h:15
ThreadMap * getThreadMap()
access the whole map
void setThreadID(const std::string &threadID)
associate Gaudi ID to pthread
const std::string threadSeparator
Definition: ThreadGaudi.cpp:11
const std::string algToolSeparator
Definition: ThreadGaudi.cpp:12
ThreadHandle threadSelf()
thread handle "accessor"
Definition: System.h:101
bool isGaudiThreaded(const std::string &name)
test if current Gaudi object is running /will run in a thread
Definition: ThreadGaudi.cpp:75
std::string getGaudiThreadIDfromName(const std::string &name)
helper function to extract Gaudi Thread ID from thread copy name
Definition: ThreadGaudi.cpp:28