The Gaudi Framework  master (594c33fa)
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::stringname () 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...
 
virtual StatusCode addService (IService *svc, int prio=DEFAULT_SVC_PRIORITY)=0
 Add a service to the "active" list of services of the factory. More...
 
virtual StatusCode addService (const std::string &typ, const std::string &nam, int prio)
 Add a service to the "active" list of services of the factory. More...
 
virtual StatusCode addService (const Gaudi::Utils::TypeNameString &nametype, int prio=DEFAULT_SVC_PRIORITY)=0
 Add a service to the "active" list of services of the factory. More...
 
virtual SmartIF< IService > & createService (const Gaudi::Utils::TypeNameString &nametype)=0
 Creates and instance of a service type that has been declared beforehand and assigns it a name. More...
 
virtual StatusCode createService (const std::string &svctype, const std::string &svcname, IService *&svc)
 Creates and instance of a service type that has been declared beforehand and assigns it a name. More...
 
- Public Member Functions inherited from extends< ComponentManager, ISvcManager, ISvcLocator >
void * 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::stringgetInterfaceNames () 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...
 
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_mutexm_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 46 of file ServiceManager.h.

Member Typedef Documentation

◆ ListSvc

Definition at line 59 of file ServiceManager.h.

◆ MapType

Constructor & Destructor Documentation

◆ ServiceManager()

ServiceManager::ServiceManager ( IInterface application)

default creator

Definition at line 54 of file ServiceManager.cpp.

55  : base_class( application, IService::interfaceID() ), m_appSvc( application ) {
56  // Set the service locator to myself
57  m_svcLocator = this;
58  addRef(); // increase ref count, so we live forever...
59 }

◆ ~ServiceManager()

ServiceManager::~ServiceManager ( )
override

virtual destructor

Definition at line 62 of file ServiceManager.cpp.

62  {
63  //-- inform the orphan services that I am gone....
64  for ( auto& svc : m_listsvc ) svc.service->setServiceManager( nullptr );
65 }

Member Function Documentation

◆ addService() [1/5]

virtual StatusCode ISvcManager::addService

Add a service to the "active" list of services of the factory.

Parameters
svcPointer to the service
Returns
StatusCode indicating success or failure.

◆ addService() [2/5]

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

implementation of ISvcManager::addService

Definition at line 135 of file ServiceManager.cpp.

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

◆ addService() [3/5]

virtual StatusCode ISvcManager::addService
inline

Add a service to the "active" list of services of the factory.

Parameters
svcPointer to the service
Returns
StatusCode indicating success or failure.

Definition at line 58 of file ISvcManager.h.

58  {
59  return addService( Gaudi::Utils::TypeNameString( nam, typ ), prio );
60  }

◆ addService() [4/5]

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

implementation of ISvcManager::addService

Definition at line 119 of file ServiceManager.cpp.

121 {
122  auto it = find( svc );
123  auto lck = std::scoped_lock{ m_gLock };
124  if ( it != m_listsvc.end() ) {
125  it->priority = prio; // if the service is already known, it is equivalent to a setPriority
126  it->active = true; // and make it active
127  } else {
128  m_listsvc.emplace_back( svc, prio, true );
129  }
130  return StatusCode::SUCCESS;
131 }

◆ addService() [5/5]

virtual StatusCode ISvcManager::addService

Add a service to the "active" list of services of the factory.

Parameters
svcPointer to the service
Returns
StatusCode indicating success or failure.

◆ createService() [1/3]

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 69 of file ServiceManager.cpp.

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

◆ createService() [2/3]

virtual SmartIF<IService>& ISvcManager::createService

Creates and instance of a service type that has been declared beforehand and assigns it a name.

It returns a pointer to an IService.

Parameters
nametypename/type of the service to create
Returns
SmartIF& to the created service.

NOTE: as this returns a &, the underlying implementation must guarantee that once created, these SmartIF remain pinned in their location, thus constraining the underlying implementation (i.e. one cannot use something like std::vector<SmartIF<IService>>). If this interface had used value-semantics, and returned just plain SmartIF<IService> (i.e. WITHOUT the &) then the underlying implementation would have much more freedom)

