#include <glib.h>
#include <tizen_core.h>
+#include <chrono>
#include <stdexcept>
#include <utility>
service->OnBaseCreate();
service->state_ = Service::State::Running;
service->NotifyStateChanged();
+ service->cond_var_.notify_one();
return false;
}, this, &source);
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;
[](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;
#include <atomic>
#include <functional>
#include <string>
+#include <condition_variable>
+#include <mutex>
#include "service_info.hh"
void Run();
void Quit();
+ void WaitRun();
bool IsRunning() const;
const std::shared_ptr<ServiceInfo>& GetServiceInfo() const;
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
}
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") {
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());
}
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; }
}
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;