From: stephan b. <sg...@us...> - 2004-12-25 20:50:40
|
Update of /cvsroot/pclasses/pclasses2/include/pclasses/Plugin In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1518/include/pclasses/Plugin Modified Files: Plugin.h Log Message: Added caching of SharedLib lookups. Removed the NonCopyable requirement because this object no longer deletes the SharedLib objects. Index: Plugin.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/Plugin/Plugin.h,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- Plugin.h 25 Dec 2004 19:56:34 -0000 1.10 +++ Plugin.h 25 Dec 2004 20:50:20 -0000 1.11 @@ -32,7 +32,7 @@ */ template <class InterfaceT> class PluginManager : - public ::P::NamedTypeFactory< InterfaceT >, public NonCopyable + public ::P::NamedTypeFactory< InterfaceT > { public: typedef InterfaceT InterfaceType; @@ -128,22 +128,37 @@ caller does NOT own the pointer - it is owned by this class. - @todo: if the SO is already opened, give back the - existing SharedLib. We can't do this until we map names to - SharedLibs, which we really have little reason to - do. + This function caches lookups, and calling addPlugin() + multiple times with the same so_name will return the + same SharedLib object. */ ::P::System::SharedLib * addPlugin( const std::string & so_name ) throw(::P::RuntimeError) { - //CERR << "addPlugin("<<so_name<<").\n"; + typedef typename PluginsMap::iterator IT; + // look for cached entry: + IT it = this->m_smap.find( so_name ); + if( this->m_smap.end() != it ) + { + return (*it).second; + } + + // find a DLL file: std::string fn = this->searchPath().find( so_name ); if( fn.empty() ) { throw ::P::System::SystemError( 0, (std::string("No plugin found for '")+so_name+"'.").c_str(), P_SOURCEINFO ); } + + // check for entry cached under DLL's file name: + it = this->m_smap.find( fn ); + if( this->m_smap.end() != it ) + { + return (*it).second; + } + + // open DLL and, if workie, cache it: ::P::System::SharedLib * shl = ::P::System::openSharedLib( fn ); - //CERR << "Opened fn="<<fn<<"\n"; m_smap.insert( std::make_pair( fn, shl ) ); return shl; } @@ -195,6 +210,11 @@ lib. That said, the default lib-opening routines do not destroy SharedLibs loaded via this class until post-main(). + + ACHTUNG #2: using the non-const PluginsMap together + with non-const member functions, e.g. addPlugin(), + from multi-threaded code is likely to Cause Greif. + To be fixed, via P's mutex support. */ PluginsMap & loadedPlugins() { @@ -256,7 +276,7 @@ template <typename T> ::P::System::SharedLib * addPlugin( const std::string & pluginname ) throw(RuntimeError) - { + { return pluginManager<T>().addPlugin( pluginname ); } |