Imported Upstream version 17.14.0
[platform/upstream/libzypp.git] / zypp / ZYppFactory.cc
index 45fc1c2..6712196 100644 (file)
@@ -38,6 +38,8 @@ using boost::interprocess::sharable_lock;
 
 using std::endl;
 
+namespace zyppintern { void repoVariablesReset(); }    // upon re-acquiring the lock...
+
 ///////////////////////////////////////////////////////////////////
 namespace zypp
 { /////////////////////////////////////////////////////////////////
@@ -188,7 +190,7 @@ namespace zypp
     bool isProcessRunning( pid_t pid_r )
     {
       // it is another program, not me, see if it is still running
-      Pathname procdir( "/proc"/str::numstring(pid_r) );
+      Pathname procdir( Pathname("/proc")/str::numstring(pid_r) );
       PathInfo status( procdir );
       MIL << "Checking " <<  status << endl;
 
@@ -282,14 +284,38 @@ namespace zypp
 
   };
 
+  ///////////////////////////////////////////////////////////////////
   namespace
   {
-    static ZYppGlobalLock & globalLock()
+    static weak_ptr<ZYpp>              _theZYppInstance;
+    static scoped_ptr<ZYppGlobalLock>  _theGlobalLock;         // on/off in sync with _theZYppInstance
+
+    ZYppGlobalLock & globalLock()
     {
-      static ZYppGlobalLock lock;
-      return lock;
+      if ( !_theGlobalLock )
+       _theGlobalLock.reset( new ZYppGlobalLock );
+      return *_theGlobalLock;
     }
-    bool           _haveZYpp = false;
+  } //namespace
+  ///////////////////////////////////////////////////////////////////
+
+  ///////////////////////////////////////////////////////////////////
+  //
+  //   CLASS NAME : ZYpp
+  //
+  ///////////////////////////////////////////////////////////////////
+
+  ZYpp::ZYpp( const Impl_Ptr & impl_r )
+  : _pimpl( impl_r )
+  {
+    ::zyppintern::repoVariablesReset();        // upon re-acquiring the lock...
+    MIL << "ZYpp is on..." << endl;
+  }
+
+  ZYpp::~ZYpp()
+  {
+    _theGlobalLock.reset();
+    MIL << "ZYpp is off..." << endl;
   }
 
   ///////////////////////////////////////////////////////////////////
@@ -313,31 +339,12 @@ namespace zypp
   //
   ///////////////////////////////////////////////////////////////////
 
-  ///////////////////////////////////////////////////////////////////
-  //
-  //   METHOD NAME : ZYppFactory::instance
-  //   METHOD TYPE : ZYppFactory
-  //
   ZYppFactory ZYppFactory::instance()
-  {
-    return ZYppFactory();
-  }
+  { return ZYppFactory(); }
 
-  ///////////////////////////////////////////////////////////////////
-  //
-  //   METHOD NAME : ZYppFactory::ZYppFactory
-  //   METHOD TYPE : Ctor
-  //
   ZYppFactory::ZYppFactory()
-  {
-
-  }
+  {}
 
-  ///////////////////////////////////////////////////////////////////
-  //
-  //   METHOD NAME : ZYppFactory::~ZYppFactory
-  //   METHOD TYPE : Dtor
-  //
   ZYppFactory::~ZYppFactory()
   {}
 
@@ -345,8 +352,7 @@ namespace zypp
   //
   ZYpp::Ptr ZYppFactory::getZYpp() const
   {
-    static ZYpp::Ptr _instance;
-
+    ZYpp::Ptr _instance = _theZYppInstance.lock();
     if ( ! _instance )
     {
       if ( geteuid() != 0 )
@@ -365,7 +371,7 @@ namespace zypp
        {
          MIL << "Waiting whether pid " << globalLock().lockerPid() << " ends within $LOCK_TIMEOUT=" << LOCK_TIMEOUT << " sec." << endl;
          unsigned delay = 1;
-         Pathname procdir( "/proc"/str::numstring(globalLock().lockerPid()) );
+          Pathname procdir( Pathname("/proc")/str::numstring(globalLock().lockerPid()) );
          for ( long i = 0; i < LOCK_TIMEOUT; i += delay )
          {
            if ( PathInfo( procdir ).isDir() )  // wait for /proc/pid to disapear
@@ -378,7 +384,7 @@ namespace zypp
              {
                // another proc locked faster. maybe it ends fast as well....
                MIL << "Waiting whether pid " << globalLock().lockerPid() << " ends within " << (LOCK_TIMEOUT-i) << " sec." << endl;
-               procdir = Pathname( "/proc"/str::numstring(globalLock().lockerPid()) );
+                procdir = Pathname( Pathname("/proc")/str::numstring(globalLock().lockerPid()) );
              }
              else
              {
@@ -387,7 +393,6 @@ namespace zypp
              }
            }
          }
-
        }
        if ( failed )
        {
@@ -400,9 +405,11 @@ namespace zypp
        }
       }
       // Here we go...
-      _instance = new ZYpp( ZYpp::Impl_Ptr(new ZYpp::Impl) );
-      if ( _instance )
-        _haveZYpp = true;
+      static ZYpp::Impl_Ptr _theImplInstance;  // for now created once
+      if ( !_theImplInstance )
+       _theImplInstance.reset( new ZYpp::Impl );
+      _instance.reset( new ZYpp( _theImplInstance ) );
+      _theZYppInstance = _instance;
     }
 
     return _instance;
@@ -411,7 +418,7 @@ namespace zypp
   ///////////////////////////////////////////////////////////////////
   //
   bool ZYppFactory::haveZYpp() const
-  { return _haveZYpp; }
+  { return !_theZYppInstance.expired(); }
 
   /******************************************************************
   **