From ed272288f29f659a06c1260f1939b8621efd092d Mon Sep 17 00:00:00 2001 From: Joonbum Ko Date: Wed, 10 Jan 2024 19:19:40 +0900 Subject: [PATCH] utils_gthread: add a new API to attach idle source Change-Id: Ic28c36f5b8d67cfd2f967b130cb854d1a9301ecf Signed-off-by: Joonbum Ko --- src/tpl_utils_gthread.c | 41 ++++++++++++++++++++++++----------------- src/tpl_utils_gthread.h | 13 +++++++++++++ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/tpl_utils_gthread.c b/src/tpl_utils_gthread.c index c744b5b..2e46ee4 100644 --- a/src/tpl_utils_gthread.c +++ b/src/tpl_utils_gthread.c @@ -507,7 +507,6 @@ _thread_idle_cb(gpointer data) tpl_result_t tpl_gthread_wait_idle(tpl_gthread *gthread) { - GSource *idle_source = NULL; gint64 end_time; gboolean ret = TRUE; tpl_result_t res = TPL_ERROR_NONE; @@ -516,22 +515,9 @@ tpl_gthread_wait_idle(tpl_gthread *gthread) g_mutex_lock(>hread->idle_mutex); - idle_source = g_idle_source_new(); - if (idle_source == NULL) { - TPL_WARN("Failed to create and attach idle source"); - res = TPL_ERROR_INVALID_OPERATION; - g_mutex_unlock(>hread->idle_mutex); - return res; - } - - g_source_set_priority(idle_source, G_PRIORITY_LOW); - g_source_set_callback(idle_source, - _thread_idle_cb, (gpointer)gthread, - NULL); - g_source_attach(idle_source, g_main_loop_get_context(gthread->loop)); - g_source_unref(idle_source); + tpl_gthread_add_idle(gthread, (tpl_gsource_func)_thread_idle_cb, gthread); - /* 200ms timeout */ + /* 500ms timeout */ end_time = g_get_monotonic_time() + (WAIT_IDLE_TIMEOUT * G_TIME_SPAN_MILLISECOND); do { @@ -600,4 +586,25 @@ tpl_gthread_continue(tpl_gthread *gthread) if (!gthread->paused) return; g_mutex_unlock(>hread->pause_mutex); -} \ No newline at end of file +} + +tpl_result_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); + + 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; + } + + g_source_set_priority(idle_source, G_PRIORITY_DEFAULT + 10); + g_source_set_callback(idle_source, + idle_cb, (gpointer)data, + NULL); + g_source_attach(idle_source, g_main_loop_get_context(gthread->loop)); + g_source_unref(idle_source); + + return TPL_ERROR_NONE; +} diff --git a/src/tpl_utils_gthread.h b/src/tpl_utils_gthread.h index b5f0eb5..2c1eee6 100644 --- a/src/tpl_utils_gthread.h +++ b/src/tpl_utils_gthread.h @@ -12,6 +12,8 @@ typedef struct _tpl_gsource tpl_gsource; typedef struct _tpl_gsource_functions tpl_gsource_functions; typedef void (*tpl_gthread_func) (void *user_data); +//typedef GSourceFunc tpl_gsource_func; +typedef int (*tpl_gsource_func) (void *user_data); typedef GMutex tpl_gmutex; typedef GCond tpl_gcond; @@ -237,6 +239,17 @@ tpl_gthread_pause_in_idle(tpl_gthread *gthread); void tpl_gthread_continue(tpl_gthread *gthread); +/** + * attach new idle source with callback func + * + * @param gthread Pointer to tpl_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 +*/ +tpl_result_t +tpl_gthread_add_idle(tpl_gthread *gthread, tpl_gsource_func idle_cb, void *data); -- 2.34.1