The Gaudi Framework  v32r0 (3325bb39)
AlgoExecutionTask.cpp
Go to the documentation of this file.
1 // local includes
2 #include "AlgoExecutionTask.h"
3 #include "RetCodeGuard.h"
4 
5 // Framework
11 #include <Gaudi/Algorithm.h>
12 
13 #include <functional>
14 
15 namespace Gaudi {
16  namespace Concurrency {
17  extern thread_local bool ThreadInitDone;
18  }
19 } // namespace Gaudi
20 
22 
23  // Get queue data
25  if ( !m_scheduler->m_scheduledQueue.try_pop( queuePop ) ) return nullptr;
26 
27  std::string algName = m_scheduler->index2algname( queuePop.algIndex );
28  EventContext& evtCtx = *queuePop.contextPtr;
29  IAlgorithm* iAlgoPtr = queuePop.algPtr;
30 
31  Gaudi::Algorithm* this_algo = dynamic_cast<Gaudi::Algorithm*>( iAlgoPtr );
32  if ( !this_algo ) { throw GaudiException( "Cast to Algorithm failed!", "AlgoExecutionTask", StatusCode::FAILURE ); }
33 
34  bool eventfailed = false;
36 
37  // Get the IProperty interface of the ApplicationMgr to pass it to RetCodeGuard
38  const SmartIF<IProperty> appmgr( m_serviceLocator );
39 
40  SmartIF<IMessageSvc> messageSvc( m_serviceLocator );
41  MsgStream log( messageSvc, "AlgoExecutionTask" );
42 
44  log << MSG::DEBUG << "New thread detected: 0x" << std::hex << pthread_self() << std::dec
45  << ". Doing thread local initialization." << endmsg;
47  tps = m_serviceLocator->service( "ThreadPoolSvc" );
48  if ( !tps.isValid() ) {
49  log << MSG::ERROR << "unable to get the ThreadPoolSvc to trigger thread local initialization" << endmsg;
50  throw GaudiException( "retrieval of ThrePoolSvc failed", "AlgoExecutionTask", StatusCode::FAILURE );
51  }
52 
53  tps->initThisThread();
54  }
55 
56  // select the appropriate store
57  this_algo->whiteboard()->selectStore( evtCtx.valid() ? evtCtx.slot() : 0 ).ignore();
59  try {
61  sc = iAlgoPtr->sysExecute( evtCtx );
62  if ( UNLIKELY( !sc.isSuccess() ) ) {
63  log << MSG::WARNING << "Execution of algorithm " << algName << " failed" << endmsg;
64  eventfailed = true;
65  }
66  rcg.ignore(); // disarm the guard
67  } catch ( const GaudiException& Exception ) {
68  log << MSG::FATAL << ".executeEvent(): Exception with tag=" << Exception.tag() << " thrown by " << algName
69  << endmsg;
70  log << MSG::ERROR << Exception << endmsg;
71  eventfailed = true;
72  } catch ( const std::exception& Exception ) {
73  log << MSG::FATAL << ".executeEvent(): Standard std::exception thrown by " << algName << endmsg;
74  log << MSG::ERROR << Exception.what() << endmsg;
75  eventfailed = true;
76  } catch ( ... ) {
77  log << MSG::FATAL << ".executeEvent(): UNKNOWN Exception thrown by " << algName << endmsg;
78  eventfailed = true;
79  }
80 
81  // DP it is important to propagate the failure of an event.
82  // We need to stop execution when this happens so that execute run can
83  // then receive the FAILURE
84  m_aess->updateEventStatus( eventfailed, evtCtx );
85 
86  // Release algorithm
87  StatusCode releaseAlgSC = m_scheduler->m_algResourcePool->releaseAlgorithm( algName, iAlgoPtr );
88  if ( !releaseAlgSC.isSuccess() ) {
89  log << MSG::ERROR << "[Event " << evtCtx.evt() << ", Slot " << evtCtx.slot() << "] "
90  << "Instance of algorithm " << algName << " could not be properly put back." << endmsg;
91  }
92 
93  // Create a lambda to update scheduler state
94  m_scheduler->m_actionsQueue.push( [this, queuePop]() -> StatusCode {
95  return this->m_scheduler->promoteToExecuted( queuePop.algIndex, queuePop.slotIndex, queuePop.contextPtr );
96  } );
97 
99 
100  return nullptr;
101 }
#define UNLIKELY(x)
Definition: Kernel.h:89
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Define general base for Gaudi exception.
Helper class to set the application return code in case of early exit (e.g.
Definition: RetCodeGuard.h:9
unsigned int algIndex
ContextID_t slot() const
Definition: EventContext.h:41
int slotIndex
bool isSuccess() const
Definition: StatusCode.h:267
SmartIF< IHiveWhiteBoard > & whiteboard() const
Definition: Algorithm.cpp:685
virtual void initThisThread()=0
Do thread local initialization of current thread.
EventContext * contextPtr
Struct to hold entries in the alg queues.
This class represents an entry point to all the event specific data.
Definition: EventContext.h:24
tbb::task * execute() override
ContextEvt_t evt() const
Definition: EventContext.h:40
STL class.
constexpr int UnhandledException
Definition: AppReturnCode.h:27
virtual StatusCode selectStore(size_t partitionIndex)=0
Activate an given &#39;slot&#39; for all subsequent calls within the same thread id.
T what(T...args)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
virtual StatusCode sysExecute(const EventContext &)=0
System execution. This method invokes the execute() method of a concrete algorithm.
virtual const std::string & tag() const
name tag for the exception, or exception type
STL class.
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:28
GAUDI_API void setCurrentContext(const EventContext *ctx)
thread_local bool ThreadInitDone
IAlgorithm * algPtr
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:62
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:79
constexpr static const auto FAILURE
Definition: StatusCode.h:86
GAUDI_API void setCurrentContextEvt(long int evtN)
T hex(T...args)
void ignore()
Definition: RetCodeGuard.h:12
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
bool valid() const
Definition: EventContext.h:44