Clean up thread-related code in media-server 08/231208/6
authorMinje Ahn <minje.ahn@samsung.com>
Mon, 20 Apr 2020 06:17:41 +0000 (15:17 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Tue, 21 Apr 2020 04:31:06 +0000 (13:31 +0900)
Change-Id: I06a092205022f86b78dfd40de99c9d4b2dba5ace
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/server/include/media-server-db.h
src/server/include/media-server-dcm.h
src/server/include/media-server-thumb.h
src/server/media-server-db.c
src/server/media-server-dcm.c
src/server/media-server-main.c
src/server/media-server-thumb.c

index b859468..7d14c7f 100755 (executable)
@@ -24,7 +24,7 @@
 
 #include <glib.h>
 
-GMainLoop *ms_db_get_mainloop(void);
+void ms_db_finalize(void);
 gboolean ms_db_get_thread_status(void);
 gpointer ms_db_thread(gpointer data);
 
index d4673d2..00b8e5a 100755 (executable)
 #ifndef _MEDIA_SERVER_DCM_H_
 #define _MEDIA_SERVER_DCM_H_
 
+void ms_dcm_finalize(void);
 int ms_dcm_get_server_pid(void);
 void ms_dcm_reset_server_status(void);
 gpointer ms_dcm_agent_start_thread(gpointer data);
 
-
 #endif /*_MEDIA_SERVER_DCM_H_*/
 
index eed1916..cfb920d 100755 (executable)
@@ -30,7 +30,7 @@
 
 #define MAX_THUMB_REQUEST 100
 
-GMainLoop * ms_get_thumb_thread_mainloop(void);
+void ms_thumb_finalize(void);
 int ms_thumb_get_server_pid(void);
 void ms_thumb_reset_server_status(void);
 gpointer ms_thumb_agent_start_thread(gpointer data);
index 6127188..1f9c359 100755 (executable)
 #include "media-server-db.h"
 
 static GMainLoop *g_db_mainloop = NULL;
+static guint g_db_source_id;
 static bool db_thread_ready = false;
 
-GMainLoop *ms_db_get_mainloop(void)
+void ms_db_finalize(void)
 {
-       return g_db_mainloop;
+       if (g_main_loop_is_running(g_db_mainloop)) {
+               g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), g_db_source_id));
+               g_main_loop_quit(g_db_mainloop);
+       }
 }
+
 gboolean ms_db_get_thread_status(void)
 {
        return db_thread_ready;
@@ -63,16 +68,17 @@ gpointer ms_db_thread(gpointer data)
        context = g_main_context_new();
        /*Init main loop*/
        g_db_mainloop = g_main_loop_new(context, FALSE);
+       g_main_context_push_thread_default(context);
 
        /* Create new channel to watch UDP socket */
        channel = g_io_channel_unix_new(sockfd);
        source = g_io_create_watch(channel, G_IO_IN);
+       g_io_channel_unref(channel);
 
        /* Set callback to be called when socket is readable */
        g_source_set_callback(source, (GSourceFunc)ms_read_db_tcp_socket, db_handle, NULL);
-       g_source_attach(source, context);
-
-       g_main_context_push_thread_default(context);
+       g_db_source_id = g_source_attach(source, context);
+       g_source_unref(source);
 
        MS_DBG_INFO("Media Server DB thread is running");
        db_thread_ready = true;
@@ -81,13 +87,7 @@ gpointer ms_db_thread(gpointer data)
 
        MS_DBG_INFO("Media Server DB thread is shutting down");
        db_thread_ready = false;
-
-       g_io_channel_shutdown(channel, FALSE, NULL);
-       g_io_channel_unref(channel);
-
-       /*close socket*/
        close(sockfd);
-
        g_main_loop_unref(g_db_mainloop);
 
        return NULL;
index 4ff3051..57afe87 100755 (executable)
 static GMainLoop *g_dcm_agent_loop = NULL;
 static gboolean g_folk_dcm_server = FALSE;
 static gboolean g_shutdowning_dcm_server = FALSE;
-static int g_dcm_comm_sock = 0;
+static int g_dcm_comm_sock = -1;
 static int g_dcm_timer_id = 0;
 static int g_dcm_service_pid = 0;
+static guint g_dcm_source_id;
 
 static GQueue *g_dcm_request_queue = NULL;
 static gboolean g_dcm_queue_work = FALSE;
@@ -50,6 +51,18 @@ typedef struct {
 
 extern char MEDIA_IPC_PATH[][70];
 
+void ms_dcm_finalize(void)
+{
+       if (g_dcm_comm_sock != -1) {
+               close(g_dcm_comm_sock);
+               g_dcm_comm_sock = -1;
+       }
+
+       if (g_main_loop_is_running(g_dcm_agent_loop)) {
+               g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), g_dcm_source_id));
+               g_main_loop_quit(g_dcm_agent_loop);
+       }
+}
 
 int ms_dcm_get_server_pid(void)
 {
@@ -217,7 +230,7 @@ static gboolean __ms_dcm_agent_send_msg_to_dcm_server(dcmMsg *recv_msg, dcmMsg *
 ERROR:
        close(sock);
 
-       return FALSE;
+       return G_SOURCE_REMOVE;
 }
 
 static gboolean __ms_dcm_agent_timer(gpointer data)
@@ -249,15 +262,15 @@ static gboolean __ms_dcm_agent_timer(gpointer data)
        return FALSE;
 }
 
-static void __ms_dcm_create_timer(int id)
+static void __ms_dcm_create_timer(void)
 {
-       if (id > 0)
-               g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), id));
+       if (g_dcm_timer_id > 0)
+               g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), g_dcm_timer_id));
 
        GSource *timer_src = g_timeout_source_new_seconds(MS_TIMEOUT_SEC_20);
        g_source_set_callback(timer_src, __ms_dcm_agent_timer, NULL, NULL);
        g_dcm_timer_id = g_source_attach(timer_src, g_main_context_get_thread_default());
-
+       g_source_unref(timer_src);
 }
 
 static gboolean __ms_dcm_request_to_server(gpointer data)
@@ -289,11 +302,11 @@ static gboolean __ms_dcm_request_to_server(gpointer data)
                                return G_SOURCE_REMOVE;
                        }
 
-                       __ms_dcm_create_timer(g_dcm_timer_id);
+                       __ms_dcm_create_timer();
                }
        } else {
                /* Timer is re-created*/
-               __ms_dcm_create_timer(g_dcm_timer_id);
+               __ms_dcm_create_timer();
        }
 
        dcmRequest *req = NULL;
@@ -445,34 +458,26 @@ gpointer ms_dcm_agent_start_thread(gpointer data)
 
        context = g_main_context_new();
 
-       if (context == NULL)
-               MS_DBG_ERR("g_main_context_new failed");
-       else
-               MS_DBG("g_main_context_new success");
-
        g_dcm_agent_loop = g_main_loop_new(context, FALSE);
        g_main_context_push_thread_default(context);
 
        /* Create new channel to watch udp socket */
        channel = g_io_channel_unix_new(sockfd);
        source = g_io_create_watch(channel, G_IO_IN);
+       g_io_channel_unref(channel);
 
        /* Set callback to be called when socket is readable */
        g_source_set_callback(source, (GSourceFunc)__ms_dcm_agent_read_socket, NULL, NULL);
-       g_source_attach(source, context);
+       g_dcm_source_id = g_source_attach(source, context);
+       g_source_unref(source);
 
        MS_DBG_INFO("DCM Agent thread is running");
        g_main_loop_run(g_dcm_agent_loop);
        MS_DBG_INFO("DCM Agent thread is shutting down");
 
        /*close an IO channel & remove resources */
-       g_io_channel_shutdown(channel, FALSE, NULL);
-       g_io_channel_unref(channel);
-       close(g_dcm_comm_sock);
-
-       g_main_loop_unref(g_dcm_agent_loop);
-
        close(sockfd);
+       g_main_loop_unref(g_dcm_agent_loop);
 
        return NULL;
 }
index 876650c..1622287 100644 (file)
@@ -51,12 +51,12 @@ bool power_off; /*If this is true, poweroff notification received*/
 #define SMARTHUB_RESET_VCONF "db/smarthub/reset/firstscreen"
 bool smarthub_reset_start;
 bool smarthub_reset;
+static guint main_source_id;
 
 static void __ms_check_mediadb(void);
 static void __ms_update_storage_status(void);
 static void __ms_add_signal_handler(void);
-static void __ms_add_event_receiver(GIOChannel *channel);
-static void __ms_add_request_receiver(GMainLoop *mainloop, GIOChannel **channel);
+static void __ms_add_event_receiver(void);
 
 static bool __ms_is_dcm_supported(void);
 static bool __ms_is_external_supported(void);
