3 #include "GaudiKernel/IService.h"
4 #include "GaudiKernel/MsgStream.h"
5 #include "GaudiKernel/TypeNameString.h"
6 #include "GaudiKernel/System.h"
7 #include "GaudiKernel/Service.h"
8 #include "GaudiKernel/ObjectFactory.h"
9 #include "GaudiKernel/SmartIF.h"
10 #include "GaudiKernel/IIncidentSvc.h"
11 #include "GaudiKernel/IIncidentListener.h"
12 #include "GaudiKernel/Incident.h"
13 #include "GaudiKernel/reverse.h"
20 #define ON_DEBUG if (msgLevel(MSG::DEBUG))
21 #define ON_VERBOSE if (msgLevel(MSG::VERBOSE))
23 #define DEBMSG ON_DEBUG debug()
24 #define VERMSG ON_VERBOSE verbose()
32 std::vector<IService*> activeSvc(
const C& lst) {
33 std::vector<IService*> v; v.reserve(lst.size());
35 if (
i.active) v.push_back(
i.service.get());
54 for (
auto& svc :
m_listsvc) svc.service->setServiceManager(
nullptr);
68 const std::string &
name = typeName.
name();
79 auto ip = type.find(
"__");
80 if ( ip != std::string::npos) type.erase(ip,type.length());
84 fatal() <<
"No Service factory for " << type <<
" available." <<
endmsg;
89 fatal() <<
"Incompatible interface IService version for " << type <<
endmsg;
133 error() <<
"Unable to initialize service \"" << typeName.
name() <<
"\""
159 const std::string &
name = typeName.
name();
164 auto it =
find(name);
170 <<
"Initialization loop detected when creating service \"" << name
180 if( name ==
"ApplicationMgr" ||
187 if ( createIf &&
addService(typeName).isSuccess()){
188 return find(name)->service;
201 [](ListSvc::const_reference
i) {
202 return const_cast<IService*
>(i.service.get());
211 return find(
name) != m_listsvc.end();
228 auto it =
find(name);
236 const std::string& svctype )
241 if ( !p.second ) p.first->second = svctype;
257 const std::string&
name = it->name();
258 switch (it->FSMState()) {
260 DEBMSG <<
"Service " << name <<
" already initialized" <<
endmsg;
264 sc = it->sysInitialize();
266 error() <<
"Unable to initialize Service: " << name <<
endmsg;
270 error() <<
"Service " << name
271 <<
" not in the correct state to be initialized ("
272 << it->FSMState() <<
")" <<
endmsg;
291 const std::string&
name = it->name();
292 switch (it->FSMState()) {
294 DEBMSG <<
"Service " << name
295 <<
" already started" <<
endmsg;
301 error() <<
"Unable to start Service: " << name <<
endmsg;
305 error() <<
"Service " << name
306 <<
" not in the correct state to be started ("
307 << it->FSMState() <<
")" <<
endmsg;
328 const std::string&
name = svc->name();
329 switch (svc->FSMState()) {
331 DEBMSG <<
"Service " << name <<
" already stopped" <<
endmsg;
337 error() <<
"Unable to stop Service: " << name <<
endmsg;
341 DEBMSG <<
"Service " << name
342 <<
" not in the correct state to be stopped ("
343 << svc->FSMState() <<
")" <<
endmsg;
362 sc = svc->sysReinitialize();
364 error() <<
"Unable to re-initialize Service: " << svc->name() <<
endmsg;
383 sc = svc->sysRestart();
385 error() <<
"Unable to re-start Service: " << svc->name() <<
endmsg;
413 std::vector<IIncidentListener*> postFinList;
414 auto p_inc = service<IIncidentSvc>(
"IncidentSvc",
false);
416 p_inc->getListeners(postFinList,IncidentType::SvcPostFinalize);
434 const std::string&
name = svc->name();
438 if ( !svc->sysFinalize().isSuccess() ) {
439 warning() <<
"Finalization of service " << name <<
" failed" <<
endmsg;
446 if (!postFinList.empty()) {
447 DEBMSG <<
"Will call SvcPostFinalize for " << postFinList.size() <<
" clients"
449 Incident inc(
"ServiceManager", IncidentType::SvcPostFinalize);
450 for (
auto& itr : postFinList) itr->handle(inc);
455 DEBMSG <<
"Looping over all active services..." <<
endmsg;
458 DEBMSG <<
"---- " << it->service->name()
459 <<
" (refCount = " << it->service->refCount() <<
")"
461 if (it->service->refCount() < 1) {
462 warning() <<
"Too low reference count for " << it->service->name()
463 <<
" (should not go below 1 at this point)" <<
endmsg;
464 it->service->addRef();
480 auto it =
find(name);
481 return (it !=
m_listsvc.end()) ? it->priority: 0;
488 auto it =
find(name);
516 <<
"===================== listing all services ===================\n"
517 <<
" prior ref name active\n";
522 log.
flags(std::ios_base::right);
523 log << svc.priority <<
" ";
525 log << svc.service->refCount() <<
" ";
527 log.
flags(std::ios_base::left);
528 log << svc.service->name() <<
" ";
530 log << svc.active << std::endl;
534 log <<
"=================================================================\n";
The ServiceManager class is in charge of the creation of concrete instances of Services.
std::list< IService * > m_listOfPtrs
List of pointers to the know services used to implement getServices()
Definition of the MsgStream class used to transmit messages.
void setLoopCheckEnabled(bool en) override
Set the value of the initialization loop check flag.
SmartIF< IService > m_appSvc
Pointer to the application IService interface.
reverse_wrapper< T > reverse(T &&iterable)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
std::pair< iterator, bool > emplace(Args &&...args)
int getPriority(const std::string &name) const override
manage priorities of services
bool isSuccess() const
Test for a status code of SUCCESS.
auto begin(reverse_wrapper< T > &w)
bool m_loopCheck
Check for service initialization loops.
bool existsService(const std::string &name) const override
implementation of ISvcLocation::existsService
ServiceManager(IInterface *application)
default creator
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
bool isFailure() const
Test for a status code of FAILURE.
MapType m_maptype
Map of service name and service type.
TYPE * get() const
Get interface pointer.
virtual StatusCode sysInitialize()=0
Initialize Service.
#define DECLARE_OBJECT_FACTORY(x)
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Helper class to parse a string of format "type/name".
std::recursive_mutex m_svcinitmutex
Mutex to synchronize shared service initialization between threads.
bool loopCheckEnabled() const override
Get the value of the initialization loop check flag.
General service interface definition.
auto end(reverse_wrapper< T > &w)
This class is used for returning status codes from appropriate routines.
Definition of the basic interface.
const std::list< IService * > & getServices() const override
Return the list of Services.
~ServiceManager() override
virtual destructor
iterator find(const key_type &key)
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
StatusCode start() override
Start (from INITIALIZED to RUNNING).
virtual StatusCode sysStart()=0
Start Service.
Base class used to extend a class implementing other interfaces.
long flags() const
IOS emulation.
const std::string & type() const
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Base class for all Incidents (computing events).
ListSvc::iterator find(const std::string &name)
StatusCode declareSvcType(const std::string &svcname, const std::string &svctype) override
implementation of ISvcManager::declareSvcType
bool isValidInterface(I *i)
Templated function that throws an exception if the version if the interface implemented by the object...
const std::string & name() const
StatusCode setPriority(const std::string &name, int pri) override
StatusCode removeService(IService *svc) override
implementation of ISvcManager::removeService
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)
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF for all services – and because...
virtual void setServiceManager(ISvcManager *)=0
SmartIF< IService > & createService(const Gaudi::Utils::TypeNameString &nametype) override
implementation of ISvcManager::createService NOTE: as this returns a &, we must guarantee that once c...
StatusCode addService(IService *svc, int prio=DEFAULT_SVC_PRIORITY) override
implementation of ISvcManager::addService
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).