From 55a24589f1a3082566bafe845f63db3b2ef87983 Mon Sep 17 00:00:00 2001 From: SukhyungKang Date: Tue, 15 Apr 2025 17:47:54 +0900 Subject: [PATCH] Add priority for united service configurefile using wait lock Signed-off-by: SukhyungKang --- src/service.cc | 17 +++++++++++++---- src/service.hh | 6 ++++++ src/service_loader.cc | 23 +++++++++++++++++++---- src/stub_service_loader.cc | 7 ++++--- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/service.cc b/src/service.cc index 092999a..d4a8033 100644 --- a/src/service.cc +++ b/src/service.cc @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -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 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(user_data); auto& cb = args->GetService()->state_changed_cb_; - - int state = static_cast(args->GetService()->GetState()); - _E("NotifyStateChanged : %d", state); - if (cb != nullptr) cb(args->GetService(), args->GetState()); delete args; return G_SOURCE_REMOVE; diff --git a/src/service.hh b/src/service.hh index eb79e0e..692f660 100644 --- a/src/service.hh +++ b/src/service.hh @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include "service_info.hh" @@ -56,6 +58,7 @@ class Service : public std::enable_shared_from_this { void Run(); void Quit(); + void WaitRun(); bool IsRunning() const; const std::shared_ptr& GetServiceInfo() const; @@ -91,6 +94,9 @@ class Service : public std::enable_shared_from_this { std::atomic running_{false}; pid_t tid_ = -1; StateChangedCb state_changed_cb_ = nullptr; + + std::mutex mutex_; + std::condition_variable cond_var_; }; } // namespace tizen_base diff --git a/src/service_loader.cc b/src/service_loader.cc index ec27f14..0f25974 100755 --- a/src/service_loader.cc +++ b/src/service_loader.cc @@ -214,9 +214,12 @@ void ServiceLoader::RunService(const std::string& name) { } void ServiceLoader::RunAllServices() { + unsigned int previois_prority = 0; + std::shared_ptr 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()); } diff --git a/src/stub_service_loader.cc b/src/stub_service_loader.cc index 5e29a55..cffe52f 100644 --- a/src/stub_service_loader.cc +++ b/src/stub_service_loader.cc @@ -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; -- 2.34.1