The Gaudi Framework  v40r0 (475e45c1)
ServiceManager Class Reference

#include </builds/gaudi/Gaudi/GaudiCoreSvc/src/ApplicationMgr/ServiceManager.h>

Inheritance diagram for ServiceManager:
Collaboration diagram for ServiceManager:

Classes

struct  ServiceItem
 

Public Types

typedef std::list< ServiceItemListSvc
 
typedef std::map< std::string, std::string, std::less<> > MapType
 
- Public Types inherited from extends< ComponentManager, ISvcManager, ISvcLocator >
using base_class = extends
 Typedef to this class. More...
 
using extend_interfaces_base = extend_interfaces< Interfaces... >
 Typedef to the base of this class. More...
 
- Public Types inherited from CommonMessaging< implements< IComponentManager > >
using base_class = CommonMessaging
 
- Public Types inherited from extend_interfaces< Interfaces... >
using ext_iids = typename Gaudi::interface_list_cat< typename Interfaces::ext_iids... >::type
 take union of the ext_iids of all Interfaces... More...
 

Public Member Functions

 ServiceManager (IInterface *application)
 default creator More...
 
SmartIF< ISvcLocator > & serviceLocator () const override
 Function needed by CommonMessaging. More...
 
 ~ServiceManager () override
 virtual destructor More...
 
const std::list< IService * > & getServices () const override
 Return the list of Services. More...
 
bool existsService (std::string_view name) const override
 implementation of ISvcLocation::existsService More...
 
StatusCode addService (IService *svc, int prio=DEFAULT_SVC_PRIORITY) override
 implementation of ISvcManager::addService More...
 
StatusCode addService (const Gaudi::Utils::TypeNameString &typeName, int prio=DEFAULT_SVC_PRIORITY) override
 implementation of ISvcManager::addService More...
 
StatusCode removeService (IService *svc) override
 implementation of ISvcManager::removeService More...
 
StatusCode removeService (std::string_view name) override
 implementation of ISvcManager::removeService More...
 
StatusCode declareSvcType (std::string svcname, std::string svctype) override
 implementation of ISvcManager::declareSvcType More...
 
SmartIF< IService > & createService (const Gaudi::Utils::TypeNameString &nametype) override
 implementation of ISvcManager::createService NOTE: as this returns a &, we must guarantee that once created, these SmartIF remain pinned in their location, thus constraining the underlying implementation... More...
 
StatusCode initialize () override
 Initialization (from CONFIGURED to INITIALIZED). More...
 
StatusCode start () override
 Start (from INITIALIZED to RUNNING). More...
 
StatusCode stop () override
 Stop (from RUNNING to INITIALIZED). More...
 
StatusCode finalize () override
 Finalize (from INITIALIZED to CONFIGURED). More...
 
StatusCode reinitialize () override
 Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED). More...
 
StatusCode restart () override
 Initialization (from RUNNING to RUNNING, via INITIALIZED). More...
 
int getPriority (std::string_view name) const override
 manage priorities of services More...
 
StatusCode setPriority (std::string_view name, int pri) override
 
bool loopCheckEnabled () const override
 Get the value of the initialization loop check flag. More...
 
void setLoopCheckEnabled (bool en) override
 Set the value of the initialization loop check flag. More...
 
const std::string & name () const override
 Return the name of the manager (implementation of INamedInterface) More...
 
SmartIF< IService > & service (const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
 Returns a smart pointer to a service. More...
 
template<typename T >
SmartIF< T > service (const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)
 Returns a smart pointer to the requested interface of a service. More...
 
void outputLevelUpdate () override
 Function to call to update the outputLevel of the components (after a change in MessageSvc). More...
 
- Public Member Functions inherited from extends< ComponentManager, ISvcManager, ISvcLocator >
void const * i_cast (const InterfaceID &tid) const override
 Implementation of IInterface::i_cast. More...
 
StatusCode queryInterface (const InterfaceID &ti, void **pp) override
 Implementation of IInterface::queryInterface. More...
 
std::vector< std::string > getInterfaceNames () const override
 Implementation of IInterface::getInterfaceNames. More...
 
- Public Member Functions inherited from ComponentManager
 ComponentManager (IInterface *application, const InterfaceID &baseIID)
 Constructor. More...
 
const InterfaceIDcomponentBaseInterface () const override
 Basic interface id of the managed components. More...
 
StatusCode queryInterface (const InterfaceID &iid, void **pinterface) override
 Specialized queryInterface implementation. More...
 
void const * i_cast (const InterfaceID &iid) const override
 Specialized i_cast implementation. More...
 
SmartIF< ISvcLocator > & serviceLocator () const override
 
StatusCode configure () override
 Configuration (from OFFLINE to CONFIGURED). More...
 
StatusCode initialize () override
 Initialization (from CONFIGURED to INITIALIZED). More...
 
StatusCode start () override
 Start (from INITIALIZED to RUNNING). More...
 
StatusCode stop () override
 Stop (from RUNNING to INITIALIZED). More...
 
StatusCode finalize () override
 Finalize (from INITIALIZED to CONFIGURED). More...
 
StatusCode terminate () override
 Initialization (from CONFIGURED to OFFLINE). More...
 
StatusCode reinitialize () override
 Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED). More...
 
