The Gaudi Framework  v36r7 (7f57a304)
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 134 of file ServiceManager.cpp.

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

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

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

◆ 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 
111  service->setServiceManager( this );
112  return m_listsvc.back().service; // DANGER: returns a reference to a SmartIF in m_listsvc, and hence does no longer
113  // allow relocations of those...
114 }

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

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

◆ dump()

void ServiceManager::dump ( ) const
private

Definition at line 512 of file ServiceManager.cpp.

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

◆ existsService()

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

implementation of ISvcLocation::existsService

Definition at line 231 of file ServiceManager.cpp.

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

◆ finalize()

StatusCode ServiceManager::finalize ( )
override

Finalize (from INITIALIZED to CONFIGURED).

Definition at line 409 of file ServiceManager.cpp.

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

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

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

◆ getServices()

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

Return the list of Services.

Definition at line 221 of file ServiceManager.cpp.

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

◆ initialize()

StatusCode ServiceManager::initialize ( )
override

Initialization (from CONFIGURED to INITIALIZED).

Definition at line 266 of file ServiceManager.cpp.

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

◆ loopCheckEnabled()

bool ServiceManager::loopCheckEnabled ( ) const
override

Get the value of the initialization loop check flag.

Definition at line 503 of file ServiceManager.cpp.

503 { 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 537 of file ServiceManager.cpp.

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

◆ reinitialize()

StatusCode ServiceManager::reinitialize ( )
override

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

Definition at line 367 of file ServiceManager.cpp.

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

◆ removeService() [1/2]

StatusCode ServiceManager::removeService ( IService svc)
override

implementation of ISvcManager::removeService

Definition at line 238 of file ServiceManager.cpp.

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

◆ removeService() [2/2]

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

implementation of ISvcManager::removeService

Definition at line 248 of file ServiceManager.cpp.

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

◆ restart()

StatusCode ServiceManager::restart ( )
override

Initialization (from RUNNING to RUNNING, via INITIALIZED).

Definition at line 388 of file ServiceManager.cpp.

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

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

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

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

507 { m_loopCheck = en; }

◆ setPriority()

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

Definition at line 492 of file ServiceManager.cpp.

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

◆ start()

StatusCode ServiceManager::start ( )
override

Start (from INITIALIZED to RUNNING).

Definition at line 300 of file ServiceManager.cpp.

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

◆ stop()

StatusCode ServiceManager::stop ( )
override

Stop (from RUNNING to INITIALIZED).

Definition at line 333 of file ServiceManager.cpp.

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

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:118
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:177
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:485
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:231
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:492