Use thread default context 89/270489/2
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 3 Feb 2022 09:15:09 +0000 (18:15 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 11 Mar 2022 05:47:55 +0000 (14:47 +0900)
After this patch is applied, the callback function will be invoked to
the caller thread. For example, the app_control_cb callback function
will be invoked to the current thread default context.

Change-Id: Ia85ebbf223ab06a6a10c94aaf28722a359ff2020
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/aul_launch.c

index c686dd5..beb7dc2 100644 (file)
@@ -81,6 +81,7 @@ typedef struct launch_context_s {
        data_control_provider_handler dcp;
        GList* clients;
        GRecMutex mutex;
+       GMainContext *thread_default_context;
 } launch_context;
 
 static launch_context __context;
@@ -445,6 +446,23 @@ static gboolean __dispatch_request(gpointer data)
        return G_SOURCE_REMOVE;
 }
 
+static guint __g_idle_add_full(gint priority, GSourceFunc func, gpointer data)
+{
+       GSource *source;
+       guint tag;
+
+       source = g_idle_source_new();
+       if (!source)
+               return 0;
+
+       g_source_set_callback(source, (GSourceFunc)func, data, NULL);
+       g_source_set_priority(source, priority);
+       tag = g_source_attach(source, __context.thread_default_context);
+       g_source_unref(source);
+
+       return tag;
+}
+
 static void __process_app_pkt(app_pkt_t *pkt, int clifd)
 {
        struct aul_request_s *req;
@@ -464,7 +482,7 @@ static void __process_app_pkt(app_pkt_t *pkt, int clifd)
                return;
        }
 
-       g_idle_add(__dispatch_request, req);
+       __g_idle_add_full(G_PRIORITY_DEFAULT, __dispatch_request, req);
 }
 
 static bool __received_event_cb(int fd, int condition, void *user_data)
@@ -569,6 +587,11 @@ static void __finalize_context(void)
                __context.worker = NULL;
        }
 
+       if (__context.thread_default_context) {
+               g_main_context_unref(__context.thread_default_context);
+               __context.thread_default_context = NULL;
+       }
+
        g_rec_mutex_lock(&__context.mutex);
        if (__context.clients) {
                g_list_free_full(__context.clients, __destroy_client_channel);
@@ -599,6 +622,8 @@ static int __initialize_context(void)
 
        g_rec_mutex_init(&__context.mutex);
 
+       __context.thread_default_context = g_main_context_ref_thread_default();
+
        __context.worker = aul_worker_create("aul+");
        if (!__context.worker) {
                __finalize_context();
@@ -663,7 +688,7 @@ API int aul_launch_argv_handler(int argc, char **argv)
        if (!b)
                _E("Bundle is nullptr");
 
-       if (!g_idle_add_full(G_PRIORITY_HIGH, __app_start_cb, b, NULL)) {
+       if (!__g_idle_add_full(G_PRIORITY_HIGH, __app_start_cb, b)) {
                _E("Failed to add idler");
                return AUL_R_ERROR;
        }
@@ -681,7 +706,7 @@ API int aul_launch_local(bundle *b)
        if (!b)
                _E("Bundle is nullptr");
 
-       if (!g_idle_add(__app_start_cb, b)) {
+       if (!__g_idle_add_full(G_PRIORITY_DEFAULT, __app_start_cb, b)) {
                _E("Failed to add idler");
                return AUL_R_ERROR;
        }