Modify rpc-port APIs to be thread-safe 96/168296/5
authorJunghoon Park <jh9216.park@samsung.com>
Thu, 25 Jan 2018 11:18:00 +0000 (20:18 +0900)
committerJunghoon Park <jh9216.park@samsung.com>
Fri, 26 Jan 2018 07:38:47 +0000 (16:38 +0900)
Change-Id: I3f27851e09491b3b2c7638c3b9dcf6419f57684f
Signed-off-by: Junghoon Park <jh9216.park@samsung.com>
src/port-internal.cc
src/port-internal.h
src/rpc-port.cc

index caa354c..695f617 100644 (file)
@@ -53,6 +53,7 @@ int Port::Read(void* buf, unsigned int size) {
   const struct timespec TRY_SLEEP_TIME = { 0, 500 * 1000 * 1000 };
   int bytes_read = 0;
   char* buffer = static_cast<char*>(buf);
+  std::lock_guard<std::recursive_mutex> lock(mutex_);
 
   while (left && (retry_cnt < MAX_RETRY_CNT)) {
     nb = read(fd_, buffer, left);
@@ -92,6 +93,7 @@ int Port::Write(const void* buf, unsigned int size) {
   int ret;
   int bytes_write = 0;
   const char* buffer = static_cast<const char*>(buf);
+  std::lock_guard<std::recursive_mutex> lock(mutex_);
 
   fds[0].fd = fd_;
   fds[0].events = POLLOUT;
index d612cb7..5fd3a1a 100644 (file)
@@ -17,6 +17,8 @@
 #ifndef PORT_INTERNAL_H_
 #define PORT_INTERNAL_H_
 
+#include <thread>
+#include <mutex>
 #include <string>
 #include <memory>
 
@@ -41,6 +43,7 @@ class Port {
  private:
   int fd_;
   std::string id_;
+  std::recursive_mutex mutex_;
 };
 
 }  // namespace internal
index 53e3d3a..ba9ead2 100755 (executable)
@@ -16,6 +16,9 @@
 
 #include <glib.h>
 
+#include <thread>
+#include <mutex>
+
 #include "rpc-port.h"
 #include "port-internal.h"
 #include "proxy-internal.h"
@@ -91,6 +94,10 @@ class ProxyExt : public Proxy, public Proxy::IEventListener {
     }
   }
 
+  std::recursive_mutex& GetMutex() const {
+    return mutex_;
+  }
+
  private:
   std::list<std::unique_ptr<Event<rpc_port_proxy_connected_event_cb>>>
       connected_events_;
@@ -100,6 +107,7 @@ class ProxyExt : public Proxy, public Proxy::IEventListener {
       rejected_events_;
   std::list<std::unique_ptr<Event<rpc_port_proxy_received_event_cb>>>
       received_events_;
+  mutable std::recursive_mutex mutex_;
 };
 
 class StubExt : public Stub, public Stub::IEventListener {
@@ -147,6 +155,10 @@ class StubExt : public Stub, public Stub::IEventListener {
     return 0;
   }
 
+  std::recursive_mutex& GetMutex() const {
+    return mutex_;
+  }
+
  private:
   std::list<std::unique_ptr<Event<rpc_port_stub_connected_event_cb>>>
       connected_events_;
@@ -154,6 +166,7 @@ class StubExt : public Stub, public Stub::IEventListener {
       disconnected_events_;
   std::list<std::unique_ptr<Event<rpc_port_stub_received_event_cb>>>
       received_events_;
+  mutable std::recursive_mutex mutex_;
 };
 
 }  // namespace
@@ -206,6 +219,7 @@ RPC_API int rpc_port_proxy_connect(rpc_port_proxy_h h, const char* appid,
     return -1;
 
   auto p = static_cast<::ProxyExt*>(h);
+  std::lock_guard<std::recursive_mutex> lock(p->GetMutex());
 
   p->Connect(appid, port, p);
   return 0;
@@ -217,6 +231,7 @@ RPC_API int rpc_port_proxy_add_connected_event_cb(rpc_port_proxy_h h,
     return -1;
 
   auto p = static_cast<::ProxyExt*>(h);
+  std::lock_guard<std::recursive_mutex> lock(p->GetMutex());
 
   p->AddConnectedEventListener(cb, data);
   return 0;
@@ -228,6 +243,7 @@ RPC_API int rpc_port_proxy_add_disconnected_event_cb(rpc_port_proxy_h h,
     return -1;
 
   auto p = static_cast<::ProxyExt*>(h);
+  std::lock_guard<std::recursive_mutex> lock(p->GetMutex());
 
   p->AddDisconnectedEventListener(cb, data);
   return 0;
@@ -239,6 +255,7 @@ RPC_API int rpc_port_proxy_add_rejected_event_cb(rpc_port_proxy_h h,
     return -1;
 
   auto p = static_cast<::ProxyExt*>(h);
+  std::lock_guard<std::recursive_mutex> lock(p->GetMutex());
 
   p->AddRejectedEventListener(cb, data);
   return 0;
@@ -250,6 +267,7 @@ RPC_API int rpc_port_proxy_add_received_event_cb(rpc_port_proxy_h h,
     return -1;
 
   auto p = static_cast<::ProxyExt*>(h);
+  std::lock_guard<std::recursive_mutex> lock(p->GetMutex());
 
   p->AddReceivedEventListener(cb, data);
   return 0;
@@ -291,6 +309,7 @@ RPC_API int rpc_port_stub_listen(rpc_port_stub_h h) {
     return -1;
 
   auto p = static_cast<::StubExt*>(h);
+  std::lock_guard<std::recursive_mutex> lock(p->GetMutex());
 
   p->Listen(p);
   return 0;
@@ -302,6 +321,8 @@ RPC_API int rpc_port_stub_add_privilege(rpc_port_stub_h h,
     return -1;
 
   auto p = static_cast<::StubExt*>(h);
+  std::lock_guard<std::recursive_mutex> lock(p->GetMutex());
+
   p->AddPrivilege(privilege);
   return 0;
 }
@@ -312,6 +333,7 @@ RPC_API int rpc_port_stub_add_connected_event_cb(rpc_port_stub_h h,
     return -1;
 
   auto p = static_cast<::StubExt*>(h);
+  std::lock_guard<std::recursive_mutex> lock(p->GetMutex());
 
   p->AddConnectedEventListener(cb, data);
   return 0;
@@ -323,6 +345,7 @@ RPC_API int rpc_port_stub_add_disconnected_event_cb(rpc_port_stub_h h,
     return -1;
 
   auto p = static_cast<::StubExt*>(h);
+  std::lock_guard<std::recursive_mutex> lock(p->GetMutex());
 
   p->AddDisconnectedEventListener(cb, data);
   return 0;
@@ -334,6 +357,7 @@ RPC_API int rpc_port_stub_add_received_event_cb(rpc_port_stub_h h,
     return -1;
 
   auto p = static_cast<::StubExt*>(h);
+  std::lock_guard<std::recursive_mutex> lock(p->GetMutex());
 
   p->AddReceivedEventListener(cb, data);
   return 0;