[Common] Adding guard to protect container of Instances* 39/194439/1
authorSzymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
Thu, 28 Jun 2018 06:35:46 +0000 (08:35 +0200)
committerSzymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
Tue, 4 Dec 2018 08:22:13 +0000 (09:22 +0100)
[Verification] TCT Application, Systeminfo, Filesystem 100%

Change-Id: Ife71f69fc031d14c6bcb1ec694cd2be102ed7f75
Signed-off-by: Szymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
src/common/extension.cc
src/common/extension.h

index 5e12aad..9fc1547 100644 (file)
@@ -218,26 +218,36 @@ int32_t Extension::XW_Initialize(XW_Extension extension, XW_GetInterface get_int
   return XW_OK;
 }
 
+std::mutex Instance::instances_mutex_;
 std::unordered_set<Instance*> Instance::all_instances_;
 
 Instance::Instance() : xw_instance_(0) {
   ScopeLogger();
-  { all_instances_.insert(this); }
+  {
+    std::lock_guard<std::mutex> lock{instances_mutex_};
+    all_instances_.insert(this);
+  }
 }
 
 Instance::~Instance() {
   ScopeLogger();
-  { all_instances_.erase(this); }
+  {
+    std::lock_guard<std::mutex> lock{instances_mutex_};
+    all_instances_.erase(this);
+  }
   Assert(xw_instance_ == 0);
 }
 
 void Instance::PostMessage(Instance* that, const char* msg) {
   ScopeLogger();
-  if (that && all_instances_.end() != all_instances_.find(that)) {
-    that->PostMessage(msg);
-  } else {
-    LoggerE("Trying to post message to non-existing instance: [%p], ignoring", that);
+  if (nullptr != that) {
+    std::lock_guard<std::mutex> lock{instances_mutex_};
+    if (all_instances_.end() != all_instances_.find(that)) {
+      that->PostMessage(msg);
+      return;
+    }
   }
+  LoggerE("Trying to post message to non-existing instance: [%p], ignoring", that);
 }
 
 void Instance::PostMessage(const char* msg) {
index 907d1df..9bb16f4 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <functional>
 #include <map>
+#include <mutex>
 #include <string>
 #include <unordered_set>
 
@@ -109,6 +110,7 @@ class Instance {
  private:
   friend class Extension;
 
+  static std::mutex instances_mutex_;
   static std::unordered_set<Instance*> all_instances_;
 
   XW_Instance xw_instance_;