[network-manager-applet/aleksander/mobile-providers: 6/9] libnm-gtk, mobile-providers: make refcounting thread-safe in the defined types



commit 1fac55cee69300f0daaae0cb4955e2a93bfa934d
Author: Aleksander Morgado <aleksander lanedo com>
Date:   Fri Nov 23 12:42:52 2012 +0100

    libnm-gtk,mobile-providers: make refcounting thread-safe in the defined types

 src/libnm-gtk/nm-mobile-providers.c |   30 ++++++++++++++++++------------
 1 files changed, 18 insertions(+), 12 deletions(-)
---
diff --git a/src/libnm-gtk/nm-mobile-providers.c b/src/libnm-gtk/nm-mobile-providers.c
index 5086531..10899ca 100644
--- a/src/libnm-gtk/nm-mobile-providers.c
+++ b/src/libnm-gtk/nm-mobile-providers.c
@@ -87,6 +87,8 @@ G_DEFINE_BOXED_TYPE (NMAMobileAccessMethod,
                      nma_mobile_access_method_unref)
 
 struct _NMAMobileAccessMethod {
+    volatile gint refs;
+
     char *name;
     /* maps lang (char *) -> name (char *) */
     GHashTable *lcl_names;
@@ -100,8 +102,6 @@ struct _NMAMobileAccessMethod {
     char *gsm_apn;
 
     NMAMobileAccessMethodType type;
-
-    gint refs;
 };
 
 static NMAMobileAccessMethod *
@@ -124,7 +124,7 @@ nma_mobile_access_method_ref (NMAMobileAccessMethod *method)
     g_return_val_if_fail (method != NULL, NULL);
     g_return_val_if_fail (method->refs > 0, NULL);
 
-    method->refs++;
+    g_atomic_int_inc (&method->refs);
 
     return method;
 }
@@ -135,7 +135,7 @@ nma_mobile_access_method_unref (NMAMobileAccessMethod *method)
     g_return_if_fail (method != NULL);
     g_return_if_fail (method->refs > 0);
 
-    if (--method->refs == 0) {
+    if (g_atomic_int_dec_and_test (&method->refs)) {
         g_free (method->name);
         g_hash_table_destroy (method->lcl_names);
         g_free (method->username);
@@ -249,6 +249,8 @@ G_DEFINE_BOXED_TYPE (NMAMobileProvider,
                      nma_mobile_provider_unref)
 
 struct _NMAMobileProvider {
+    volatile gint refs;
+
     char *name;
     /* maps lang (char *) -> name (char *) */
     GHashTable *lcl_names;
@@ -257,8 +259,6 @@ struct _NMAMobileProvider {
 
     GSList *gsm_mcc_mnc; /* GSList of NmaGsmMccMnc */
     GSList *cdma_sid; /* GSList of guint32 */
-
-    gint refs;
 };
 
 static NMAMobileProvider *
@@ -278,7 +278,10 @@ provider_new (void)
 NMAMobileProvider *
 nma_mobile_provider_ref (NMAMobileProvider *provider)
 {
-    provider->refs++;
+    g_return_val_if_fail (provider != NULL, NULL);
+    g_return_val_if_fail (provider->refs > 0, NULL);
+
+    g_atomic_int_inc (&provider->refs);
 
     return provider;
 }
@@ -286,7 +289,7 @@ nma_mobile_provider_ref (NMAMobileProvider *provider)
 void
 nma_mobile_provider_unref (NMAMobileProvider *provider)
 {
-    if (--provider->refs == 0) {
+    if (g_atomic_int_dec_and_test (&provider->refs)) {
         g_free (provider->name);
         g_hash_table_destroy (provider->lcl_names);
 
@@ -369,11 +372,11 @@ G_DEFINE_BOXED_TYPE (NMACountryInfo,
                      nma_country_info_unref)
 
 struct _NMACountryInfo {
+    volatile gint refs;
+
     char *country_code;
     char *country_name;
     GSList *providers;
-
-    gint refs;
 };
 
 static NMACountryInfo *
@@ -392,7 +395,10 @@ country_info_new (const char *country_code,
 NMACountryInfo *
 nma_country_info_ref (NMACountryInfo *country_info)
 {
-    country_info->refs++;
+    g_return_val_if_fail (country_info != NULL, NULL);
+    g_return_val_if_fail (country_info->refs > 0, NULL);
+
+    g_atomic_int_inc (&country_info->refs);
 
     return country_info;
 }
@@ -400,7 +406,7 @@ nma_country_info_ref (NMACountryInfo *country_info)
 void
 nma_country_info_unref (NMACountryInfo *country_info)
 {
-    if (--country_info->refs == 0) {
+    if (g_atomic_int_dec_and_test (&country_info->refs)) {
         g_free (country_info->country_code);
         g_free (country_info->country_name);
         g_slist_free_full (country_info->providers,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]