From: Ben Russell <thematrixeatsyou@gmail.com>
To: alsa-devel@alsa-project.org
Cc: Ben Russell <thematrixeatsyou@gmail.com>
Subject: [alsa-devel] [PATCH 1/3] pcm_local: Add snd_pcm_is_locked
Date: Sun, 22 Sep 2019 15:28:51 +1200 [thread overview]
Message-ID: <20190922032853.6123-2-thematrixeatsyou@gmail.com> (raw)
In-Reply-To: <20190922032853.6123-1-thematrixeatsyou@gmail.com>
This is needed in preparation for a fix to be applied to
pcm_ioplug which will be in a following commit.
Signed-off-by: Ben Russell <thematrixeatsyou@gmail.com>
---
src/pcm/pcm_local.h | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/src/pcm/pcm_local.h b/src/pcm/pcm_local.h
index 05ed935f..649d84f2 100644
--- a/src/pcm/pcm_local.h
+++ b/src/pcm/pcm_local.h
@@ -1171,9 +1171,41 @@ static inline void snd_pcm_unlock(snd_pcm_t *pcm)
if (pcm->lock_enabled && pcm->need_lock)
pthread_mutex_unlock(&pcm->lock);
}
+
+/*
+ * snd_pcm_is_locked() is used to determine if the lock is currently held.
+ * It is mostly provided as a workaround for pcm_ioplug, which otherwise would
+ * for some functions assume that the plugin is locked even when it isn't.
+ *
+ * If $LIBASOUND_THREAD_SAFE=0 is set then this always returns 0 as there
+ * is no mutex in that case.
+ */
+static inline int snd_pcm_is_locked(snd_pcm_t *pcm)
+{
+ int trylock_result;
+ int unlock_result;
+
+ if (pcm->lock_enabled) {
+ trylock_result = pthread_mutex_trylock(&pcm->lock);
+ assert(trylock_result == 0 || trylock_result == EBUSY);
+ if (trylock_result == 0) {
+ /* we managed to grab the lock; therefore, it wasn't locked */
+ unlock_result = pthread_mutex_unlock(&pcm->lock);
+ assert(unlock_result == 0);
+ return 0;
+ } else {
+ /* we failed to grab the lock; therefore, it was locked */
+ return 1;
+ }
+ } else {
+ return 0;
+ }
+}
+
#else /* THREAD_SAFE_API */
#define __snd_pcm_lock(pcm) do {} while (0)
#define __snd_pcm_unlock(pcm) do {} while (0)
#define snd_pcm_lock(pcm) do {} while (0)
#define snd_pcm_unlock(pcm) do {} while (0)
+#define snd_pcm_is_locked(pcm) 0
#endif /* THREAD_SAFE_API */
--
2.23.0
_______________________________________________
Alsa-devel mailing list
Alsa-devel@alsa-project.org
https://mailman.alsa-project.org/mailman/listinfo/alsa-devel
next prev parent reply other threads:[~2019-09-22 3:32 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-22 3:28 [alsa-devel] [PATCH 0/3] Make pcm_ioplug check lock status before locking (fixes pcm_jack lockups) Ben Russell
2019-09-22 3:28 ` Ben Russell [this message]
2019-09-22 3:28 ` [alsa-devel] [PATCH 2/3] pcm_ioplug: Don't unlock+lock if we're not locked Ben Russell
2019-09-22 3:28 ` [alsa-devel] [PATCH 3/3] pcm_local: assert() when using mutexes incorrectly Ben Russell
2019-09-22 20:25 ` [alsa-devel] [PATCH 0/3] Make pcm_ioplug check lock status before locking (fixes pcm_jack lockups) Takashi Iwai
2019-09-24 7:45 ` Ben Russell
2019-09-24 10:18 ` Takashi Iwai
2019-09-25 6:10 ` Ben Russell
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=20190922032853.6123-2-thematrixeatsyou@gmail.com \
--to=thematrixeatsyou@gmail.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