[asterisk-scf-commits] asterisk-scf/integration/ice-util-c++.git branch "master" updated.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Wed Oct 20 11:23:07 CDT 2010


branch "master" has been updated
       via  07c51f8564573e23fd6cd90811c374a2de5dceed (commit)
      from  dafceb220e0a3f82fb70d0a00aae9eeac01f424a (commit)

Summary of changes:
 StateReplicator/src/CMakeLists.txt    |    4 +-
 StateReplicator/src/StateReplicator.h |  156 +++++++++++++++++++--------------
 2 files changed, 95 insertions(+), 65 deletions(-)


- Log -----------------------------------------------------------------
commit 07c51f8564573e23fd6cd90811c374a2de5dceed
Author: Ken Hunt <ken.hunt at digium.com>
Date:   Wed Oct 20 11:22:01 2010 -0500

    Added locks.

diff --git a/StateReplicator/src/CMakeLists.txt b/StateReplicator/src/CMakeLists.txt
index 28f589b..1dc097d 100644
--- a/StateReplicator/src/CMakeLists.txt
+++ b/StateReplicator/src/CMakeLists.txt
@@ -1,9 +1,10 @@
-# Define the SIP Channel Service component
+# Define the state replicator template component
 
 asterisk_scf_component_init(StateReplicator CXX)
 
 asterisk_scf_component_add_file(StateReplicator StateReplicator.h)
 asterisk_scf_component_add_file(StateReplicator StateReplicator.cpp)
+asterisk_scf_component_add_boost_libraries(StateReplicator thread)
 
 asterisk_scf_component_build_library(StateReplicator)
 
@@ -13,5 +14,6 @@ if(APPLE)
   target_link_libraries(StateReplicator ${ICE_CXX_LIB_ZeroCIce})
 endif()
 
+
 asterisk_scf_component_install(StateReplicator LIBRARY lib "State Replicator" statereplicator ARCHIVE DESTINATION lib)
 
diff --git a/StateReplicator/src/StateReplicator.h b/StateReplicator/src/StateReplicator.h
index fc0233b..bff5432 100644
--- a/StateReplicator/src/StateReplicator.h
+++ b/StateReplicator/src/StateReplicator.h
@@ -8,6 +8,8 @@
 #pragma once
 
 #include <Ice/Ice.h>
