The Gaudi Framework  v33r1 (b1225454)
ServiceManager Class Reference

The ServiceManager class is in charge of the creation of concrete instances of Services. More...

#include <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...
 
- Public Member Functions inherited from CommonMessagingBase
virtual ~CommonMessagingBase ()=default
 Virtual destructor. More...
 
const SmartIF< IMessageSvc > & msgSvc () const
 The standard message service. More...
 
MsgStreammsgStream () const
 Return an uninitialized MsgStream. More...
 
MsgStreammsgStream (const MSG::Level level) const
 Predefined configurable message stream for the efficient printouts. More...
 
MsgStreamalways () const
 shortcut for the method msgStream(MSG::ALWAYS) More...
 
MsgStreamfatal () const
 shortcut for the method msgStream(MSG::FATAL) More...
 
MsgStreamerr () const
 shortcut for the method msgStream(MSG::ERROR) More...
 
MsgStreamerror () const
 shortcut for the method msgStream(MSG::ERROR) More...
 
MsgStreamwarning () const
 shortcut for the method msgStream(MSG::WARNING) More...
 
MsgStreaminfo () const
 shortcut for the method msgStream(MSG::INFO) More...
 
MsgStreamdebug () const
 shortcut for the method msgStream(MSG::DEBUG) More...
 
MsgStreamverbose () const
 shortcut for the method msgStream(MSG::VERBOSE) More...
 
MsgStreammsg () const
 shortcut for the method msgStream(MSG::INFO) 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 53 of file ServiceManager.cpp.

54  : base_class( application, IService::interfaceID() ), m_appSvc( application ) {
55  // Set the service locator to myself
56  m_svcLocator = this;
57  addRef(); // increase ref count, so we live forever...
58 }
SmartIF< IService > m_appSvc
Pointer to the application IService interface.
SmartIF< ISvcLocator > m_svcLocator
Service locator (needed to access the MessageSvc)
extends base_class
Typedef to this class.
Definition: extends.h:24

◆ ~ServiceManager()

ServiceManager::~ServiceManager ( )
override

virtual destructor

Definition at line 61 of file ServiceManager.cpp.

61  {
62  //-- inform the orphan services that I am gone....
63  for ( auto& svc : m_listsvc ) svc.service->setServiceManager( nullptr );
64 }
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...

Member Function Documentation

◆ addService() [1/5]

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

implementation of ISvcManager::addService

Definition at line 110 of file ServiceManager.cpp.

112 {
113  auto it = find( svc );
114  auto lck = std::scoped_lock{m_gLock};
115  if ( it != m_listsvc.end() ) {
116  it->priority = prio; // if the service is already known, it is equivalent to a setPriority
117  it->active = true; // and make it active
118  } else {
119  m_listsvc.emplace_back( svc, prio, true );
120  }
121  return StatusCode::SUCCESS;
122 }
ListSvc::iterator find(std::string_view name)
std::recursive_mutex m_gLock
Mutex to synchronize shared service initialization between threads.
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
T end(T... args)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
T emplace_back(T... args)

◆ addService() [2/5]

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

implementation of ISvcManager::addService

Definition at line 126 of file ServiceManager.cpp.

