utils_gthread: add a new API to attach idle source 98/304498/1
authorJoonbum Ko <joonbum.ko@samsung.com>
Wed, 10 Jan 2024 10:19:40 +0000 (19:19 +0900)
committerJoonbum Ko <joonbum.ko@samsung.com>
Fri, 12 Jan 2024 12:09:05 +0000 (21:09 +0900)
Change-Id: Ic28c36f5b8d67cfd2f967b130cb854d1a9301ecf
Signed-off-by: Joonbum Ko <joonbum.ko@samsung.com>
src/tpl_utils_gthread.c
src/tpl_utils_gthread.h

index c744b5b33bfed65f29b6ce5d80038936185bf451..2e46ee4821b013fccb45342f4bdbdf38245ff5bc 100644 (file)
@@ -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(&gthread->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(&gthread->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(&gthread->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;
+}
index b5f0eb5661fab868dd6826dfb24aa6e403be9a28..2c1eee6134b2dccec21d4a3b706cd0c0d41c04e6 100644 (file)
@@ -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);