[Multithread support] Maintain ref_count for Dbus signal registration/deregistration 61/196061/1 accepted/tizen/unified/20190115.060004 submit/tizen/20190114.060735 submit/tizen/20190114.074207
authorShagun Garg <shagun1.garg@samsung.com>
Fri, 21 Dec 2018 05:39:32 +0000 (11:09 +0530)
committerShagun Garg <shagun1.garg@samsung.com>
Fri, 21 Dec 2018 05:47:33 +0000 (11:17 +0530)
Maintain ref_count for Dbus signal registration/deregistration.
Register for DBus signals only once for a process, irrespective of multiple threads.
Deregiter Dbus signals when ref_count becomes zero.
This patch also handles the case when duplicated signals are being sent to main thread because of multiple threads signal registrations.

Change-Id: I33b1b53f69538e39fdef183fc7f06122e16c324f
Signed-off-by: Shagun Garg <shagun1.garg@samsung.com>
src/wifi-direct-dbus.c [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 92f5223..a8c3b02
@@ -34,6 +34,7 @@ typedef struct {
        guint signal_subscribe_id;
 } gdbus_connection_data;
 
+static int ref_count = 0;
 static __thread gdbus_connection_data gdbus_conn = {NULL, 0};
 
 typedef struct {
@@ -49,7 +50,7 @@ typedef struct {
                          gpointer user_data);
 } wifi_direct_dbus_signal_map_s;
 
-static __thread wifi_direct_dbus_signal_map_s wifi_direct_dbus_signal_map[] = {
+static wifi_direct_dbus_signal_map_s wifi_direct_dbus_signal_map[] = {
        {
                0,
                WFD_MANAGER_MANAGE_INTERFACE,
@@ -203,10 +204,11 @@ gboolean wifi_direct_dbus_init(void)
                return FALSE; //LCOV_EXCL_LINE
        }
 
-       /* subscribe signal handler */
-       for (i = 0; wifi_direct_dbus_signal_map[i].member != NULL; i++) {
-               wifi_direct_dbus_signal_map[i].sub_id =
-                       g_dbus_connection_signal_subscribe(gdbus_conn.connection,
+       if(ref_count == 0) {
+               /* subscribe signal handler */
+               for (i = 0; wifi_direct_dbus_signal_map[i].member != NULL; i++) {
+                       wifi_direct_dbus_signal_map[i].sub_id =
+                               g_dbus_connection_signal_subscribe(gdbus_conn.connection,
                                                           WFD_MANAGER_SERVICE, /* bus name */
                                                           wifi_direct_dbus_signal_map[i].interface, /* interface */
                                                           wifi_direct_dbus_signal_map[i].member, /* member */
@@ -216,29 +218,35 @@ gboolean wifi_direct_dbus_init(void)
                                                           wifi_direct_dbus_signal_map[i].function,
                                                           NULL,
                                                           NULL);
-               WDC_LOGD("Subscribed dbus signals [%d]", wifi_direct_dbus_signal_map[i].sub_id);
+                       WDC_LOGD("Subscribed dbus signals [%d]", wifi_direct_dbus_signal_map[i].sub_id);
+               }
        }
+       ref_count++;
 
        return TRUE;
 }
 
 void wifi_direct_dbus_deinit(void)
 {
+       __WDC_LOG_FUNC_START__;
        int i = 0;
 
        if (gdbus_conn.connection == NULL)
                return;
 
-       /* unsubscribe signal handler */
-       for (i = 0; wifi_direct_dbus_signal_map[i].member != NULL; i++) {
-               g_dbus_connection_signal_unsubscribe(gdbus_conn.connection,
-                                                    wifi_direct_dbus_signal_map[i].sub_id);
-               wifi_direct_dbus_signal_map[i].sub_id = 0;
+       ref_count--;
+       if(ref_count == 0) {
+               /* unsubscribe signal handler */
+               for (i = 0; wifi_direct_dbus_signal_map[i].member != NULL; i++) {
+                       g_dbus_connection_signal_unsubscribe(gdbus_conn.connection,
+                                                               wifi_direct_dbus_signal_map[i].sub_id);
+                       wifi_direct_dbus_signal_map[i].sub_id = 0;
+               }
        }
-
        /* unref gdbus connection */
        g_object_unref(gdbus_conn.connection);
        gdbus_conn.connection = NULL;
+       __WDC_LOG_FUNC_END__;
 }
 
 //LCOV_EXCL_START