Skip to contentSkip to conversation

bluetooth: Add reference count for bluetooth utils

PATCH Superseded Frédéric Danis 3 participants 3 messages Jan 19, 2011 – Jan 20, 2011

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 bluetooth: Add reference count for bluetooth utils

Add bluetooth_ref()/bluetooth_unref() to support reference count in bluetooth utils.

---
 plugins/bluetooth.c |   59 ++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 44 insertions(+), 15 deletions(-)
plugins/bluetooth.c +44 -15
index 602c6da..8202381 100644
@@ -40,6 +40,7 @@
 static DBusConnection *connection;
 static GHashTable *uuid_hash = NULL;
 static GHashTable *adapter_address_hash = NULL;
+static gint ref_count;
 
 void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
 				char *buf, int size)
@@ -504,12 +505,12 @@ static guint adapter_added_watch;
 static guint adapter_removed_watch;
 static guint property_watch;
 
-int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
+static int bluetooth_ref(void)
 {
 	int err;
 
-	if (uuid_hash)
-		goto done;
+	if (ref_count > 0)
+		return 0;
 
 	connection = ofono_dbus_get_connection();
 
@@ -543,12 +544,7 @@ int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
 	adapter_address_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
 						g_free, g_free);
 
-done:
-	g_hash_table_insert(uuid_hash, g_strdup(uuid), profile);
-
-	bluetooth_send_with_reply("/", BLUEZ_MANAGER_INTERFACE, "GetProperties",
-				manager_properties_cb, NULL, NULL, -1,
-				DBUS_TYPE_INVALID);
+	g_atomic_int_inc(&ref_count);
 
 	return 0;
 
@@ -560,11 +556,13 @@ remove:
 	return err;
 }
 
-void bluetooth_unregister_uuid(const char *uuid)
+static void bluetooth_unref(void)
 {
-	g_hash_table_remove(uuid_hash, uuid);
+	gboolean is_zero;
+
+	is_zero = g_atomic_int_dec_and_test(&ref_count);
 
-	if (g_hash_table_size(uuid_hash))
+	if (is_zero == FALSE)
 		return;
 
 	g_dbus_remove_watch(connection, bluetooth_watch);
@@ -572,9 +570,40 @@ void bluetooth_unregister_uuid(const char *uuid)
 	g_dbus_remove_watch(connection, adapter_removed_watch);
 	g_dbus_remove_watch(connection, property_watch);
 
-	g_hash_table_destroy(uuid_hash);
-	g_hash_table_destroy(adapter_address_hash);
-	uuid_hash = NULL;
+	if (uuid_hash) {
+		g_hash_table_destroy(uuid_hash);
+		uuid_hash = NULL;
+	}
+
+	if (adapter_address_hash) {
+		g_hash_table_destroy(adapter_address_hash);
+		adapter_address_hash = NULL;
+	}
+}
+
+int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
+{
+	int err;
+
+	err = bluetooth_ref();
+
+	if (err != 0)
+		return err;
+
+	g_hash_table_insert(uuid_hash, g_strdup(uuid), profile);
+
+	bluetooth_send_with_reply("/", BLUEZ_MANAGER_INTERFACE, "GetProperties",
+				manager_properties_cb, NULL, NULL, -1,
+				DBUS_TYPE_INVALID);
+
+	return 0;
+}
+
+void bluetooth_unregister_uuid(const char *uuid)
+{
+	g_hash_table_remove(uuid_hash, uuid);
+
+	bluetooth_unref();
 }
 
 OFONO_PLUGIN_DEFINE(bluetooth, "Bluetooth Utils Plugins", VERSION,
Signature · 1 line
1.7.1
#1 lore raw
Quoted 5 lines · Frédéric Danis

Add bluetooth_ref()/bluetooth_unref() to support reference count in bluetooth utils.

---
 plugins/bluetooth.c |   59 ++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 44 insertions(+), 15 deletions(-)

Acked.

Signature · 2 lines
Gustavo F. Padovan
http://profusion.mobi
#2 lore raw
In reply to Frédéric Danis

Hi Fred,

plugins/bluetooth.c line 43

Quoted 9 lines
plugins/bluetooth.c +1 -0
index 602c6da..8202381 100644
@@ -40,6 +40,7 @@
 static DBusConnection *connection;
 static GHashTable *uuid_hash = NULL;
 static GHashTable *adapter_address_hash = NULL;
+static gint ref_count;

you need a better name for this. This is too generic. Maybe using bluetooth_refcount is better.

Quoted 5 lines
plugins/bluetooth.c +0 -0
 void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
 				char *buf, int size)
@@ -504,12 +505,12 @@ static guint adapter_added_watch;
 static guint adapter_removed_watch;
 static guint property_watch;

plugins/bluetooth.c static guint adapter_added_watch; · line 513

Quoted 10 lines
plugins/bluetooth.c +3 -3
 
-int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
+static int bluetooth_ref(void)
 {
 	int err;
 
-	if (uuid_hash)
-		goto done;
+	if (ref_count > 0)
+		return 0;

This is pretty much wrong. A reference count can't work this way. You need to track the number of times this has been taken.

	g_atomic_int_inc(&bluetooth_refcount);

	if (bluetooth_refcount > 1)
		return 0;

In addition, I think the return value is pointless, just make sure to decrease the reference count in an error case.

plugins/bluetooth.c remove: · line 566

Quoted 10 lines
plugins/bluetooth.c +5 -2
+static void bluetooth_unref(void)
 {
-	g_hash_table_remove(uuid_hash, uuid);
+	gboolean is_zero;
+
+	is_zero = g_atomic_int_dec_and_test(&ref_count);
 
-	if (g_hash_table_size(uuid_hash))
+	if (is_zero == FALSE)
 		return;

I know that we have it like this in GAtChat, but just do the check right away is fine.

	if (g_atomic_int_dec_and_test(&bluetooth_refcount) == FALSE)
		return;

Regards

Marcel