+#include <boost/thread/thread.hpp>
+#include <boost/thread/locks.hpp>
 
 namespace AsteriskSCF
 {
@@ -103,10 +105,14 @@ public:
     */
    void addListener(const L& listener, const Ice::Current& = ::Ice::Current())
    {
-      mListeners.push_back(listener);
-
-      // Give this listener the current state.
-      listener->stateSet(mStateItems);
+       {   // critical scope
+           boost::unique_lock<boost::shared_mutex> lock(mListenerLock); 
+           mListeners.push_back(listener);
+       }
+
+       // Give this listener the current state.
+       boost::shared_lock<boost::shared_mutex> lock(mStateLock); 
+       listener->stateSet(mStateItems);
    }
 
    /**
@@ -115,12 +121,13 @@ public:
     */
    void removeListener(const L& listener, const Ice::Current& = ::Ice::Current())
    {
-      typename std::vector<L>::iterator it = std::find_if(mListeners.begin(), mListeners.end(), IdentifyListener<L>(listener));
+       boost::unique_lock<boost::shared_mutex> lock(mListenerLock); 
+       typename std::vector<L>::iterator it = std::find_if(mListeners.begin(), mListeners.end(), IdentifyListener<L>(listener));
 
-      if (it != mListeners.end())
-      {
-         mListeners.erase(it);
-      }
+       if (it != mListeners.end())
+       {
+           mListeners.erase(it);
+       }
    }
 
    /**
@@ -128,7 +135,8 @@ public:
     */
    void clearListeners()
    {
-      mListeners.clear();
+       boost::unique_lock<boost::shared_mutex> lock(mListenerLock); 
+       mListeners.clear();
    }
 
    /**
@@ -136,16 +144,19 @@ public:
     */
    void clearState()
    {
-      std::vector<K> allIds;
-
-      for(typename std::vector<S>::const_iterator it = mStateItems.begin(); it != mStateItems.end(); ++it)
-      {
-         allIds.push_back((*it)->key);
-      }
-
-      mStateItems.clear();
-
-      for_each(mListeners.begin(), mListeners.end(), RemoveStateNotice<L,std::vector<K> >(allIds));
+       std::vector<K> allIds;
+
+       {   // critical scope
+           boost::unique_lock<boost::shared_mutex> lock(mStateLock);
+           for(typename std::vector<S>::const_iterator it = mStateItems.begin(); it != mStateItems.end(); ++it)
+           {
+               allIds.push_back((*it)->key);
+           }
+           mStateItems.clear();
+       }
+
+       boost::shared_lock<boost::shared_mutex> lock(mListenerLock); 
+       for_each(mListeners.begin(), mListeners.end(), RemoveStateNotice<L,std::vector<K> >(allIds));
    }
 
    /**
@@ -154,22 +165,27 @@ public:
     */
    void setState(const std::vector<S>& items, const Ice::Current& = ::Ice::Current())
    {
-      for (typename std::vector<S>::const_iterator iter = items.begin();
-            iter != items.end(); ++iter)
-      {
-         typename std::vector<S>::iterator it = std::find_if(mStateItems.begin(), mStateItems.end(), MatchItem<S,K >((*iter)->key));
-
-         if (it != mStateItems.end())
-         { 
-            (*it) = (*iter);
-         }
-         else
-         {
-            mStateItems.push_back(*iter);
-         }
-      }
-
-      for_each( mListeners.begin(), mListeners.end(), SetStateNotice<L,std::vector<S> >(items) );
+       {    // critical scope
+            boost::unique_lock<boost::shared_mutex> lock(mStateLock);
+
+            for (typename std::vector<S>::const_iterator iter = items.begin();
+                iter != items.end(); ++iter)
+            {
+                 typename std::vector<S>::iterator it = std::find_if(mStateItems.begin(), mStateItems.end(), MatchItem<S,K >((*iter)->key));
+
+                 if (it != mStateItems.end())
+                 { 
+                    (*it) = (*iter);
+                 }
+                 else
+                 {
+                    mStateItems.push_back(*iter);
+                 }
+            }
+        }
+
+       boost::shared_lock<boost::shared_mutex> lock(mListenerLock); 
+       for_each( mListeners.begin(), mListeners.end(), SetStateNotice<L,std::vector<S> >(items) );
    }
 
    /**
@@ -178,39 +194,47 @@ public:
     */
    void removeState(const std::vector<K>& ids, const Ice::Current& = ::Ice::Current())
    {
-      for (typename std::vector<K>::const_iterator keyIter = ids.begin(); keyIter != ids.end(); ++keyIter)
-      {
-         typename std::vector<S>::iterator locateIt = std::find_if(mStateItems.begin(), mStateItems.end(), MatchItem<S,K >(*keyIter));
+       {   // critical scope
+           boost::unique_lock<boost::shared_mutex> lock(mStateLock);
+
+           for (typename std::vector<K>::const_iterator keyIter = ids.begin(); keyIter != ids.end(); ++keyIter)
+           {
+               typename std::vector<S>::iterator locateIt = std::find_if(mStateItems.begin(), mStateItems.end(), MatchItem<S,K >(*keyIter));
 
-         if (locateIt != mStateItems.end())
-         {
-            mStateItems.erase(locateIt);
-         }
-      }
+               if (locateIt != mStateItems.end())
+               {
+                   mStateItems.erase(locateIt);
+               }
+           }
+       }
 
+      boost::shared_lock<boost::shared_mutex> lock(mListenerLock);
       for_each(mListeners.begin(), mListeners.end(), RemoveStateNotice<L,std::vector<K> >(ids));
    }
 
-   /**
+    /**
     * Retrieve the state variables identifed by the key collection.  
     * @Override
     */
-   std::vector<S> getState(const std::vector<K>& itemKeys, const Ice::Current& = ::Ice::Current())
-   { 
-      std::vector<S> results;
-
-      for(typename std::vector<K>::const_iterator keyIt = itemKeys.begin(); keyIt != itemKeys.end(); ++keyIt)
-      {
-         typename std::vector<S>::iterator locateIt = std::find_if(mStateItems.begin(), mStateItems.end(), MatchItem<S,K >(*keyIt));
-
-         if (locateIt != mStateItems.end())
-         {
-            results.push_back(*locateIt);
-         }
-
-      }
-      return results;
-   }
+    std::vector<S> getState(const std::vector<K>& itemKeys, const Ice::Current& = ::Ice::Current())
+    { 
+        std::vector<S> results;
+
+        {   // critical scope
+            boost::shared_lock<boost::shared_mutex> lock(mStateLock);
+
+            for(typename std::vector<K>::const_iterator keyIt = itemKeys.begin(); keyIt != itemKeys.end(); ++keyIt)
+            {
+                typename std::vector<S>::iterator locateIt = std::find_if(mStateItems.begin(), mStateItems.end(), MatchItem<S,K >(*keyIt));
+
+                if (locateIt != mStateItems.end())
+                {
+                   results.push_back(*locateIt);
+                }
+            }
+        }
+        return results;
+    }
 
    /**
     * Retrieve all the state variables currently known to this replicator. 
@@ -218,7 +242,8 @@ public:
     */
    std::vector<S> getAllState(const Ice::Current& = ::Ice::Current())
    {
-      return mStateItems;
+       boost::shared_lock<boost::shared_mutex> lock(mStateLock);
+       return mStateItems;
    }
 
    /**
@@ -226,13 +251,16 @@ public:
     */
    int getListenerCount()
    {
-      return mListeners.size();
+       boost::shared_lock<boost::shared_mutex> lock(mListenerLock);
+       return mListeners.size();
    }
 
 private:
    std::vector<L> mListeners;
    std::vector<S> mStateItems;
-   
+
+   boost::shared_mutex mListenerLock;
+   boost::shared_mutex mStateLock;
 };
 
 } // end namespace StateReplication

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


-- 
asterisk-scf/integration/ice-util-c++.git



More information about the asterisk-scf-commits mailing list