tpl: Add tpl_gsource_remove() 82/318882/1
authorTaeHyeon Jeong <thyeon.jeong@samsung.com>
Thu, 10 Oct 2024 05:40:01 +0000 (14:40 +0900)
committerTaeHyeon Jeong <thyeon.jeong@samsung.com>
Thu, 10 Oct 2024 09:00:21 +0000 (18:00 +0900)
- This can remove the gsource registered in a specific GMainContext.

Change-Id: I2d0b1f15da0270a1e17577e045bcd06092689a76

src/tpl_utils_gthread.c
src/tpl_utils_gthread.h
src/tpl_wl_egl_thread.c

index 1befeda6ff37c2f1c652c9564fa6b725b9b46d91..a5c77bde983d146bd747b183c02b68c26d4a2410 100644 (file)
@@ -531,12 +531,13 @@ tpl_gthread_wait_idle(tpl_gthread *gthread)
        gint64 end_time;
        gboolean ret = TRUE;
        tpl_result_t res = TPL_ERROR_NONE;
+       tpl_gid_t id;
 
        TPL_LOG_D("[WAIT_IDLE]", "BEGIN");
 
        g_mutex_lock(&gthread->idle_mutex);
 
-       tpl_gthread_add_idle(gthread, (tpl_gsource_func)_thread_idle_cb, gthread);
+       id = tpl_gthread_add_idle(gthread, (tpl_gsource_func)_thread_idle_cb, gthread);
 
        /* 500ms timeout */
        end_time = g_get_monotonic_time() +
@@ -546,6 +547,7 @@ tpl_gthread_wait_idle(tpl_gthread *gthread)
                                                                &gthread->idle_mutex,
                                                                end_time);
                if (!ret) {
+                       tpl_gsource_remove(gthread, id);
                        TPL_ERR("wait_idle timeout!");
                        res = TPL_ERROR_TIME_OUT;
                        break;
@@ -609,15 +611,16 @@ tpl_gthread_continue(tpl_gthread *gthread)
        g_mutex_unlock(&gthread->pause_mutex);
 }
 
-tpl_result_t
+tpl_gid_t
 tpl_gthread_add_idle(tpl_gthread *gthread, tpl_gsource_func idle_cb, void *data)
 {
        TPL_CHECK_ON_NULL_RETURN_VAL(gthread, TPL_ERROR_INVALID_PARAMETER);
 
+       tpl_gid_t id;
        GSource *idle_source = g_idle_source_new();
        if (idle_source == NULL) {
                TPL_WARN("Failed to create and attach idle source");
-               return TPL_ERROR_INVALID_OPERATION;
+               return 0; // failure
        }
 
        g_source_set_priority(idle_source, G_PRIORITY_DEFAULT + 10);
@@ -626,9 +629,19 @@ tpl_gthread_add_idle(tpl_gthread *gthread, tpl_gsource_func idle_cb, void *data)
                                                  NULL);
 
        g_mutex_lock(&gthread->thread_mutex);
-       g_source_attach(idle_source, g_main_loop_get_context(gthread->loop));
+       id = g_source_attach(idle_source, g_main_loop_get_context(gthread->loop));
        g_mutex_unlock(&gthread->thread_mutex);
        g_source_unref(idle_source);
 
-       return TPL_ERROR_NONE;
+       return id;
+}
+
+void
+tpl_gsource_remove(tpl_gthread *gthread, tpl_gid_t id)
+{
+       GMainContext *context = g_main_loop_get_context(gthread->loop);
+       GSource *source = g_main_context_find_source_by_id(context, id);
+
+       if (source)
+               g_source_destroy(source);
 }
index 304fa74fb74ed2d4d4bb40466eeff95d4f2a558a..888e4a22b87fe993047d5be3511fb0b6ebbc5f8f 100644 (file)
@@ -10,6 +10,7 @@
 typedef struct _tpl_gthread tpl_gthread;
 typedef struct _tpl_gsource tpl_gsource;
 typedef struct _tpl_gsource_functions tpl_gsource_functions;
+typedef guint tpl_gid_t;
 
 typedef void (*tpl_gthread_func) (void *user_data);
 //typedef GSourceFunc tpl_gsource_func;
@@ -299,11 +300,16 @@ tpl_gthread_continue(tpl_gthread *gthread);
  * @param callback Callback function to be called on idle
  * @param data User data to pass to idle callback
  *