StatusCode restart () override
 Initialization (from RUNNING to RUNNING, via INITIALIZED). More...
 
Gaudi::StateMachine::State FSMState () const override
 Get the current state. More...
 
Gaudi::StateMachine::State targetFSMState () const override
 When we are in the middle of a transition, get the state where the transition is leading us. More...
 
- Public Member Functions inherited from CommonMessaging< implements< IComponentManager > >
MSG::Level msgLevel () const
 get the cached level (originally extracted from the embedded MsgStream) More...
 
bool msgLevel (MSG::Level lvl) const
 get the output level from the embedded MsgStream More...
 

Private Member Functions

ListSvc::iterator find (std::string_view name)
 
ListSvc::const_iterator find (std::string_view name) const
 
ListSvc::iterator find (const IService *ptr)
 
ListSvc::const_iterator find (const IService *ptr) const
 
void dump () const
 

Private Attributes

ListSvc m_listsvc
 List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because there can be SmartIF<T>& 'out there' that refer to these specific SmarIF<T>, we unfortunately must guarantee that they never move after creation. More...
 
MapType m_maptype
 Map of service name and service type. More...
 
bool m_loopCheck = true
 Check for service initialization loops. More...
 
SmartIF< IServicem_appSvc
 Pointer to the application IService interface. More...
 
std::list< IService * > m_listOfPtrs
 List of pointers to the know services used to implement getServices() More...
 
GaudiUtils::Map< InterfaceID, SmartIF< IInterface > > m_defaultImplementations
 
std::recursive_mutex m_gLock
 Mutex to synchronize shared service initialization between threads. More...
 
std::map< std::string, std::recursive_mutex > m_lockMap
 

Additional Inherited Members

- Protected Member Functions inherited from CommonMessaging< implements< IComponentManager > >
MSG::Level setUpMessaging () const
 Set up local caches. More...
 
MSG::Level resetMessaging ()
 Reinitialize internal states. More...
 
void updateMsgStreamOutputLevel (int level)
 Update the output level of the cached MsgStream. More...
 
- Protected Attributes inherited from ComponentManager
SmartIF< IInterfacem_application
 Pointer to the owner of the manager. More...
 
SmartIF< IStatefulm_stateful
 Pointer to the IStateful interface of the owner. More...
 
InterfaceID m_basicInterfaceId
 Basic interface id of the managed components. More...
 
SmartIF< ISvcLocatorm_svcLocator
 Service locator (needed to access the MessageSvc) More...
 
friend ApplicationMgr
 

Detailed Description

The ServiceManager class is in charge of the creation of concrete instances of Services. The ApplicationMgr delegates the creation and bookkeeping of services to the ServiceManager. In order to be able to create services from which it is not know the concrete type it requires that the services has been declared in one of 3 possible ways: an abstract static creator function, a dynamic link library or an abstract factory reference.

Author
Pere Mato

Definition at line 40 of file ServiceManager.h.

Member Typedef Documentation

◆ ListSvc

Definition at line 53 of file ServiceManager.h.

◆ MapType

typedef std::map<std::string, std::string, std::less<> > ServiceManager::MapType

Definition at line 54 of file ServiceManager.h.

Constructor & Destructor Documentation

◆ ServiceManager()

ServiceManager::ServiceManager ( IInterface application)

default creator

Definition at line 50 of file ServiceManager.cpp.

52  // Set the service locator to myself
53  m_svcLocator = this;
54  addRef(); // increase ref count, so we live forever...
55 }

◆ ~ServiceManager()

ServiceManager::~ServiceManager ( )
override

virtual destructor

Definition at line 57 of file ServiceManager.cpp.

57  {
58  //-- inform the orphan services that I am gone....
59  for ( auto& svc : m_listsvc ) svc.service->setServiceManager( nullptr );
60 }

Member Function Documentation

◆ addService() [1/2]

