From: SukhyungKang Date: Fri, 4 Apr 2025 00:25:58 +0000 (+0900) Subject: Add priority for united service configurefile X-Git-Tag: accepted/tizen/unified/20250516.055915~9^2~7 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=930b2fca7ebfcf3d7515ba8231ce439a52807dc8;p=platform%2Fcore%2Fappfw%2Funited-service.git Add priority for united service configurefile Signed-off-by: SukhyungKang --- diff --git a/src/service.cc b/src/service.cc index 5351531..21ffa56 100644 --- a/src/service.cc +++ b/src/service.cc @@ -111,6 +111,7 @@ void Service::Run() { service->OnCreate(); service->state_ = Service::State::Running; ServiceManager::GetInst().NotifyServiceStateChanged(service); + service->cond_var_.notify_one(); return false; }, this, &source); @@ -122,6 +123,15 @@ void Service::Run() { running_ = true; } +void Service::WaitRun() { + _E("@@ wait run : %s", name_.c_str()); + std::unique_lock lock(mutex_); + + cond_var_.wait(lock, [this]() { return state_ == State::Running; }); + + _E("@@ wait end : %s", name_.c_str()); +} + void Service::Quit() { if (!running_) return; diff --git a/src/service.hh b/src/service.hh index 354ad59..d1a15c9 100644 --- a/src/service.hh +++ b/src/service.hh @@ -42,6 +42,7 @@ class Service : public std::enable_shared_from_this { void Run(); void Quit(); + void WaitRun(); const std::string& GetName() const; State GetState() const; diff --git a/src/service_context.cc b/src/service_context.cc index 8864417..cfd5f4b 100644 --- a/src/service_context.cc +++ b/src/service_context.cc @@ -38,6 +38,10 @@ ServiceContext::ServiceContext(std::shared_ptr info) ServiceContext::~ServiceContext() { Shutdown(); } bool ServiceContext::Init() { + return true; +} + +bool ServiceContext::Run() { if (!Load()) return false; auto* init_func = reinterpret_cast( @@ -84,6 +88,8 @@ std::shared_ptr ServiceContext::GetServiceInfo() const { const std::string& ServiceContext::GetName() const { return info_->GetName(); } +const unsigned int ServiceContext::GetPriority() const { return info_->GetPriority(); } + bool ServiceContext::Load() { if (handle_) return true; diff --git a/src/service_context.hh b/src/service_context.hh index 54939a9..00ca8f8 100644 --- a/src/service_context.hh +++ b/src/service_context.hh @@ -31,10 +31,12 @@ class ServiceContext { ~ServiceContext(); bool Init(); + bool Run(); void Shutdown(); std::shared_ptr GetService() const; std::shared_ptr GetServiceInfo() const; const std::string& GetName() const; + const unsigned int GetPriority() const; private: bool Load(); diff --git a/src/service_info.cc b/src/service_info.cc index 0c17c59..abd9108 100644 --- a/src/service_info.cc +++ b/src/service_info.cc @@ -18,6 +18,7 @@ #include +#include "exception.hh" #include "log_private.hh" namespace { @@ -28,6 +29,7 @@ static const std::string kName = kTagUnitedService + ":name"; static const std::string kMode = kTagUnitedService + ":mode"; static const std::string kType = kTagUnitedService + ":type"; static const std::string kPath = kTagUnitedService + ":path"; +static const std::string kPriority = kTagUnitedService + ":priority"; } // namespace @@ -46,6 +48,17 @@ ServiceInfo::ServiceInfo(std::string conf_name, _D("Type=%s", type_.c_str()); path_ = dictionary->Get(kPath); _D("Path=%s", path_.c_str()); + + try { + priority_ = stoul(dictionary->Get(kPriority)); + _D("Priority=%d", priority_); + + if (priority_ < 1 || priority_ > 99) + priority_ = 0; + } catch (const Exception& e) { + _E("Failed to get priority : %s", e.what()); + priority_ = 0; + } } ServiceInfo::~ServiceInfo() {} @@ -62,4 +75,6 @@ const std::string& ServiceInfo::GetType() const { return type_; } const std::string& ServiceInfo::GetPath() const { return path_; } +const unsigned int ServiceInfo::GetPriority() const { return priority_; } + } // namespace tizen_base diff --git a/src/service_info.hh b/src/service_info.hh index 3a6baa8..d912254 100644 --- a/src/service_info.hh +++ b/src/service_info.hh @@ -35,6 +35,7 @@ class ServiceInfo { const std::string& GetMode() const; const std::string& GetType() const; const std::string& GetPath() const; + const unsigned int GetPriority() const; private: std::string conf_name_; @@ -43,6 +44,7 @@ class ServiceInfo { std::string mode_; std::string type_; std::string path_; + unsigned int priority_; }; } // namespace tizen_base diff --git a/src/service_loader.cc b/src/service_loader.cc index 95f0007..86a64e4 100644 --- a/src/service_loader.cc +++ b/src/service_loader.cc @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -31,6 +32,31 @@ namespace { constexpr const char kPathUnitedService[] = "/usr/share/united-service/"; constexpr const char kConf[] = "/conf/"; +bool compare_context(const std::shared_ptr& context1, + const std::shared_ptr& context2) { + return context1->GetPriority() > context2->GetPriority(); +} + +void PrintState(const std::shared_ptr& context) { + switch(context->GetService()->GetState()) { + case tizen_base::Service::State::Initialized: + _W("%s : state Initialized", context->GetService()->GetName().c_str()); + break; + case tizen_base::Service::State::Created: + _W("%s : state Created", context->GetService()->GetName().c_str()); + break; + case tizen_base::Service::State::Running: + _W("%s : state Running", context->GetService()->GetName().c_str()); + break; + case tizen_base::Service::State::Destroyed: + _W("%s : state Destroyed", context->GetService()->GetName().c_str()); + break; + default: + _W("invalid state"); + break; + } +} + } // namespace namespace tizen_base { @@ -109,11 +135,54 @@ void ServiceLoader::LoadServices() { auto info = std::make_shared(file.string(), std::move(dictionary)); auto context = std::make_shared(std::move(info)); - context->Init(); + // context->Init(); contexts_.push_back(std::move(context)); } } } + + if (!contexts_.empty()) + std::sort(contexts_.begin(), contexts_.end(), compare_context); +} + +void ServiceLoader::RunServices() { + unsigned int previous_priority = 0; + std::shared_ptr previous_service = nullptr; + + for (auto& context : contexts_) { + if (context->GetServiceInfo()->GetMode().compare("on-demand") == 0) { + _W("%s is on-demand mode. it's skipped to run", context->GetName().c_str()); + continue; + } + + if (previous_priority == 0) + previous_priority = context->GetPriority(); + + if (previous_priority == context->GetPriority()) { + _E("@@ Priority : %d, pre-priority : %d, name : %s", context->GetPriority(), previous_priority, context->GetName().c_str()); + context->Run(); + + PrintState(context); + + previous_service = context; + } else { + _E("@@ Priority : %d, pre-priority : %d, name : %s", context->GetPriority(), previous_priority, context->GetName().c_str()); + previous_service->GetService()->WaitRun(); + + _E("@@ Previous state"); + PrintState(previous_service); + + context->Run(); + + _E("@@ Current state"); + PrintState(context); + + previous_service = context; + previous_priority = context->GetPriority(); + } + } + + // throw new std::runtime_error("Failed to Run service"); } int ServiceLoader::Run() { @@ -148,6 +217,8 @@ void ServiceLoader::SendMessage(const tizen_base::Bundle& envelope) { void ServiceLoader::OnCreate() { try { LoadServices(); + + RunServices(); } catch (const fs::filesystem_error& e) { _E("Exception occurs. error: %s(%d)", e.what(), e.code().value()); } catch (const Exception& e) { diff --git a/src/service_loader.hh b/src/service_loader.hh index a4bc63d..4dab542 100644 --- a/src/service_loader.hh +++ b/src/service_loader.hh @@ -20,6 +20,7 @@ #include #include +#include "service.hh" #include "service_context.hh" #include "service_info.hh" @@ -43,6 +44,7 @@ class ServiceLoader { bool Init(); void Shutdown(); void LoadServices(); + void RunServices(); static void ChannelReceiveCb(tizen_core_channel_object_h object, void* user_data);