The Gaudi Framework  v30r3 (a5ef0a68)
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 GaudiUtils::Map< std::string, std::stringMapType
 
- 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 (const std::string &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 (const std::string &name) override
 implementation of ISvcManager::removeService More...
 
StatusCode declareSvcType (const std::string &svcname, const 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 (const std::string &name) const override
 manage priorities of services More...
 
StatusCode setPriority (const std::string &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...
 
- 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...
 
MSG::Level outputLevel () const
 Backward compatibility function for getting the output level. 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 Types

typedef boost::recursive_mutex Mutex_t
 Mutex to synchronize shared service initialization between threads. More...
 
typedef boost::lock_guard< Mutex_tLockGuard_t
 

Private Member Functions

ListSvc::iterator find (const std::string &name)
 
ListSvc::const_iterator find (const std::string &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
 
Mutex_t m_gLock
 
std::map< std::string, Mutex_tm_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 36 of file ServiceManager.h.

Member Typedef Documentation

Definition at line 50 of file ServiceManager.h.

typedef boost::lock_guard<Mutex_t> ServiceManager::LockGuard_t
private

Definition at line 185 of file ServiceManager.h.

typedef boost::recursive_mutex ServiceManager::Mutex_t
private

Mutex to synchronize shared service initialization between threads.

Definition at line 184 of file ServiceManager.h.

Constructor & Destructor Documentation

ServiceManager::ServiceManager ( IInterface application)

default creator

Definition at line 45 of file ServiceManager.cpp.

46  : base_class( application, IService::interfaceID() ), m_appSvc( application )
47 {
48  // Set the service locator to myself
49  m_svcLocator = this;
50  addRef(); // increase ref count, so we live forever...
51 }
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:15
ServiceManager::~ServiceManager ( )
override

virtual destructor

Definition at line 54 of file ServiceManager.cpp.

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

Member Function Documentation

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

implementation of ISvcManager::addService

Definition at line 104 of file ServiceManager.cpp.

106 {
107  ListSvc::iterator it = find( svc );
108  LockGuard_t lck( m_gLock );
109  if ( it != m_listsvc.end() ) {
110  it->priority = prio; // if the service is already known, it is equivalent to a setPriority
111  it->active = true; // and make it active
112  } else {
113  m_listsvc.emplace_back( svc, prio, true );
114  }
115  return StatusCode::SUCCESS;
116 }
boost::lock_guard< Mutex_t > LockGuard_t
T end(T...args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
ListSvc::iterator find(const std::string &name)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because...
T emplace_back(T...args)
StatusCode ServiceManager::addService ( const Gaudi::Utils::TypeNameString typeName,
int  prio = DEFAULT_SVC_PRIORITY 
)
override

implementation of ISvcManager::addService

Definition at line 120 of file ServiceManager.cpp.

122 {
123  auto it = find( typeName.name() ); // try to find the service by name
124  if ( it == m_listsvc.end() ) { // not found
125  // If the service does not exist, we create it
126  SmartIF<IService>& svc =
127  createService( typeName ); // WARNING: svc is now a reference to something that lives in m_listsvc
128  if ( !svc ) return StatusCode::FAILURE;
129  it = find( svc.get() ); // now it is in the list because createService added it
130  it->priority = prio;
132  if ( targetFSMState() >= Gaudi::StateMachine::INITIALIZED ) { // WARNING: this can trigger a recursion!!!
133  sc = svc->sysInitialize();
135  sc = svc->sysStart();
136  }
137  }
138  if ( sc.isFailure() ) { // if initialization failed, remove it from the list
139  error() << "Unable to initialize service \"" << typeName.name() << "\"" << endmsg;
140  LockGuard_t lck( m_gLock );
141  m_listsvc.erase( it );
142  // Note: removing it from the list + the SmartIF going out of scope should trigger the delete
143  // delete svc.get();
144  return sc;
145  }
146  // initialization successful, we can work with the service
147  // Move the just initialized service to the back of the list
148  // (we care more about order of initialization than of creation)
149  LockGuard_t lck( m_gLock );
150  m_listsvc.push_back( *it );
151  m_listsvc.erase( it );
152  it = std::prev( std::end( m_listsvc ) ); // last entry (the iterator was invalidated by erase)
153  } else {
154  // if the service is already known, it is equivalent to a setPriority
155  it->priority = prio;
156  }
157  // 'it' is defined because either we found the service or we created it
158  // Now we can activate the service
159  it->active = true; // and make it active
160  return StatusCode( StatusCode::SUCCESS, true );
161 }
constexpr static const auto FAILURE
Definition: StatusCode.h:88
boost::lock_guard< Mutex_t > LockGuard_t
bool isSuccess() const
Definition: StatusCode.h:287
Gaudi::StateMachine::State targetFSMState() const override
When we are in the middle of a transition, get the state where the transition is leading us...
T end(T...args)
bool isFailure() const
Definition: StatusCode.h:139
T prev(T...args)
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:82
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:51
T erase(T...args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
virtual StatusCode sysStart()=0
Start Service.
ListSvc::iterator find(const std::string &name)
const std::string & name() const
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:209
SmartIF< IService > & ServiceManager::createService ( const Gaudi::Utils::TypeNameString nametype)
override

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

Fix-Me:
: what does this mean?

Definition at line 62 of file ServiceManager.cpp.

64 {
65  // Check if the service is already existing
66  if ( existsService( typeName.name() ) ) {
67  // return an error because a service with that name already exists
68  return no_service;
69  }
70 
71  const std::string& name = typeName.name();
72  std::string type = typeName.type();
73  if ( !typeName.haveType() ) { // the type is not explicit
74  // see we have some specific type mapping for the name
75  auto it = m_maptype.find( typeName.name() );
76  if ( it != m_maptype.end() ) {
77  type = it->second; // use the declared type
78  }
79  }
80 
82  auto ip = type.find( "__" );
83  if ( ip != std::string::npos ) type.erase( ip, type.length() );
84 
85  IService* service = Service::Factory::create( type, name, this ).release();
86  if ( !service ) {
87  fatal() << "No Service factory for " << type << " available." << endmsg;
88  return no_service;
89  }
90  // Check the compatibility of the version of the interface obtained
91  if ( !isValidInterface( service ) ) {
92  fatal() << "Incompatible interface IService version for " << type << endmsg;
93  return no_service;
94  }
95 
96  m_listsvc.push_back( service );
97  service->setServiceManager( this );
98  return m_listsvc.back().service; // DANGER: returns a reference to a SmartIF in m_listsvc, and hence does no longer
99  // allow relocations of those...
100 }
bool isValidInterface(I *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:378
MapType m_maptype
Map of service name and service type.
STL class.
T push_back(T...args)
bool existsService(const std::string &name) const override
implementation of ISvcLocation::existsService
iterator end()
Definition: Map.h:134
General service interface definition.
Definition: IService.h:18
T erase(T...args)
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
iterator find(const key_type &key)
Definition: Map.h:151
T find(T...args)
T length(T...args)
T back(T...args)
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
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:23
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because...
virtual void setServiceManager(ISvcManager *)=0
SmartIF< IService > service
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
StatusCode ServiceManager::declareSvcType ( const std::string svcname,
const std::string svctype 
)
override

implementation of ISvcManager::declareSvcType

Definition at line 252 of file ServiceManager.cpp.

254 {
255  // once we get to C++17, replace with m_maptype.insert_or_assign...
256  auto p = m_maptype.emplace( svcname, svctype );
257  if ( !p.second ) p.first->second = svctype;
258  return StatusCode::SUCCESS;
259 }
MapType m_maptype
Map of service name and service type.
std::pair< iterator, bool > emplace(Args &&...args)
Definition: Map.h:169
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
void ServiceManager::dump ( ) const
private

Definition at line 513 of file ServiceManager.cpp.

514 {
515 
516  auto& log = info();
517  log << "\n"
518  << "===================== listing all services ===================\n"
519  << " prior ref name active\n";
520 
521  for ( const auto& svc : m_listsvc ) {
522 
523  log.width( 6 );
524  log.flags( std::ios_base::right );
525  log << svc.priority << " ";
526  log.width( 5 );
527  log << svc.service->refCount() << " ";
528  log.width( 30 );
529  log.flags( std::ios_base::left );
530  log << svc.service->name() << " ";
531  log.width( 2 );
532  log << svc.active << std::endl;
533  }
534 
535  log << "=================================================================\n";
536  log << endmsg;
537 }
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
T endl(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:209
bool ServiceManager::existsService ( const std::string name) const
override

implementation of ISvcLocation::existsService

Definition at line 225 of file ServiceManager.cpp.

227 {
228  return find( name ) != m_listsvc.end();
229 }
T end(T...args)
ListSvc::iterator find(const std::string &name)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because...
StatusCode ServiceManager::finalize ( )
override

Finalize (from INITIALIZED to CONFIGURED).

Definition at line 405 of file ServiceManager.cpp.

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

Definition at line 136 of file ServiceManager.h.

137  {
138  boost::lock_guard<boost::recursive_mutex> lck( m_gLock );
139  return std::find( m_listsvc.begin(), m_listsvc.end(), name );
140  }
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...
ListSvc::const_iterator ServiceManager::find ( const std::string name) const
inlineprivate

Definition at line 141 of file ServiceManager.h.

142  {
143  boost::lock_guard<boost::recursive_mutex> lck( m_gLock );
144  return std::find( m_listsvc.begin(), m_listsvc.end(), name );
145  }
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...
ListSvc::iterator ServiceManager::find ( const IService ptr)
inlineprivate

Definition at line 146 of file ServiceManager.h.

147  {
148  boost::lock_guard<boost::recursive_mutex> lck( m_gLock );
149  return std::find( m_listsvc.begin(), m_listsvc.end(), ptr );
150  }
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...
ListSvc::const_iterator ServiceManager::find ( const IService ptr) const
inlineprivate

Definition at line 151 of file ServiceManager.h.

152  {
153  boost::lock_guard<boost::recursive_mutex> lck( m_gLock );
154  return std::find( m_listsvc.begin(), m_listsvc.end(), ptr );
155  }
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...
int ServiceManager::getPriority ( const std::string name) const
override

manage priorities of services

Definition at line 484 of file ServiceManager.cpp.

485 {
486  //------------------------------------------------------------------------------
487  auto it = find( name );
488  return ( it != m_listsvc.end() ) ? it->priority : 0;
489 }
T end(T...args)
ListSvc::iterator find(const std::string &name)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because...
const std::list< IService * > & ServiceManager::getServices ( ) const
override

Return the list of Services.

Definition at line 215 of file ServiceManager.cpp.

217 {
220  []( ListSvc::const_reference i ) { return const_cast<IService*>( i.service.get() ); } );
221  return m_listOfPtrs;
222 }
std::list< IService * > m_listOfPtrs
List of pointers to the know services used to implement getServices()
T end(T...args)
General service interface definition.
Definition: IService.h:18
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...
StatusCode ServiceManager::initialize ( )
override

Initialization (from CONFIGURED to INITIALIZED).

Definition at line 262 of file ServiceManager.cpp.

264 {
265  // ensure that the list is ordered by priority
266  m_listsvc.sort();
267  // we work on a copy to avoid to operate twice on the services created on demand
268  // which are already in the correct state.
269 
270  StatusCode sc( StatusCode::SUCCESS, true );
271  // call initialize() for all services
272  for ( auto& it : activeSvc( m_listsvc ) ) {
273  const std::string& name = it->name();
274  switch ( it->FSMState() ) {
276  DEBMSG << "Service " << name << " already initialized" << endmsg;
277  break;
279  DEBMSG << "Initializing service " << name << endmsg;
280  sc = it->sysInitialize();
281  if ( !sc.isSuccess() ) {
282  error() << "Unable to initialize Service: " << name << endmsg;
283  return sc;
284  }
285  break;
286  default:
287  error() << "Service " << name << " not in the correct state to be initialized (" << it->FSMState() << ")"
288  << endmsg;
289  return StatusCode::FAILURE;
290  }
291  }
292  return StatusCode::SUCCESS;
293 }
constexpr static const auto FAILURE
Definition: StatusCode.h:88
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:51
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
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:209
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; }
bool m_loopCheck
Check for service initialization loops.
const std::string& ServiceManager::name ( ) const
inlineoverride

Return the name of the manager (implementation of INamedInterface)

Definition at line 111 of file ServiceManager.h.

112  {
113  static std::string _name = "ServiceManager";
114  return _name;
115  }
STL class.
void ServiceManager::outputLevelUpdate ( )
override

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

Definition at line 539 of file ServiceManager.cpp.

540 {
541  resetMessaging();
542  for ( auto& svcItem : m_listsvc ) {
543  const auto svc = dynamic_cast<Service*>( svcItem.service.get() );
544  if ( svc ) svc->resetMessaging();
545  }
546 }
MSG::Level resetMessaging()
Reinitialize internal states.
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because...
Base class for all services.
Definition: Service.h:36
StatusCode ServiceManager::reinitialize ( )
override

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

Definition at line 363 of file ServiceManager.cpp.

365 {
366  // ensure that the list is ordered by priority
367  m_listsvc.sort();
368  // we work on a copy to avoid to operate twice on the services created on demand
369  // which are already in the correct state.
370  // only act on active services
371  StatusCode sc( StatusCode::SUCCESS, true );
372  // Re-Initialize all services
373  for ( auto& svc : activeSvc( m_listsvc ) ) {
374  sc = svc->sysReinitialize();
375  if ( !sc.isSuccess() ) {
376  error() << "Unable to re-initialize Service: " << svc->name() << endmsg;
377  return StatusCode::FAILURE;
378  }
379  }
380  return StatusCode::SUCCESS;
381 }
constexpr static const auto FAILURE
Definition: StatusCode.h:88
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
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:209
StatusCode ServiceManager::removeService ( IService svc)
override

implementation of ISvcManager::removeService

Definition at line 232 of file ServiceManager.cpp.

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

implementation of ISvcManager::removeService

Definition at line 242 of file ServiceManager.cpp.

244 {
245  auto it = find( name );
246  if ( it == m_listsvc.end() ) return StatusCode::FAILURE;
247  m_listsvc.erase( it );
248  return StatusCode::SUCCESS;
249 }
constexpr static const auto FAILURE
Definition: StatusCode.h:88
T end(T...args)
T erase(T...args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
ListSvc::iterator find(const std::string &name)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because...
StatusCode ServiceManager::restart ( )
override

Initialization (from RUNNING to RUNNING, via INITIALIZED).

Definition at line 384 of file ServiceManager.cpp.

386 {
387  // ensure that the list is ordered by priority
388  m_listsvc.sort();
389  // we work on a copy to avoid to operate twice on the services created on demand
390  // which are already in the correct state.
391  // only act on active services
392  StatusCode sc( StatusCode::SUCCESS, true );
393  // Re-Start all services
394  for ( auto& svc : activeSvc( m_listsvc ) ) {
395  sc = svc->sysRestart();
396  if ( !sc.isSuccess() ) {
397  error() << "Unable to re-start Service: " << svc->name() << endmsg;
398  return StatusCode::FAILURE;
399  }
400  }
401  return StatusCode::SUCCESS;
402 }
constexpr static const auto FAILURE
Definition: StatusCode.h:88
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
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:209
SmartIF< IService > & ServiceManager::service ( const Gaudi::Utils::TypeNameString typeName,
const bool  createIf = true 
)
override

Returns a smart pointer to a service.

Definition at line 165 of file ServiceManager.cpp.

166 {
167  const std::string& name = typeName.name();
168 
169  // Acquire the RAII lock to avoid simultaneous attempts from different threads to initialize a service
170 
171  Mutex_t* imut;
172  {
173  // get the global lock, then extract/create the service specific mutex
174  // then release global lock
175 
176  LockGuard_t lk( m_gLock );
177  auto mit = m_lockMap.find( name );
178  if ( mit == m_lockMap.end() ) {
180  .first;
181  }
182  imut = &mit->second;
183  }
184 
185  {
186  // now we have the service specific lock on the above mutex
187  LockGuard_t lk2( *imut );
188 
189  auto it = find( name );
190 
191  if ( it != m_listsvc.end() ) {
192  if ( m_loopCheck && ( createIf && it->service->FSMState() == Gaudi::StateMachine::CONFIGURED ) ) {
193  error() << "Initialization loop detected when creating service \"" << name << "\"" << endmsg;
194  return no_service;
195  }
196  return it->service;
197  }
198 
199  // Service not found. The user may be interested in one of the interfaces
200  // of the application manager itself
201  if ( name == "ApplicationMgr" || name == "APPMGR" || name == "" ) {
202  return m_appSvc;
203  }
204 
205  // last resort: we try to create the service
206  if ( createIf && addService( typeName ).isSuccess() ) {
207  return find( name )->service;
208  }
209 
210  return no_service;
211  }
212 }
SmartIF< IService > m_appSvc
Pointer to the application IService interface.
boost::lock_guard< Mutex_t > LockGuard_t
T forward_as_tuple(T...args)
bool m_loopCheck
Check for service initialization loops.
boost::recursive_mutex Mutex_t
Mutex to synchronize shared service initialization between threads.
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)
ListSvc::iterator find(const std::string &name)
const std::string & name() const
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:209
std::map< std::string, Mutex_t > m_lockMap
StatusCode addService(IService *svc, int prio=DEFAULT_SVC_PRIORITY) override
implementation of ISvcManager::addService
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 122 of file ServiceManager.h.

123  {
124  return SmartIF<T>{service( typeName, createIf )};
125  }
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:15
SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Returns a smart pointer to a service.
SmartIF<ISvcLocator>& ServiceManager::serviceLocator ( ) const
inlineoverride

Function needed by CommonMessaging.

Definition at line 57 of file ServiceManager.h.

57 { return m_svcLocator; }
SmartIF< ISvcLocator > m_svcLocator
Service locator (needed to access the MessageSvc)
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; }
bool m_loopCheck
Check for service initialization loops.
StatusCode ServiceManager::setPriority ( const std::string name,
int  pri 
)
override

Definition at line 492 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 }
constexpr static const auto FAILURE
Definition: StatusCode.h:88
T end(T...args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
ListSvc::iterator find(const std::string &name)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because...
StatusCode ServiceManager::start ( )
override

Start (from INITIALIZED to RUNNING).

Definition at line 296 of file ServiceManager.cpp.

298 {
299  // ensure that the list is ordered by priority
300  m_listsvc.sort();
301  // we work on a copy to avoid to operate twice on the services created on demand
302  // (which are already in the correct state.
303  // only act on active services
304  StatusCode sc( StatusCode::SUCCESS, true );
305  // call initialize() for all services
306  for ( auto& it : activeSvc( m_listsvc ) ) {
307  const std::string& name = it->name();
308  switch ( it->FSMState() ) {
310  DEBMSG << "Service " << name << " already started" << endmsg;
311  break;
313  DEBMSG << "Starting service " << name << endmsg;
314  sc = it->sysStart();
315  if ( !sc.isSuccess() ) {
316  error() << "Unable to start Service: " << name << endmsg;
317  return sc;
318  }
319  break;
320  default:
321  error() << "Service " << name << " not in the correct state to be started (" << it->FSMState() << ")" << endmsg;
322  return StatusCode::FAILURE;
323  }
324  }
325  return StatusCode::SUCCESS;
326 }
constexpr static const auto FAILURE
Definition: StatusCode.h:88
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:51
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
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:209
StatusCode ServiceManager::stop ( )
override

Stop (from RUNNING to INITIALIZED).

Definition at line 329 of file ServiceManager.cpp.

331 {
332  // ensure that the list is ordered by priority
333  m_listsvc.sort();
334  // we work on a copy to avoid to operate twice on the services created on demand
335  // which are already in the correct state.
336  // only act on active services
337 
338  StatusCode sc( StatusCode::SUCCESS, true );
339  // call stop() for all services
340  for ( const auto& svc : reverse( activeSvc( m_listsvc ) ) ) {
341  const std::string& name = svc->name();
342  switch ( svc->FSMState() ) {
344  DEBMSG << "Service " << name << " already stopped" << endmsg;
345  break;
347  DEBMSG << "Stopping service " << name << endmsg;
348  sc = svc->sysStop();
349  if ( !sc.isSuccess() ) {
350  error() << "Unable to stop Service: " << name << endmsg;
351  return sc;
352  }
353  break;
354  default:
355  DEBMSG << "Service " << name << " not in the correct state to be stopped (" << svc->FSMState() << ")" << endmsg;
356  return StatusCode::FAILURE;
357  }
358  }
359  return StatusCode::SUCCESS;
360 }
constexpr static const auto FAILURE
Definition: StatusCode.h:88
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:51
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
details::reverse_wrapper< T > reverse(T &&iterable)
Definition: reverse.h:52
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
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:209

Member Data Documentation

SmartIF<IService> ServiceManager::m_appSvc
private

Pointer to the application IService interface.

Definition at line 176 of file ServiceManager.h.

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

Definition at line 181 of file ServiceManager.h.

Mutex_t ServiceManager::m_gLock
mutableprivate

Definition at line 187 of file ServiceManager.h.

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

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

Definition at line 179 of file ServiceManager.h.

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

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

Definition at line 188 of file ServiceManager.h.

bool ServiceManager::m_loopCheck = true
private

Check for service initialization loops.

Definition at line 173 of file ServiceManager.h.

MapType ServiceManager::m_maptype
private

Map of service name and service type.

Definition at line 172 of file ServiceManager.h.


The documentation for this class was generated from the following files: