Salome HOME
PR: container lifecycle, new design, try to dlclose, still not safe...
authorprascle <prascle>
Mon, 18 Apr 2005 13:58:20 +0000 (13:58 +0000)
committerprascle <prascle>
Mon, 18 Apr 2005 13:58:20 +0000 (13:58 +0000)
src/Container/Component_i.cxx
src/Container/Container_i.cxx
src/Container/SALOME_Container_i.hxx

index 04eff5a0d25599213588980d420968f58e6da52a..e0622fec3dc95c9b2cd18661d684209c9f0259ea 100644 (file)
@@ -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);
 }
 
 //=============================================================================
index c8528d97950dae3fda406d8da0c6e6f907875bbd..90586a832ef7793a5d55a68a067320a92aa830e5 100644 (file)
@@ -55,6 +55,10 @@ extern "C" {void ActSigIntHandler() ; }
 extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; }
 
 const char *Engines_Container_i::_defaultContainerName="FactoryServer";
+map<std::string, int> Engines_Container_i::_cntInstances_map;
+map<std::string, void *> Engines_Container_i::_library_map;
+map<std::string, void *> 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<string, void *>::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<string, void *>::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();
+}
 
 //=============================================================================
 /*! 
index 6fcfafddafc203d9f5a1b6810634c2fe86cfed48..0791322edc0d81502cc78e8d25f624cf6098005f 100644 (file)
@@ -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<std::string, int> _cntInstances_map;
+  static std::map<std::string, void *> _library_map; // library names, loaded
+  static std::map<std::string, void *> _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<std::string, void *> _library_map; // library names, loaded
   std::map<std::string,Engines::Component_var> _listInstances_map;
-  std::map<std::string, void *> _handle_map ;
-  std::map<std::string, void *> _remove_map ;
-  omni_mutex _numInstanceMutex ;       // if several threads on the same object
 
   //private: