Modify AppCoreUiThreadBase class
[platform/core/appfw/app-core.git] / tizen-cpp / app-core-ui-cpp / app_core_ui_base.cc
index e7f8c0e..9b1c99f 100644 (file)
@@ -39,6 +39,7 @@
 #include "app-core-ui-cpp/app_core_task_base.hh"
 #include "app-core-ui-cpp/app_core_ui_delegator_private.hh"
 #include "app-core-ui-cpp/app_core_ui_plugin_private.hh"
+#include "app-core-ui-cpp/app_core_ui_thread_base.hh"
 #include "app-core-ui-cpp/wayland_handler_private.hh"
 #include "app-core-ui-cpp/window_position_private.hh"
 #include "common/ecore_handler.hh"
@@ -51,22 +52,6 @@ constexpr const char K_SERVICE_THREAD[] = "__K_SERVICE_THREAD";
 constexpr const char K_HINT_RECENT_SCREEN_POSITION[] =
     "__K_HINT_RECENT_SCREEN_POSITION";
 
-void SetComm(const std::string& name) {
-  pid_t tid = syscall(__NR_gettid);
-  std::string path = "/proc/" + std::to_string(tid) + "/comm";
-  int fd = open(path.c_str(), O_WRONLY);
-  if (fd < 0) {
-    _E("open(%s) is failed. errno(%d)", path.c_str(), errno);
-    return;
-  }
-
-  ssize_t bytes_written = write(fd, name.c_str(), name.length() + 1);
-  if (bytes_written < 0)
-    _E("write(%d) is failed. errno(%d)", fd, errno);
-
-  close(fd);
-}
-
 class AppCoreUiBase::Impl {
  public:
   Impl(AppCoreUiBase* parent, unsigned int hint)
@@ -151,11 +136,10 @@ class AppCoreUiBase::Impl {
   std::unique_ptr<AppCoreUiDelegator> plugin_delegator_;
   std::unique_ptr<AppCoreUiPlugin> plugin_;
   std::unique_ptr<AppCoreTaskBase> service_;
-  GMainContext* context_ = nullptr;
-  std::thread thread_;
   std::unique_ptr<WindowPosition> position_;
   Ecore_Wl2_Window* window_ = nullptr;
   std::unique_ptr<WindowPositionManager> wp_manager_;
+  std::unique_ptr<AppCoreUiThreadBase> thread_;
 };
 
 AppCoreUiBase::AppCoreUiBase(unsigned int hint)
@@ -526,28 +510,16 @@ void AppCoreUiBase::DoExit() {
 
 void AppCoreUiBase::Impl::Run(int argc, char** argv) {
   if (hint_ & HINT_DUAL_THREAD) {
-    // For the loader case
-    while (ecore_shutdown() != 0) {}
-
     service_ = parent_->CreateTask();
-    context_ = g_main_context_new();
-    std::string env = std::to_string(
-        reinterpret_cast<unsigned long int>(context_));
-    setenv("TIZEN_GLIB_CONTEXT", env.c_str(), 1);
-
-    thread_ = std::thread([&] {
-          SetComm("UIThread+");
-          parent_->DoRun(argc, argv);
-        });
+    auto* thread_context = AppCoreUiThreadBase::GetContext();
+    if (thread_context == nullptr) {
+      thread_.reset(new AppCoreUiThreadBase());
+      thread_->Run(argc, argv);
+      thread_context = AppCoreUiThreadBase::GetContext();
+    }
 
+    thread_context->Post([&]() { parent_->DoRun(argc, argv); });
     service_->Run(argc, argv);
-
-    if (thread_.joinable())
-      thread_.join();
-
-    setenv("TIZEN_GLIB_CONTEXT", "0", 1);
-    g_main_context_unref(context_);
-    context_ = nullptr;
     return;
   }
 
@@ -556,11 +528,11 @@ void AppCoreUiBase::Impl::Run(int argc, char** argv) {
 
 void AppCoreUiBase::Impl::Exit() {
   if (hint_ & HINT_DUAL_THREAD) {
-    GLib::IdleAdd(context_, [](gpointer user_data) {
-          auto* impl = static_cast<AppCoreUiBase::Impl*>(user_data);
-          impl->parent_->DoExit();
-          return G_SOURCE_REMOVE;
-        }, this);
+    auto* thread_context = AppCoreUiThreadBase::GetContext();
+    if (thread_context != nullptr) {
+      thread_context->Post([&]() { parent_->DoExit(); });
+      thread_context->Exit();
+    }
 
     service_->Exit();
     return;