StatusCode ServiceManager::addService ( const Gaudi::Utils::TypeNameString typeName,
int  prio = DEFAULT_SVC_PRIORITY 
)
override

implementation of ISvcManager::addService

Definition at line 120 of file ServiceManager.cpp.

120  {
121  auto it = find( typeName.name() ); // try to find the service by name
122  if ( it == m_listsvc.end() ) { // not found
123  // If the service does not exist, we create it
124  SmartIF<IService>& svc =
125  createService( typeName ); // WARNING: svc is now a reference to something that lives in m_listsvc
126  if ( !svc ) return StatusCode::FAILURE;
127  it = find( svc.get() ); // now it is in the list because createService added it
128  it->priority = prio;
130  if ( targetFSMState() >= Gaudi::StateMachine::INITIALIZED ) { // WARNING: this can trigger a recursion!!!
131  sc = svc->sysInitialize();
132  if ( sc.isSuccess() && targetFSMState() >= Gaudi::StateMachine::RUNNING ) { sc = svc->sysStart(); }
133  }
134  if ( sc.isFailure() ) { // if initialization failed, remove it from the list
135  error() << "Unable to initialize service \"" << typeName.name() << "\"" << endmsg;
136  auto lck = std::scoped_lock{ m_gLock };
137  m_listsvc.erase( it );
138  // Note: removing it from the list + the SmartIF going out of scope should trigger the delete
139  // delete svc.get();
140  return sc;
141  }
142  // initialization successful, we can work with the service
143  // Move the just initialized service to the back of the list
144  // (we care more about order of initialization than of creation)
145  auto lck = std::scoped_lock{ m_gLock };
146  m_listsvc.push_back( *it );
147  m_listsvc.erase( it );
148  it = std::prev( std::end( m_listsvc ) ); // last entry (the iterator was invalidated by erase)
149  } else {
150  // if the service is already known, it is equivalent to a setPriority
151  it->priority = prio;
152  }
153  // 'it' is defined because either we found the service or we created it
154  // Now we can activate the service
155  it->active = true; // and make it active
156  return StatusCode::SUCCESS;
157 }

◆ addService() [2/2]

StatusCode ServiceManager::addService ( IService svc,
int  prio = DEFAULT_SVC_PRIORITY 
)
override

implementation of ISvcManager::addService

Definition at line 108 of file ServiceManager.cpp.

108  {
109  auto it = find( svc );
110  auto lck = std::scoped_lock{ m_gLock };
111  if ( it != m_listsvc.end() ) {
112  it->priority = prio; // if the service is already known, it is equivalent to a setPriority
113  it->active = true; // and make it active
114  } else {
115  m_listsvc.emplace_back( svc, prio, true );
116  }
117  return StatusCode::SUCCESS;
118 }

◆ createService()

SmartIF< IService > & ServiceManager::createService ( const Gaudi::Utils::TypeNameString nametype)
override

implementation of ISvcManager::createService NOTE: as this returns a &, we must guarantee that once created, these SmartIF remain pinned in their location, thus constraining the underlying implementation...

Fix-Me:
: what does this mean?

Definition at line 62 of file ServiceManager.cpp.

62  {
63  // Check if the service is already existing
64  if ( existsService( typeName.name() ) ) {
65  // return an error because a service with that name already exists
66  return no_service;
67  }
68 
69  const std::string& name = typeName.name();
70  std::string type = typeName.type();
71  if ( !typeName.haveType() ) { // the type is not explicit
72  // see we have some specific type mapping for the name
73  auto it = m_maptype.find( typeName.name() );
74  if ( it != m_maptype.end() ) {
75  type = it->second; // use the declared type
76  }
77  }
78 
80  auto ip = type.find( "__" );
81  if ( ip != std::string::npos ) type.erase( ip, type.length() );
82 
83  IService* service = Service::Factory::create( type, name, this ).release();
84  if ( !service ) {
85  fatal() << "No Service factory for " << type << " available." << endmsg;
86  return no_service;
87  }
88  // Check the compatibility of the version of the interface obtained
89  if ( !isValidInterface( service ) ) {
90  fatal() << "Incompatible interface IService version for " << type << endmsg;
91  return no_service;
92  }
93 
94  if ( name == "JobOptionsSvc" ) {
95  if ( !dynamic_cast<Gaudi::Interfaces::IOptionsSvc*>( service ) ) {
96  fatal() << typeName << " does not implement Gaudi::Interfaces::IOptionsSvc" << endmsg;
97  return no_service;
98  }
99  }
100 
101  auto lck = std::scoped_lock{ m_gLock };
102  m_listsvc.push_back( service );
103  service->setServiceManager( this );
104  return m_listsvc.back().service; // DANGER: returns a reference to a SmartIF in m_listsvc, and hence does no longer
105  // allow relocations of those...
106 }