- * @return tpl_result_t result of add idle source
+ * @return ID(0 means failure) for the source within the gthread
 */
-tpl_result_t
+tpl_gid_t
 tpl_gthread_add_idle(tpl_gthread *gthread, tpl_gsource_func idle_cb, void *data);
 
-
-
-
+/**
+ * remove the source by id from a specific tpl_gthread
+ *
+ * @param gthread Pointer to tpl_gthread
+ * @param id ID of the source to remove
+*/
+void
+tpl_gsource_remove(tpl_gthread *gthread, tpl_gid_t id);
index 763959f240d623cf63d2714bf1d024c623ac6f95..76e602f9770b88ec5a92f9e33315b122aa07a58d 100755 (executable)
@@ -2445,6 +2445,7 @@ static void
 __tpl_wl_egl_surface_fini(tpl_surface_t *surface)
 {
        tpl_wl_egl_display_t *wl_egl_display = NULL;
+       tpl_gid_t id;
 
        TPL_ASSERT(surface);
        TPL_ASSERT(surface->display);
@@ -2468,8 +2469,8 @@ __tpl_wl_egl_surface_fini(tpl_surface_t *surface)
 
        wl_egl_surface->need_force_release = TPL_FALSE;
        wl_egl_surface->buffers_finalize_done = TPL_FALSE;
-       tpl_gthread_add_idle(wl_egl_display->thread,
-                                                __idle_cb_buffers_finalize, wl_egl_surface);
+       id = tpl_gthread_add_idle(wl_egl_display->thread,
+                                                         __idle_cb_buffers_finalize, wl_egl_surface);
        tpl_gcond_timed_wait(&wl_egl_surface->surf_cond,
                                                 &wl_egl_surface->surf_mutex,
                                                 BUFFER_CLEAR_WAITING_TIMEOUT_MS)
@@ -2477,6 +2478,7 @@ __tpl_wl_egl_surface_fini(tpl_surface_t *surface)
                if (wl_egl_surface->buffers_finalize_done)
                        break;
        } else {
+               tpl_gsource_remove(wl_egl_display->thread, id);
                TPL_WARN("buffer clear timeout. wl_egl_surface(%p)", wl_egl_surface);
 
                wl_egl_surface->buffers_finalize_done = TPL_TRUE;
@@ -2852,6 +2854,7 @@ __tpl_wl_egl_surface_dequeue_buffer(tpl_surface_t *surface, uint64_t timeout_ns,
        tpl_wl_egl_surface_t wl_egl_surface(surface->backend.data);
        tpl_wl_egl_display_t wl_egl_display(surface->display->backend.data);
        tpl_wl_egl_buffer_t *wl_egl_buffer   = NULL;
+       tpl_gid_t id;
 
        tbm_surface_queue_error_e tsq_err    = TBM_SURFACE_QUEUE_ERROR_NONE;
        int             bo_name              = 0;
@@ -2864,8 +2867,8 @@ __tpl_wl_egl_surface_dequeue_buffer(tpl_surface_t *surface, uint64_t timeout_ns,
        tpl_gmutex_lock(&wl_egl_surface->surf_mutex);
        if (wl_egl_surface->reset == TPL_TRUE) {
                wl_egl_surface->buffers_commit_done = TPL_FALSE;
-               tpl_gthread_add_idle(wl_egl_display->thread,
-                                                        __idle_cb_check_buffers_commit, wl_egl_surface);
+               id = tpl_gthread_add_idle(wl_egl_display->thread,
+                                                                 __idle_cb_check_buffers_commit, wl_egl_surface);
                tpl_gcond_timed_wait(&wl_egl_surface->surf_cond,
                                                         &wl_egl_surface->surf_mutex,
                                                         CHECK_COMMIT_TIMEOUT_MS)
@@ -2873,6 +2876,7 @@ __tpl_wl_egl_surface_dequeue_buffer(tpl_surface_t *surface, uint64_t timeout_ns,
                        if (wl_egl_surface->buffers_commit_done)
                                break;
                } else {
+                       tpl_gsource_remove(wl_egl_display->thread, id);
                        TPL_WARN("wl_egl_surface(%p) timeout error occured", wl_egl_surface);
                        _print_buffer_lists(wl_egl_surface);
                }