Add priority for united service configurefile run from state callback
authorSukhyungKang <shine.kang@samsung.com>
Fri, 25 Apr 2025 01:56:19 +0000 (10:56 +0900)
committerSukhyungKang <shine.kang@samsung.com>
Mon, 28 Apr 2025 00:45:00 +0000 (09:45 +0900)
Signed-off-by: SukhyungKang <shine.kang@samsung.com>
src/service.cc
src/service.hh
src/service_info.cc
src/service_loader.cc
src/service_loader.hh

index d4a80330a95bace902dc12eb899e124df4441aff..1dade7f114dbb32d7e59e2f5c446683f961093a8 100644 (file)
@@ -19,7 +19,6 @@
 #include <glib.h>
 #include <tizen_core.h>
 
-#include <chrono>
 #include <stdexcept>
 #include <utility>
 
@@ -141,7 +140,6 @@ void Service::Run() {
         service->OnBaseCreate();
         service->state_ = Service::State::Running;
         service->NotifyStateChanged();
-        service->cond_var_.notify_one();
         return false;
       }, this, &source);
 
@@ -180,17 +178,6 @@ void Service::Quit() {
   if (source == nullptr) _E("Failed to add idle job");
 }
 
-void Service::WaitRun() {
-  _E("@@ wait run : %s", GetName().c_str());
-  std::unique_lock<std::mutex> lock(mutex_);
-
-  if (!cond_var_.wait_for (lock, std::chrono::milliseconds(500),
-                          [this]() { return state_ == State::Running; }))
-    _E("@@ wait timeout : %s", GetName().c_str());
-
-  _E("@@ wait end : %s", GetName().c_str());
-}
-
 void Service::SendMessage(const tizen_base::Bundle& envelope) {
   tizen_base::Bundle msg(envelope);
   tizen_core_channel_object_h object = nullptr;
index 692f6600ca8d054c9b4886a3c61e4becca4e330d..eb79e0e4c992a132f5ef2074d3a968e8c48e4942 100644 (file)
@@ -24,8 +24,6 @@
 #include <atomic>
 #include <functional>
 #include <string>
-#include <condition_variable>
-#include <mutex>
 
 #include "service_info.hh"
 
@@ -58,7 +56,6 @@ class Service : public std::enable_shared_from_this<Service> {
 
   void Run();
   void Quit();
-  void WaitRun();
 
   bool IsRunning() const;
   const std::shared_ptr<ServiceInfo>& GetServiceInfo() const;
@@ -94,9 +91,6 @@ class Service : public std::enable_shared_from_this<Service> {
   std::atomic<bool> running_{false};
   pid_t tid_ = -1;
   StateChangedCb state_changed_cb_ = nullptr;
-
-  std::mutex mutex_;
-  std::condition_variable cond_var_;
 };
 
 } // namespace tizen_base
index e38e9c2b7a31979c1f4b29667615bd9d950bec88..d9835817d1e7e28687425527e65c99640e275151 100644 (file)
@@ -52,7 +52,12 @@ ServiceInfo::ServiceInfo(std::string conf_name,
   assembly_ = std::make_shared<ServiceAssembly>(path_);
 
   try {
-    priority_ = stoul(dictionary->Get(kPriority));
+    std::string priority_str = dictionary->Get(kPriority);
+
+    if (priority_str.empty() || !isdigit(priority_str[0]))
+      THROW(SERVICE_ERROR_INVALID_PARAMETER);
+
+    priority_ = stoul(priority_str);
     _D("Priority=%d", priority_);
 
     if (priority_ < 1 || priority_ > 99) {
index 0f25974c72c6137c7a6341c6b7af5ee1f5846e7a..f3f26166ca3d54e045d363ba18171a8e5f637052 100755 (executable)
@@ -32,12 +32,12 @@ namespace {
 constexpr const char kPathUnitedService[] = "/usr/share/united-service/";
 constexpr const char kConf[] = "/conf/";
 
-bool compare_priority(const std::pair<std::string, unsigned int>& pair1,
+bool ComparePriority(const std::pair<std::string, unsigned int>& pair1,
     const std::pair<std::string, unsigned int>& pair2) {
   return pair1.second > pair2.second;
 }
 
-std::string GetServiceState(tizen_base::Service::State state) {
+std::string ServiceStateToString(tizen_base::Service::State state) {
   switch(state) {
     case tizen_base::Service::State::Initialized:
       return std::string("Initialized");
@@ -149,7 +149,7 @@ void ServiceLoader::LoadServices() {
   }
 
   if (!orders_.empty())
-    std::sort(orders_.begin(), orders_.end(), compare_priority);
+    std::sort(orders_.begin(), orders_.end(), ComparePriority);
 }
 
 int ServiceLoader::Run() {
@@ -182,6 +182,8 @@ void ServiceLoader::SendMessage(const tizen_base::Bundle& envelope) {
 }
 
 void ServiceLoader::RunService(const std::string& name) {
+  _W("Run service : %s", name.c_str());
+
   auto service = GetService(name);
   if (service != nullptr && service->IsRunning()) {
     _E("Already running. name=%s", name.c_str());
@@ -214,39 +216,26 @@ void ServiceLoader::RunService(const std::string& name) {
 }
 
 void ServiceLoader::RunAllServices() {
-  unsigned int previois_prority = 0;
-  std::shared_ptr<Service> previous_service = nullptr;
+  _W("RunAllServices : %d", orders_.size());
 
   for (auto& order : orders_) {
-    try {
-      _E("Run service : %s, priority : %d, pre priority : %d", order.first.c_str(), order.second, previois_prority);
-
-      auto info = GetServiceInfo(order.first);
-      if (info->GetMode() == "on-demand") {
-        _E("%s is on-demand mode. skip to run", order.first.c_str());
-        continue;
-      }
-
-      if (previois_prority == 0)
-        previois_prority = order.second;
+    _E("Add order queue service : %s, priority : %d", order.first.c_str(), order.second);
 
-      if (previois_prority == order.second) {
-        RunService(order.first);
+    auto info = GetServiceInfo(order.first);
+    if (info->GetMode() == "on-demand") {
+      _E("%s is on-demand mode. skip to run", order.first.c_str());
+      continue;
+    }
 
-        previous_service = GetService(order.first);
-      } else {
-        if (previous_service != nullptr) {
-          previous_service->WaitRun();
-          _E("[%s] pre service state is %s", previous_service->GetName().c_str(), GetServiceState(previous_service->GetState()).c_str());
-        }
+    order_queue_.push(order.first);
+  }
 
-        RunService(order.first);
+  try {
+    RunService(order_queue_.front());
 
-        previous_service = GetService(order.first);
-      }
-    } catch (const Exception& e) {
-      _E("Error=%s", e.what());
-    }
+    order_queue_.pop();
+  } catch (const Exception& e) {
+    _E("Error=%s", e.what());
   }
 }
 
@@ -349,7 +338,19 @@ std::shared_ptr<Service> ServiceLoader::GetService(const std::string& name) {
 
 void ServiceLoader::ServiceStateChangedCb(const Service* service,
                                           Service::State state) {
-  _W("ServiceStateChangedCb %s, state : %s", service->GetName().c_str(), GetServiceState(state).c_str());
+  _W("ServiceStateChangedCb %s, state : %s",
+      service->GetName().c_str(), ServiceStateToString(state).c_str());
+
+  if (state == Service::State::Running) {
+    if (!order_queue_.empty()) {
+      try {
+        RunService(order_queue_.front());
+        order_queue_.pop();
+      } catch (const Exception& e) {
+        _E("Error=%s", e.what());
+      }
+    }
+  }
 
   OnServiceStateChanged(service, state);
   if (state == Service::State::Destroyed) {
index 78c144528368fa98e1f84acf032820ad4b1eba97..bb7be241f9541a05995861d52f59360fe8e4f369 100644 (file)
@@ -24,6 +24,7 @@
 #include <string>
 #include <unordered_map>
 #include <algorithm>
+#include <queue>
 
 #include "service.hh"
 #include "service_info.hh"
@@ -78,6 +79,7 @@ class ServiceLoader {
   std::unordered_map<std::string, std::shared_ptr<Service>> services_;
 
   std::vector<std::pair<std::string, unsigned int>> orders_;
+  std::queue<std::string> order_queue_;
 };
 
 } // namespace tizen_base