◆ createService() [3/3]

virtual StatusCode ISvcManager::createService
inline

Creates and instance of a service type that has been declared beforehand and assigns it a name.

It returns a pointer to an IService.

Parameters
svctypeService type name
svcnameService name to be set
svcReturned service pointer
Returns
StatusCode indicating success or failure.

Definition at line 131 of file ISvcManager.h.

131  {
132  SmartIF<IService> s = createService( svctype + "/" + svcname );
133  svc = s.get();
134  if ( svc ) {
135  svc->addRef(); // Needed to maintain the correct reference counting.
136  return StatusCode::SUCCESS;
137  }
138  return StatusCode::FAILURE;
139  }

◆ declareSvcType()

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

implementation of ISvcManager::declareSvcType

Definition at line 259 of file ServiceManager.cpp.

261 {
262  m_maptype.insert_or_assign( std::move( svcname ), std::move( svctype ) );
263  return StatusCode::SUCCESS;
264 }

◆ dump()

void ServiceManager::dump ( ) const
private

Definition at line 513 of file ServiceManager.cpp.

513  {
514 
515  auto& log = info();
516  log << "\n"
517  << "===================== listing all services ===================\n"
518  << " prior ref name active\n";
519 
520  for ( const auto& svc : m_listsvc ) {
521 
522  log.width( 6 );
523  log.flags( std::ios_base::right );
524  log << svc.priority << " ";
525  log.width( 5 );
526  log << svc.service->refCount() << " ";
527  log.width( 30 );
528  log.flags( std::ios_base::left );
529  log << svc.service->name() << " ";
530  log.width( 2 );
531  log << svc.active << std::endl;
532  }
533 
534  log << "=================================================================\n";
535  log << endmsg;
536 }

◆ existsService()

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

implementation of ISvcLocation::existsService

Definition at line 232 of file ServiceManager.cpp.

234 {
235  return find( name ) != m_listsvc.end();
236 }

◆ finalize()

StatusCode ServiceManager::finalize ( )
override

Finalize (from INITIALIZED to CONFIGURED).

Definition at line 410 of file ServiceManager.cpp.

412 {
413  // make sure that HistogramDataSvc and THistSvc get finalized after the
414  // ToolSvc, and the FileMgr after that
415  int pri_tool = getPriority( "ToolSvc" );
416  if ( pri_tool != 0 ) {
417  setPriority( "THistSvc", pri_tool - 10 ).ignore();
418  setPriority( "ChronoStatSvc", pri_tool - 20 ).ignore();
419  setPriority( "AuditorSvc", pri_tool - 30 ).ignore();
420  setPriority( "NTupleSvc", pri_tool - 10 ).ignore();
421  setPriority( "HistogramDataSvc", pri_tool - 10 ).ignore();
422  // Preserve the relative ordering between HistogramDataSvc and HistogramPersistencySvc
423  setPriority( "HistogramPersistencySvc", pri_tool - 20 ).ignore();
424  setPriority( "HistorySvc", pri_tool - 30 ).ignore();
425  setPriority( "FileMgr", pri_tool - 40 ).ignore();
426  }
427 
428  // get list of PostFinalize clients
430  auto p_inc = service<IIncidentSvc>( "IncidentSvc", false );
431  if ( p_inc ) {
432  p_inc->getListeners( postFinList, IncidentType::SvcPostFinalize );
433  p_inc.reset();
434  }
435 
436  // ensure that the list is ordered by priority
437  m_listsvc.sort();
438  // dump();
439 
441  {
442  // we work on a copy to avoid to operate twice on the services created on demand
443  // which are already in the correct state.
444  // only act on active services
445  // call finalize() for all services in reverse order
446  for ( const auto& svc : reverse( activeSvc( m_listsvc ) ) ) {
447  const std::string& name = svc->name();
448  // ignore the current state for the moment
449  // if( Gaudi::StateMachine::INITIALIZED == svc->state() )
450  DEBMSG << "Finalizing service " << name << endmsg;
451  if ( !svc->sysFinalize().isSuccess() ) {
452  warning() << "Finalization of service " << name << " failed" << endmsg;
453  sc = StatusCode::FAILURE;
454  }
455  }
456  }
457 
458  // call SvcPostFinalize on all clients
459  if ( !postFinList.empty() ) {
460  DEBMSG << "Will call SvcPostFinalize for " << postFinList.size() << " clients" << endmsg;
461  Incident inc( "ServiceManager", IncidentType::SvcPostFinalize );
462  for ( auto& itr : postFinList ) itr->handle( inc );
463  }
464 
465  // loop over all Active Services, removing them one by one.
466  // They should be deleted because the reference counting goes to 0.
467  DEBMSG << "Looping over all active services..." << endmsg;
468  auto it = m_listsvc.begin();
469  while ( it != m_listsvc.end() ) {
470  DEBMSG << "---- " << it->service->name() << " (refCount = " << it->service->refCount() << ")" << endmsg;
471  if ( it->service->refCount() < 1 ) {
472  warning() << "Too low reference count for " << it->service->name() << " (should not go below 1 at this point)"
473  << endmsg;
474  it->service->addRef();
475  }
476  if ( it->active ) {
477  it = m_listsvc.erase( it );
478  } else {
479  ++it;
480  }
481  }
482  return sc;
483 }

◆ find() [1/4]

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

Definition at line 151 of file ServiceManager.h.

151  {
152  auto lck = std::scoped_lock{ m_gLock };
153  return std::find( m_listsvc.begin(), m_listsvc.end(), ptr );
154  }

◆ find() [2/4]

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

Definition at line 155 of file ServiceManager.h.

155  {
156  auto lck = std::scoped_lock{ m_gLock };
157  return std::find( m_listsvc.begin(), m_listsvc.end(), ptr );
158  }

◆ find() [3/4]

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

Definition at line 143 of file ServiceManager.h.

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

◆ find() [4/4]

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

Definition at line 147 of file ServiceManager.h.

147  {
148  auto lck = std::scoped_lock{ m_gLock };
149  return std::find( m_listsvc.begin(), m_listsvc.end(), name );
150  }

◆ getPriority()

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

manage priorities of services

Definition at line 486 of file ServiceManager.cpp.

486  {
487  //------------------------------------------------------------------------------
488  auto it = find( name );
489  return ( it != m_listsvc.end() ) ? it->priority : 0;
490 }

◆ getServices()

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

Return the list of Services.

Definition at line 222 of file ServiceManager.cpp.

224 {
227  []( ListSvc::const_reference i ) { return i.service.get(); } );
228  return m_listOfPtrs;
229 }

◆ initialize()

StatusCode ServiceManager::initialize ( )
override

Initialization (from CONFIGURED to INITIALIZED).

Definition at line 267 of file ServiceManager.cpp.

269 {
270  // ensure that the list is ordered by priority
271  m_listsvc.sort();
272  // we work on a copy to avoid to operate twice on the services created on demand
273  // which are already in the correct state.
274 
276  // call initialize() for all services
277  for ( auto& it : activeSvc( m_listsvc ) ) {
278  const std::string& name = it->name();
279  switch ( it->FSMState() ) {
281  DEBMSG << "Service " << name << " already initialized" << endmsg;
282  break;
284  DEBMSG << "Initializing service " << name << endmsg;
285  sc = it->sysInitialize();
286  if ( !sc.isSuccess() ) {
287  error() << "Unable to initialize Service: " << name << endmsg;
288  return sc;
289  }
290  break;
291  default:
292  error() << "Service " << name << " not in the correct state to be initialized (" << it->FSMState() << ")"
293  << endmsg;
294  return StatusCode::FAILURE;
295  }
296  }
297  return StatusCode::SUCCESS;
298 }

◆ loopCheckEnabled()

bool ServiceManager::loopCheckEnabled ( ) const
override

Get the value of the initialization loop check flag.

Definition at line 504 of file ServiceManager.cpp.

504 { return m_loopCheck; }

◆ name()

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

Return the name of the manager (implementation of INamedInterface)

