Loading [MathJax]/extensions/MathMenu.js
The Gaudi Framework  v36r7 (7f57a304)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Bootstrap.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include <iostream>
12 
13 #include "GaudiKernel/Bootstrap.h"
14 #include "GaudiKernel/System.h"
15 
16 #include "GaudiKernel/IAlgorithm.h"
17 #include "GaudiKernel/IAppMgrUI.h"
19 #include "GaudiKernel/IInterface.h"
20 #include "GaudiKernel/IService.h"
22 
24 #include "GaudiKernel/Service.h"
25 #include <Gaudi/Algorithm.h>
26 
28 
29 #include "RVersion.h"
30 
31 namespace Gaudi {
32 
48  class BootSvcLocator : public implements<ISvcLocator> {
49  public:
50 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
52  IInterface*& pinterface ) override;
54  const bool createIf = true ) override;
55 #endif
56  const std::list<IService*>& getServices() const override;
57  bool existsService( std::string_view name ) const override;
58 
60  SmartIF<IService>& service( const Gaudi::Utils::TypeNameString& typeName, const bool createIf = true ) override;
61  };
62 } // namespace Gaudi
63 
64 static SmartIF<ISvcLocator> s_svclocInstance;
65 static SmartIF<IAppMgrUI> s_appmgrInstance;
66 
67 //------------------------------------------------------------------------------
68 IAppMgrUI* Gaudi::createApplicationMgr( const std::string& dllname, const std::string& factname )
69 //------------------------------------------------------------------------------
70 {
71  // Allow not for multiple AppManagers: If already instantiated then just
72  // return it
73  if ( !s_appmgrInstance ) {
74  s_appmgrInstance = createApplicationMgrEx( dllname, factname );
75  s_svclocInstance = s_appmgrInstance;
76  }
77  return s_appmgrInstance.get();
78 }
79 
80 //------------------------------------------------------------------------------
81 IAppMgrUI* Gaudi::createApplicationMgrEx( const std::string& dllname, const std::string& factname )
82 //------------------------------------------------------------------------------
83 {
84  // Create an instance of the application Manager
85  auto iif = make_SmartIF( Gaudi::createInstance( "ApplicationMgr", factname, dllname ) );
86  // Locate few interfaces of the Application Manager
87  return iif ? iif.as<IAppMgrUI>().get() : nullptr;
88 }
89 
90 //------------------------------------------------------------------------------
92 //------------------------------------------------------------------------------
93 //
94 // A dual-stage bootstrap mechanism is used to ensure an orderly startup
95 // of the ApplicationMgr. If this function is called before the singleton
96 // ApplicationMgr instance exists, a BootSvcLocator singleton instance is
97 // created. This responds to any subsequent requests for services by
98 // returning StatusCode::FAILURE, unless the ApplicationMgr singleton
99 // instance has been created in the interim. In this case, the BootSvcLocator
100 // forwards the request to the ApplicationMgr instance. The motivation for
101 // this is to handle static object instances where the constructor attempts
102 // to locate services and would otherwise instantiate the ApplicationMgr
103 // instance in an unorderly manner. This logic requires that the
104 // ApplicationMgr instance is created explicitly.
105 
106 {
107  if ( !s_svclocInstance ) {
108  IAppMgrUI* iappmgr = createApplicationMgr();
109  if ( iappmgr ) {
110  s_svclocInstance = iappmgr;
111  if ( s_svclocInstance ) return s_svclocInstance;
112  }
113  //---Reverted change to create a Minimal SvcLocator in case is requested before AppMgr is created
114  // if( 0 == s_appmgrInstance ) {
115  // s_svclocInstance = new BootSvcLocator();
116  //} else {
117  // StatusCode sc = s_appmgrInstance->queryInterface( ISvcLocator::interfaceID(),
118  // pp_cast<void>(&s_svclocInstance) );
119  // if( sc.isSuccess() ) {
120  // return s_svclocInstance;
121  // }
122  //}
123  }
124  return s_svclocInstance;
125 }
126 
127 //------------------------------------------------------------------------------
129 //------------------------------------------------------------------------------
130 {
131  ISvcLocator* oldInstance = s_svclocInstance.get();
132  s_svclocInstance = newInstance;
133  s_appmgrInstance = s_svclocInstance;
134  return oldInstance;
135 }
136 
137 //------------------------------------------------------------------------------
138 IAppMgrUI* Gaudi::setInstance( IAppMgrUI* newInstance )
139 //------------------------------------------------------------------------------
140 {
141  IAppMgrUI* oldInstance = s_appmgrInstance.get();
142  s_appmgrInstance = newInstance;
143  s_svclocInstance = s_appmgrInstance;
144  return oldInstance;
145 }
146 
147 //------------------------------------------------------------------------------
148 IInterface* Gaudi::createInstance( const std::string& name, const std::string& factname, const std::string& dllname )
149 //------------------------------------------------------------------------------
150 {
151 
152  IInterface* ii = ObjFactory::create( factname, nullptr ).release();
153  if ( ii ) return ii;
154  IService* is = Service::Factory::create( factname, name, nullptr ).release();
155  if ( is ) return is;
156  IAlgorithm* ia = Algorithm::Factory::create( factname, name, nullptr ).release();
157  if ( ia ) return ia;
158 
159  void* libHandle = nullptr;
160  if ( System::loadDynamicLib( dllname, &libHandle ) ) {
161  ii = ObjFactory::create( factname, nullptr ).release();
162  if ( ii ) return ii;
163  is = Service::Factory::create( factname, name, nullptr ).release();
164  if ( is ) return is;
165  ia = Algorithm::Factory::create( factname, name, nullptr ).release();
166  if ( ia ) return ia;
167  } else {
168  // DLL library not loaded. Try in the local module
170  std::cout << "Gaudi::Bootstrap: Not found DLL " << dllname << std::endl;
171  }
172  return nullptr;
173 }
174 
175 //------------------------------------------------------------------------------
177  //------------------------------------------------------------------------------
178  return createApplicationMgr( dllname, "ApplicationMgr" );
179 }
180 
181 //------------------------------------------------------------------------------
183  //------------------------------------------------------------------------------
184  return createApplicationMgr( "GaudiCoreSvc", "ApplicationMgr" );
185 }
186 
187 //=======================================================================
188 // BootSvcLocator
189 //=======================================================================
190 
191 static std::list<IService*> s_bootServices;
192 static SmartIF<IService> s_bootService;
193 static SmartIF<IInterface> s_bootInterface;
194 
196 
197 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
199  IInterface*& pinterface ) {
201  if ( s_appmgrInstance ) {
202  sc = s_svclocInstance->getService( typeName, iid, pinterface );
203  } else {
204  pinterface = s_bootInterface.get();
205  }
206  return sc;
207 }
209  const bool createIf ) {
211  if ( s_appmgrInstance ) {
212  sc = s_svclocInstance->getService( typeName, svc, createIf );
213  } else {
214  svc = s_bootService.get();
215  }
216  return sc;
217 }
218 #endif
219 
221  return s_appmgrInstance ? s_svclocInstance->getServices() : s_bootServices;
222 }
223 bool Gaudi::BootSvcLocator::existsService( std::string_view name ) const {
224  return s_appmgrInstance && s_svclocInstance->existsService( name );
225 }
226 
228  return s_appmgrInstance ? s_svclocInstance->service( typeName, createIf ) : s_bootService;
229 }
230 
231 #ifdef GAUDI_HASCLASSVISIBILITY
232 # pragma GCC visibility push( default )
233 #endif
234 // Python bootstrap helpers
235 extern "C" {
236 #define PyHelper( x ) py_bootstrap_##x
239  auto svcloc = SmartIF<ISvcLocator>( app );
240  return svcloc ? svcloc->service<IInterface>( std::string_view{ name } ).get() : nullptr;
241 }
242 bool PyHelper( setProperty )( IInterface* p, char* name, char* value ) {
243  auto prop = SmartIF<IProperty>( p );
244  return prop && prop->setProperty( name, value ).isSuccess();
245 }
246 const char* PyHelper( getProperty )( IInterface* p, char* name ) {
247  auto prop = SmartIF<IProperty>( p );
248  if ( !prop ) return nullptr;
249  // this is needed to guarantee that Python can access the returned char*
250  static std::string value;
251  value = prop->getProperty( name ).toString();
252  return value.c_str();
253 }
254 void PyHelper( setOption )( IInterface* app, char* key, char* value ) {
255  if ( auto svcloc = SmartIF<ISvcLocator>( app ) ) { svcloc->getOptsSvc().set( key, value ); }
256 }
258  auto ui = SmartIF<IAppMgrUI>( app );
259  return ui && ui->configure().isSuccess();
260 }
262 
263 bool py_bootstrap_app_run( IInterface* i, int maxevt ) {
264  auto ep = SmartIF<IEventProcessor>( i );
265  return ep && ep->executeRun( maxevt ).isSuccess();
266 }
267 
268 #define PyFSMHelper( s ) \
269  bool py_bootstrap_fsm_##s( IInterface* i ) { \
270  auto fsm = SmartIF<IStateful>( i ); \
271  return fsm && fsm->s().isSuccess(); \
272  }
273 
274 // clang-format off
276 PyFSMHelper( initialize )
278 PyFSMHelper( stop )
279 PyFSMHelper( finalize )
280 PyFSMHelper( terminate )
281 // clang-format on
282 }
283 #ifdef GAUDI_HASCLASSVISIBILITY
284 # pragma GCC visibility pop
285 #endif
IService
Definition: IService.h:28
Gaudi::createInstance
GAUDI_API IInterface * createInstance(const std::string &name, const std::string &factname, const std::string &ddlname)
Gaudi::createApplicationMgr
GAUDI_API IAppMgrUI * createApplicationMgr(const std::string &dllname, const std::string &factname)
System::loadDynamicLib
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
Definition: System.cpp:145
IService.h
std::string
STL class.
setProperty
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:242
std::list< IService * >
Read.app
app
Definition: Read.py:36
System.h
Aida2RootEx.configure
def configure(gaudi=None)
Definition: Aida2RootEx.py:126
ISvcLocator
Definition: ISvcLocator.h:46
py_bootstrap_app_run
bool py_bootstrap_app_run(IInterface *i, int maxevt)
Definition: Bootstrap.cpp:263
getService
IInterface *PyHelper() getService(IInterface *app, char *name)
Definition: Bootstrap.cpp:238
ObjectFactory.h
configureApp
bool PyHelper() configureApp(IInterface *app)
Definition: Bootstrap.cpp:257
IAppMgrUI.h
Gaudi::createApplicationMgrEx
GAUDI_API IAppMgrUI * createApplicationMgrEx(const std::string &dllname, const std::string &factname)
Gaudi::BootSvcLocator::service
SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Returns a smart pointer to a service.
Definition: Bootstrap.cpp:227
PyHelper
#define PyHelper(x)
Definition: Bootstrap.cpp:236
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:20
Gaudi::Functional::details::get
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
Definition: FunctionalDetails.h:444
IClassManager.h
TimingHistograms.name
name
Definition: TimingHistograms.py:25
StatusCode
Definition: StatusCode.h:65
IInterface.h
IAlgorithm
Definition: IAlgorithm.h:38
std::cout
std::string::c_str
T c_str(T... args)
Gaudi::setInstance
GAUDI_API ISvcLocator * setInstance(ISvcLocator *newInstance)
Set new instance of service locator.
Gaudi::BootSvcLocator
Definition: Bootstrap.cpp:48
GaudiPython.Bindings.nullptr
nullptr
Definition: Bindings.py:93
Algorithm.h
SmartIF< IService >
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Service.h
ROOT_VERSION_CODE
int PyHelper() ROOT_VERSION_CODE()
Definition: Bootstrap.cpp:261
PyFSMHelper
#define PyFSMHelper(s)
Definition: Bootstrap.cpp:268
implements
Base class used to implement the interfaces.
Definition: implements.h:19
IAppMgrUI
Definition: IAppMgrUI.h:34
make_SmartIF
SmartIF< IFace > make_SmartIF(IFace *iface)
Definition: SmartIF.h:150
std::endl
T endl(T... args)
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:86
GaudiDict::typeName
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31
IInterface
Definition: IInterface.h:237
Bootstrap.h
Gaudi::BootSvcLocator::getService
StatusCode getService(const Gaudi::Utils::TypeNameString &typeName, const InterfaceID &iid, IInterface *&pinterface) override
Definition: Bootstrap.cpp:198
InterfaceID
Definition: IInterface.h:39
IAlgorithm.h
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
getProperty
const char *PyHelper() getProperty(IInterface *p, char *name)
Definition: Bootstrap.cpp:246
IInterface::release
virtual unsigned long release()=0
Release Interface instance.
ISvcLocator.h
Gaudi::BootSvcLocator::existsService
bool existsService(std::string_view name) const override
Definition: Bootstrap.cpp:223
System::getLastErrorString
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition: System.cpp:272
IOTest.start
def start
Definition: IOTest.py:113
ProduceConsume.key
key
Definition: ProduceConsume.py:81
IEventProcessor.h
setOption
void PyHelper() setOption(IInterface *app, char *key, char *value)
Definition: Bootstrap.cpp:254
Gaudi::BootSvcLocator::getServices
const std::list< IService * > & getServices() const override
Definition: Bootstrap.cpp:220