alsa-devel.alsa-project.org archive mirror
 help / color / mirror / Atom feed
From: Jerome Forissier <jerome@taodyne.com>
To: alsa-devel@alsa-project.org
Cc: Jerome Forissier <jerome@taodyne.com>
Subject: [PATCH 1/2] Make snd_device_name_hint() thread-safe by locking a mutex
Date: Wed, 30 Jan 2013 16:22:16 +0100	[thread overview]
Message-ID: <1359559337-15533-2-git-send-email-jerome@taodyne.com> (raw)
In-Reply-To: <1359559337-15533-1-git-send-email-jerome@taodyne.com>

Signed-off-by: Jerome Forissier <jerome@taodyne.com>
---
 src/control/namehint.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/src/control/namehint.c b/src/control/namehint.c
index 19352be..b30f75d 100644
--- a/src/control/namehint.c
+++ b/src/control/namehint.c
@@ -26,6 +26,9 @@
  */
 
 #include "local.h"
+#ifdef HAVE_LIBPTHREAD
+#include <pthread.h>
+#endif
 
 #ifndef DOC_HIDDEN
 struct hint_list {
@@ -44,6 +47,25 @@ struct hint_list {
 	int show_all;
 	char *cardname;
 };
+
+#ifdef HAVE_LIBPTHREAD
+static pthread_mutex_t snd_device_name_hint_mutex =
+                                PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+#endif
+
+static inline void snd_device_name_hint_lock()
+{
+#ifdef HAVE_LIBPTHREAD
+	pthread_mutex_lock(&snd_device_name_hint_mutex);
+#endif
+}
+
+static inline void snd_device_name_hint_unlock()
+{
+#ifdef HAVE_LIBPTHREAD
+	pthread_mutex_unlock(&snd_device_name_hint_mutex);
+#endif
+}
 #endif
 
 static int hint_list_add(struct hint_list *list,
@@ -553,9 +575,13 @@ int snd_device_name_hint(int card, const char *iface, void ***hints)
 
 	if (hints == NULL)
 		return -EINVAL;
+	snd_device_name_hint_lock();
 	err = snd_config_update();
 	if (err < 0)
+	{
+		snd_device_name_hint_unlock();
 		return err;
+	}
 	list.list = NULL;
 	list.count = list.allocated = 0;
 	list.siface = iface;
@@ -574,7 +600,10 @@ int snd_device_name_hint(int card, const char *iface, void ***hints)
 	else if (strcmp(iface, "ctl") == 0)
 		list.iface = SND_CTL_ELEM_IFACE_MIXER;
 	else
+	{
+		snd_device_name_hint_unlock();
 		return -EINVAL;
+	}
 	list.show_all = 0;
 	list.cardname = NULL;
 	if (snd_config_search(snd_config, "defaults.namehint.showall", &conf) >= 0)
@@ -618,6 +647,7 @@ int snd_device_name_hint(int card, const char *iface, void ***hints)
       		snd_device_name_free_hint((void **)list.list);
       		if (list.cardname)
 	      		free(list.cardname);
+		snd_device_name_hint_unlock();
       		return err;
       	} else {
       		err = hint_list_add(&list, NULL, NULL);
@@ -627,6 +657,7 @@ int snd_device_name_hint(int card, const char *iface, void ***hints)
       		if (list.cardname)
 	      		free(list.cardname);
 	}
+	snd_device_name_hint_unlock();
       	return 0;
 }
 
-- 
1.8.1.2

  reply	other threads:[~2013-01-30 15:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-30 15:22 [PATCH 0/2] Fix thread safety issues Jerome Forissier
2013-01-30 15:22 ` Jerome Forissier [this message]
2013-01-30 15:22 ` [PATCH 2/2] snd_pcm_direct_parse_open_conf(): use thread-safe getgrnam_r() Jerome Forissier
2013-01-30 16:03 ` [PATCH 0/2] Fix thread safety issues Takashi Iwai
2013-01-30 18:13   ` Jérôme Forissier
2013-01-31  8:54     ` Takashi Iwai
2013-01-31  9:48       ` Jérôme Forissier
2013-01-31 10:09         ` Takashi Iwai
2013-01-31 11:38           ` Jaroslav Kysela

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=1359559337-15533-2-git-send-email-jerome@taodyne.com \
    --to=jerome@taodyne.com \
    --cc=alsa-devel@alsa-project.org \
    /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;
as well as URLs for NNTP newsgroup(s).