Definition at line 120 of file ServiceManager.h.

120  {
121  static std::string _name = "ServiceManager";
122  return _name;
123  }

◆ outputLevelUpdate()

void ServiceManager::outputLevelUpdate ( )
override

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

Definition at line 538 of file ServiceManager.cpp.

538  {
539  resetMessaging();
540  for ( auto& svcItem : m_listsvc ) {
541  const auto svc = dynamic_cast<Service*>( svcItem.service.get() );
542  if ( svc ) svc->resetMessaging();
543  }
544 }

◆ reinitialize()

StatusCode ServiceManager::reinitialize ( )
override

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

Definition at line 368 of file ServiceManager.cpp.

370 {
371  // ensure that the list is ordered by priority
372  m_listsvc.sort();
373  // we work on a copy to avoid to operate twice on the services created on demand
374  // which are already in the correct state.
375  // only act on active services
377  // Re-Initialize all services
378  for ( auto& svc : activeSvc( m_listsvc ) ) {
379  sc = svc->sysReinitialize();
380  if ( !sc.isSuccess() ) {
381  error() << "Unable to re-initialize Service: " << svc->name() << endmsg;
382  return StatusCode::FAILURE;
383  }
384  }
385  return StatusCode::SUCCESS;
386 }

◆ removeService() [1/2]

StatusCode ServiceManager::removeService ( IService svc)
override

implementation of ISvcManager::removeService

Definition at line 239 of file ServiceManager.cpp.

241 {
242  auto it = find( svc );
243  if ( it == m_listsvc.end() ) return StatusCode::FAILURE;
244  m_listsvc.erase( it );
245  return StatusCode::SUCCESS;
246 }

◆ removeService() [2/2]

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

implementation of ISvcManager::removeService

Definition at line 249 of file ServiceManager.cpp.

251 {
252  auto it = find( name );
253  if ( it == m_listsvc.end() ) return StatusCode::FAILURE;
254  m_listsvc.erase( it );
255  return StatusCode::SUCCESS;
256 }

◆ restart()

StatusCode ServiceManager::restart ( )
override

Initialization (from RUNNING to RUNNING, via INITIALIZED).

Definition at line 389 of file ServiceManager.cpp.

391 {
392  // ensure that the list is ordered by priority
393  m_listsvc.sort();
394  // we work on a copy to avoid to operate twice on the services created on demand
395  // which are already in the correct state.
396  // only act on active services
398  // Re-Start all services
399  for ( auto& svc : activeSvc( m_listsvc ) ) {
400  sc = svc->sysRestart();
401  if ( !sc.isSuccess() ) {
402  error() << "Unable to re-start Service: " << svc->name() << endmsg;
403  return StatusCode::FAILURE;
404  }
405  }
406  return StatusCode::SUCCESS;
407 }

◆ 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 130 of file ServiceManager.h.

130  {
131  return SmartIF<T>{ service( typeName, createIf ) };
132  }

◆ 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 178 of file ServiceManager.cpp.

178  {
179  const std::string& name = typeName.name();
180 
181  // Acquire the RAII lock to avoid simultaneous attempts from different threads to initialize a service
182 
183  auto* imut = [&] {
184  // get the global lock, then extract/create the service specific mutex
185  // then release global lock
186 
187  auto lk = std::scoped_lock{ this->m_gLock };
188  auto mit = m_lockMap.find( name );
189  if ( mit == m_lockMap.end() ) {
191  .first;
192  }
193  return &mit->second;
194  }();
195 
196  {
197  // now we have the service specific lock on the above mutex
198  auto lk2 = std::scoped_lock{ *imut };
199 
200  auto it = find( name );
201 
202  if ( it != m_listsvc.end() ) {
203  if ( m_loopCheck && ( createIf && it->service->FSMState() == Gaudi::StateMachine::CONFIGURED ) ) {
204  error() << "Initialization loop detected when creating service \"" << name << "\"" << endmsg;
205  return no_service;
206  }
207  return it->service;
208  }
209 
210  // Service not found. The user may be interested in one of the interfaces
211  // of the application manager itself
212  if ( name == "ApplicationMgr" || name == "APPMGR" || name == "" ) { return m_appSvc; }
213 
214  // last resort: we try to create the service
215  if ( createIf && addService( typeName ).isSuccess() ) { return find( name )->service; }
216 
217  return no_service;
218  }
219 }

◆ serviceLocator()

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

Function needed by CommonMessaging.

Definition at line 66 of file ServiceManager.h.

66 { return m_svcLocator; }

◆ setLoopCheckEnabled()

void ServiceManager::setLoopCheckEnabled ( bool  en)
override

Set the value of the initialization loop check flag.

Definition at line 508 of file ServiceManager.cpp.

508 { m_loopCheck = en; }

◆ setPriority()

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

Definition at line 493 of file ServiceManager.cpp.

493  {
494  //------------------------------------------------------------------------------
495  auto it = find( name );
496  if ( it == m_listsvc.end() ) return StatusCode::FAILURE;
497  it->priority = prio;
498  return StatusCode::SUCCESS;
499 }

◆ start()

StatusCode ServiceManager::start ( )
override

Start (from INITIALIZED to RUNNING).

Definition at line 301 of file ServiceManager.cpp.

303 {
304  // ensure that the list is ordered by priority
305  m_listsvc.sort();
306  // we work on a copy to avoid to operate twice on the services created on demand
307  // (which are already in the correct state.
308  // only act on active services
310  // call initialize() for all services
311  for ( auto& it : activeSvc( m_listsvc ) ) {
312  const std::string& name = it->name();
313  switch ( it->FSMState() ) {
315  DEBMSG << "Service " << name << " already started" << endmsg;
316  break;
318  DEBMSG << "Starting service " << name << endmsg;
319  sc = it->sysStart();
320  if ( !sc.isSuccess() ) {
321  error() << "Unable to start Service: " << name << endmsg;
322  return sc;
323  }
324  break;
325  default:
326  error() << "Service " << name << " not in the correct state to be started (" << it->FSMState() << ")" << endmsg;
327  return StatusCode::FAILURE;
328  }
329  }
330  return StatusCode::SUCCESS;
331 }

◆ stop()

StatusCode ServiceManager::stop ( )
override

Stop (from RUNNING to INITIALIZED).

Definition at line 334 of file ServiceManager.cpp.

336 {
337  // ensure that the list is ordered by priority
338  m_listsvc.sort();
339  // we work on a copy to avoid to operate twice on the services created on demand
340  // which are already in the correct state.
341  // only act on active services
342 
344  // call stop() for all services
345  for ( const auto& svc : reverse( activeSvc( m_listsvc ) ) ) {
346  const std::string& name = svc->name();
347  switch ( svc->FSMState() ) {
349  DEBMSG << "Service " << name << " already stopped" << endmsg;
350  break;
352  DEBMSG << "Stopping service " << name << endmsg;
353  sc = svc->sysStop();
354  if ( !sc.isSuccess() ) {
355  error() << "Unable to stop Service: " << name << endmsg;
356  return sc;
357  }
358  break;
359  default:
360  DEBMSG << "Service " << name << " not in the correct state to be stopped (" << svc->FSMState() << ")" << endmsg;
361  return StatusCode::FAILURE;
362  }
363  }
364  return StatusCode::SUCCESS;
365 }

Member Data Documentation

◆ m_appSvc

SmartIF<IService> ServiceManager::m_appSvc
private

Pointer to the application IService interface.

Definition at line 179 of file ServiceManager.h.

◆ m_defaultImplementations

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

Definition at line 184 of file ServiceManager.h.

◆ m_gLock

std::recursive_mutex ServiceManager::m_gLock
mutableprivate

Mutex to synchronize shared service initialization between threads.

Definition at line 187 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 182 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 161 of file ServiceManager.h.

◆ m_lockMap

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

Definition at line 188 of file ServiceManager.h.

◆ m_loopCheck

bool ServiceManager::m_loopCheck = true
private

Check for service initialization loops.

Definition at line 176 of file ServiceManager.h.

◆ m_maptype

MapType ServiceManager::m_maptype
private

Map of service name and service type.

Definition at line 175 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:73
IService
Definition: IService.h:28
std::string
STL class.
Gaudi.Configuration.log
log
Definition: Configuration.py:28
std::move
T move(T... args)
CommonMessaging< implements< IComponentManager > >::resetMessaging
MSG::Level resetMessaging()
Reinitialize internal states.
Definition: CommonMessaging.h:179
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
reverse
::details::reverse_wrapper< T > reverse(T &&iterable)
Definition: reverse.h:59
gaudirun.s
string s
Definition: gaudirun.py:346
std::vector
STL class.
std::map::find
T find(T... args)
std::vector::size
T size(T... args)
std::back_inserter
T back_inserter(T... args)
std::map::emplace
T emplace(T... args)
std::list::back
T back(T... args)
ServiceManager::ServiceItem::service
SmartIF< IService > service
Definition: ServiceManager.h:50
std::list::sort
T sort(T... args)
extends< ComponentManager, ISvcManager, ISvcLocator >::base_class
extends base_class
Typedef to this class.
Definition: extends.h:24
Service
Definition: Service.h:46
std::list::clear
T clear(T... args)
std::list::push_back
T push_back(T... args)
ServiceManager::m_appSvc
SmartIF< IService > m_appSvc
Pointer to the application IService interface.
Definition: ServiceManager.h:179
std::piecewise_construct_t
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:20
ServiceManager::addService
StatusCode addService(IService *svc, int prio=DEFAULT_SVC_PRIORITY) override
implementation of ISvcManager::addService
Definition: ServiceManager.cpp:119
Gaudi::StateMachine::CONFIGURED
@ CONFIGURED
Definition: StateMachine.h:24
StatusCode
Definition: StatusCode.h:65
DEBMSG
#define DEBMSG
Definition: ServiceManager.cpp:34
Gaudi::StateMachine::OFFLINE
@ OFFLINE
Definition: StateMachine.h:23
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:178
ServiceManager::m_listsvc
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
Definition: ServiceManager.h:161
std::list::erase
T erase(T... args)
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:69
ServiceManager::m_loopCheck
bool m_loopCheck
Check for service initialization loops.
Definition: ServiceManager.h:176
SmartIF< IService >
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
std::forward_as_tuple
T forward_as_tuple(T... args)
ServiceManager::m_lockMap
std::map< std::string, std::recursive_mutex > m_lockMap
Definition: ServiceManager.h:188
std::transform
T transform(T... args)
Gaudi::StateMachine::RUNNING
@ RUNNING
Definition: StateMachine.h:26
ServiceManager::find
ListSvc::iterator find(std::string_view name)
Definition: ServiceManager.h:143
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:182
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
gaudirun.type
type
Definition: gaudirun.py:160
std::list::emplace_back
T emplace_back(T... args)
ComponentManager::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Service locator (needed to access the MessageSvc)
Definition: ComponentManager.h:86
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
std::endl
T endl(T... args)
ServiceManager::getPriority
int getPriority(std::string_view name) const override
manage priorities of services
Definition: ServiceManager.cpp:486
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:86
GaudiDict::typeName
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31
std::list::begin
T begin(T... args)
ServiceManager::existsService
bool existsService(std::string_view name) const override
implementation of ISvcLocation::existsService
Definition: ServiceManager.cpp:232
Gaudi::StateMachine::INITIALIZED
@ INITIALIZED
Definition: StateMachine.h:25
isValidInterface
bool isValidInterface(IFace *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:334
std::vector::empty
T empty(T... args)
ServiceManager::m_gLock
std::recursive_mutex m_gLock
Mutex to synchronize shared service initialization between threads.
Definition: ServiceManager.h:187
std::list::end
T end(T... args)
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
std::prev
T prev(T... args)
ServiceManager::name
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
Definition: ServiceManager.h:120
Incident
Definition: Incident.h:27
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:175
ServiceManager::setPriority
StatusCode setPriority(std::string_view name, int pri) override
Definition: ServiceManager.cpp:493