@@ -262,32 +262,19 @@ int ms_write_media_server_status(void)
 #ifdef _USE_DEVICED_DBUS
 static void __power_off_cb(void *data)
 {
-       int fd = -1;
        MS_DBG_ERR("POWER OFF");
-
-       GIOChannel *channel = (GIOChannel *)data;
-
        power_off = true;
 
        /*Quit Thumbnail Thread*/
-       GMainLoop *thumb_mainloop = ms_get_thumb_thread_mainloop();
-       if (thumb_mainloop && g_main_loop_is_running(thumb_mainloop))
-               g_main_loop_quit(thumb_mainloop);
+       ms_thumb_finalize();
 
        /*Quit DB Thread*/
-       GMainLoop *db_mainloop = ms_db_get_mainloop();
-       if (db_mainloop && g_main_loop_is_running(db_mainloop))
-               g_main_loop_quit(db_mainloop);
-
-       /*close an IO channel*/
-       fd = g_io_channel_unix_get_fd(channel);
-       g_io_channel_shutdown(channel, FALSE, NULL);
-       g_io_channel_unref(channel);
-
-       if (fd > 0) {
-               if (close(fd) < 0)
-                       MS_DBG_STRERROR("CLOSE ERROR");
-       }
+       ms_db_finalize();
+
+       /*Quit DCM Thread*/
+       ms_dcm_finalize();
+
+       g_source_remove(main_source_id);
 
        ms_sys_unset_device_block_event_cb();
        ms_sys_unset_poweroff_cb();
@@ -397,6 +384,7 @@ int main(int argc, char **argv)
        bool is_dcm_supported = __ms_is_dcm_supported();
        power_off = false;
        int ret = 0;
+       int sockfd = -1;
 
        ret = ms_load_functions();
        if (ret != MS_MEDIA_ERR_NONE) {
@@ -439,10 +427,24 @@ int main(int argc, char **argv)
                return -1;
        }
 
-       __ms_add_request_receiver(mainloop, &channel);
+       /*prepare socket*/
+       g_mkdir_with_parents(tzplatform_mkpath(TZ_SYS_RUN, "media-server"), S_IRWXU | S_IRWXG | S_IRWXO);
+
+       if (ms_ipc_create_server_socket(MS_SCANNER_PORT, &sockfd) != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("Failed to create socket");
+               return -1;
+       } else {
+               if (ms_cynara_enable_credentials_passing(sockfd) != MS_MEDIA_ERR_NONE)
+                       MS_DBG_ERR("Failed to setup credential passing");
+
+               channel = g_io_channel_unix_new(sockfd);
+               g_io_channel_set_close_on_unref(channel, TRUE);
+               main_source_id = g_io_add_watch(channel, G_IO_IN, ms_read_socket, NULL);
+               g_io_channel_unref(channel);
+       }
 
        /* recevie event from other modules */
-       __ms_add_event_receiver(channel);
+       __ms_add_event_receiver();
 
        __ms_add_signal_handler();
 
@@ -492,39 +494,7 @@ int main(int argc, char **argv)
        return 0;
 }
 
-static void __ms_add_request_receiver(GMainLoop *mainloop, GIOChannel **channel)
-{
-
-       int sockfd = -1;
-       GSource *source = NULL;
-       GMainContext *context = NULL;
-
-       /*prepare socket*/
-       /* create dir socket */
-       g_mkdir_with_parents(tzplatform_mkpath(TZ_SYS_RUN, "media-server"), S_IRWXU | S_IRWXG | S_IRWXO);
-
-       /* Create and bind new UDP socket */
-       if (ms_ipc_create_server_socket(MS_SCANNER_PORT, &sockfd) != MS_MEDIA_ERR_NONE) {
-               MS_DBG_ERR("Failed to create socket");
-               return;
-       } else {
-               if (ms_cynara_enable_credentials_passing(sockfd) != MS_MEDIA_ERR_NONE)
-                       MS_DBG_ERR("Failed to setup credential passing");
-
-               context = g_main_loop_get_context(mainloop);
-
-               /* Create new channel to watch udp socket */
-               *channel = g_io_channel_unix_new(sockfd);
-               source = g_io_create_watch(*channel, G_IO_IN);
-
-               /* Set callback to be called when socket is readable */
-               g_source_set_callback(source, (GSourceFunc)ms_read_socket, *channel, NULL);
-               g_source_attach(source, context);
-               g_source_unref(source);
-       }
-}
-
-static void __ms_add_event_receiver(GIOChannel *channel)
+static void __ms_add_event_receiver(void)
 {
        int err = 0;
 #ifdef _USE_META_UPDATE
@@ -560,7 +530,7 @@ static void __ms_add_event_receiver(GIOChannel *channel)
 #endif
        /*set power off callback function*/
 #ifdef _USE_DEVICED_DBUS
-       ms_sys_set_poweroff_cb(__power_off_cb, channel);
+       ms_sys_set_poweroff_cb(__power_off_cb, NULL);
 #endif
        ms_sys_set_device_block_event_cb(ms_device_block_changed_cb, NULL);
 }
