From: Takashi Iwai Date: Fri, 21 Apr 2017 18:36:40 +0000 (+0200) Subject: ALSA: pcm: Use recursive mutex X-Git-Tag: v1.1.4~6 X-Git-Url: https://git.alsa-project.org/?a=commitdiff_plain;h=1cb217ead9aff029f194208bf484be1ba956b194;p=alsa-lib.git ALSA: pcm: Use recursive mutex 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 Signed-off-by: Takashi Iwai --- diff --git a/src/pcm/pcm.c b/src/pcm/pcm.c index a16fd86f..8a7a5950 100644 --- a/src/pcm/pcm.c +++ b/src/pcm/pcm.c @@ -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 */