| # Gaudi Framework, version v21r11 |

Home |  | Generated: 30 Sep 2010

# ThreadGaudi.cpp

Go to the documentation of this file.

    00001 // -*-C++-*-
    00002 // $Id: ThreadGaudi.cpp,v 1.4 2006/04/20 16:42:58 hmd Exp $
    00003 // $Name:  $
    00004
    00005 #include "GaudiKernel/ThreadGaudi.h"
    00006
    00007 #include <sstream>
    00008
    00009 #include <iostream>
    00010
    00011 const std::string threadSeparator = "__" ;
    00012 const std::string algToolSeparator = "." ;
    00013
    00014 //
    00015 //   getGaudiThreadIDfromName
    00016 //   ------------------------
    00017
    00018 std::string getGaudiThreadIDfromID(int iCopy) {
    00019   std::ostringstream ost;
    00020   ost << threadSeparator << iCopy << std::ends;
    00021   return ost.str();
    00022 }
    00023
    00024 //
    00025 //   getGaudiThreadIDfromName
    00026 //   ------------------------
    00027
    00028 std::string getGaudiThreadIDfromName(const std::string& name) {
    00029   std::string threadAppendix = "" ;
    00030
    00031   // find parent if name of an AlgTool
    00032   std::string parent_name = name ;
    00033   std::string tool_name  = "" ;
    00034   size_t pp = name.find(".") ;
    00035   if ( (pp > 0) && (pp <= name.length()) ) {
    00036     parent_name = name.substr(0,name.find(".")-1) ;
    00037     tool_name = name.substr(name.find("."));
    00038   }
    00039
    00040   // get from (parent_)name thread ID
    00041   pp = parent_name.find(threadSeparator) ;
    00042   if ( (pp > 0) && (pp <= parent_name.length()) ) {
    00043     threadAppendix = parent_name.substr(parent_name.find(threadSeparator)) ;
    00044   }
    00045   return threadAppendix ;
    00046 }
    00047
    00048 //
    00049 //   getGaudiThreadGenericName
    00050 //   -------------------------
    00051
    00052 std::string getGaudiThreadGenericName(const std::string& name) {
    00053   std::string genericName = name ;
    00054
    00055   // find parent if name of an AlgTool
    00056   std::string parent_name = name ;
    00057   std::string tool_name  = "" ;
    00058   size_t pp = name.find(".") ;
    00059   if ( (pp > 0) && (pp <= name.length()) ) {
    00060     parent_name = name.substr(0,name.find(".")-1) ;
    00061     tool_name = name.substr(name.find("."));
    00062   }
    00063
    00064   // construct gneric name
    00065   pp = parent_name.find(threadSeparator) ;
    00066   if ( (pp > 0) && (pp <= parent_name.length()) ) {
    00067     genericName = parent_name.substr(0,parent_name.find(threadSeparator))+tool_name ;
    00068   }
    00069   return genericName ;
    00070 }
    00071
    00072 //
    00073 //  isGaudiThreaded
    00074 //
    00075 bool isGaudiThreaded(const std::string& name) {
    00076   return  (!(getGaudiThreadIDfromName(name).empty()));
    00077 }
    00078
    00079 //
    00080 //   ThreadGaudi
    00081 //   -----------
    00082
    00083 ThreadGaudi* ThreadGaudiInstance = 0 ;
    00084
    00085 ThreadGaudi::ThreadGaudi() {
    00086   m_threadMap = new ThreadMap();
    00087   (*m_threadMap)[0] = "" ;
    00088 }
    00089
    00090 ThreadGaudi::~ThreadGaudi() {
    00091   delete m_threadMap;
    00092 }
    00093
    00094 ThreadGaudi* ThreadGaudi::instance() {
    00095   if ( 0 == ThreadGaudiInstance ) {
    00096     ThreadGaudiInstance = new ThreadGaudi();
    00097   }
    00098   return ThreadGaudiInstance ;
    00099 }
    00100
    00101 void ThreadGaudi::setThreadID(const std::string& threadID) {
    00102   ThreadMap* p_threadMap = getThreadMap() ;
    00103   // get from name thread ID
    00104   System::ThreadHandle s_pid = System::threadSelf() ;
    00105   if (p_threadMap->count(s_pid) == 0 ) {
    00106     (*p_threadMap)[s_pid] = threadID ;
    00107 #ifdef THREAD_GAUDI__DEBUG
    00108     std::cout << " *** ThreadGaudi setThreadID *** value set for *** " << threadID << " thread ID : " << s_pid << std::endl ;
    00109 #endif
    00110   }
    00111 }
    00112
    00113 ThreadGaudi::ThreadMap* ThreadGaudi::getThreadMap() {
    00114   return ThreadGaudi::instance()->m_threadMap;
    00115 }
    00116
    00117 const std::string& ThreadGaudi::getThreadID() {
    00118   ThreadMap* p_threadMap = getThreadMap() ;
    00119   System::ThreadHandle s_pid = System::threadSelf() ;
    00120   if ( p_threadMap->find(s_pid) != p_threadMap->end() ) {
    00121     return (*p_threadMap->find(s_pid)).second ;
    00122   } else {
    00123     return (*p_threadMap->find(0)).second ; ;
    00124   }
    00125 }