Add mutex in DBHandler 59/255559/4
authorChanggyu Choi <changyu.choi@samsung.com>
Fri, 19 Mar 2021 07:35:44 +0000 (16:35 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Tue, 23 Mar 2021 00:18:46 +0000 (00:18 +0000)
Add shared_mutex in DBHandler.
When it received get request, then use shared_lock.
So multiple thread is accessible.
On the other hand, when be received set request,
it use unique_lock.

Change-Id: Ifbf02d50713479d26372adaa4ad4772a030be2ef
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
src/common/database/abstract_db_handler.cc
src/common/database/abstract_db_handler.hh
src/common/database/appinfo_db_handler.cc
src/common/database/cert_get_db_handler.cc
src/common/database/cert_set_db_handler.cc
src/common/database/db_handle_provider.hh
src/common/database/depinfo_db_handler.cc
src/common/database/pkg_get_db_handler.cc
src/common/database/pkg_set_db_handler.cc
src/common/database/query_handler.cc

index 4b74a93..aad27e8 100644 (file)
@@ -223,6 +223,8 @@ uid_t ConvertUID(uid_t uid) {
 namespace pkgmgr_common {
 namespace database {
 
+std::shared_timed_mutex AbstractDBHandler::lock_;
+
 AbstractDBHandler::AbstractDBHandler(uid_t uid, int pid) {
   uid_ = uid;
   pid_ = pid;
index 31c7342..8590405 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef ABSTRACT_DB_HANDLER_HH_
 #define ABSTRACT_DB_HANDLER_HH_
 
+#include <shared_mutex>
 #include <string>
 #include <vector>
 
@@ -57,7 +58,7 @@ class EXPORT_API AbstractDBHandler {
   int GetPID();
   std::vector<std::pair<sqlite3*, uid_t>> GetConnection();
   std::string GetLocale();
-
+  static std::shared_timed_mutex lock_;
  private:
   std::vector<std::pair<std::string, uid_t>> GetDBPath();
   DBType db_type_ = DB_TYPE_NONE;
index 6f19f34..09e9c83 100644 (file)
@@ -17,6 +17,7 @@
 #include "appinfo_db_handler.hh"
 
 #include <vector>
+#include <shared_mutex>
 
 #include "pkgmgrinfo_basic.h"
 #include "pkgmgrinfo_internal.h"
@@ -55,6 +56,7 @@ void AppInfoDBHandler::SetFilter(pkgmgrinfo_filter_x* filter) {
 }
 
 int AppInfoDBHandler::Execute() {
+  std::shared_lock<std::shared_timed_mutex> s(lock_);
   SetOpType(OPERATION_TYPE_READ);
   SetDBType(DB_TYPE_FILE_PKGDB);
   if (!Connect())
index 426e4c2..80232c0 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "cert_get_db_handler.hh"
 
+#include <shared_mutex>
 #include <vector>
 
 #include "pkgmgrinfo_debug.h"
@@ -38,6 +39,7 @@ void CertGetDBHandler::SetPkgID(std::string pkgid) {
 }
 
 int CertGetDBHandler::Execute() {
+  std::shared_lock<std::shared_timed_mutex> s(lock_);
   SetOpType(OPERATION_TYPE_READ);
   SetDBType(DB_TYPE_FILE_CERTDB);
   if (!Connect()) {
index 00f0252..eb6e4b6 100644 (file)
@@ -35,6 +35,7 @@ void CertSetDBHandler::SetCertHandle(pkgmgr_certinfo_x* cert_info) {
 }
 
 int CertSetDBHandler::Execute() {
+  std::unique_lock<std::shared_timed_mutex> u(lock_);
   SetOpType(OPERATION_TYPE_WRITE);
   SetDBType(DB_TYPE_FILE_CERTDB);
   if (!Connect())
index c7ad5a6..104f927 100644 (file)
@@ -43,7 +43,7 @@ class EXPORT_API DBHandleProvider {
   static bool IsCrashedWriteRequest();
   std::vector<std::pair<std::string, uid_t>> GetParserDBPath(int pid, bool write);
   std::string GetCertDBPath(int pid, bool write);
-  void SetMemoryMode(int pid, bool flag);
+  void SetMemoryMode(pid_t pid, bool flag);
 
  private:
   DBHandleProvider(uid_t uid);
index 5bd39dc..c032256 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "depinfo_db_handler.hh"
 
+#include <shared_mutex>
 #include <vector>
 
 #include "pkgmgrinfo_debug.h"
@@ -38,6 +39,7 @@ void DepInfoGetDBHandler::SetPkgID(const std::string& pkgid) {
 }
 
 int DepInfoGetDBHandler::Execute() {
+  std::shared_lock<std::shared_timed_mutex> s(lock_);
   SetOpType(OPERATION_TYPE_READ);
   SetDBType(DB_TYPE_FILE_PKGDB);
 
index 6fc539d..dd878b2 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "pkg_get_db_handler.hh"
 
+#include <shared_mutex>
 #include <vector>
 
 #include "pkgmgrinfo_debug.h"
@@ -55,6 +56,7 @@ void PkgGetDBHandler::SetFilter(pkgmgrinfo_filter_x* filter) {
 }
 
 int PkgGetDBHandler::Execute() {
+  std::shared_lock<std::shared_timed_mutex> s(lock_);
   SetOpType(OPERATION_TYPE_READ);
   SetDBType(DB_TYPE_FILE_PKGDB);
   if (!Connect()) {
index a0c6080..f102a21 100644 (file)
@@ -48,6 +48,7 @@ std::vector<std::vector<std::string>> PkgSetDBHandler::GetResult() {
 }
 
 int PkgSetDBHandler::Execute() {
+  std::unique_lock<std::shared_timed_mutex> u(lock_);
   SetOpType(OPERATION_TYPE_WRITE);
   SetDBType(DB_TYPE_FILE_PKGDB);
 
index be553df..f8745e3 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "query_handler.hh"
 
+#include <shared_mutex>
 #include <vector>
 
 #include "pkgmgrinfo_debug.h"
@@ -42,6 +43,7 @@ std::vector<std::vector<std::string>> QueryHandler::GetResult() {
 }
 
 int QueryHandler::Execute() {
+  std::shared_lock<std::shared_timed_mutex> s(lock_);
   if (!Connect()) {
     _LOGE("Failed to connect database");
     return PMINFO_R_ERROR;