128 {
129  auto it = find( typeName.name() ); // try to find the service by name
130  if ( it == m_listsvc.end() ) { // not found
131  // If the service does not exist, we create it
132  SmartIF<IService>& svc =
133  createService( typeName ); // WARNING: svc is now a reference to something that lives in m_listsvc
134  if ( !svc ) return StatusCode::FAILURE;
135  it = find( svc.get() ); // now it is in the list because createService added it
136  it->priority = prio;
138  if ( targetFSMState() >= Gaudi::StateMachine::INITIALIZED ) { // WARNING: this can trigger a recursion!!!
139  sc = svc->sysInitialize();
140  if ( sc.isSuccess() && targetFSMState() >= Gaudi::StateMachine::RUNNING ) { sc = svc->sysStart(); }
141  }
142  if ( sc.isFailure() ) { // if initialization failed, remove it from the list
143  error() << "Unable to initialize service \"" << typeName.name() << "\"" << endmsg;
144  auto lck = std::scoped_lock{m_gLock};
145  m_listsvc.erase( it );
146  // Note: removing it from the list + the SmartIF going out of scope should trigger the delete
147  // delete svc.get();
148  return sc;
149  }
150  // initialization successful, we can work with the service
151  // Move the just initialized service to the back of the list
152  // (we care more about order of initialization than of creation)
153  auto lck = std::scoped_lock{m_gLock};
154  m_listsvc.push_back( *it );
155  m_listsvc.erase( it );
156  it = std::prev( std::end( m_listsvc ) ); // last entry (the iterator was invalidated by erase)
157  } else {
158  // if the service is already known, it is equivalent to a setPriority
159  it->priority = prio;
160  }
161  // 'it' is defined because either we found the service or we created it
162  // Now we can activate the service
163  it->active = true; // and make it active
164  return StatusCode( StatusCode::SUCCESS, true );
165 }
Gaudi::StateMachine::State targetFSMState() const override
When we are in the middle of a transition, get the state where the transition is leading us.
ListSvc::iterator find(std::string_view name)
std::recursive_mutex m_gLock
Mutex to synchronize shared service initialization between threads.
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
T end(T... args)
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:86
T prev(T... args)
virtual StatusCode sysInitialize()=0
Initialize Service.
T push_back(T... args)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
T erase(T... args)
bool isSuccess() const
Definition: StatusCode.h:365
virtual StatusCode sysStart()=0
Start Service.
constexpr static const auto FAILURE
Definition: StatusCode.h:101
bool isFailure() const
Definition: StatusCode.h:145
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
SmartIF< IService > & createService(const Gaudi::Utils::TypeNameString &nametype) override
implementation of ISvcManager::createService NOTE: as this returns a &, we must guarantee that once c...
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202

