Linux bluetooth development
 help / color / mirror / Atom feed
From: "Gustavo F. Padovan" <gustavo@padovan.org>
To: Zhenhua Zhang <zhenhua.zhang@intel.com>
Cc: linux-bluetooth@vger.kernel.org
Subject: Re: [PATCH 1/6] bluetooth: Add reference count for bluetooth utils
Date: Sat, 31 Jul 2010 20:25:06 -0300	[thread overview]
Message-ID: <20100731232506.GC29039@vigoh> (raw)
In-Reply-To: <1280387901-8581-2-git-send-email-zhenhua.zhang@intel.com>

Hi Zhenhua,

* Zhenhua Zhang <zhenhua.zhang@intel.com> [2010-07-29 15:18:16 +0800]:

> Add bluetooth_ref()/bluetooth_unref() to support reference count in
> bluetooth utils.
> ---
>  plugins/bluetooth.c |   62 +++++++++++++++++++++++++++++++++++++--------------
>  1 files changed, 45 insertions(+), 17 deletions(-)
> 
> diff --git a/plugins/bluetooth.c b/plugins/bluetooth.c
> index 5a85eaa..10cc49d 100644
> --- a/plugins/bluetooth.c
> +++ b/plugins/bluetooth.c
> @@ -39,6 +39,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)
> @@ -503,13 +504,10 @@ 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_init()
>  {
>  	int err;
>  
> -	if (uuid_hash)
> -		goto done;
> -
>  	connection = ofono_dbus_get_connection();
>  
>  	bluetooth_watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
> @@ -542,13 +540,6 @@ 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);
> -
>  	return 0;
>  
>  remove:
> @@ -556,14 +547,27 @@ remove:
>  	g_dbus_remove_watch(connection, adapter_added_watch);
>  	g_dbus_remove_watch(connection, adapter_removed_watch);
>  	g_dbus_remove_watch(connection, property_watch);
> +
>  	return err;
>  }
>  
> -void bluetooth_unregister_uuid(const char *uuid)
> +static int bluetooth_ref()
>  {
> -	g_hash_table_remove(uuid_hash, uuid);
> +	g_atomic_int_inc(&ref_count);
> +
> +	if (ref_count > 1)
> +		return 0;
> +
> +	return bluetooth_init();
> +}
> +
> +static void bluetooth_unref()
> +{
> +	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;

We can remove the is_zero variable, and test g_atomic_int_dec_and_test()
directly. What do you think?

>  
>  	g_dbus_remove_watch(connection, bluetooth_watch);
> @@ -571,9 +575,33 @@ 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);
> +
> +	if (adapter_address_hash)
> +		g_hash_table_destroy(adapter_address_hash);
> +}
> +
> +int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
> +{
> +	int 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,
> -- 
> 1.7.0.4
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

-- 
Gustavo F. Padovan
http://padovan.org

  reply	other threads:[~2010-07-31 23:25 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-29  7:18 [PATCH 0/6] oFono patches for BT DUN server Zhenhua Zhang
2010-07-29  7:18 ` [PATCH 1/6] bluetooth: Add reference count for bluetooth utils Zhenhua Zhang
2010-07-31 23:25   ` Gustavo F. Padovan [this message]
2010-08-02  0:20     ` Zhang, Zhenhua
2010-07-29  7:18 ` [PATCH 2/6] bluetooth: Add Btio library for DUN Zhenhua Zhang
2010-07-29  7:18 ` [PATCH 3/6] bluetooth: Add bluetooth server support " Zhenhua Zhang
2010-07-29  7:18 ` [PATCH 4/6] modem: Add method to get modem by path Zhenhua Zhang
2010-07-29  7:18 ` [PATCH 5/6] dun_gw: Add DUN server plugin for oFono Zhenhua Zhang
2010-07-29  7:18 ` [PATCH 6/6] emulator: Add emulator atom in oFono Zhenhua Zhang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100731232506.GC29039@vigoh \
    --to=gustavo@padovan.org \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=zhenhua.zhang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox