]> git.alsa-project.org Git - alsa-lib.git/commitdiff
ALSA: pcm: Use recursive mutex
authorTakashi Iwai <tiwai@suse.de>
Fri, 21 Apr 2017 18:36:40 +0000 (20:36 +0200)
committerTakashi Iwai <tiwai@suse.de>
Fri, 21 Apr 2017 18:36:40 +0000 (20:36 +0200)
The recent thread-safety pthread implementation caused deadlocks in
some situations, e.g. when an external plugin calls snd_pcm_state() in
its callback.  One can avoid the deadlock by carefully using the
unlocked version, but it's often error-prone, and it might be still
problem with the old binaries.

In this patch, we initialize the pthread mutex as recursive for fixing
such a problem.

Suggested-by: Timo Wischer <twischer@de.adit-jv.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
src/pcm/pcm.c

index a16fd86fb3b004ca93bb07b568c4724182d5dd61..8a7a5950de3cd79a7a14549682792dfa85391ab6 100644 (file)
@@ -2581,6 +2581,10 @@ int snd_pcm_new(snd_pcm_t **pcmp, snd_pcm_type_t type, const char *name,
                snd_pcm_stream_t stream, int mode)
 {
        snd_pcm_t *pcm;
+#ifdef THREAD_SAFE_API
+       pthread_mutexattr_t attr;
+#endif
+
        pcm = calloc(1, sizeof(*pcm));
        if (!pcm)
                return -ENOMEM;
@@ -2595,7 +2599,9 @@ int snd_pcm_new(snd_pcm_t **pcmp, snd_pcm_type_t type, const char *name,
        pcm->fast_op_arg = pcm;
        INIT_LIST_HEAD(&pcm->async_handlers);
 #ifdef THREAD_SAFE_API
-       pthread_mutex_init(&pcm->lock, NULL);
+       pthread_mutexattr_init(&attr);
+       pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+       pthread_mutex_init(&pcm->lock, &attr);
        /* use locking as default;
         * each plugin may suppress this in its open call
         */