[asterisk-scf-commits] asterisk-scf/integration/ice-util-cpp.git branch "smartproxy-threadsafety-update" created.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Thu Apr 14 11:53:17 CDT 2011


branch "smartproxy-threadsafety-update" has been created
        at  ba0385fba8d1167eb4dfc10bffc76c520d3b5155 (commit)

- Log -----------------------------------------------------------------
commit ba0385fba8d1167eb4dfc10bffc76c520d3b5155
Author: Brent Eagles <beagles at digium.com>
Date:   Thu Apr 14 14:19:29 2011 -0230

    Modified initialization and the reference operator to be more consistent
    if multiple threads invoke the assignment and reference operators concurrently.

diff --git a/SmartProxy/include/AsteriskSCF/SmartProxy.h b/SmartProxy/include/AsteriskSCF/SmartProxy.h
index 27b8961..e8d4663 100644
--- a/SmartProxy/include/AsteriskSCF/SmartProxy.h
+++ b/SmartProxy/include/AsteriskSCF/SmartProxy.h
@@ -67,17 +67,18 @@ public:
         initialize();
     }
 
-    P& operator->()
+    P operator->()
     {
         // everything this calls is thread safe.  no need to lock.
         assert(mServiceLocator && mLocatorParams);
 
-        if (!initializeOnce())
+        P t = initialize();
+        if (!t)
         {
             throw AsteriskSCF::Core::Discovery::V1::ServiceNotFound();
         }
 
-        return mProxy;
+        return t;
     }
 
     SmartProxy &operator=(const SmartProxy &rhs)
@@ -105,51 +106,36 @@ public:
         return mProxy != 0;
     }
 
-    /**
-     * Initializes this SmartProxy, but only if it hasn't already been
-     * initialized.
-     *
-     * @return True if initialization successful; false if unsuccessful.
-     */
-    bool initializeOnce()
-    {
-        // isInitialized() and initialize() are thread safe.  no need to lock
-        if (isInitialized())
-        {
-            return true;
-        }
-
-        // Try again to initialize.
-        initialize();
-        return isInitialized();
-    }
-
 private:
     /**
      * Initialization. Primarily involves acquring access to specific IceStorm
      * topic.
      */
-    void initialize()
+    P initialize()
     {
         // thread safe
         IceUtil::Mutex::Lock myLock(mMutex);
-
+        if (mProxy)
+        {
+            return mProxy;
+        }
+            
         using namespace AsteriskSCF::System::Logging;
         if (!mServiceLocator)
         {
             // uninitialized; probably no logger setup yet, either
             std::clog << "Missing ServiceLocator proxy.  Cannot initialize.\n";
-            return;
+            return 0;
         }
-
+            
         // all paths to set mServiceLocator also set mLogger.
         // but just in case things change in the future...
         assert(mLogger);
-
+            
         if (!mLocatorParams)
         {
             (*mLogger)(Error) << "Cannot find service with null parameters";
-            return;
+            return 0;
         }
 
         try
@@ -167,13 +153,14 @@ private:
         {
             (*mLogger)(Error) << "Exception locating "
                               << mLocatorParams->category << ": " << e.what();
-            return;
+            return 0;
         }
 
         if (mProxy == 0)
         {
             (*mLogger)(Error) << "Unable to locate " << mLocatorParams->category;
         }
+        return mProxy;
     }
 
     void copy(const SmartProxy &rhs)

-----------------------------------------------------------------------


-- 
asterisk-scf/integration/ice-util-cpp.git



More information about the asterisk-scf-commits mailing list