|
Gaudi Framework, version v23r2 |
| Home | Generated: Thu Jun 28 2012 |
The ServiceManager class is in charge of the creation of concrete instances of Services. More...
#include <ServiceManager.h>


Classes | |
| struct | ServiceItem |
Public Types | |
| typedef std::list< ServiceItem > | ListSvc |
| typedef GaudiUtils::Map < std::string, std::string > | MapType |
Public Member Functions | |
| ServiceManager (IInterface *application) | |
| default creator | |
| SmartIF< ISvcLocator > & | serviceLocator () const |
| Function needed by CommonMessaging. | |
| virtual | ~ServiceManager () |
| virtual destructor | |
| virtual const std::list < IService * > & | getServices () const |
| Return the list of Services. | |
| virtual bool | existsService (const std::string &name) const |
| implementation of ISvcLocation::existsService | |
| virtual StatusCode | addService (IService *svc, int prio=10) |
| implementation of ISvcManager::addService | |
| virtual StatusCode | addService (const Gaudi::Utils::TypeNameString &typeName, int prio=10) |
| implementation of ISvcManager::addService | |
| virtual StatusCode | removeService (IService *svc) |
| implementation of ISvcManager::removeService | |
| virtual StatusCode | removeService (const std::string &name) |
| implementation of ISvcManager::removeService | |
| virtual StatusCode | declareSvcType (const std::string &svcname, const std::string &svctype) |
| implementation of ISvcManager::declareSvcType | |
| virtual SmartIF< IService > & | createService (const Gaudi::Utils::TypeNameString &nametype) |
| implementation of ISvcManager::createService | |
| virtual StatusCode | initialize () |
| Initialization (from CONFIGURED to INITIALIZED). | |
| virtual StatusCode | start () |
| Start (from INITIALIZED to RUNNING). | |
| virtual StatusCode | stop () |
| Stop (from RUNNING to INITIALIZED). | |
| virtual StatusCode | finalize () |
| Finalize (from INITIALIZED to CONFIGURED). | |
| virtual StatusCode | reinitialize () |
| Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED). | |
| virtual StatusCode | restart () |
| Initialization (from RUNNING to RUNNING, via INITIALIZED). | |
| virtual int | getPriority (const std::string &name) const |
| manage priorities of services | |
| virtual StatusCode | setPriority (const std::string &name, int pri) |
| virtual bool | loopCheckEnabled () const |
| Get the value of the initialization loop check flag. | |
| virtual void | setLoopCheckEnabled (bool en) |
| Set the value of the initialization loop check flag. | |
| const std::string & | name () const |
| Return the name of the manager (implementation of INamedInterface) | |
| virtual SmartIF< IService > & | service (const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) |
| Returns a smart pointer to a service. | |
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 |
Private Attributes | |
| ListSvc | m_listsvc |
| List of service maintained by ServiceManager. | |
| MapType | m_maptype |
| Map of service name and service type. | |
| bool | m_loopCheck |
| Check for service initialization loops. | |
| SmartIF< IService > | m_appSvc |
| Pointer to the application IService interface. | |
| std::list< IService * > | m_listOfPtrs |
| List of pointers to the know services used to implement getServices() | |
| GaudiUtils::Map< InterfaceID, SmartIF< IInterface > > | m_defaultImplementations |
| boost::recursive_mutex | m_svcinitmutex |
| Mutex to synchronize shared service initialization between threads. | |
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.
Definition at line 37 of file ServiceManager.h.
| typedef std::list<ServiceItem> ServiceManager::ListSvc |
Definition at line 58 of file ServiceManager.h.
Definition at line 59 of file ServiceManager.h.
| ServiceManager::ServiceManager | ( | IInterface * | application ) |
default creator
Definition at line 26 of file ServiceManager.cpp.
: base_class(application, IService::interfaceID()), m_loopCheck(true), m_appSvc(application) { // Set the service locator to myself m_svcLocator = static_cast<ISvcLocator*>(this); addRef(); // Initial count set to 1 }
| ServiceManager::~ServiceManager | ( | ) | [virtual] |
virtual destructor
Definition at line 38 of file ServiceManager.cpp.
{
//-- inform the orphan services that I am gone....
for (ListSvc::iterator it = m_listsvc.begin(); it != m_listsvc.end(); it++ ) {
it->service->setServiceManager(0);
}
}
| StatusCode ServiceManager::addService | ( | IService * | svc, |
| int | prio = 10 |
||
| ) | [virtual] |
implementation of ISvcManager::addService
Definition at line 97 of file ServiceManager.cpp.
{
ListSvc::iterator it = find(svc);
if (it != m_listsvc.end()) {
it->priority = prio; // if the service is already known, it is equivalent to a setPriority
it->active = true; // and make it active
} else {
m_listsvc.push_back(ServiceItem(svc,prio,true));
}
return StatusCode::SUCCESS;
}
| StatusCode ServiceManager::addService | ( | const Gaudi::Utils::TypeNameString & | typeName, |
| int | prio = 10 |
||
| ) | [virtual] |
implementation of ISvcManager::addService
Definition at line 112 of file ServiceManager.cpp.
{
ListSvc::iterator it = find(typeName.name()); // try to find the service by name
if (it == m_listsvc.end()) { // not found
// If the service does not exist, we create it
SmartIF<IService> &svc = createService(typeName);
if (svc.isValid()) {
StatusCode sc = StatusCode(StatusCode::SUCCESS, true);
if (targetFSMState() >= Gaudi::StateMachine::INITIALIZED) {
sc = svc->sysInitialize();
if (sc.isSuccess() && targetFSMState() >= Gaudi::StateMachine::RUNNING) {
sc = svc->sysStart();
}
}
it = find(svc.get()); // now it is in the list because createService added it
if(sc.isFailure()) { // if initialization failed, remove it from the list
error() << "Unable to initialize service \"" << typeName.name() << "\""
<< endmsg;
m_listsvc.erase(it);
// Note: removing it from the list + the SmartIF going out of scope should trigger the delete
// delete svc.get();
return sc;
} else { // initialization successful, we can work with the service
// Move the just initialized service to the back of the list
// (we care more about order of initialization than of creation)
m_listsvc.push_back(*it);
m_listsvc.erase(it);
it = --m_listsvc.end(); // last entry (the iterator was invalidated by erase)
}
} else {
return StatusCode::FAILURE;
}
}
// 'it' is defined because either we found the service or we created it
// Now we can activate the service
it->priority = prio; // if the service is already known, it is equivalent to a setPriority
it->active = true; // and make it active
return StatusCode(StatusCode::SUCCESS, true);
}
| SmartIF< IService > & ServiceManager::createService | ( | const Gaudi::Utils::TypeNameString & | nametype ) | [virtual] |
implementation of ISvcManager::createService
: check how this hack works
: what does this mean?
Definition at line 47 of file ServiceManager.cpp.
{
// Check if the service is already existing
if(existsService(typeName.name())) {
// return an error because a service with that name already exists
return no_service;
}
StatusCode rc = StatusCode::FAILURE;
rc.setChecked(); //hack to avoid going into infinite recursion on ~StatusCode
const std::string &name = typeName.name();
std::string type = typeName.type();
if (!typeName.haveType()) { // the type is not explicit
// see we have some specific type mapping for the name
MapType::iterator it = m_maptype.find(typeName.name());
if( it != m_maptype.end() ) {
type = (*it).second; // use the declared type
}
}
std::string::size_type ip;
if ( (ip = type.find("__")) != std::string::npos) {
type.erase(ip,type.length());
}
IService* service = PluginService::Create<IService*>(type, name, static_cast<ISvcLocator*>(this)); // serviceLocator().get());
if ( !service ) {
service = PluginService::CreateWithId<IService*>(type, name, static_cast<ISvcLocator*>(this)); // serviceLocator().get());
}
if ( service ) {
m_listsvc.push_back(service);
// Check the compatibility of the version of the interface obtained
if( !isValidInterface(service) ) {
fatal() << "Incompatible interface IService version for " << type << endmsg;
return no_service;
}
service->setServiceManager(this);
return m_listsvc.back().service;
}
fatal() << "No Service factory for " << type << " available." << endmsg;
return no_service;
}
| StatusCode ServiceManager::declareSvcType | ( | const std::string & | svcname, |
| const std::string & | svctype | ||
| ) | [virtual] |
implementation of ISvcManager::declareSvcType
Definition at line 233 of file ServiceManager.cpp.
{
std::pair<MapType::iterator, bool> p = m_maptype.insert(std::make_pair(svcname, svctype));
if( p.second == false) {
m_maptype.erase ( p.first );
p = m_maptype.insert(std::make_pair(svcname, svctype) );
if( p.second == false) return StatusCode::FAILURE;
}
return StatusCode::SUCCESS;
}
| bool ServiceManager::existsService | ( | const std::string & | name ) | const [virtual] |
| StatusCode ServiceManager::finalize | ( | ) | [virtual] |
Finalize (from INITIALIZED to CONFIGURED).
Reimplemented from ComponentManager.
Definition at line 401 of file ServiceManager.cpp.
{
// make sure that HistogramDataSvc and THistSvc get finalized after the
// ToolSvc, and StatusCodeSvc after that
int pri_tool = getPriority("ToolSvc");
if (pri_tool != 0) {
setPriority("THistSvc",pri_tool-1).ignore();
setPriority("ChronoStatSvc",pri_tool-2).ignore();
setPriority("AuditorSvc",pri_tool-3).ignore();
setPriority("NTupleSvc",pri_tool-1).ignore();
setPriority("HistogramDataSvc",pri_tool-1).ignore();
// Preserve the relative ordering between HistogramDataSvc and HistogramPersistencySvc
setPriority("HistogramPersistencySvc",pri_tool-2).ignore();
}
// make sure the StatusCodeSvc gets finalized really late:
setPriority("StatusCodeSvc",-9999).ignore();
m_listsvc.sort(); // ensure that the list is ordered by priority
// we work on a copy to avoid to operate twice on the services created on demand
// (which are already in the correct state).
ListSvc tmpList(m_listsvc);
StatusCode sc(StatusCode::SUCCESS, true);
// call finalize() for all services in reverse order
ListSvc::reverse_iterator rit;
for (rit = tmpList.rbegin(); rit != tmpList.rend(); ++rit ) {
if (!rit->active) continue; // only act on active services
const std::string& name = rit->service->name();
// ignore the current state for the moment
// if( Gaudi::StateMachine::INITIALIZED == rit->service->state() ) {
DEBMSG << "Finalizing service " << name << endmsg;
if ( !(rit->service->sysFinalize()).isSuccess() ) {
warning() << "Finalization of service " << name << " failed" << endmsg;
sc = StatusCode::FAILURE;
}
}
DEBMSG << "Service reference count check:" << endmsg;
ListSvc::iterator it;
while (!tmpList.empty()) {
it = tmpList.begin();
const std::string& name = it->service->name();
const unsigned long rc = it->service->refCount() - 1; // exclude the count due to the temporary list
DEBMSG << "---- " << name
<< " (refCount = " << rc << ")" << endmsg;
if (rc < 1) {
warning() << "Too low reference count for " << name
<< " (should not go below 1 at this point)" << endmsg;
it->service->addRef();
}
tmpList.pop_front();
}
// loop over all Active Services, removing them one by one.
// They should be deleted because the reference counting goes to 0.
it = m_listsvc.begin();
while (it != m_listsvc.end()) {
if (it->active) {
it = m_listsvc.erase(it);
} else {
++it;
}
}
return sc ;
}
| ListSvc::const_iterator ServiceManager::find | ( | const std::string & | name ) | const [inline, private] |
| ListSvc::iterator ServiceManager::find | ( | const std::string & | name ) | [inline, private] |
| ListSvc::iterator ServiceManager::find | ( | const IService * | ptr ) | [inline, private] |
| ListSvc::const_iterator ServiceManager::find | ( | const IService * | ptr ) | const [inline, private] |
| int ServiceManager::getPriority | ( | const std::string & | name ) | const [virtual] |
manage priorities of services
Definition at line 471 of file ServiceManager.cpp.
{
//------------------------------------------------------------------------------
ListSvc::const_iterator it = find(name);
return (it != m_listsvc.end()) ? it->priority: 0;
}
Return the list of Services.
Definition at line 191 of file ServiceManager.cpp.
{
m_listOfPtrs.clear();
for (ListSvc::const_iterator it = m_listsvc.begin(); it != m_listsvc.end(); ++it) {
m_listOfPtrs.push_back(const_cast<IService*>(it->service.get()));
}
return m_listOfPtrs;
}
| StatusCode ServiceManager::initialize | ( | ) | [virtual] |
Initialization (from CONFIGURED to INITIALIZED).
Reimplemented from ComponentManager.
Definition at line 247 of file ServiceManager.cpp.
{
m_listsvc.sort(); // ensure that the list is ordered by priority
// we work on a copy to avoid to operate twice on the services created on demand
// (which are already in the correct state.
ListSvc tmpList(m_listsvc);
StatusCode sc(StatusCode::SUCCESS, true);
// call initialize() for all services
for (ListSvc::iterator it = tmpList.begin(); it != tmpList.end(); ++it ) {
if (!it->active) continue; // only act on active services
const std::string& name = it->service->name();
switch (it->service->FSMState()) {
case Gaudi::StateMachine::INITIALIZED:
DEBMSG << "Service " << name << " already initialized" << endmsg;
break;
case Gaudi::StateMachine::OFFLINE:
DEBMSG << "Initializing service " << name << endmsg;
sc = it->service->sysInitialize();
if( !sc.isSuccess() ) {
error() << "Unable to initialize Service: " << name << endmsg;
return sc;
} break;
default:
error() << "Service " << name
<< " not in the correct state to be initialized ("
<< it->service->FSMState() << ")" << endmsg;
return StatusCode::FAILURE;
}
}
return StatusCode::SUCCESS;
}
| bool ServiceManager::loopCheckEnabled | ( | ) | const [virtual] |
Get the value of the initialization loop check flag.
Definition at line 492 of file ServiceManager.cpp.
{
return m_loopCheck;
}
| const std::string& ServiceManager::name | ( | ) | const [inline, virtual] |
Return the name of the manager (implementation of INamedInterface)
Implements CommonMessaging< implements1< IComponentManager > >.
Definition at line 117 of file ServiceManager.h.
{
static std::string _name = "ServiceManager";
return _name;
}
| StatusCode ServiceManager::reinitialize | ( | ) | [virtual] |
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Reimplemented from ComponentManager.
Definition at line 355 of file ServiceManager.cpp.
{
m_listsvc.sort(); // ensure that the list is ordered by priority
// we work on a copy to avoid to operate twice on the services created on demand
// (which are already in the correct state.
ListSvc tmpList(m_listsvc);
StatusCode sc(StatusCode::SUCCESS, true);
ListSvc::iterator it;
// Re-Initialize all services
for ( it = tmpList.begin(); it != tmpList.end(); ++it ) {
if (!it->active) continue; // only act on active services
sc = it->service->sysReinitialize();
if( !sc.isSuccess() ) {
error() << "Unable to re-initialize Service: " << it->service->name() << endmsg;
return StatusCode::FAILURE;
}
}
return StatusCode::SUCCESS;
}
| StatusCode ServiceManager::removeService | ( | IService * | svc ) | [virtual] |
implementation of ISvcManager::removeService
Definition at line 209 of file ServiceManager.cpp.
{
ListSvc::iterator it = find(svc);
if (it != m_listsvc.end()) {
m_listsvc.erase(it);
return StatusCode(StatusCode::SUCCESS,true);
}
return StatusCode(StatusCode::FAILURE,true);
}
| StatusCode ServiceManager::removeService | ( | const std::string & | name ) | [virtual] |
implementation of ISvcManager::removeService
Definition at line 221 of file ServiceManager.cpp.
{
ListSvc::iterator it = find(name);
if (it != m_listsvc.end()) {
m_listsvc.erase(it);
return StatusCode::SUCCESS;
}
return StatusCode::FAILURE;
}
| StatusCode ServiceManager::restart | ( | ) | [virtual] |
Initialization (from RUNNING to RUNNING, via INITIALIZED).
Reimplemented from ComponentManager.
Definition at line 378 of file ServiceManager.cpp.
{
m_listsvc.sort(); // ensure that the list is ordered by priority
// we work on a copy to avoid to operate twice on the services created on demand
// (which are already in the correct state.
ListSvc tmpList(m_listsvc);
StatusCode sc(StatusCode::SUCCESS, true);
ListSvc::iterator it;
// Re-Start all services
for ( it = tmpList.begin(); it != tmpList.end(); ++it ) {
if (!it->active) continue; // only act on active services
sc = it->service->sysRestart();
if( !sc.isSuccess() ) {
error() << "Unable to re-start Service: " << it->service->name() << endmsg;
return StatusCode::FAILURE;
}
}
return StatusCode::SUCCESS;
}
| SmartIF< IService > & ServiceManager::service | ( | const Gaudi::Utils::TypeNameString & | typeName, |
| const bool | createIf = true |
||
| ) | [virtual] |
Returns a smart pointer to a service.
Definition at line 155 of file ServiceManager.cpp.
{
const std::string &name = typeName.name();
// Acquire the RAII lock to avoid simultaneous attempts from different threads to initialize a service
boost::lock_guard<boost::recursive_mutex> lck(m_svcinitmutex);
ListSvc::iterator it = find(name);
if (it != m_listsvc.end()) {
if (m_loopCheck &&
(createIf && it->service->FSMState() == Gaudi::StateMachine::CONFIGURED)) {
error()
<< "Initialization loop detected when creating service \"" << name
<< "\""
<< endmsg;
return no_service;
}
return it->service;
} else {
// Service not found. The user may be interested in one of the interfaces
// of the application manager itself
if( name == "ApplicationMgr" ||
name == "APPMGR" ||
name == "" ) {
return m_appSvc;
} else if ( createIf ){
//last resort: we try to create the service
if (addService(typeName).isSuccess()){
return find(name)->service;
}
}
}
return no_service;
}
| SmartIF<ISvcLocator>& ServiceManager::serviceLocator | ( | ) | const [inline, virtual] |
Function needed by CommonMessaging.
Reimplemented from ComponentManager.
Definition at line 65 of file ServiceManager.h.
{
return m_svcLocator;
}
| void ServiceManager::setLoopCheckEnabled | ( | bool | en ) | [virtual] |
Set the value of the initialization loop check flag.
Definition at line 498 of file ServiceManager.cpp.
{
m_loopCheck = en;
}
| StatusCode ServiceManager::setPriority | ( | const std::string & | name, |
| int | pri | ||
| ) | [virtual] |
Definition at line 479 of file ServiceManager.cpp.
{
//------------------------------------------------------------------------------
ListSvc::iterator it = find(name);
if (it != m_listsvc.end()) {
it->priority = prio;
return StatusCode::SUCCESS;
}
return StatusCode::FAILURE;
}
| StatusCode ServiceManager::start | ( | ) | [virtual] |
Start (from INITIALIZED to RUNNING).
Reimplemented from ComponentManager.
Definition at line 282 of file ServiceManager.cpp.
{
m_listsvc.sort(); // ensure that the list is ordered by priority
// we work on a copy to avoid to operate twice on the services created on demand
// (which are already in the correct state.
ListSvc tmpList(m_listsvc);
StatusCode sc(StatusCode::SUCCESS, true);
// call initialize() for all services
for (ListSvc::iterator it = tmpList.begin(); it != tmpList.end(); ++it ) {
if (!it->active) continue; // only act on active services
const std::string& name = it->service->name();
switch (it->service->FSMState()) {
case Gaudi::StateMachine::RUNNING:
DEBMSG << "Service " << name
<< " already started" << endmsg;
break;
case Gaudi::StateMachine::INITIALIZED:
DEBMSG << "Starting service " << name << endmsg;
sc = it->service->sysStart();
if( !sc.isSuccess() ) {
error() << "Unable to start Service: " << name << endmsg;
return sc;
} break;
default:
error() << "Service " << name
<< " not in the correct state to be started ("
<< it->service->FSMState() << ")" << endmsg;
return StatusCode::FAILURE;
}
}
return StatusCode::SUCCESS;
}
| StatusCode ServiceManager::stop | ( | ) | [virtual] |
Stop (from RUNNING to INITIALIZED).
Reimplemented from ComponentManager.
Definition at line 319 of file ServiceManager.cpp.
{
m_listsvc.sort(); // ensure that the list is ordered by priority
// we work on a copy to avoid to operate twice on the services created on demand
// (which are already in the correct state.
ListSvc tmpList(m_listsvc);
StatusCode sc(StatusCode::SUCCESS, true);
ListSvc::reverse_iterator it;
// call stop() for all services
for (it = tmpList.rbegin(); it != tmpList.rend(); ++it ) {
if (!it->active) continue; // only act on active services
const std::string& name = it->service->name();
switch (it->service->FSMState()) {
case Gaudi::StateMachine::INITIALIZED:
DEBMSG << "Service " << name << " already stopped" << endmsg;
break;
case Gaudi::StateMachine::RUNNING:
DEBMSG << "Stopping service " << name << endmsg;
sc = it->service->sysStop();
if( !sc.isSuccess() ) {
error() << "Unable to stop Service: " << name << endmsg;
return sc;
} break;
default:
DEBMSG << "Service " << name
<< " not in the correct state to be stopped ("
<< it->service->FSMState() << ")" << endmsg;
return StatusCode::FAILURE;
}
}
return StatusCode::SUCCESS;
}
SmartIF<IService> ServiceManager::m_appSvc [private] |
Pointer to the application IService interface.
Definition at line 151 of file ServiceManager.h.
GaudiUtils::Map<InterfaceID, SmartIF<IInterface> > ServiceManager::m_defaultImplementations [private] |
Definition at line 156 of file ServiceManager.h.
std::list<IService*> ServiceManager::m_listOfPtrs [mutable, private] |
List of pointers to the know services used to implement getServices()
Definition at line 154 of file ServiceManager.h.
ListSvc ServiceManager::m_listsvc [private] |
List of service maintained by ServiceManager.
Definition at line 146 of file ServiceManager.h.
bool ServiceManager::m_loopCheck [private] |
Check for service initialization loops.
Definition at line 148 of file ServiceManager.h.
MapType ServiceManager::m_maptype [private] |
Map of service name and service type.
Definition at line 147 of file ServiceManager.h.
boost::recursive_mutex ServiceManager::m_svcinitmutex [private] |
Mutex to synchronize shared service initialization between threads.
Definition at line 159 of file ServiceManager.h.