◆ declareSvcType()

StatusCode ServiceManager::declareSvcType ( std::string  svcname,
std::string  svctype 
)
override

implementation of ISvcManager::declareSvcType

Definition at line 225 of file ServiceManager.cpp.

225  {
226  m_maptype.insert_or_assign( std::move( svcname ), std::move( svctype ) );
227  return StatusCode::SUCCESS;
228 }

◆ dump()

void ServiceManager::dump ( ) const
private

Definition at line 446 of file ServiceManager.cpp.

446  {
447 
448  auto& log = info();
449  log << "\n"
450  << "===================== listing all services ===================\n"
451  << " prior ref name active\n";
452 
453  for ( const auto& svc : m_listsvc ) {
454 
455  log.width( 6 );
456  log.flags( std::ios_base::right );
457  log << svc.priority << " ";
458  log.width( 5 );
459  log << svc.service->refCount() << " ";
460  log.width( 30 );
461  log.flags( std::ios_base::left );
462  log << svc.service->name() << " ";
463  log.width( 2 );
464  log << svc.active << std::endl;
465  }
466 
467  log << "=================================================================\n";
468  log << endmsg;
469 }

◆ existsService()

bool ServiceManager::existsService ( std::string_view  name) const
override

implementation of ISvcLocation::existsService

Definition at line 209 of file ServiceManager.cpp.

209 { return find( name ) != m_listsvc.end(); }

◆ finalize()

StatusCode ServiceManager::finalize ( )
override

Finalize (from INITIALIZED to CONFIGURED).

Definition at line 358 of file ServiceManager.cpp.

358  {
359  // make sure that HistogramDataSvc and THistSvc get finalized after the
360  // ToolSvc, and the FileMgr after that
361  int pri_tool = getPriority( "ToolSvc" );
362  if ( pri_tool != 0 ) {
363  setPriority( "THistSvc", pri_tool - 10 ).ignore();
364  setPriority( "ChronoStatSvc", pri_tool - 20 ).ignore();
365  setPriority( "AuditorSvc", pri_tool - 30 ).ignore();
366  setPriority( "NTupleSvc", pri_tool - 10 ).ignore();
367  setPriority( "HistogramDataSvc", pri_tool - 10 ).ignore();
368  // Preserve the relative ordering between HistogramDataSvc and HistogramPersistencySvc
369  setPriority( "HistogramPersistencySvc", pri_tool - 20 ).ignore();
370  setPriority( "HistorySvc", pri_tool - 30 ).ignore();
371  setPriority( "FileMgr", pri_tool - 40 ).ignore();
372  }
373 
374  // get list of PostFinalize clients
375  std::vector<IIncidentListener*> postFinList;
376  auto p_inc = service<IIncidentSvc>( "IncidentSvc", false );
377  if ( p_inc ) {
378  p_inc->getListeners( postFinList, IncidentType::SvcPostFinalize );
379  p_inc.reset();
380  }
381 
382  // ensure that the list is ordered by priority
383  m_listsvc.sort();
384  // dump();
385 
387  {
388  // we work on a copy to avoid to operate twice on the services created on demand
389  // which are already in the correct state.
390  // only act on active services
391  // call finalize() for all services in reverse order
392  for ( const auto& svc : reverse( activeSvc( m_listsvc ) ) ) {
393  const std::string& name = svc->name();
394  // ignore the current state for the moment
395  // if( Gaudi::StateMachine::INITIALIZED == svc->state() )
396  DEBMSG << "Finalizing service " << name << endmsg;
397  if ( !svc->sysFinalize().isSuccess() ) {
398  warning() << "Finalization of service " << name << " failed" << endmsg;
399  sc = StatusCode::FAILURE;
400  }
401  }
402  }
403 
404  // call SvcPostFinalize on all clients
405  if ( !postFinList.empty() ) {
406  DEBMSG << "Will call SvcPostFinalize for " << postFinList.size() << " clients" << endmsg;
407  Incident inc( "ServiceManager", IncidentType::SvcPostFinalize );
408  for ( auto& itr : postFinList ) itr->handle( inc );
409  }
410 
411  // loop over all Active Services, removing them one by one.
412  // They should be deleted because the reference counting goes to 0.
413  DEBMSG << "Looping over all active services..." << endmsg;
414  auto it = m_listsvc.begin();
415  while ( it != m_listsvc.end() ) {
416  DEBMSG << "---- " << it->service->name() << " (refCount = " << it->service->refCount() << ")" << endmsg;
417  if ( it->service->refCount() < 1 ) {
418  warning() << "Too low reference count for " << it->service->name() << " (should not go below 1 at this point)"
419  << endmsg;
420  it->service->addRef();
421  }
422  if ( it->active ) {
423  it = m_listsvc.erase( it );
424  } else {
425  ++it;
426  }
427  }
428  return sc;
429 }

◆ find() [1/4]

ListSvc::iterator ServiceManager::find ( const IService ptr)
inlineprivate

Definition at line 140 of file ServiceManager.h.

140  {
141  auto lck = std::scoped_lock{ m_gLock };
142  return std::find( m_listsvc.begin(), m_listsvc.end(), ptr );
143  }

◆ find() [2/4]

ListSvc::const_iterator ServiceManager::find ( const IService ptr) const
inlineprivate

Definition at line 144 of file ServiceManager.h.

144  {
145  auto lck = std::scoped_lock{ m_gLock };
146  return std::find( m_listsvc.begin(), m_listsvc.end(), ptr );
147  }

◆ find() [3/4]

ListSvc::iterator ServiceManager::find ( std::string_view  name)
inlineprivate

Definition at line 132 of file ServiceManager.h.

132  {
133  auto lck = std::scoped_lock{ m_gLock };
134  return std::find( m_listsvc.begin(), m_listsvc.end(), name );
135  }

◆ find() [4/4]

ListSvc::const_iterator ServiceManager::find ( std::string_view  name) const
inlineprivate

Definition at line 136 of file ServiceManager.h.

136  {
137  auto lck = std::scoped_lock{ m_gLock };
138  return std::find( m_listsvc.begin(), m_listsvc.end(), name );
139  }

◆ getPriority()

int ServiceManager::getPriority ( std::string_view  name) const
override

manage priorities of services

Definition at line 431 of file ServiceManager.cpp.

431  {
432  auto it = find( name );
433  return ( it != m_listsvc.end() ) ? it->priority : 0;
434 }

◆ getServices()

const std::list< IService * > & ServiceManager::getServices ( ) const
override

Return the list of Services.

Definition at line 202 of file ServiceManager.cpp.

202  {
203  m_listOfPtrs.clear();
204  std::transform( std::begin( m_listsvc ), std::end( m_listsvc ), std::back_inserter( m_listOfPtrs ),
205  []( ListSvc::const_reference i ) { return i.service.get(); } );
206  return m_listOfPtrs;
207 }

◆ initialize()

StatusCode ServiceManager::initialize ( )
override

Initialization (from CONFIGURED to INITIALIZED).

Definition at line 230 of file ServiceManager.cpp.

230  {
231  // ensure that the list is ordered by priority
232  m_listsvc.sort();
233  // we work on a copy to avoid to operate twice on the services created on demand
234  // which are already in the correct state.
235 
237  // call initialize() for all services
238  for ( auto& it : activeSvc( m_listsvc ) ) {
239  const std::string& name = it->name();
240  switch ( it->FSMState() ) {
242  DEBMSG << "Service " << name << " already initialized" << endmsg;
243  break;
245  DEBMSG << "Initializing service " << name << endmsg;
246  sc = it->sysInitialize();
247  if ( !sc.isSuccess() ) {
248  error() << "Unable to initialize Service: " << name << endmsg;
249  return sc;
250  }
251  break;
252  default:
253  error() << "Service " << name << " not in the correct state to be initialized (" << it->FSMState() << ")"
254  << endmsg;
255  return StatusCode::FAILURE;
256  }
257  }
258  return StatusCode::SUCCESS;
259 }

◆ loopCheckEnabled()

bool ServiceManager::loopCheckEnabled ( ) const
override

Get the value of the initialization loop check flag.

Definition at line 443 of file ServiceManager.cpp.

443 { return m_loopCheck; }

◆ name()

const std::string& ServiceManager::name ( ) const
inlineoverride

Return the name of the manager (implementation of INamedInterface)

Definition at line 114 of file ServiceManager.h.

114  {
115  static std::string _name = "ServiceManager";
116  return _name;
117  }

◆ outputLevelUpdate()

void ServiceManager::outputLevelUpdate ( )
override

Function to call to update the outputLevel of the components (after a change in MessageSvc).

Definition at line 471 of file ServiceManager.cpp.

471  {
472  resetMessaging();
473  for ( auto& svcItem : m_listsvc ) {
474  const auto svc = dynamic_cast<Service*>( svcItem.service.get() );
475  if ( svc ) svc->resetMessaging();
476  }
477 }

◆ reinitialize()

StatusCode ServiceManager::reinitialize ( )
override

Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).

Definition at line 322 of file ServiceManager.cpp.

322  {
323  // ensure that the list is ordered by priority
324  m_listsvc.sort();
325  // we work on a copy to avoid to operate twice on the services created on demand
326  // which are already in the correct state.
327  // only act on active services
329  // Re-Initialize all services
330  for ( auto& svc : activeSvc( m_listsvc ) ) {
331  sc = svc->sysReinitialize();
332  if ( !sc.isSuccess() ) {
333  error() << "Unable to re-initialize Service: " << svc->name() << endmsg;
334  return StatusCode::FAILURE;
335  }
336  }
337  return StatusCode::SUCCESS;
338 }

◆ removeService() [1/2]

StatusCode ServiceManager::removeService ( IService svc)
override

implementation of ISvcManager::removeService

Definition at line 211 of file ServiceManager.cpp.

211  {
212  auto it = find( svc );
213  if ( it == m_listsvc.end() ) return StatusCode::FAILURE;
214  m_listsvc.erase( it );
215  return StatusCode::SUCCESS;
216 }

◆ removeService() [2/2]

StatusCode ServiceManager::removeService ( std::string_view  name)
override

implementation of ISvcManager::removeService

Definition at line 218 of file ServiceManager.cpp.

218  {
219  auto it = find( name );
220  if ( it == m_listsvc.end() ) return StatusCode::FAILURE;
221  m_listsvc.erase( it );
222  return StatusCode::SUCCESS;
223 }

◆ restart()

StatusCode ServiceManager::restart ( )
override

Initialization (from RUNNING to RUNNING, via INITIALIZED).

Definition at line 340 of file ServiceManager.cpp.

340  {
341  // ensure that the list is ordered by priority
342  m_listsvc.sort();
343  // we work on a copy to avoid to operate twice on the services created on demand
344  // which are already in the correct state.
345  // only act on active services
347  // Re-Start all services
348  for ( auto& svc : activeSvc( m_listsvc ) ) {
349  sc = svc->sysRestart();
350  if ( !sc.isSuccess() ) {
351  error() << "Unable to re-start Service: " << svc->name() << endmsg;
352  return StatusCode::FAILURE;
353  }
354  }
355  return StatusCode::SUCCESS;
356 }

◆ service() [1/2]

template<typename T >
SmartIF<T> ServiceManager::service ( const Gaudi::Utils::TypeNameString typeName,
const bool  createIf = true 
)
inline

Returns a smart pointer to the requested interface of a service.

Definition at line 124 of file ServiceManager.h.

124  {
125  return SmartIF<T>{ service( typeName, createIf ) };
126  }

◆ service() [2/2]

SmartIF< IService > & ServiceManager::service ( const Gaudi::Utils::TypeNameString typeName,
const bool  createIf = true 
)
override

Returns a smart pointer to a service.

Definition at line 159 of file ServiceManager.cpp.

159  {
160  const std::string& name = typeName.name();
161 
162  // Acquire the RAII lock to avoid simultaneous attempts from different threads to initialize a service
163 
164  auto* imut = [&] {
165  // get the global lock, then extract/create the service specific mutex
166  // then release global lock
167 
168  auto lk = std::scoped_lock{ this->m_gLock };
169  auto mit = m_lockMap.find( name );
170  if ( mit == m_lockMap.end() ) {
171  mit = m_lockMap.emplace( std::piecewise_construct_t{}, std::forward_as_tuple( name ), std::forward_as_tuple() )
172  .first;
173  }
174  return &mit->second;
175  }();
176 
177  {
178  // now we have the service specific lock on the above mutex
179  auto lk2 = std::scoped_lock{ *imut };
180 
181  auto it = find( name );
182 
183  if ( it != m_listsvc.end() ) {
184  if ( m_loopCheck && ( createIf && it->service->FSMState() == Gaudi::StateMachine::CONFIGURED ) ) {
185  error() << "Initialization loop detected when creating service \"" << name << "\"" << endmsg;
186  return no_service;
187  }
188  return it->service;
189  }
190 
191  // Service not found. The user may be interested in one of the interfaces
192  // of the application manager itself
193  if ( name == "ApplicationMgr" || name == "APPMGR" || name == "" ) { return m_appSvc; }
194 
195  // last resort: we try to create the service
196  if ( createIf && addService( typeName ).isSuccess() ) { return find( name )->service; }
197 
198  return no_service;
199  }
200 }

◆ serviceLocator()

