Add priority for united service configurefile using wait lock
authorSukhyungKang <shine.kang@samsung.com>
Tue, 15 Apr 2025 08:47:54 +0000 (17:47 +0900)
committerSukhyungKang <shine.kang@samsung.com>
Wed, 16 Apr 2025 01:53:04 +0000 (10:53 +0900)
Signed-off-by: SukhyungKang <shine.kang@samsung.com>
src/service.cc
src/service.hh
src/service_loader.cc
src/stub_service_loader.cc

index 092999a6c4832bce0459efb81387260e9efcfc0a..d4a80330a95bace902dc12eb899e124df4441aff 100644 (file)
@@ -19,6 +19,7 @@
 #include <glib.h>
 #include <tizen_core.h>
 
+#include <chrono>
 #include <stdexcept>
 #include <utility>
 
@@ -140,6 +141,7 @@ void Service::Run() {
         service->OnBaseCreate();
         service->state_ = Service::State::Running;
         service->NotifyStateChanged();
+        service->cond_var_.notify_one();
         return false;
       }, this, &source);
 
@@ -178,6 +180,17 @@ 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;
@@ -231,10 +244,6 @@ void Service::NotifyStateChanged() {
       [](gpointer user_data) {
         auto* args = static_cast<ServiceStateChangedEventArgs*>(user_data);
         auto& cb = args->GetService()->state_changed_cb_;
-
-        int state = static_cast<int>(args->GetService()->GetState());
-        _E("NotifyStateChanged : %d", state);
-
         if (cb != nullptr) cb(args->GetService(), args->GetState());
         delete args;
         return G_SOURCE_REMOVE;
index eb79e0e4c992a132f5ef2074d3a968e8c48e4942..692f6600ca8d054c9b4886a3c61e4becca4e330d 100644 (file)
@@ -24,6 +24,8 @@
 #include <atomic>
 #include <functional>
 #include <string>
+#include <condition_variable>
+#include <mutex>
 
 #include "service_info.hh"
 
@@ -56,6 +58,7 @@ 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;
@@ -91,6 +94,9 @@ 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 ec27f14d8a93f75bd42980e2564f6f5e417b6f8c..0f25974c72c6137c7a6341c6b7af5ee1f5846e7a 100755 (executable)
@@ -214,9 +214,12 @@ void ServiceLoader::RunService(const std::string& name) {
 }
 
 void ServiceLoader::RunAllServices() {
+  unsigned int previois_prority = 0;
+  std::shared_ptr<Service> previous_service = nullptr;
+
   for (auto& order : orders_) {
     try {
-      _E("Run service : %s, priority : %d", order.first.c_str(), order.second);
+      _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") {
@@ -224,11 +227,23 @@ void ServiceLoader::RunAllServices() {
         continue;
       }
 
-      RunService(order.first);
+      if (previois_prority == 0)
+        previois_prority = order.second;
+
+      if (previois_prority == order.second) {
+        RunService(order.first);
 
-      auto service = GetService(order.first);
+        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());
+        }
 
-      _E("[%s] service state is %s", order.first.c_str(), GetServiceState(service->GetState()).c_str());
+        RunService(order.first);
+
+        previous_service = GetService(order.first);
+      }
     } catch (const Exception& e) {
       _E("Error=%s", e.what());
     }
index 5e29a55a18d1b67af4d04ec781449cc0ff19be97..cffe52ffa691bb453239b402113dba85409d6ede 100644 (file)
@@ -41,6 +41,7 @@ class ServiceLoaderExt : public tizen_base::ServiceLoader {
   ServiceLoaderExt(int argc, char** argv, std::string name)
       : tizen_base::ServiceLoader(argc, argv, std::move(name)) {
     if (context != nullptr) throw std::runtime_error("Already exits");
+    context = this;
   }
 
   virtual ~ServiceLoaderExt() { context = nullptr; }
@@ -91,9 +92,9 @@ API int service_loader_run(int argc, char** argv, const char* name,
   }
 
   try {
-    ::context = new ::ServiceLoaderExt(argc, argv, name);
-    ::context->SetCallback(callback, user_data);
-    ::context->Run();
+    ::ServiceLoaderExt loader(argc, argv, name);
+    loader.SetCallback(callback, user_data);
+    loader.Run();
   } catch (const std::runtime_error& e) {
     _E("Exception occurs. error: %s", e.what());
     return SERVICE_ERROR_INVALID_CONTEXT;