Modify AppCoreUiThreadBase class 01/294301/2
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 16 Jun 2023 01:08:14 +0000 (01:08 +0000)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 16 Jun 2023 02:01:48 +0000 (02:01 +0000)
To use the Post() method before calling the Run() method, the GMainContext
should be created when the instance is created.

Change-Id: I6638965e671ee83b1691fd64e9615ed441e3afa0
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
tizen-cpp/app-core-ui-cpp/app_core_ui_thread_base.cc

index 2b3fe35..67bd5bf 100644 (file)
@@ -38,33 +38,43 @@ AppCoreUiThreadBase* context = nullptr;
 
 class AppCoreUiThreadBase::Impl {
  public:
-  Impl(AppCoreUiThreadBase* parent) : parent_(parent) {}
+  Impl(AppCoreUiThreadBase* parent) : parent_(parent) {
+    context_ = g_main_context_new();
+    std::string context_str = std::to_string(
+        reinterpret_cast<unsigned long int>(context_));
+    setenv("TIZEN_GLIB_CONTEXT", context_str.c_str(), 1);
+  }
 
   ~Impl() {
     if (thread_.joinable())
       thread_.join();
+
+    if (context_ != nullptr) {
+      setenv("TIZEN_GLIB_CONTEXT", "0", 1);
+      g_main_context_unref(context_);
+    }
   }
 
   void Run(int argc, char** argv) {
-    if (context_ != nullptr) {
+    if (running_) {
       _E("Already running");
       return;
     }
 
-    context_ = g_main_context_new();
-    std::string context_str = std::to_string(
-        reinterpret_cast<unsigned long int>(context_));
-    setenv("TIZEN_GLIB_CONTEXT", context_str.c_str(), 1);
-
+    running_ = true;
     thread_ = std::thread([&] {
           pthread_setname_np(pthread_self(), "UIThread+");
           parent_->OnLoopInit(argc, argv);
           parent_->OnLoopRun();
+          running_ = false;
           parent_->OnLoopFinish();
         });
   }
 
   void Exit() {
+    if (!running_)
+      return;
+
     parent_->OnLoopExit();
   }
 
@@ -90,6 +100,7 @@ class AppCoreUiThreadBase::Impl {
   std::thread thread_;
   GMainContext* context_ = nullptr;
   tizen_base::SharedQueue<Runner> queue_;
+  bool running_ = false;
 };
 
 AppCoreUiThreadBase::AppCoreUiThreadBase() : impl_(new Impl(this)) {