SmartIF<ISvcLocator>& ServiceManager::serviceLocator ( ) const
inlineoverride

Function needed by CommonMessaging.

Definition at line 60 of file ServiceManager.h.

60 { return m_svcLocator; }

◆ setLoopCheckEnabled()

void ServiceManager::setLoopCheckEnabled ( bool  en)
override

Set the value of the initialization loop check flag.

Definition at line 444 of file ServiceManager.cpp.

444 { m_loopCheck = en; }

◆ setPriority()

StatusCode ServiceManager::setPriority ( std::string_view  name,
int  pri 
)
override

Definition at line 436 of file ServiceManager.cpp.

436  {
437  auto it = find( name );
438  if ( it == m_listsvc.end() ) return StatusCode::FAILURE;
439  it->priority = prio;
440  return StatusCode::SUCCESS;
441 }

◆ start()

StatusCode ServiceManager::start ( )
override

Start (from INITIALIZED to RUNNING).

Definition at line 261 of file ServiceManager.cpp.

261  {
262  // ensure that the list is ordered by priority
263  m_listsvc.sort();
264  // we work on a copy to avoid to operate twice on the services created on demand
265  // (which are already in the correct state.
266  // only act on active services
268  // call initialize() for all services
269  for ( auto& it : activeSvc( m_listsvc ) ) {
270  const std::string& name = it->name();
271  switch ( it->FSMState() ) {
273  DEBMSG << "Service " << name << " already started" << endmsg;
274  break;
276  DEBMSG << "Starting service " << name << endmsg;
277  sc = it->sysStart();
278  if ( !sc.isSuccess() ) {
279  error() << "Unable to start Service: " << name << endmsg;
280  return sc;
281  }
282  break;
283  default:
284  error() << "Service " << name << " not in the correct state to be started (" << it->FSMState() << ")" << endmsg;
285  return StatusCode::FAILURE;
286  }
287  }
288  return StatusCode::SUCCESS;
289 }

◆ stop()

StatusCode ServiceManager::stop ( )
override

Stop (from RUNNING to INITIALIZED).

Definition at line 291 of file ServiceManager.cpp.

291  {
292  // ensure that the list is ordered by priority
293  m_listsvc.sort();
294  // we work on a copy to avoid to operate twice on the services created on demand
295  // which are already in the correct state.
296  // only act on active services
297 
299  // call stop() for all services
300  for ( const auto& svc : reverse( activeSvc( m_listsvc ) ) ) {
301  const std::string& name = svc->name();
302  switch ( svc->FSMState() ) {
304  DEBMSG << "Service " << name << " already stopped" << endmsg;
305  break;
307  DEBMSG << "Stopping service " << name << endmsg;
308  sc = svc->sysStop();
309  if ( !sc.isSuccess() ) {
310  error() << "Unable to stop Service: " << name << endmsg;
311  return sc;
312  }
313  break;
314  default:
315  DEBMSG << "Service " << name << " not in the correct state to be stopped (" << svc->FSMState() << ")" << endmsg;
316  return StatusCode::FAILURE;
317  }
318  }
319  return StatusCode::SUCCESS;
320 }

Member Data Documentation

◆ m_appSvc

SmartIF<IService> ServiceManager::m_appSvc
private

Pointer to the application IService interface.

Definition at line 168 of file ServiceManager.h.

◆ m_defaultImplementations

GaudiUtils::Map<InterfaceID, SmartIF<IInterface> > ServiceManager::m_defaultImplementations
private

Definition at line 173 of file ServiceManager.h.

◆ m_gLock

std::recursive_mutex ServiceManager::m_gLock
mutableprivate

Mutex to synchronize shared service initialization between threads.

Definition at line 176 of file ServiceManager.h.

◆ m_listOfPtrs

std::list<IService*> ServiceManager::m_listOfPtrs
mutableprivate

List of pointers to the know services used to implement getServices()

Definition at line 171 of file ServiceManager.h.

◆ m_listsvc

ListSvc ServiceManager::m_listsvc
private

List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because there can be SmartIF<T>& 'out there' that refer to these specific SmarIF<T>, we unfortunately must guarantee that they never move after creation.

Hence, we cannot use a plain std::vector here, as that may cause relocation and/or swapping of SmartIF<T>'s, and then the already handed out references may refer to the wrong item.... Note that we could use an std::vector<std::unique_ptr<ServiceItem>> (sometimes known as 'stable vector') as then the individual ServiceItems would stay pinned in their original location, but that would put ServiceItem on the heap... And maybe I'm way too paranoid...

Definition at line 150 of file ServiceManager.h.

◆ m_lockMap

