From 94afe79dce2e92d33d2e8b06410b5b3bd820f0b5 Mon Sep 17 00:00:00 2001 From: prascle Date: Mon, 18 Apr 2005 13:58:20 +0000 Subject: [PATCH] PR: container lifecycle, new design, try to dlclose, still not safe... --- src/Container/Component_i.cxx | 1 + src/Container/Container_i.cxx | 69 +++++++++++++++++++++++----- src/Container/SALOME_Container_i.hxx | 9 ++-- 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/src/Container/Component_i.cxx b/src/Container/Component_i.cxx index 04eff5a0d..e0622fec3 100644 --- a/src/Container/Component_i.cxx +++ b/src/Container/Component_i.cxx @@ -136,6 +136,7 @@ Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb, Engines_Component_i::~Engines_Component_i() { MESSAGE("Component destructor"); + Engines_Container_i::decInstanceCnt(_interfaceName); } //============================================================================= diff --git a/src/Container/Container_i.cxx b/src/Container/Container_i.cxx index c8528d979..90586a832 100644 --- a/src/Container/Container_i.cxx +++ b/src/Container/Container_i.cxx @@ -55,6 +55,10 @@ extern "C" {void ActSigIntHandler() ; } extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; } const char *Engines_Container_i::_defaultContainerName="FactoryServer"; +map Engines_Container_i::_cntInstances_map; +map Engines_Container_i::_library_map; +map Engines_Container_i::_toRemove_map; +omni_mutex Engines_Container_i::_numInstanceMutex ; //============================================================================= /*! @@ -231,9 +235,13 @@ Engines_Container_i::load_component_Library(const char* componentLibraryName) string impl_name = componentLibraryName; SCRUTE(impl_name); + _numInstanceMutex.lock(); // lock to be alone + // (see decInstanceCnt, finalize_removal)) + if (_toRemove_map[impl_name]) _toRemove_map.erase(impl_name); if (_library_map[impl_name]) { MESSAGE("Library " << impl_name << " already loaded"); + _numInstanceMutex.unlock(); return true; } void* handle; @@ -242,13 +250,16 @@ Engines_Container_i::load_component_Library(const char* componentLibraryName) { INFOS("Can't load shared library : " << impl_name); INFOS("error dlopen: " << dlerror()); + _numInstanceMutex.unlock(); return false; } else { _library_map[impl_name] = handle; + _numInstanceMutex.unlock(); return true; } + _numInstanceMutex.unlock(); } //============================================================================= @@ -349,28 +360,35 @@ void Engines_Container_i::remove_impl(Engines::Component_ptr component_i) MESSAGE("unload component " << instanceName); _listInstances_map.erase(instanceName); component_i->destroy() ; + _NS->Destroy_Name(instanceName.c_str()); } //============================================================================= /*! - * CORBA method: Discharges all components from the container. + * CORBA method: Discharges unused libraries from the container. */ //============================================================================= void Engines_Container_i::finalize_removal() { MESSAGE("finalize unload : dlclose"); - map::iterator im ; - _numInstanceMutex.lock() ; // lock on the explore _remove_map & dlclose - for (im = _remove_map.begin() ; im != _remove_map.end() ; im ++) + _numInstanceMutex.lock(); // lock to be alone + // (see decInstanceCnt, load_component_Library) + map::iterator ith; + for (ith = _toRemove_map.begin(); ith != _toRemove_map.end(); ith++) { - void * handle = (*im).second ; - dlclose(handle) ; - MESSAGE("dlclose " << (*im).first); + void *handle = (*ith).second; + string impl_name= (*ith).first; + if (handle) + { + SCRUTE(handle); + SCRUTE(impl_name); +// dlclose(handle); // SALOME unstable after ... +// _library_map.erase(impl_name); + } } - _remove_map.clear() ; - _numInstanceMutex.unlock() ; - MESSAGE("_remove_map.clear()"); + _toRemove_map.clear(); + _numInstanceMutex.unlock(); } //============================================================================= @@ -543,7 +561,7 @@ Engines_Container_i::createInstance(string genericRegisterName, // --- Instanciate required CORBA object - PortableServer::ObjectId * id ; + PortableServer::ObjectId *id ; //not owner, do not delete (nore use var) id = (Component_factory) ( _orb, _poa, _id, instanceName.c_str(), aGenRegisterName.c_str() ) ; @@ -559,6 +577,9 @@ Engines_Container_i::createInstance(string genericRegisterName, servant->_remove_ref(); // compensate previous id_to_reference SCRUTE(servant->pd_refCount); _listInstances_map[instanceName] = iobject; + _cntInstances_map[aGenRegisterName] += 1; + SCRUTE(aGenRegisterName); + SCRUTE(_cntInstances_map[aGenRegisterName]); SCRUTE(servant->pd_refCount); ASSERT(servant->setStudyId(studyId)); @@ -575,6 +596,32 @@ Engines_Container_i::createInstance(string genericRegisterName, return iobject._retn(); } +//============================================================================= +/*! + * + */ +//============================================================================= + +void Engines_Container_i::decInstanceCnt(string genericRegisterName) +{ + string aGenRegisterName =genericRegisterName; + MESSAGE("Engines_Container_i::decInstanceCnt " << aGenRegisterName); + ASSERT(_cntInstances_map[aGenRegisterName] > 0); + _numInstanceMutex.lock(); // lock to be alone + // (see finalize_removal, load_component_Library) + _cntInstances_map[aGenRegisterName] -= 1; + SCRUTE(_cntInstances_map[aGenRegisterName]); + if (_cntInstances_map[aGenRegisterName] == 0) + { + string impl_name = + Engines_Component_i::GetDynLibraryName(aGenRegisterName.c_str()); + SCRUTE(impl_name); + void* handle = _library_map[impl_name]; + ASSERT(handle); + _toRemove_map[impl_name] = handle; + } + _numInstanceMutex.unlock(); +} //============================================================================= /*! diff --git a/src/Container/SALOME_Container_i.hxx b/src/Container/SALOME_Container_i.hxx index 6fcfafdda..0791322ed 100644 --- a/src/Container/SALOME_Container_i.hxx +++ b/src/Container/SALOME_Container_i.hxx @@ -103,10 +103,15 @@ public: static bool isPythonContainer(const char* ContainerName); static std::string BuildContainerNameForNS(const char *ContainerName, const char *hostname); + static void decInstanceCnt(std::string genericRegisterName); protected: static const char *_defaultContainerName; + static std::map _cntInstances_map; + static std::map _library_map; // library names, loaded + static std::map _toRemove_map;// library names to remove + static omni_mutex _numInstanceMutex ; // lib and instance protection SALOME_NamingService *_NS ; std::string _library_path; @@ -115,11 +120,7 @@ protected: PortableServer::POA_var _poa; PortableServer::ObjectId * _id ; int _numInstance ; - std::map _library_map; // library names, loaded std::map _listInstances_map; - std::map _handle_map ; - std::map _remove_map ; - omni_mutex _numInstanceMutex ; // if several threads on the same object //private: -- 2.39.2