◆ addService() [3/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() [4/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  }
Helper class to parse a string of format "type/name".
StatusCode addService(IService *svc, int prio=DEFAULT_SVC_PRIORITY) override
implementation of ISvcManager::addService

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

70 {
71  // Check if the service is already existing
72  if ( existsService( typeName.name() ) ) {
73  // return an error because a service with that name already exists
74  return no_service;
75  }
76 
77  const std::string& name = typeName.name();
78  std::string type = typeName.type();
79  if ( !typeName.haveType() ) { // the type is not explicit
80  // see we have some specific type mapping for the name
81  auto it = m_maptype.find( typeName.name() );
82  if ( it != m_maptype.end() ) {
83  type = it->second; // use the declared type
84  }
85  }
86 
88  auto ip = type.find( "__" );
89  if ( ip != std::string::npos ) type.erase( ip, type.length() );
90 
91  IService* service = Service::Factory::create( type, name, this ).release();
92  if ( !service ) {
93  fatal() << "No Service factory for " << type << " available." << endmsg;
94  return no_service;
95  }
96  // Check the compatibility of the version of the interface obtained
97  if ( !isValidInterface( service ) ) {
98  fatal() << "Incompatible interface IService version for " << type << endmsg;
99  return no_service;
100  }
101 
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 }
T end(T... args)
bool isValidInterface(I *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:351
bool existsService(std::string_view name) const override
implementation of ISvcLocation::existsService
MapType m_maptype
Map of service name and service type.
STL class.
T push_back(T... args)
General service interface definition.
Definition: IService.h:28
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
T find(T... args)
T back(T... args)
SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Returns a smart pointer to a service.
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
virtual void setServiceManager(ISvcManager *)=0
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
SmartIF< IService > service
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202

◆ 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  }
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
string s
Definition: gaudirun.py:328
constexpr static const auto FAILURE
Definition: StatusCode.h:101
SmartIF< IService > & createService(const Gaudi::Utils::TypeNameString &nametype) override
implementation of ISvcManager::createService NOTE: as this returns a &, we must guarantee that once c...

◆ declareSvcType()

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

implementation of ISvcManager::declareSvcType

Definition at line 250 of file ServiceManager.cpp.

252 {
253  m_maptype.insert_or_assign( std::move( svcname ), std::move( svctype ) );
254  return StatusCode::SUCCESS;
255 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
MapType m_maptype
Map of service name and service type.
T move(T... args)

◆ dump()

void ServiceManager::dump ( ) const
private

Definition at line 507 of file ServiceManager.cpp.

507  {
508 
509  auto& log = info();
510  log << "\n"
511  << "===================== listing all services ===================\n"
512  << " prior ref name active\n";
513 
514  for ( const auto& svc : m_listsvc ) {
515 
516  log.width( 6 );
517  log.flags( std::ios_base::right );
518  log << svc.priority << " ";
519  log.width( 5 );
520  log << svc.service->refCount() << " ";
521  log.width( 30 );
522  log.flags( std::ios_base::left );
523  log << svc.service->name() << " ";
524  log.width( 2 );
525  log << svc.active << std::endl;
526  }
527 
528  log << "=================================================================\n";
529  log << endmsg;
530 }
T endl(T... args)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202

◆ existsService()

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

implementation of ISvcLocation::existsService

Definition at line 223 of file ServiceManager.cpp.

225 {
226  return find( name ) != m_listsvc.end();
227 }
ListSvc::iterator find(std::string_view name)
T end(T... args)
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...

◆ finalize()

StatusCode ServiceManager::finalize ( )
override

Finalize (from INITIALIZED to CONFIGURED).

Definition at line 401 of file ServiceManager.cpp.

403 {
404  // make sure that HistogramDataSvc and THistSvc get finalized after the
405  // ToolSvc, and the FileMgr and StatusCodeSvc after that
406  int pri_tool = getPriority( "ToolSvc" );
407  if ( pri_tool != 0 ) {
408  setPriority( "THistSvc", pri_tool - 10 ).ignore();
409  setPriority( "ChronoStatSvc", pri_tool - 20 ).ignore();
410  setPriority( "AuditorSvc", pri_tool - 30 ).ignore();
411  setPriority( "NTupleSvc", pri_tool - 10 ).ignore();
412  setPriority( "HistogramDataSvc", pri_tool - 10 ).ignore();
413  // Preserve the relative ordering between HistogramDataSvc and HistogramPersistencySvc
414  setPriority( "HistogramPersistencySvc", pri_tool - 20 ).ignore();
415  setPriority( "HistorySvc", pri_tool - 30 ).ignore();
416  setPriority( "FileMgr", pri_tool - 40 ).ignore();
417  }
418 
419  // get list of PostFinalize clients
421  auto p_inc = service<IIncidentSvc>( "IncidentSvc", false );
422  if ( p_inc ) {
423  p_inc->getListeners( postFinList, IncidentType::SvcPostFinalize );
424  p_inc.reset();
425  }
426 
427  // make sure the StatusCodeSvc gets finalized really late:
428  setPriority( "StatusCodeSvc", -9999 ).ignore();
429 
430  // ensure that the list is ordered by priority
431  m_listsvc.sort();
432  // dump();
433 
434  StatusCode sc( StatusCode::SUCCESS, true );
435  {
436  // we work on a copy to avoid to operate twice on the services created on demand
437  // which are already in the correct state.
438  // only act on active services
439  // call finalize() for all services in reverse order
440  for ( const auto& svc : reverse( activeSvc( m_listsvc ) ) ) {
441  const std::string& name = svc->name();
442  // ignore the current state for the moment
443  // if( Gaudi::StateMachine::INITIALIZED == svc->state() )
444  DEBMSG << "Finalizing service " << name << endmsg;
445  if ( !svc->sysFinalize().isSuccess() ) {
446  warning() << "Finalization of service " << name << " failed" << endmsg;
447  sc = StatusCode::FAILURE;
448  }
449  }
450  }
451 
452  // call SvcPostFinalize on all clients
453  if ( !postFinList.empty() ) {
454  DEBMSG << "Will call SvcPostFinalize for " << postFinList.size() << " clients" << endmsg;
455  Incident inc( "ServiceManager", IncidentType::SvcPostFinalize );
456  for ( auto& itr : postFinList ) itr->handle( inc );
457  }
458 
459  // loop over all Active Services, removing them one by one.
460  // They should be deleted because the reference counting goes to 0.
461  DEBMSG << "Looping over all active services..." << endmsg;
462  auto it = m_listsvc.begin();
463  while ( it != m_listsvc.end() ) {
464  DEBMSG << "---- " << it->service->name() << " (refCount = " << it->service->refCount() << ")" << endmsg;
465  if ( it->service->refCount() < 1 ) {
466  warning() << "Too low reference count for " << it->service->name() << " (should not go below 1 at this point)"
467  << endmsg;
468  it->service->addRef();
469  }
470  if ( it->active ) {
471  it = m_listsvc.erase( it );
472  } else {
473  ++it;
474  }
475  }
476  return sc;
477 }
T empty(T... args)
int getPriority(std::string_view name) const override
manage priorities of services
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
::details::reverse_wrapper< T > reverse(T &&iterable)
Definition: reverse.h:59
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
T end(T... args)
STL class.
StatusCode setPriority(std::string_view name, int pri) override
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
T erase(T... args)
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
T size(T... args)
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:168
STL class.
T begin(T... args)
Base class for all Incidents (computing events).
Definition: Incident.h:27
constexpr static const auto FAILURE
Definition: StatusCode.h:101
T sort(T... args)
#define DEBMSG
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202

◆ find() [1/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  }
std::recursive_mutex m_gLock
Mutex to synchronize shared service initialization between threads.
T end(T... args)
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
T find(T... args)
T begin(T... args)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...

◆ find() [2/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  }
std::recursive_mutex m_gLock
Mutex to synchronize shared service initialization between threads.
T end(T... args)
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
T find(T... args)
T begin(T... args)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...

◆ find() [3/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  }
std::recursive_mutex m_gLock
Mutex to synchronize shared service initialization between threads.
T end(T... args)
T find(T... args)
T begin(T... args)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...

◆ find() [4/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  }
std::recursive_mutex m_gLock
Mutex to synchronize shared service initialization between threads.
T end(T... args)
T find(T... args)
T begin(T... args)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...

◆ getPriority()

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

manage priorities of services

Definition at line 480 of file ServiceManager.cpp.

480  {
481  //------------------------------------------------------------------------------
482  auto it = find( name );
483  return ( it != m_listsvc.end() ) ? it->priority : 0;
484 }
ListSvc::iterator find(std::string_view name)
T end(T... args)
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...

◆ getServices()

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

Return the list of Services.

Definition at line 213 of file ServiceManager.cpp.

215 {
218  []( ListSvc::const_reference i ) { return i.service.get(); } );
219  return m_listOfPtrs;
220 }
std::list< IService * > m_listOfPtrs
List of pointers to the know services used to implement getServices()
T end(T... args)
T clear(T... args)
T begin(T... args)
T back_inserter(T... args)
T transform(T... args)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...

◆ initialize()

StatusCode ServiceManager::initialize ( )
override

Initialization (from CONFIGURED to INITIALIZED).

Definition at line 258 of file ServiceManager.cpp.

260 {
261  // ensure that the list is ordered by priority
262  m_listsvc.sort();
263  // we work on a copy to avoid to operate twice on the services created on demand
264  // which are already in the correct state.
265 
266  StatusCode sc( StatusCode::SUCCESS, true );
267  // call initialize() for all services
268  for ( auto& it : activeSvc( m_listsvc ) ) {
269  const std::string& name = it->name();
270  switch ( it->FSMState() ) {
272  DEBMSG << "Service " << name << " already initialized" << endmsg;
273  break;
275  DEBMSG << "Initializing service " << name << endmsg;
276  sc = it->sysInitialize();
277  if ( !sc.isSuccess() ) {
278  error() << "Unable to initialize Service: " << name << endmsg;
279  return sc;
280  }
281  break;
282  default:
283  error() << "Service " << name << " not in the correct state to be initialized (" << it->FSMState() << ")"
284  << endmsg;
285  return StatusCode::FAILURE;
286  }
287  }
288  return StatusCode::SUCCESS;
289 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
STL class.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
constexpr static const auto FAILURE
Definition: StatusCode.h:101
T sort(T... args)
#define DEBMSG
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202

◆ loopCheckEnabled()

bool ServiceManager::loopCheckEnabled ( ) const
override

Get the value of the initialization loop check flag.

Definition at line 498 of file ServiceManager.cpp.

498 { return m_loopCheck; }
bool m_loopCheck
Check for service initialization loops.

◆ 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  }
STL class.

◆ outputLevelUpdate()

void ServiceManager::outputLevelUpdate ( )
override

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

Definition at line 532 of file ServiceManager.cpp.

532  {
533  resetMessaging();
534  for ( auto& svcItem : m_listsvc ) {
535  const auto svc = dynamic_cast<Service*>( svcItem.service.get() );
536  if ( svc ) svc->resetMessaging();
537  }
538 }
MSG::Level resetMessaging()
Reinitialize internal states.
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...

◆ reinitialize()

StatusCode ServiceManager::reinitialize ( )
override

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

Definition at line 359 of file ServiceManager.cpp.

361 {
362  // ensure that the list is ordered by priority
363  m_listsvc.sort();
364  // we work on a copy to avoid to operate twice on the services created on demand
365  // which are already in the correct state.
366  // only act on active services
367  StatusCode sc( StatusCode::SUCCESS, true );
368  // Re-Initialize all services
369  for ( auto& svc : activeSvc( m_listsvc ) ) {
370  sc = svc->sysReinitialize();
371  if ( !sc.isSuccess() ) {
372  error() << "Unable to re-initialize Service: " << svc->name() << endmsg;
373  return StatusCode::FAILURE;
374  }
375  }
376  return StatusCode::SUCCESS;
377 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
constexpr static const auto FAILURE
Definition: StatusCode.h:101
T sort(T... args)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202

◆ removeService() [1/2]

StatusCode ServiceManager::removeService ( IService svc)
override

implementation of ISvcManager::removeService

Definition at line 230 of file ServiceManager.cpp.

232 {
233  auto it = find( svc );
234  if ( it == m_listsvc.end() ) return StatusCode( StatusCode::FAILURE, true );
235  m_listsvc.erase( it );
236  return StatusCode( StatusCode::SUCCESS, true );
237 }
ListSvc::iterator find(std::string_view name)
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
T end(T... args)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
T erase(T... args)
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...

◆ removeService() [2/2]

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

implementation of ISvcManager::removeService

Definition at line 240 of file ServiceManager.cpp.

242 {
243  auto it = find( name );
244  if ( it == m_listsvc.end() ) return StatusCode::FAILURE;
245  m_listsvc.erase( it );
246  return StatusCode::SUCCESS;
247 }
ListSvc::iterator find(std::string_view name)
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
T end(T... args)
T erase(T... args)
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...

◆ restart()

StatusCode ServiceManager::restart ( )
override

Initialization (from RUNNING to RUNNING, via INITIALIZED).

Definition at line 380 of file ServiceManager.cpp.

382 {
383  // ensure that the list is ordered by priority
384  m_listsvc.sort();
385  // we work on a copy to avoid to operate twice on the services created on demand
386  // which are already in the correct state.
387  // only act on active services
388  StatusCode sc( StatusCode::SUCCESS, true );
389  // Re-Start all services
390  for ( auto& svc : activeSvc( m_listsvc ) ) {
391  sc = svc->sysRestart();
392  if ( !sc.isSuccess() ) {
393  error() << "Unable to re-start Service: " << svc->name() << endmsg;
394  return StatusCode::FAILURE;
395  }
396  }
397  return StatusCode::SUCCESS;
398 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
constexpr static const auto FAILURE
Definition: StatusCode.h:101
T sort(T... args)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202

◆ service() [1/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 169 of file ServiceManager.cpp.

169  {
170  const std::string& name = typeName.name();
171 
172  // Acquire the RAII lock to avoid simultaneous attempts from different threads to initialize a service
173 
174  auto* imut = [&] {
175  // get the global lock, then extract/create the service specific mutex
176  // then release global lock
177 
178  auto lk = std::scoped_lock{this->m_gLock};
179  auto mit = m_lockMap.find( name );
180  if ( mit == m_lockMap.end() ) {
182  .first;
183  }
184  return &mit->second;
185  }();
186 
187  {
188  // now we have the service specific lock on the above mutex
189  auto lk2 = std::scoped_lock{*imut};
190 
191  auto it = find( name );
192 
193  if ( it != m_listsvc.end() ) {
194  if ( m_loopCheck && ( createIf && it->service->FSMState() == Gaudi::StateMachine::CONFIGURED ) ) {
195  error() << "Initialization loop detected when creating service \"" << name << "\"" << endmsg;
196  return no_service;
197  }
198  return it->service;
199  }
200 
201  // Service not found. The user may be interested in one of the interfaces
202  // of the application manager itself
203  if ( name == "ApplicationMgr" || name == "APPMGR" || name == "" ) { return m_appSvc; }
204 
205  // last resort: we try to create the service
206  if ( createIf && addService( typeName ).isSuccess() ) { return find( name )->service; }
207 
208  return no_service;
209  }
210 }
SmartIF< IService > m_appSvc
Pointer to the application IService interface.
ListSvc::iterator find(std::string_view name)
T forward_as_tuple(T... args)
std::recursive_mutex m_gLock
Mutex to synchronize shared service initialization between threads.
bool m_loopCheck
Check for service initialization loops.
std::map< std::string, std::recursive_mutex > m_lockMap
T end(T... args)
STL class.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
T find(T... args)
T emplace(T... args)
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
StatusCode addService(IService *svc, int prio=DEFAULT_SVC_PRIORITY) override
implementation of ISvcManager::addService

◆ service() [2/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  }
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:25
SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Returns a smart pointer to a service.
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31

◆ serviceLocator()

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

Function needed by CommonMessaging.

Definition at line 66 of file ServiceManager.h.

66 { return m_svcLocator; }
SmartIF< ISvcLocator > m_svcLocator
Service locator (needed to access the MessageSvc)

◆ setLoopCheckEnabled()

void ServiceManager::setLoopCheckEnabled ( bool  en)
override

Set the value of the initialization loop check flag.

Definition at line 502 of file ServiceManager.cpp.

502 { m_loopCheck = en; }
bool m_loopCheck
Check for service initialization loops.

◆ setPriority()

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

Definition at line 487 of file ServiceManager.cpp.

487  {
488  //------------------------------------------------------------------------------
489  auto it = find( name );
490  if ( it == m_listsvc.end() ) return StatusCode::FAILURE;
491  it->priority = prio;
492  return StatusCode::SUCCESS;
493 }
ListSvc::iterator find(std::string_view name)
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
T end(T... args)
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...

◆ start()

StatusCode ServiceManager::start ( )
override

Start (from INITIALIZED to RUNNING).

Definition at line 292 of file ServiceManager.cpp.

294 {
295  // ensure that the list is ordered by priority
296  m_listsvc.sort();
297  // we work on a copy to avoid to operate twice on the services created on demand
298  // (which are already in the correct state.
299  // only act on active services
300  StatusCode sc( StatusCode::SUCCESS, true );
301  // call initialize() for all services
302  for ( auto& it : activeSvc( m_listsvc ) ) {
303  const std::string& name = it->name();
304  switch ( it->FSMState() ) {
306  DEBMSG << "Service " << name << " already started" << endmsg;
307  break;
309  DEBMSG << "Starting service " << name << endmsg;
310  sc = it->sysStart();
311  if ( !sc.isSuccess() ) {
312  error() << "Unable to start Service: " << name << endmsg;
313  return sc;
314  }
315  break;
316  default:
317  error() << "Service " << name << " not in the correct state to be started (" << it->FSMState() << ")" << endmsg;
318  return StatusCode::FAILURE;
319  }
320  }
321  return StatusCode::SUCCESS;
322 }
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
STL class.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
constexpr static const auto FAILURE
Definition: StatusCode.h:101
T sort(T... args)
#define DEBMSG
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202

◆ stop()

StatusCode ServiceManager::stop ( )
override

Stop (from RUNNING to INITIALIZED).

Definition at line 325 of file ServiceManager.cpp.

327 {
328  // ensure that the list is ordered by priority
329  m_listsvc.sort();
330  // we work on a copy to avoid to operate twice on the services created on demand
331  // which are already in the correct state.
332  // only act on active services
333 
334  StatusCode sc( StatusCode::SUCCESS, true );
335  // call stop() for all services
336  for ( const auto& svc : reverse( activeSvc( m_listsvc ) ) ) {
337  const std::string& name = svc->name();
338  switch ( svc->FSMState() ) {
340  DEBMSG << "Service " << name << " already stopped" << endmsg;
341  break;
343  DEBMSG << "Stopping service " << name << endmsg;
344  sc = svc->sysStop();
345  if ( !sc.isSuccess() ) {
346  error() << "Unable to stop Service: " << name << endmsg;
347  return sc;
348  }
349  break;
350  default:
351  DEBMSG << "Service " << name << " not in the correct state to be stopped (" << svc->FSMState() << ")" << endmsg;
352  return StatusCode::FAILURE;
353  }
354  }
355  return StatusCode::SUCCESS;
356 }
::details::reverse_wrapper< T > reverse(T &&iterable)
Definition: reverse.h:59
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
STL class.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
constexpr static const auto FAILURE
Definition: StatusCode.h:101
T sort(T... args)
#define DEBMSG
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202

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: