Remove thread-scope variables for sharing handles between threads 85/249585/3
authorNishant Chaprana <n.chaprana@samsung.com>
Tue, 15 Dec 2020 07:09:49 +0000 (12:39 +0530)
committerNishant Chaprana <n.chaprana@samsung.com>
Wed, 16 Dec 2020 05:44:09 +0000 (11:14 +0530)
Change-Id: If5c78bfec5fde5695ce9b664eee895f402fa672c
Signed-off-by: Nishant Chaprana <n.chaprana@samsung.com>
include/tethering_private.h
packaging/capi-network-tethering.spec
src/tethering.c
src/tethering_private.c

index e5797c1..c0b12a6 100644 (file)
@@ -77,6 +77,23 @@ extern "C" {
        } while (0)
 
 /**
+ * To lock and unlock Mutex
+ */
+
+#define TETHERING_LOCK \
+       do { \
+               _tethering_lock(); \
+       } while(0)
+
+#define TETHERING_UNLOCK \
+       do { \
+               _tethering_unlock(); \
+       } while(0)
+
+void _tethering_lock(void);
+void _tethering_unlock(void);
+
+/**
  * To check supported feature
  */
 
index bc7a90f..b5926e2 100644 (file)
@@ -1,6 +1,6 @@
 Name:          capi-network-tethering
 Summary:       Tethering Framework
-Version:       1.1.4
+Version:       1.1.5
 Release:       1
 Group:         System/Network
 License:       Apache-2.0
index f330fb2..85f21f7 100755 (executable)
@@ -363,6 +363,7 @@ static void __handle_dhcp(GDBusConnection *connection, const gchar *sender_name,
                GVariant *parameters, gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
 
@@ -426,6 +427,8 @@ DONE:
        g_free(ip);
        g_free(mac);
        g_free(name);
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -434,6 +437,7 @@ static void __handle_net_closed(GDBusConnection *connection, const gchar *sender
                GVariant *parameters, gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
 
@@ -454,6 +458,7 @@ static void __handle_net_closed(GDBusConnection *connection, const gchar *sender
                dcb(TETHERING_ERROR_NONE, type, code, data);
        }
 
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -462,6 +467,7 @@ static void __handle_wifi_tether_on(GDBusConnection *connection, const gchar *se
                        GVariant *parameters, gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
 
@@ -471,12 +477,23 @@ static void __handle_wifi_tether_on(GDBusConnection *connection, const gchar *se
        tethering_enabled_cb ecb = NULL;
        void *data = NULL;
 
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
+               return;
+       }
+
        ecb = th->enabled_cb[type];
-       if (ecb == NULL)
+       if (ecb == NULL) {
+               TETHERING_UNLOCK;
                return;
+       }
+
        data = th->enabled_user_data[type];
 
        ecb(TETHERING_ERROR_NONE, type, is_requested, data);
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -485,6 +502,7 @@ static void __handle_wifi_tether_off(GDBusConnection *connection, const gchar *s
                        GVariant *parameters, gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
 
@@ -495,9 +513,18 @@ static void __handle_wifi_tether_off(GDBusConnection *connection, const gchar *s
        void *data = NULL;
        char *buf = NULL;
 
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
+               return;
+       }
+
        dcb = th->disabled_cb[type];
-       if (dcb == NULL)
+       if (dcb == NULL) {
+               TETHERING_UNLOCK;
                return;
+       }
+
        data = th->disabled_user_data[type];
        g_variant_get(parameters, "(s)", &buf);
        if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE))
@@ -508,6 +535,7 @@ static void __handle_wifi_tether_off(GDBusConnection *connection, const gchar *s
        g_free(buf);
        dcb(TETHERING_ERROR_NONE, type, code, data);
 
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -516,6 +544,7 @@ static void __handle_usb_tether_on(GDBusConnection *connection, const gchar *sen
                        GVariant *parameters, gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
 
@@ -525,12 +554,23 @@ static void __handle_usb_tether_on(GDBusConnection *connection, const gchar *sen
        tethering_enabled_cb ecb = NULL;
        void *data = NULL;
 
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
+               return;
+       }
+
        ecb = th->enabled_cb[type];
-       if (ecb == NULL)
+       if (ecb == NULL) {
+               TETHERING_UNLOCK;
                return;
+       }
+
        data = th->enabled_user_data[type];
 
        ecb(TETHERING_ERROR_NONE, type, is_requested, data);
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -539,6 +579,7 @@ static void __handle_usb_tether_off(GDBusConnection *connection, const gchar *se
                        GVariant *parameters, gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
 
@@ -549,9 +590,18 @@ static void __handle_usb_tether_off(GDBusConnection *connection, const gchar *se
        void *data = NULL;
        char *buf = NULL;
 
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
+               return;
+       }
+
        dcb = th->disabled_cb[type];
-       if (dcb == NULL)
+       if (dcb == NULL) {
+               TETHERING_UNLOCK;
                return;
+       }
+
        data = th->disabled_user_data[type];
 
        g_variant_get(parameters, "(s)", &buf);
@@ -560,6 +610,8 @@ static void __handle_usb_tether_off(GDBusConnection *connection, const gchar *se
 
        dcb(TETHERING_ERROR_NONE, type, code, data);
        g_free(buf);
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -568,6 +620,7 @@ static void __handle_bt_tether_on(GDBusConnection *connection, const gchar *send
                        GVariant *parameters, gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
 
@@ -577,12 +630,23 @@ static void __handle_bt_tether_on(GDBusConnection *connection, const gchar *send
        tethering_enabled_cb ecb = NULL;
        void *data = NULL;
 
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
+               return;
+       }
+
        ecb = th->enabled_cb[type];
-       if (ecb == NULL)
+       if (ecb == NULL) {
+               TETHERING_UNLOCK;
                return;
+       }
+
        data = th->enabled_user_data[type];
 
        ecb(TETHERING_ERROR_NONE, type, is_requested, data);
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -591,6 +655,7 @@ static void __handle_bt_tether_off(GDBusConnection *connection, const gchar *sen
                        GVariant *parameters, gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
 
@@ -601,9 +666,18 @@ static void __handle_bt_tether_off(GDBusConnection *connection, const gchar *sen
        void *data = NULL;
        char *buf = NULL;
 
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
+               return;
+       }
+
        dcb = th->disabled_cb[type];
-       if (dcb == NULL)
+       if (dcb == NULL) {
+               TETHERING_UNLOCK;
                return;
+       }
+
        data = th->disabled_user_data[type];
        g_variant_get(parameters, "(s)", &buf);
        if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE))
@@ -614,6 +688,8 @@ static void __handle_bt_tether_off(GDBusConnection *connection, const gchar *sen
        dcb(TETHERING_ERROR_NONE, type, code, data);
 
        g_free(buf);
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -622,6 +698,7 @@ static void __handle_no_data_timeout(GDBusConnection *connection, const gchar *s
                        GVariant *parameters, gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
 
@@ -631,6 +708,12 @@ static void __handle_no_data_timeout(GDBusConnection *connection, const gchar *s
        void *data = NULL;
        tethering_disabled_cause_e code = TETHERING_DISABLED_BY_TIMEOUT;
 
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
+               return;
+       }
+
        for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) {
                dcb = th->disabled_cb[type];
                if (dcb == NULL)
@@ -639,6 +722,8 @@ static void __handle_no_data_timeout(GDBusConnection *connection, const gchar *s
 
                dcb(TETHERING_ERROR_NONE, type, code, data);
        }
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -647,6 +732,7 @@ static void __handle_low_battery_mode(GDBusConnection *connection, const gchar *
                        GVariant *parameters, gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
 
@@ -656,6 +742,12 @@ static void __handle_low_battery_mode(GDBusConnection *connection, const gchar *
        void *data = NULL;
        tethering_disabled_cause_e code = TETHERING_DISABLED_BY_LOW_BATTERY;
 
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
+               return;
+       }
+
        for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) {
                dcb = th->disabled_cb[type];
                if (dcb == NULL)
@@ -664,6 +756,8 @@ static void __handle_low_battery_mode(GDBusConnection *connection, const gchar *
 
                dcb(TETHERING_ERROR_NONE, type, code, data);
        }
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -672,6 +766,7 @@ static void __handle_flight_mode(GDBusConnection *connection, const gchar *sende
                        GVariant *parameters, gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
 
@@ -681,6 +776,12 @@ static void __handle_flight_mode(GDBusConnection *connection, const gchar *sende
        void *data = NULL;
        tethering_disabled_cause_e code = TETHERING_DISABLED_BY_FLIGHT_MODE;
 
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
+               return;
+       }
+
        for (type = TETHERING_TYPE_USB; type <= TETHERING_TYPE_BT; type++) {
                dcb = th->disabled_cb[type];
                if (dcb == NULL)
@@ -689,6 +790,8 @@ static void __handle_flight_mode(GDBusConnection *connection, const gchar *sende
 
                dcb(TETHERING_ERROR_NONE, type, code, data);
        }
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -698,6 +801,7 @@ static void __handle_security_type_changed(GDBusConnection *connection, const gc
 
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
        __tethering_h *th = (__tethering_h *)user_data;
@@ -707,9 +811,17 @@ static void __handle_security_type_changed(GDBusConnection *connection, const gc
        tethering_wifi_security_type_e security_type;
        char *buf = NULL;
 
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
+               return;
+       }
+
        scb = th->security_type_changed_cb;
-       if (scb == NULL)
+       if (scb == NULL) {
+               TETHERING_UNLOCK;
                return;
+       }
 
        g_variant_get(parameters, "(s)", &buf);
        data = th->security_type_user_data;
@@ -724,11 +836,13 @@ static void __handle_security_type_changed(GDBusConnection *connection, const gc
        else {
                SERR("Unknown type : %s\n", buf);
                g_free(buf);
+               TETHERING_UNLOCK;
                return;
        }
        g_free(buf);
        scb(security_type, data);
 
+       TETHERING_UNLOCK;
        return;
 }
 
@@ -737,6 +851,7 @@ static void __handle_ssid_visibility_changed(GDBusConnection *connection, const
                GVariant *parameters, gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
        __tethering_h *th = (__tethering_h *)user_data;
@@ -746,8 +861,15 @@ static void __handle_ssid_visibility_changed(GDBusConnection *connection, const
        bool visible = false;
        char *buf = NULL;
 
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
+               return;
+       }
+
        scb = th->ssid_visibility_changed_cb;
        if (scb == NULL) {
+               TETHERING_UNLOCK;
                DBG("-\n");
                return;
        }
@@ -758,6 +880,8 @@ static void __handle_ssid_visibility_changed(GDBusConnection *connection, const
 
        scb(visible, data);
        g_free(buf);
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -766,6 +890,7 @@ static void __handle_passphrase_changed(GDBusConnection *connection, const gchar
                GVariant *parameters, gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
        __tethering_h *th = (__tethering_h *)user_data;
@@ -773,13 +898,23 @@ static void __handle_passphrase_changed(GDBusConnection *connection, const gchar
        tethering_wifi_passphrase_changed_cb pcb = NULL;
        void *data = NULL;
 
+       if (!_tethering_check_handle(th)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
+               return;
+       }
+
        pcb = th->passphrase_changed_cb;
-       if (pcb == NULL)
+       if (pcb == NULL) {
+               TETHERING_UNLOCK;
                return;
+       }
 
        data = th->passphrase_user_data;
 
        pcb(data);
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -787,6 +922,7 @@ static void __wifi_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                gpointer user_data)
 {
        INFO("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
        GError *g_error = NULL;
@@ -799,8 +935,11 @@ static void __wifi_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
        tethering_enabled_cb ecb = th->enabled_cb[type];
        void *data = th->enabled_user_data[type];
 
-       if (!_tethering_check_handle((tethering_h)user_data))
+       if (!_tethering_check_handle((tethering_h)user_data)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
                return;
+       }
 
        g_var  = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
        if (g_error) {
@@ -809,6 +948,7 @@ static void __wifi_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                                ++retry < TETHERING_ERROR_RECOVERY_MAX) {
                        g_error_free(g_error);
                        tethering_enable((tethering_h)th, type);
+                       TETHERING_UNLOCK;
                        return;
                } else if (g_error->code == G_DBUS_ERROR_ACCESS_DENIED)
                        error = TETHERING_ERROR_PERMISSION_DENIED;
@@ -831,11 +971,14 @@ static void __wifi_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
        SINFO("Tethering enabled event ! error(%d)", error);
 
        if (!ecb) {
+               TETHERING_UNLOCK;
                INFO("-\n");
                return;
        }
        ecb(error, type, true, data);
        g_variant_unref(g_var);
+
+       TETHERING_UNLOCK;
        INFO("-\n");
 }
 
@@ -843,6 +986,8 @@ static void __bt_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
+
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
        GError *g_error = NULL;
        GVariant *g_var;
@@ -853,8 +998,11 @@ static void __bt_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
        tethering_enabled_cb ecb = th->enabled_cb[TETHERING_TYPE_BT];
        void *data = th->enabled_user_data[TETHERING_TYPE_BT];
 
-       if (!_tethering_check_handle((tethering_h)user_data))
+       if (!_tethering_check_handle((tethering_h)user_data)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
                return;
+       }
 
        g_var  = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
        if (g_error) {
@@ -863,6 +1011,7 @@ static void __bt_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                                ++retry < TETHERING_ERROR_RECOVERY_MAX) {
                        g_error_free(g_error);
                        tethering_enable((tethering_h)th, TETHERING_TYPE_BT);
+                       TETHERING_UNLOCK;
                        DBG("-\n");
                        return;
                }
@@ -884,11 +1033,14 @@ static void __bt_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                        sigs[E_SIGNAL_BT_TETHER_ON].cb, (gpointer)th, NULL);
 
        if (!ecb) {
+               TETHERING_UNLOCK;
                DBG("-\n");
                return;
        }
 
        ecb(error, TETHERING_TYPE_BT, true, data);
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -896,6 +1048,7 @@ static void __usb_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                                        gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
        __tethering_h *th = (__tethering_h *)user_data;
@@ -906,8 +1059,11 @@ static void __usb_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
        tethering_enabled_cb ecb = th->enabled_cb[TETHERING_TYPE_USB];
        void *data = th->enabled_user_data[TETHERING_TYPE_USB];
 
-       if (!_tethering_check_handle((tethering_h)user_data))
+       if (!_tethering_check_handle((tethering_h)user_data)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
                return;
+       }
 
        g_var  = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
        if (g_error) {
@@ -916,6 +1072,7 @@ static void __usb_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                                ++retry < TETHERING_ERROR_RECOVERY_MAX) {
                        g_error_free(g_error);
                        tethering_enable((tethering_h)th, TETHERING_TYPE_USB);
+                       TETHERING_UNLOCK;
                        DBG("-\n");
                        return;
                }
@@ -937,11 +1094,14 @@ static void __usb_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                        sigs[E_SIGNAL_USB_TETHER_ON].cb, (gpointer)th, NULL);
 
        if (!ecb) {
+               TETHERING_UNLOCK;
                DBG("-\n");
                return;
        }
 
        ecb(error, TETHERING_TYPE_USB, true, data);
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -949,6 +1109,7 @@ static void __p2p_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                                        gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
        __tethering_h *th = (__tethering_h *)user_data;
@@ -959,8 +1120,11 @@ static void __p2p_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
        tethering_enabled_cb ecb = th->enabled_cb[TETHERING_TYPE_P2P];
        void *data = th->enabled_user_data[TETHERING_TYPE_P2P];
 
-       if (!_tethering_check_handle((tethering_h)user_data))
+       if (!_tethering_check_handle((tethering_h)user_data)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
                return;
+       }
 
        g_var  = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
        if (g_error) {
@@ -969,6 +1133,7 @@ static void __p2p_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                                ++retry < TETHERING_ERROR_RECOVERY_MAX) {
                        g_error_free(g_error);
                        tethering_enable((tethering_h)th, TETHERING_TYPE_P2P);
+                       TETHERING_UNLOCK;
                        DBG("-\n");
                        return;
                }
@@ -985,11 +1150,14 @@ static void __p2p_enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
        retry = 0;
 
        if (!ecb) {
+               TETHERING_UNLOCK;
                DBG("-\n");
                return;
        }
 
        ecb(error, TETHERING_TYPE_P2P, true, data);
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -997,6 +1165,7 @@ static void __disabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                gpointer user_data)
 {
        INFO("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
        GError *g_error = NULL;
@@ -1010,13 +1179,17 @@ static void __disabled_cfm_cb(GObject *source_object, GAsyncResult *res,
        tethering_disabled_cb dcb = NULL;
        void *data = NULL;
 
-       if (!_tethering_check_handle((tethering_h)user_data))
+       if (!_tethering_check_handle((tethering_h)user_data)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
                return;
+       }
 
        g_var  = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
        if (g_error) {
                ERR("DBus error [%s]\n", g_error->message);
                g_error_free(g_error);
+               TETHERING_UNLOCK;
                return;
        }
        g_variant_get(g_var, "(uu)", &event_type, &info);
@@ -1101,6 +1274,8 @@ static void __disabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                ERR("Invalid event\n");
                break;
        }
+
+       TETHERING_UNLOCK;
        INFO("-\n");
 }
 
@@ -1108,6 +1283,7 @@ static void __get_data_usage_cb(GObject *source_object, GAsyncResult *res,
                                gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
 
@@ -1119,6 +1295,12 @@ static void __get_data_usage_cb(GObject *source_object, GAsyncResult *res,
        tethering_error_e tethering_error = TETHERING_ERROR_NONE;
        bool flag = false;
 
+       if (!_tethering_check_handle((tethering_h)user_data)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
+               return;
+       }
+
        g_var = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
        if (g_error) {
                ERR("DBus fail [%s]\n", g_error->message);
@@ -1131,6 +1313,7 @@ static void __get_data_usage_cb(GObject *source_object, GAsyncResult *res,
        }
        if (th->data_usage_cb == NULL) {
                ERR("There is no data_usage_cb\n");
+               TETHERING_UNLOCK;
                return;
        }
        if (flag) {
@@ -1144,6 +1327,7 @@ static void __get_data_usage_cb(GObject *source_object, GAsyncResult *res,
        th->data_usage_cb = NULL;
        th->data_usage_user_data = NULL;
 
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
@@ -1151,6 +1335,7 @@ static void __settings_reloaded_cb(GObject *source_object, GAsyncResult *res,
                gpointer user_data)
 {
        DBG("+\n");
+       TETHERING_LOCK;
 
        _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
        GError *g_error = NULL;
@@ -1159,6 +1344,12 @@ static void __settings_reloaded_cb(GObject *source_object, GAsyncResult *res,
        __tethering_h *th = (__tethering_h *)user_data;
        tethering_error_e tethering_error = TETHERING_ERROR_NONE;
 
+       if (!_tethering_check_handle((tethering_h)user_data)) {
+               DBG("Tethering handle is not valid now, ignore it.");
+               TETHERING_UNLOCK;
+               return;
+       }
+
        g_var  = g_dbus_proxy_call_finish(th->client_bus_proxy, res, &g_error);
        if (g_error) {
                ERR("DBus fail [%s]\n", g_error->message);
@@ -1176,6 +1367,7 @@ static void __settings_reloaded_cb(GObject *source_object, GAsyncResult *res,
 
        if (th->settings_reloaded_cb == NULL) {
                DBG("There is no settings_reloaded_cb\n-\n");
+               TETHERING_UNLOCK;
                return;
        }
 
@@ -1184,6 +1376,8 @@ static void __settings_reloaded_cb(GObject *source_object, GAsyncResult *res,
 
        th->settings_reloaded_cb = NULL;
        th->settings_reloaded_user_data = NULL;
+
+       TETHERING_UNLOCK;
        DBG("-\n");
 }
 
index b169250..aa81905 100755 (executable)
 * limitations under the License.
 */
 
+#define _GNU_SOURCE
+#include <pthread.h>
 #include <stdlib.h>
 #include <string.h>
 #include <system_info.h>
 #include "tethering_private.h"
 
-static __thread bool is_feature_checked[TETHERING_SUPPORTED_FEATURE_MAX] = {0, };
-static __thread bool feature_supported[TETHERING_SUPPORTED_FEATURE_MAX] = {0, };
-static __thread GSList *tethering_handle_list = NULL;
+static pthread_mutex_t g_tethering_thread_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+static bool is_feature_checked[TETHERING_SUPPORTED_FEATURE_MAX] = {0, };
+static bool feature_supported[TETHERING_SUPPORTED_FEATURE_MAX] = {0, };
+static GSList *tethering_handle_list = NULL;
+
+void _tethering_lock(void)
+{
+       pthread_mutex_lock(&g_tethering_thread_mutex);
+}
+
+void _tethering_unlock(void)
+{
+       pthread_mutex_unlock(&g_tethering_thread_mutex);
+}
 
 bool __check_feature_supported(const char *key, tethering_supported_feature_e feature)
 {
+       TETHERING_LOCK;
+       bool is_supported = false;
+
        if (!is_feature_checked[feature]) {
                if (system_info_get_platform_bool(key, &feature_supported[feature]) < 0) {
                        ERR("Get feature is failed");
+                       TETHERING_UNLOCK;
                        return false;
                }
 
                is_feature_checked[feature] = true;
        }
-       return feature_supported[feature];
+
+       is_supported = feature_supported[feature];
+
+       TETHERING_UNLOCK;
+       return is_supported;
 }
 
 int _tethering_check_feature_supported(const char* feature, ...)
@@ -75,19 +96,28 @@ int _tethering_check_feature_supported(const char* feature, ...)
 
 void _tethering_add_handle(tethering_h handle)
 {
+       TETHERING_LOCK;
        tethering_handle_list = g_slist_append(tethering_handle_list, handle);
+       TETHERING_UNLOCK;
 }
 
 void _tethering_remove_handle(tethering_h handle)
 {
+       TETHERING_LOCK;
        tethering_handle_list = g_slist_remove(tethering_handle_list, handle);
+       TETHERING_UNLOCK;
 }
 
 bool _tethering_check_handle(tethering_h handle)
 {
-       if (g_slist_find(tethering_handle_list, handle) != NULL)
+       TETHERING_LOCK;
+
+       if (g_slist_find(tethering_handle_list, handle) != NULL) {
+               TETHERING_UNLOCK;
                return true;
-       else
-               return false;
+       }
+
+       TETHERING_UNLOCK;
+       return false;
 }