Modify DataCache code files to support thread synchronization
authorYounghyunJoo <yh_.joo@samsung.com>
Wed, 29 Jul 2015 01:18:39 +0000 (10:18 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Wed, 29 Jul 2015 02:21:01 +0000 (02:21 +0000)
- add thread synchronization for the subscriberList
- modify notifyObservers()'s parameter to const variable

Change-Id: I13e18a56be2ec2f6e7e4c0eadc371161dc6b5f3b
Signed-off-by: YounghyunJoo <yh_.joo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1949
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Hun-je Yeon <hunje.yeon@samsung.com>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/resource-encapsulation/src/resourceCache/include/DataCache.h
service/resource-encapsulation/src/resourceCache/src/DataCache.cpp

index 5d92cc4..3e20f54 100644 (file)
@@ -24,6 +24,7 @@
 #include <list>
 #include <string>
 #include <memory>
+#include <mutex>
 
 #include "CacheTypes.h"
 #include "ExpiryTimer.h"
@@ -85,7 +86,9 @@ namespace OIC
 
                 CacheID generateCacheID();
                 SubscriberInfoPair findSubscriber(CacheID id);
-                void notifyObservers(RCSResourceAttributes Att);
+                void notifyObservers(const RCSResourceAttributes Att);
+
+                mutable std::mutex m_mutex;
         };
     } // namespace Service
 } // namespace OIC
index 79a4165..eac00e9 100644 (file)
@@ -100,6 +100,7 @@ namespace OIC
 
             newItem.reportID = generateCacheID();
 
+            std::lock_guard<std::mutex> lock(m_mutex);
             if (subscriberList != nullptr)
             {
                 subscriberList->insert(
@@ -114,6 +115,8 @@ namespace OIC
             CacheID ret = 0;
 
             SubscriberInfoPair pair = findSubscriber(id);
+
+            std::lock_guard<std::mutex> lock(m_mutex);
             if (pair.first != 0)
             {
                 ret = pair.first;
@@ -127,6 +130,7 @@ namespace OIC
         {
             SubscriberInfoPair ret;
 
+            std::lock_guard<std::mutex> lock(m_mutex);
             for (auto &i : *subscriberList)
             {
                 if (i.first == id)
@@ -205,7 +209,7 @@ namespace OIC
             notifyObservers(_rep.getAttributes());
         }
 
-        void DataCache::notifyObservers(RCSResourceAttributes Att)
+        void DataCache::notifyObservers(const RCSResourceAttributes Att)
         {
             if (attributes == Att)
             {
@@ -214,12 +218,12 @@ namespace OIC
 
             attributes = Att;
 
-            RCSResourceAttributes retAtt = Att;
+            std::lock_guard<std::mutex> lock(m_mutex);
             for (auto &i : * subscriberList)
             {
                 if (i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
                 {
-                    i.second.second(this->sResource, retAtt);
+                    i.second.second(this->sResource, Att);
                 }
             }
         }
@@ -272,6 +276,7 @@ namespace OIC
 
         bool DataCache::isEmptySubscriber() const
         {
+            std::lock_guard<std::mutex> lock(m_mutex);
             return (subscriberList != nullptr) ? subscriberList->empty() : true;
         }
     } // namespace Service