std::map<std::string, std::recursive_mutex> ServiceManager::m_lockMap
mutableprivate

Definition at line 177 of file ServiceManager.h.

◆ m_loopCheck

bool ServiceManager::m_loopCheck = true
private

Check for service initialization loops.

Definition at line 165 of file ServiceManager.h.

◆ m_maptype

MapType ServiceManager::m_maptype
private

Map of service name and service type.

Definition at line 164 of file ServiceManager.h.


The documentation for this class was generated from the following files:
ComponentManager::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
When we are in the middle of a transition, get the state where the transition is leading us.
Definition: ComponentManager.h:76
IService
Definition: IService.h:26
Gaudi.Configuration.log
log
Definition: Configuration.py:28
CommonMessaging< implements< IComponentManager > >::resetMessaging
MSG::Level resetMessaging()
Reinitialize internal states.
Definition: CommonMessaging.h:178
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
reverse
::details::reverse_wrapper< T > reverse(T &&iterable)
Definition: reverse.h:58
extend_interfaces< INamedInterface, IStateful >::interfaceID
static const InterfaceID & interfaceID()
Definition: extend_interfaces.h:29
extends< ComponentManager, ISvcManager, ISvcLocator >::base_class
extends base_class
Typedef to this class.
Definition: extends.h:23
Service
Definition: Service.h:39
ServiceManager::m_appSvc
SmartIF< IService > m_appSvc
Pointer to the application IService interface.
Definition: ServiceManager.h:168
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:135
ServiceManager::addService
StatusCode addService(IService *svc, int prio=DEFAULT_SVC_PRIORITY) override
implementation of ISvcManager::addService
Definition: ServiceManager.cpp:108
Gaudi::StateMachine::CONFIGURED
@ CONFIGURED
Definition: StateMachine.h:23
StatusCode
Definition: StatusCode.h:64
DEBMSG
#define DEBMSG
Definition: ServiceManager.cpp:31
Gaudi::StateMachine::OFFLINE
@ OFFLINE
Definition: StateMachine.h:22
ServiceManager::service
SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Returns a smart pointer to a service.
Definition: ServiceManager.cpp:159
ServiceManager::m_listsvc
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
Definition: ServiceManager.h:150
ServiceManager::createService
SmartIF< IService > & createService(const Gaudi::Utils::TypeNameString &nametype) override
implementation of ISvcManager::createService NOTE: as this returns a &, we must guarantee that once c...
Definition: ServiceManager.cpp:62
ServiceManager::m_loopCheck
bool m_loopCheck
Check for service initialization loops.
Definition: ServiceManager.h:165
SmartIF< IService >
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:198
ServiceManager::m_lockMap
std::map< std::string, std::recursive_mutex > m_lockMap
Definition: ServiceManager.h:177
Gaudi::StateMachine::RUNNING
@ RUNNING
Definition: StateMachine.h:25
ServiceManager::find
ListSvc::iterator find(std::string_view name)
Definition: ServiceManager.h:132
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
ServiceManager::m_listOfPtrs
std::list< IService * > m_listOfPtrs
List of pointers to the know services used to implement getServices()
Definition: ServiceManager.h:171
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
gaudirun.type
type
Definition: gaudirun.py:160
ComponentManager::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Service locator (needed to access the MessageSvc)
Definition: ComponentManager.h:89
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:99
ServiceManager::getPriority
int getPriority(std::string_view name) const override
manage priorities of services
Definition: ServiceManager.cpp:431
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:82
GaudiDict::typeName
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31
ServiceManager::existsService
bool existsService(std::string_view name) const override
implementation of ISvcLocation::existsService
Definition: ServiceManager.cpp:209
Gaudi::StateMachine::INITIALIZED
@ INITIALIZED
Definition: StateMachine.h:24
isValidInterface
bool isValidInterface(IFace *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:354
ServiceManager::m_gLock
std::recursive_mutex m_gLock
Mutex to synchronize shared service initialization between threads.
Definition: ServiceManager.h:176
IOTest.end
end
Definition: IOTest.py:125
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:100
ServiceManager::name
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
Definition: ServiceManager.h:114
Incident
Definition: Incident.h:24
gaudirun.application
application
Definition: gaudirun.py:323
Gaudi::Interfaces::IOptionsSvc
Interface for a component that manages application configuration options.
Definition: IOptionsSvc.h:46
ServiceManager::m_maptype
MapType m_maptype
Map of service name and service type.
Definition: ServiceManager.h:164
ServiceManager::setPriority
StatusCode setPriority(std::string_view name, int pri) override
Definition: ServiceManager.cpp:436