Remove handling pending events when the loop finish 71/322371/3
authorjh9216.park <jh9216.park@samsung.com>
Tue, 8 Apr 2025 06:24:35 +0000 (15:24 +0900)
committerjh9216.park <jh9216.park@samsung.com>
Tue, 8 Apr 2025 06:58:07 +0000 (15:58 +0900)
- After loops ended, all pending events will be handled using a while
  loop. However, It cause get stuck, because some sources like idler may
not be have ended

Change-Id: I2ae6eb2d71fa1867f64249c58e83bbaf755cdcdd
Signed-off-by: jh9216.park <jh9216.park@samsung.com>
src/tizen-core/task.cc
tests/tizen-core_unittests/tizen_core_test.cc

index 3751575f569252c170094e7986f933a2b32a5d5b..50dcc06be956c4c35183769e5b1046d26447940a 100644 (file)
@@ -41,6 +41,7 @@ class IdleSource : public Source {
  private:
   static gboolean IdleCb(gpointer user_data) {
     auto* source = static_cast<IdleSource*>(user_data);
+    if (source->IsDisposing()) return G_SOURCE_REMOVE;
     auto source_ptr = source->shared_from_this();
     if (source->cb_())
       return G_SOURCE_CONTINUE;
@@ -63,6 +64,7 @@ class TimeoutSource : public Source {
  private:
   static gboolean TimeoutCb(gpointer user_data) {
     auto* source = static_cast<TimeoutSource*>(user_data);
+    if (source->IsDisposing()) return G_SOURCE_REMOVE;
     auto source_ptr = source->shared_from_this();
     if (source->cb_())
       return G_SOURCE_CONTINUE;
@@ -277,9 +279,6 @@ void Task::Run() {
     tid_ = getpid();
     g_main_loop_run(loop_);
 
-    while (g_main_context_pending(context_->GetHandle()))
-      g_main_context_dispatch(context_->GetHandle());
-
     tid_ = -1;
     g_main_loop_unref(loop_);
     loop_ = nullptr;
@@ -437,8 +436,6 @@ void Task::ThreadLoop() {
   pthread_setname_np(pthread_self(), name_.c_str());
   g_main_loop_run(loop_);
 
-  while (g_main_context_pending(context_->GetHandle()))
-    g_main_context_dispatch(context_->GetHandle());
   g_main_context_pop_thread_default(context_->GetHandle());
 
   {
index 7340f575c617d783afcdc948a3839340fdc27b94..7ac06dbddb8016f021adb9cb8d0cc1b2e074bf9a 100644 (file)
@@ -418,6 +418,35 @@ TEST_F(TizenCoreTest, tizen_core_add_idle_job_P) {
   ASSERT_EQ(touched_, true);
 }
 
+TEST_F(TizenCoreTest, tizen_core_add_idle_job_P2) {
+  tizen_core_h core = nullptr;
+  tizen_core_task_get_tizen_core(main_task_, &core);
+  ASSERT_NE(core, nullptr);
+
+  tizen_core_source_h source = nullptr;
+  int ret = tizen_core_add_idle_job(
+    core,
+    [](void* user_data) {
+      return true;
+    },
+    this, &source);
+
+  ret = tizen_core_add_idle_job(
+      core,
+      [](void* user_data) {
+        auto* test = static_cast<TizenCoreTest*>(user_data);
+        test->touched_ = true;
+        tizen_core_task_quit(test->main_task_);
+        return false;
+      },
+      this, &source);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_NONE);
+  ASSERT_NE(source, nullptr);
+
+  tizen_core_task_run(main_task_);
+  ASSERT_EQ(touched_, true);
+}
+
 TEST_F(TizenCoreTest, tizen_core_add_idle_job_N) {
   int ret = tizen_core_add_idle_job(nullptr, nullptr, nullptr, nullptr);
   ASSERT_EQ(ret, TIZEN_CORE_ERROR_INVALID_PARAMETER);