The Gaudi Framework  v38r0 (2143aa4c)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AlgContextSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // ============================================================================
12 // Include files
13 // ============================================================================
14 // Local
15 // ============================================================================
16 #include "AlgContextSvc.h"
17 // ============================================================================
18 // GaudiKernel
19 // ============================================================================
23 #include "GaudiKernel/MsgStream.h"
24 
25 // ============================================================================
33 // ============================================================================
38 // ============================================================================
39 // standard initialization of the service
40 // ============================================================================
41 StatusCode AlgContextSvc::initialize() {
42  // Initialize the base class
44  if ( sc.isFailure() ) { return sc; }
45  // Incident Service
46  if ( m_inc ) {
47  m_inc->removeListener( this );
48  m_inc.reset();
49  }
50  // perform more checks?
52  numSlots = ( 1 > numSlots ) ? 1 : numSlots;
53  if ( numSlots > 1000 ) {
54  warning() << "Num Slots are greater than 1000. Is this correct? numSlots=" << numSlots << endmsg;
55  numSlots = 1000;
56  warning() << "Setting numSlots to " << numSlots << endmsg;
57  }
58  m_inEvtLoop.resize( numSlots, 0 );
59 
60  if ( m_check ) {
61  m_inc = Service::service( "IncidentSvc", true );
62  if ( !m_inc ) {
63  error() << "Could not locate 'IncidentSvc'" << endmsg;
64  return StatusCode::FAILURE;
65  }
66  m_inc->addListener( this, IncidentType::BeginEvent );
67  m_inc->addListener( this, IncidentType::EndEvent );
68  }
69  if ( m_algorithms.get() && !m_algorithms->empty() ) {
70  warning() << "Non-empty stack of algorithms #" << m_algorithms->size() << endmsg;
71  }
72  return StatusCode::SUCCESS;
73 }
74 
75 // implementation of start
76 // needs to be removed once we have a proper service
77 // for getting configuration information at initialization time
78 // S. Kama
80  auto sc = Service::start();
82  numSlots = ( 1 > numSlots ) ? 1 : numSlots;
83  if ( numSlots > 1000 ) {
84  warning() << "Num Slots are greater than 1000. Is this correct? numSlots=" << numSlots << endmsg;
85  numSlots = 1000;
86  }
87  m_inEvtLoop.resize( numSlots, 0 );
88 
89  return sc;
90 }
91 
92 // ============================================================================
93 // standard finalization of the service @see IService
94 // ============================================================================
96  if ( m_algorithms.get() && !m_algorithms->empty() ) {
97  warning() << "Non-empty stack of algorithms #" << m_algorithms->size() << endmsg;
98  }
99  // Incident Service
100  if ( m_inc ) {
101  m_inc->removeListener( this );
102  m_inc.reset();
103  }
104  // finalize the base class
105  return Service::finalize();
106 }
107 // ============================================================================
108 // set the currently executing algorithm ("push_back") @see IAlgContextSvc
109 // ============================================================================
111  if ( !a ) {
112  warning() << "IAlgorithm* points to NULL" << endmsg;
114  }
115  if ( !m_bypassInc ) {
116  if ( !m_inEvtLoop[context.valid() ? context.slot() : 0] ) return StatusCode::SUCCESS;
117  }
118  // check whether thread-local algorithm list already exists
119  // if not, create it
120  if ( !m_algorithms.get() ) { m_algorithms.reset( new IAlgContextSvc::Algorithms() ); }
121  if ( a->type() != "IncidentProcAlg" ) m_algorithms->push_back( a );
122 
123  return StatusCode::SUCCESS;
124 }
125 // ============================================================================
126 // remove the algorithm ("pop_back") @see IAlgContextSvc
127 // ============================================================================
129  // check whether thread-local algorithm list already exists
130  // if not, create it
131  if ( !m_algorithms.get() ) { m_algorithms.reset( new IAlgContextSvc::Algorithms() ); }
132 
133  if ( !a ) {
134  warning() << "IAlgorithm* points to NULL" << endmsg;
136  }
137 
138  if ( !m_bypassInc ) {
139  if ( !m_inEvtLoop[context.valid() ? context.slot() : 0] ) return StatusCode::SUCCESS;
140  }
141 
142  if ( a->type() != "IncidentProcAlg" ) {
143  // if ( m_algorithms->empty() || m_algorithms->back() != a ){
144  // error() << "Algorithm stack is invalid" << endmsg ;
145  // return StatusCode::FAILURE ;
146  // }
147  if ( !m_algorithms->empty() ) {
148  if ( m_algorithms->back() == a ) { m_algorithms->pop_back(); }
149  }
150  }
151  return StatusCode::SUCCESS;
152 }
153 // ============================================================================
155 // ============================================================================
157  return ( m_algorithms.get() && !m_algorithms->empty() ) ? m_algorithms->back() : nullptr;
158 }
159 // ============================================================================
160 // handle incident @see IIncidentListener
161 // ============================================================================
162 void AlgContextSvc::handle( const Incident& inc ) {
163  // some false sharing is possible but it should be negligable
164  auto currSlot = inc.context().slot();
165  if ( currSlot == EventContext::INVALID_CONTEXT_ID ) { currSlot = 0; }
166  if ( inc.type() == "BeginEvent" ) {
167  m_inEvtLoop[currSlot] = 1;
168  } else if ( inc.type() == "EndEvent" ) {
169  m_inEvtLoop[currSlot] = 0;
170  }
171 
172  // This check is invalidated with RTTI AlgContext object.
173  // Whole service needs to be rewritten. Commenting the error until then
174  // to prevent test failures.
175  // if ( m_algorithms.get() && !m_algorithms->empty() ) {
176  // //skip incident processing algorithm endevent incident
177  // if((m_algorithms->size()!=1) ||
178  // (m_algorithms->back()->type()!="IncidentProcAlg")){
179  // error() << "Non-empty stack of algorithms #"
180  // << m_algorithms->size() << endmsg ;
181  // }
182  // }
183 }
184 // ============================================================================
185 // ============================================================================
187 // ============================================================================
EventContext::valid
bool valid() const
Definition: EventContext.h:54
std::vector::resize
T resize(T... args)
AlgContextSvc::currentAlg
IAlgorithm * currentAlg() const override
accessor to current algorithm:
Definition: AlgContextSvc.cpp:156
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
AlgContextSvc::m_inc
SmartIF< IIncidentSvc > m_inc
pointer to Incident Service
Definition: AlgContextSvc.h:78
AlgContextSvc::handle
void handle(const Incident &) override
handle incident
Definition: AlgContextSvc.cpp:162
AlgContextSvc::m_inEvtLoop
std::vector< int > m_inEvtLoop
Definition: AlgContextSvc.h:83
AlgContextSvc::start
StatusCode start() override
Definition: AlgContextSvc.cpp:79
Service::start
StatusCode start() override
Definition: Service.cpp:187
std::vector
STL class.
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
AlgContextSvc::setCurrentAlg
StatusCode setCurrentAlg(IAlgorithm *a, const EventContext &context) override
set the currently executing algorithm ("push_back")
Definition: AlgContextSvc.cpp:110
AlgContextSvc::m_algorithms
boost::thread_specific_ptr< IAlgContextSvc::Algorithms > m_algorithms
the stack of current algorithms
Definition: AlgContextSvc.h:76
AlgContextSvc.h
EventContext::INVALID_CONTEXT_ID
static constexpr ContextID_t INVALID_CONTEXT_ID
Definition: EventContext.h:39
IAlgorithm::type
virtual const std::string & type() const =0
The type of the algorithm.
ConcurrencyFlags.h
AlgContextSvc::m_bypassInc
Gaudi::Property< bool > m_bypassInc
Definition: AlgContextSvc.h:81
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:222
IIncidentSvc.h
Gaudi::Concurrency::ConcurrencyFlags::numConcurrentEvents
static GAUDI_API std::size_t numConcurrentEvents()
number of Concurrent Events (for MT)
Definition: ConcurrencyFlags.h:57
Incident::context
EventContext context() const
Access to the EventContext of the source of the incident.
Definition: Incident.h:60
StatusCode
Definition: StatusCode.h:65
IAlgorithm
Definition: IAlgorithm.h:38
EventContext::slot
ContextID_t slot() const
Definition: EventContext.h:51
AlgContextSvc::unSetCurrentAlg
StatusCode unSetCurrentAlg(IAlgorithm *a, const EventContext &context) override
remove the algorithm ("pop_back")
Definition: AlgContextSvc.cpp:128
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
AlgContextSvc::finalize
StatusCode finalize() override
standard finalization of the service
Definition: AlgContextSvc.cpp:95
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
AlgContextSvc
Definition: AlgContextSvc.h:43
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
EventContext
Definition: EventContext.h:34
Incident::type
const std::string & type() const
Access to the incident type.
Definition: Incident.h:48
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ISvcLocator.h
StatusCode::RECOVERABLE
constexpr static const auto RECOVERABLE
Definition: StatusCode.h:102
Incident
Definition: Incident.h:27
Service::service
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:88
MsgStream.h