alsa/0004-pcm-Better-understandable-locking-code.patch
Ismail Dönmez 6117f9abfc Accepting request 442719 from home:tiwai:branches:multimedia:libs
- Backport upstream fixes (bsc#1012594):
  - A few PCM bugs have been fixed:
    * Stall of dmix and others in a wrong PCM state
    * Refactoring of PCM locking scheme
    * SHM initialization race fix
    * plug PCM memory leaks
    * Improvement of dshare/dmix delay calculation
    * Fix endless dshare draining
    * Fix semaphore discard race fix of direct plugins
  - UCM fixes and updates for DB410c and skylake-r5286
  - Mixer code cleanup not to install bogus plugin codes
  - Documentation fixes / updates
  0001-ucm-Add-ucm-files-for-DB410c-board.patch
  0002-mixer-Fix-rounding-mode-documentation.patch
  0003-pcm-Fix-shm-initialization-race-condition.patch
  0004-pcm-Better-understandable-locking-code.patch
  0005-ucm-fix-crash-when-calling-snd_use_case_geti-with-no.patch
  0006-ucm-docs-typeset-lists-of-identifiers-explicitly.patch
  0007-Update-include-sound-tlv.h-from-4.9-pre-kernel-uapi.patch
  0008-test-use-actual-information-for-TLV-operation.patch
  0009-ctl-improve-API-documentation-for-TLV-operation.patch
  0010-ctl-improve-documentation-about-TLV-related-APIs.patch
  0011-ctl-correct-documentation-about-TLV-feature.patch
  0012-conf-ucm-skylake-add-skylake-rt286-conf-files.patch
  0013-pcm_plug-Clear-plugins-on-all-error-conditions.patch
  0014-mixer-Don-t-install-smixer-modules-unless-python-is-.patch
  0015-pcm_dshare-Do-not-discard-slave-reported-delay-in-st.patch
  0016-pcm-direct-Protect-from-freeing-semaphore-when-alrea.patch
  0017-pcm-dshare-Fix-endless-playback-of-buffer.patch
  0018-pcm-Add-the-PCM-state-checks-to-plugins.patch

OBS-URL: https://build.opensuse.org/request/show/442719
OBS-URL: https://build.opensuse.org/package/show/multimedia:libs/alsa?expand=0&rev=202
2016-11-29 18:09:28 +00:00

131 lines
4.4 KiB
Diff

From 0fc4b4d17bfd0ce44394f6040e1d5f9dfa97a5ad Mon Sep 17 00:00:00 2001
From: Takashi Iwai <tiwai@suse.de>
Date: Thu, 1 Sep 2016 14:05:00 +0200
Subject: [PATCH] pcm: Better understandable locking code
The newly added locking code seems to have confused quite a few
people, as "thread_safe=1" may be considered as if the thread-safety
lock has to be turned on. (It meant that the plugin _is_ thread-safe,
i.e. it needs no extra locking.)
For avoiding such a misunderstanding, this commit renames the relevant
pcm fields and give more comments to explain what is for what.
The former single pcm->thread_safe flag is now split to two boolean
flags, pcm->need_lock and pcm->lock_enabled. It consumes a few more
bytes, but this would be (hopefully) better understandable.
No functional change by this commit.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
src/pcm/pcm.c | 16 +++++++++++-----
src/pcm/pcm_hw.c | 2 +-
src/pcm/pcm_local.h | 27 ++++++++++++++++++++++-----
3 files changed, 34 insertions(+), 11 deletions(-)
diff --git a/src/pcm/pcm.c b/src/pcm/pcm.c
index f8323999343e..cd87bc759ded 100644
--- a/src/pcm/pcm.c
+++ b/src/pcm/pcm.c
@@ -2545,14 +2545,20 @@ 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);
+ /* use locking as default;
+ * each plugin may suppress this in its open call
+ */
+ pcm->need_lock = 1;
{
- static int default_thread_safe = -1;
- if (default_thread_safe < 0) {
+ /* set lock_enabled field depending on $LIBASOUND_THREAD_SAFE */
+ static int do_lock_enable = -1; /* uninitialized */
+
+ /* evaluate env var only once at the first open for consistency */
+ if (do_lock_enable == -1) {
char *p = getenv("LIBASOUND_THREAD_SAFE");
- default_thread_safe = !p || *p != '0';
+ do_lock_enable = !p || *p != '0';
}
- if (!default_thread_safe)
- pcm->thread_safe = -1; /* force to disable */
+ pcm->lock_enabled = do_lock_enable;
}
#endif
*pcmp = pcm;
diff --git a/src/pcm/pcm_hw.c b/src/pcm/pcm_hw.c
index 3a5634c1d39a..56e88b6bf6c0 100644
--- a/src/pcm/pcm_hw.c
+++ b/src/pcm/pcm_hw.c
@@ -1514,7 +1514,7 @@ int snd_pcm_hw_open_fd(snd_pcm_t **pcmp, const char *name,
pcm->poll_events = info.stream == SND_PCM_STREAM_PLAYBACK ? POLLOUT : POLLIN;
pcm->tstamp_type = tstamp_type;
#ifdef THREAD_SAFE_API
- pcm->thread_safe = 1;
+ pcm->need_lock = 0; /* hw plugin is thread-safe */
#endif
ret = snd_pcm_hw_mmap_status(pcm);
diff --git a/src/pcm/pcm_local.h b/src/pcm/pcm_local.h
index bb7964d7833e..bba2f15ac463 100644
--- a/src/pcm/pcm_local.h
+++ b/src/pcm/pcm_local.h
@@ -244,7 +244,12 @@ struct _snd_pcm {
void *private_data;
struct list_head async_handlers;
#ifdef THREAD_SAFE_API
- int thread_safe;
+ int need_lock; /* true = this PCM (plugin) is thread-unsafe,
+ * thus it needs a lock.
+ */
+ int lock_enabled; /* thread-safety lock is enabled on the system;
+ * it's set depending on $LIBASOUND_THREAD_SAFE.
+ */
pthread_mutex_t lock;
#endif
};
@@ -1085,24 +1090,36 @@ static inline void sw_set_period_event(snd_pcm_sw_params_t *params, int val)
#define PCMINABORT(pcm) (((pcm)->mode & SND_PCM_ABORT) != 0)
#ifdef THREAD_SAFE_API
+/*
+ * __snd_pcm_lock() and __snd_pcm_unlock() are used to lock/unlock the plugin
+ * forcibly even if it's declared as thread-safe. It's needed only for some
+ * codes that are thread-unsafe per design (e.g. snd_pcm_nonblock()).
+ *
+ * OTOH, snd_pcm_lock() and snd_pcm_unlock() are used to lock/unlock the plugin
+ * in normal situations. They do lock/unlock only when the plugin is
+ * thread-unsafe.
+ *
+ * Both __snd_pcm_lock() and snd_pcm_lock() (and their unlocks) wouldn't do
+ * any action when the whole locking is disabled via $LIBASOUND_THREAD_SAFE=0.
+ */
static inline void __snd_pcm_lock(snd_pcm_t *pcm)
{
- if (pcm->thread_safe >= 0)
+ if (pcm->lock_enabled)
pthread_mutex_lock(&pcm->lock);
}
static inline void __snd_pcm_unlock(snd_pcm_t *pcm)
{
- if (pcm->thread_safe >= 0)
+ if (pcm->lock_enabled)
pthread_mutex_unlock(&pcm->lock);
}
static inline void snd_pcm_lock(snd_pcm_t *pcm)
{
- if (!pcm->thread_safe)
+ if (pcm->lock_enabled && pcm->need_lock)
pthread_mutex_lock(&pcm->lock);
}
static inline void snd_pcm_unlock(snd_pcm_t *pcm)
{
- if (!pcm->thread_safe)
+ if (pcm->lock_enabled && pcm->need_lock)
pthread_mutex_unlock(&pcm->lock);
}
#else /* THREAD_SAFE_API */
--
2.10.2