From patchwork Thu Jul 7 14:40:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Iwai X-Patchwork-Id: 9219047 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C8ACA60574 for ; Thu, 7 Jul 2016 14:41:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B979A27C38 for ; Thu, 7 Jul 2016 14:41:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AE5A027CF9; Thu, 7 Jul 2016 14:41:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1FA4F27C38 for ; Thu, 7 Jul 2016 14:41:00 +0000 (UTC) Received: by alsa0.perex.cz (Postfix, from userid 1000) id BB39A266950; Thu, 7 Jul 2016 16:40:57 +0200 (CEST) Received: from alsa0.perex.cz (localhost [127.0.0.1]) by alsa0.perex.cz (Postfix) with ESMTP id 04643266596; Thu, 7 Jul 2016 16:40:25 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id C7FE0266596; Thu, 7 Jul 2016 16:40:23 +0200 (CEST) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by alsa0.perex.cz (Postfix) with ESMTP id 9155826586A for ; Thu, 7 Jul 2016 16:40:16 +0200 (CEST) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 4CCF9AC80 for ; Thu, 7 Jul 2016 14:40:16 +0000 (UTC) From: Takashi Iwai To: alsa-devel@alsa-project.org Date: Thu, 7 Jul 2016 16:40:13 +0200 Message-Id: <20160707144013.23953-4-tiwai@suse.de> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20160707144013.23953-1-tiwai@suse.de> References: <20160707144013.23953-1-tiwai@suse.de> Subject: [alsa-devel] [PATCH alsa-lib v2 3/3] pcm: Add LIBASOUND_THREAD_SAFE env variable check X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP For making the debugging with any deadlocks by the newly introduced thread-safety feature, add a check with LIBASOUND_THREAD_SAFE environment variable. When this variable is set to "0", alsa-lib PCM forcibly disables the whole thread-safe pthread mutex calls. Signed-off-by: Takashi Iwai --- src/pcm/pcm.c | 16 ++++++++++++++++ src/pcm/pcm_local.h | 6 ++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/pcm/pcm.c b/src/pcm/pcm.c index 1567a46146ad..1a9e9574ded8 100644 --- a/src/pcm/pcm.c +++ b/src/pcm/pcm.c @@ -494,6 +494,13 @@ aren't thread-safe, and application needs to call them carefully when they are called from multiple threads. In general, all the functions that are often called during streaming are covered as thread-safe. +This thread-safe behavior can be disabled also by passing 0 to the environment +variable LIBASOUND_THREAD_SAFE, e.g. +\code +LIBASOUND_THREAD_SAFE=0 aplay foo.wav +\endcode +for making the debugging easier. + \section pcm_dev_names PCM naming conventions The ALSA library uses a generic string representation for names of devices. @@ -2541,6 +2548,15 @@ int snd_pcm_new(snd_pcm_t **pcmp, snd_pcm_type_t type, const char *name, INIT_LIST_HEAD(&pcm->async_handlers); #ifdef THREAD_SAFE_API pthread_mutex_init(&pcm->lock, NULL); + { + static int default_thread_safe = -1; + if (default_thread_safe < 0) { + char *p = getenv("LIBASOUND_THREAD_SAFE"); + default_thread_safe = !p || *p != '0'; + } + if (!default_thread_safe) + pcm->thread_safe = -1; /* force to disable */ + } #endif *pcmp = pcm; return 0; diff --git a/src/pcm/pcm_local.h b/src/pcm/pcm_local.h index 5c7eeb4ee07d..bb7964d7833e 100644 --- a/src/pcm/pcm_local.h +++ b/src/pcm/pcm_local.h @@ -1087,11 +1087,13 @@ static inline void sw_set_period_event(snd_pcm_sw_params_t *params, int val) #ifdef THREAD_SAFE_API static inline void __snd_pcm_lock(snd_pcm_t *pcm) { - pthread_mutex_lock(&pcm->lock); + if (pcm->thread_safe >= 0) + pthread_mutex_lock(&pcm->lock); } static inline void __snd_pcm_unlock(snd_pcm_t *pcm) { - pthread_mutex_unlock(&pcm->lock); + if (pcm->thread_safe >= 0) + pthread_mutex_unlock(&pcm->lock); } static inline void snd_pcm_lock(snd_pcm_t *pcm) {