index 37c52e2..8b74706 100755 (executable)
 static GMainLoop *g_thumb_agent_loop = NULL;
 static gboolean g_thumb_server_forked = FALSE;
 static gboolean g_shutdowning_thumb_server = FALSE;
-static int g_communicate_sock = 0;
+static int g_communicate_sock = -1;
 static int g_timer_id = 0;
 static int g_server_pid = 0;
 
 static GQueue *g_request_queue = NULL;
 static gboolean g_queue_work = FALSE;
+static guint g_thumb_agent_source_id;
 
 typedef struct {
        int client_sock;
@@ -50,9 +51,17 @@ typedef struct {
 
 extern char MEDIA_IPC_PATH[][70];
 
-GMainLoop* ms_get_thumb_thread_mainloop(void)
+void ms_thumb_finalize(void)
 {
-       return g_thumb_agent_loop;
+       if (g_communicate_sock != -1) {
+               close(g_communicate_sock);
+               g_communicate_sock = -1;
+       }
+
+       if (g_main_loop_is_running(g_thumb_agent_loop)) {
+               g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), g_thumb_agent_source_id));
+               g_main_loop_quit(g_thumb_agent_loop);
+       }
 }
 
 int ms_thumb_get_server_pid(void)
@@ -304,17 +313,18 @@ static gboolean __ms_thumb_agent_timer(gpointer data)
                MS_DBG_ERR("g_server_pid is %d. Maybe there's problem in thumbnail-server", g_server_pid);
        }
 
-       return FALSE;
+       return G_SOURCE_REMOVE;
 }
 
-static void __ms_thumb_create_timer(int id)
+static void __ms_thumb_create_timer(void)
 {
-       if (id > 0)
-               g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), id));
+       if (g_timer_id > 0)
+               g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), g_timer_id));
 
        GSource *timer_src = g_timeout_source_new_seconds(MS_TIMEOUT_SEC_20);
        g_source_set_callback(timer_src, __ms_thumb_agent_timer, NULL, NULL);
        g_timer_id = g_source_attach(timer_src, g_main_context_get_thread_default());
+       g_source_unref(timer_src);
 }
 
 static gboolean __ms_thumb_request_to_server(gpointer data)
@@ -342,7 +352,7 @@ static gboolean __ms_thumb_request_to_server(gpointer data)
                        return G_SOURCE_REMOVE;
                }
        }
-       __ms_thumb_create_timer(g_timer_id);
+       __ms_thumb_create_timer();
 
        req = (thumbRequest *)g_queue_pop_head(g_request_queue);
        MS_DBG_RETVM_IF(!req, TRUE, "Failed to get a request job from queue");
@@ -517,11 +527,6 @@ gpointer ms_thumb_agent_start_thread(gpointer data)
        }
 
        context = g_main_context_new();
-       if (!context) {
-               MS_DBG_ERR("g_main_context_new failed");
-               close(sockfd);
-               return NULL;
-       }
 
        g_thumb_agent_loop = g_main_loop_new(context, FALSE);
        g_main_context_push_thread_default(context);
@@ -529,23 +534,20 @@ gpointer ms_thumb_agent_start_thread(gpointer data)
        /* Create new channel to watch udp socket */
        channel = g_io_channel_unix_new(sockfd);
        source = g_io_create_watch(channel, G_IO_IN);
+       g_io_channel_unref(channel);
 
        /* Set callback to be called when socket is readable */
        g_source_set_callback(source, (GSourceFunc)__ms_thumb_agent_read_socket, NULL, NULL);
-       g_source_attach(source, context);
+       g_thumb_agent_source_id = g_source_attach(source, context);
+       g_source_unref(source);
 
        MS_DBG_INFO("Thumbnail Agent thread is running");
        g_main_loop_run(g_thumb_agent_loop);
        MS_DBG_INFO("Thumbnail Agent thread is shutting down");
 
        /*close an IO channel*/
-       g_io_channel_shutdown(channel, FALSE, NULL);
-       g_io_channel_unref(channel);
-       close(g_communicate_sock);
-
-       g_main_loop_unref(g_thumb_agent_loop);
-
        close(sockfd);
+       g_main_loop_unref(g_thumb_agent_loop);
 
        return NULL;
 }