Skip to contentSkip to conversation

PCM thread-safety patchset

PATCH In review Takashi Iwai 1 participant 5 messages Jul 7, 2016 – Jul 11, 2016

lore
Share & tools
lore
#0 lore raw

v2 cover PCM thread-safety patchset

Hi,

here is a v2 patchset for supporting multi thread safety to alsa-lib PCM API. Now it's out from RFC and should be reviewed as an official patchset.

Basically ALSA PCM functions are thread-unsafe, and applications are supposed to do the proper protection against racy accesses. The reality is, however, that application developers don't care such, as alsa-lib works in most cases. Of course, users still get occasionally mysterious crashes.

As a workaround, this patchset adds the pthread mutex protection to most of exported PCM functions. This is slightly an overkill, but the biggest merit is that it's easy to implement; I just wrapped the functions and replaced the internal ones with unlocked versions.

To be noted, there is an optimization for the direct hw PCM access. Performance-sensitive applications like JACK should work like before without any overhead.

Another bonus by this addition is that we can finally get rid of home brew (and deadly smelling) atomic macros from the tree, since it's now more widely protected.

I lightly tested on my local machines (dmix, PA, jack and plugins) and all seem working well, so far.

Takashi

===

v1->v2:
- Drop locking for some API functions like hw_params
- Lock some functions even for hw plugin to keep PCM internals consistent
- Fix deadlock with jack ioplug
- More documentations and comments
- Add debugging option via $LIBASOUND_THREAD_SAFE=0
- Reordered patches

Takashi Iwai (3):
pcm: Add thread-safety to PCM API
pcm: Remove home brew atomic operations
pcm: Add LIBASOUND_THREAD_SAFE env variable check

 INSTALL               |   9 ++
 configure.ac          |  15 ++
 include/Makefile.am   |   2 +-
 include/iatomic.h     | 170 --------------------
 include/pcm_ioplug.h  |  10 +-
 src/pcm/Makefile.am   |   2 +-
 src/pcm/atomic.c      |  43 -----
 src/pcm/pcm.c         | 433 +++++++++++++++++++++++++++++++++++++++++---------
 src/pcm/pcm_direct.c  |   4 +-
 src/pcm/pcm_dmix.c    |  13 +-
 src/pcm/pcm_dshare.c  |  13 +-
 src/pcm/pcm_dsnoop.c  |  15 +-
 src/pcm/pcm_file.c    |  21 ++-
 src/pcm/pcm_generic.c |  10 +-
 src/pcm/pcm_hw.c      |   3 +
 src/pcm/pcm_ioplug.c  |  70 +++++---
 src/pcm/pcm_local.h   | 131 +++++++++++----
 src/pcm/pcm_mmap.c    |  18 ++-
 src/pcm/pcm_plugin.c  |  72 ++-------
 src/pcm/pcm_plugin.h  |   2 -
 src/pcm/pcm_rate.c    |  41 ++---
 src/pcm/pcm_route.c   |   2 +-
 22 files changed, 648 insertions(+), 451 deletions(-)
 delete mode 100644 include/iatomic.h
 delete mode 100644 src/pcm/atomic.c
Signature · 1 line
2.9.0
#1 lore raw

PATCH v2 1/3 pcm: Add thread-safety to PCM API

Traditionally, many of ALSA library functions are supposed to be thread-unsafe, and applications are required to take care of thread safety by themselves. However, people never be careful enough, and almost all applications fail in this regard.

This patch is an attempt to harden the thread safety in exported PCM functions in a simplistic way: just wrap some of exported functions with the pthread mutex of each PCM object. Not all API functions are wrapped by the mutex since it doesn't make sense. Instead, the patchset covers only the functions that may be likely called concurrently. The supposedly thread-safe API functions are marked in the document.

For achieving the feature, two new fields are added snd_pcm_t when the option is enabled: thread_safe and lock. The former indicates that the plugin is thread-safe that doesn't need this workaround and the latter is the pthread mutex. Currently only hw plugin have thread_safe=1. So, the most of real-time sensitive apps won't be influenced by this patchset.

Although the patch covers most of PCM ops, a few snd_pcm_fast_ops are left without the extra mutex locking: namely, the ones that may have blocking behavior, i.e. resume, drain, readi, writei, readn and writen. These are supposed to handle own locking in the callbacks.

Also, if anyone wants to disable this new thread-safe API feature, it can be still turned off via --disable-thread-safety configure option.

---
 INSTALL               |   9 ++
 configure.ac          |  15 ++
 include/pcm_ioplug.h  |  10 +-
 src/pcm/pcm.c         | 417 +++++++++++++++++++++++++++++++++++++++++---------
 src/pcm/pcm_direct.c  |   4 +-
 src/pcm/pcm_dmix.c    |  13 +-
 src/pcm/pcm_dshare.c  |  13 +-
 src/pcm/pcm_dsnoop.c  |  15 +-
 src/pcm/pcm_file.c    |  21 ++-
 src/pcm/pcm_generic.c |  10 +-
 src/pcm/pcm_hw.c      |   3 +
 src/pcm/pcm_ioplug.c  |  70 ++++++---
 src/pcm/pcm_local.h   | 129 ++++++++++++----
 src/pcm/pcm_mmap.c    |  18 ++-
 src/pcm/pcm_rate.c    |   7 +-
 src/pcm/pcm_route.c   |   2 +-
 16 files changed, 610 insertions(+), 146 deletions(-)
INSTALL +9 -0
index 64f3d0945d01..a2427e072bca 100644
@@ -104,6 +104,15 @@ option. This option disables usage of float numbers in PCM route plugin.
 ALSA could then leave much more CPU cycles for your applications, but you 
 could still need some floating point emulator.
 
+Thread-safety option
+--------------------
+
+As default, major PCM functions of alsa-lib are built to be
+thread-safe with pthread mutex (while this wasn't present in the
+versions earlier than 1.1.2).  If you want to build without this
+thread-safety support but reduce the overhead, pass
+--disable-thread-safety configure option.
+
 Jack plugin
 -----------
 
configure.ac +15 -0
index 53fca33e201a..8c888fac1635 100644
@@ -632,6 +632,21 @@ elif test "$max_cards" -gt 256; then
 fi
 AC_DEFINE_UNQUOTED(SND_MAX_CARDS, $max_cards, [Max number of cards])
 
+dnl Check for thread-safe API functions
+if test "$HAVE_LIBPTHREAD" = "yes"; then
+AC_MSG_CHECKING(for thread-safe API functions)
+AC_ARG_ENABLE(thread-safety,
+  AS_HELP_STRING([--disable-thread-safety],
+    [disable thread-safe API functions]),
+  threadsafe="$enableval", threadsafe="yes")
+if test "$threadsafe" = "yes"; then
+  AC_MSG_RESULT(yes)
+  AC_DEFINE([THREAD_SAFE_API], "1", [Disable thread-safe API functions])
+else
+  AC_MSG_RESULT(no)
+fi
+fi
+
 dnl Make a symlink for inclusion of alsa/xxx.h
 if test ! -L "$srcdir"/include/alsa ; then
   echo "Making a symlink include/alsa"
include/pcm_ioplug.h +5 -5
index e529e6a60ae5..8c25e5e19850 100644
@@ -124,19 +124,19 @@ struct snd_pcm_ioplug {
 /** Callback table of ioplug */
 struct snd_pcm_ioplug_callback {
 	/**
-	 * start the PCM; required
+	 * start the PCM; required, called inside mutex lock
 	 */
 	int (*start)(snd_pcm_ioplug_t *io);
 	/**
-	 * stop the PCM; required
+	 * stop the PCM; required, called inside mutex lock
 	 */
 	int (*stop)(snd_pcm_ioplug_t *io);
 	/**
-	 * get the current DMA position; required
+	 * get the current DMA position; required, called inside mutex lock
 	 */
 	snd_pcm_sframes_t (*pointer)(snd_pcm_ioplug_t *io);
 	/**
-	 * transfer the data; optional
+	 * transfer the data; optional, called inside mutex lock
 	 */
 	snd_pcm_sframes_t (*transfer)(snd_pcm_ioplug_t *io,
 				      const snd_pcm_channel_area_t *areas,
@@ -167,7 +167,7 @@ struct snd_pcm_ioplug_callback {
 	 */
 	int (*drain)(snd_pcm_ioplug_t *io);
 	/**
-	 * toggle pause; optional
+	 * toggle pause; optional, called inside mutex lock
 	 */
 	int (*pause)(snd_pcm_ioplug_t *io, int enable);
 	/**
src/pcm/pcm.c +344 -73

1,014 lines not on this page · raw message

src/pcm/pcm_direct.c +2 -2
index 21f98e7a1779..ecc86db4831a 100644
@@ -559,7 +559,7 @@ int snd_pcm_direct_poll_revents(snd_pcm_t *pcm, struct pollfd *pfds, unsigned in
 	events = pfds[0].revents;
 	if (events & POLLIN) {
 		snd_pcm_uframes_t avail;
-		snd_pcm_avail_update(pcm);
+		__snd_pcm_avail_update(pcm);
 		if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
 			events |= POLLOUT;
 			events &= ~POLLIN;
@@ -580,7 +580,7 @@ int snd_pcm_direct_poll_revents(snd_pcm_t *pcm, struct pollfd *pfds, unsigned in
 			snd_pcm_direct_clear_timer_queue(dmix);
 			events &= ~(POLLOUT|POLLIN);
 			/* additional check */
-			switch (snd_pcm_state(pcm)) {
+			switch (__snd_pcm_state(pcm)) {
 			case SND_PCM_STATE_XRUN:
 			case SND_PCM_STATE_SUSPENDED:
 			case SND_PCM_STATE_SETUP:
src/pcm/pcm_dmix.c +12 -1
index 007d35664ce7..2714fb93c758 100644
@@ -601,7 +601,8 @@ static int snd_pcm_dmix_drop(snd_pcm_t *pcm)
 	return 0;
 }
 
-static int snd_pcm_dmix_drain(snd_pcm_t *pcm)
+/* locked version */
+static int __snd_pcm_dmix_drain(snd_pcm_t *pcm)
 {
 	snd_pcm_direct_t *dmix = pcm->private_data;
 	snd_pcm_uframes_t stop_threshold;
@@ -659,6 +660,16 @@ static int snd_pcm_dmix_drain(snd_pcm_t *pcm)
 	return 0;
 }
 
+static int snd_pcm_dmix_drain(snd_pcm_t *pcm)
+{
+	int err;
+
+	snd_pcm_lock(pcm);
+	err = __snd_pcm_dmix_drain(pcm);
+	snd_pcm_unlock(pcm);
+	return err;
+}
+
 static int snd_pcm_dmix_pause(snd_pcm_t *pcm ATTRIBUTE_UNUSED, int enable ATTRIBUTE_UNUSED)
 {
 	return -EIO;
src/pcm/pcm_dshare.c +12 -1
index 05854dedf259..c5b3178a4990 100644
@@ -354,7 +354,8 @@ static int snd_pcm_dshare_drop(snd_pcm_t *pcm)
 	return 0;
 }
 
-static int snd_pcm_dshare_drain(snd_pcm_t *pcm)
+/* locked version */
+static int __snd_pcm_dshare_drain(snd_pcm_t *pcm)
 {
 	snd_pcm_direct_t *dshare = pcm->private_data;
 	snd_pcm_uframes_t stop_threshold;
@@ -412,6 +413,16 @@ static int snd_pcm_dshare_drain(snd_pcm_t *pcm)
 	return 0;
 }
 
+static int snd_pcm_dshare_drain(snd_pcm_t *pcm)
+{
+	int err;
+
+	snd_pcm_lock(pcm);
+	err = __snd_pcm_dshare_drain(pcm);
+	snd_pcm_unlock(pcm);
+	return err;
+}
+
 static int snd_pcm_dshare_pause(snd_pcm_t *pcm ATTRIBUTE_UNUSED, int enable ATTRIBUTE_UNUSED)
 {
 	return -EIO;
src/pcm/pcm_dsnoop.c +13 -2
index 2d45171dda01..4efbc53d177e 100644
@@ -297,7 +297,8 @@ static int snd_pcm_dsnoop_drop(snd_pcm_t *pcm)
 	return 0;
 }
 
-static int snd_pcm_dsnoop_drain(snd_pcm_t *pcm)
+/* locked version */
+static int __snd_pcm_dsnoop_drain(snd_pcm_t *pcm)
 {
 	snd_pcm_direct_t *dsnoop = pcm->private_data;
 	snd_pcm_uframes_t stop_threshold;
@@ -314,12 +315,22 @@ static int snd_pcm_dsnoop_drain(snd_pcm_t *pcm)
 			break;
 		if (pcm->mode & SND_PCM_NONBLOCK)
 			return -EAGAIN;
-		snd_pcm_wait(pcm, -1);
+		__snd_pcm_wait_in_lock(pcm, -1);
 	}
 	pcm->stop_threshold = stop_threshold;
 	return snd_pcm_dsnoop_drop(pcm);
 }
 
+static int snd_pcm_dsnoop_drain(snd_pcm_t *pcm)
+{
+	int err;
+
+	snd_pcm_lock(pcm);
+	err = __snd_pcm_dsnoop_drain(pcm);
+	snd_pcm_unlock(pcm);
+	return err;
+}
+
 static int snd_pcm_dsnoop_pause(snd_pcm_t *pcm ATTRIBUTE_UNUSED, int enable ATTRIBUTE_UNUSED)
 {
 	return -EIO;
src/pcm/pcm_file.c +17 -4
index 92eb0724c0e2..ae58c531360e 100644
@@ -440,13 +440,16 @@ static int snd_pcm_file_drop(snd_pcm_t *pcm)
 	return err;
 }
 
+/* locking */
 static int snd_pcm_file_drain(snd_pcm_t *pcm)
 {
 	snd_pcm_file_t *file = pcm->private_data;
 	int err = snd_pcm_drain(file->gen.slave);
 	if (err >= 0) {
+		__snd_pcm_lock(pcm);
 		snd_pcm_file_write_bytes(pcm, file->wbuf_used_bytes);
 		assert(file->wbuf_used_bytes == 0);
+		__snd_pcm_unlock(pcm);
 	}
 	return err;
 }
@@ -507,40 +510,49 @@ static snd_pcm_sframes_t snd_pcm_file_forward(snd_pcm_t *pcm, snd_pcm_uframes_t
 	return err;
 }
 
+/* locking */
 static snd_pcm_sframes_t snd_pcm_file_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
 {
 	snd_pcm_file_t *file = pcm->private_data;
 	snd_pcm_channel_area_t areas[pcm->channels];
-	snd_pcm_sframes_t n = snd_pcm_writei(file->gen.slave, buffer, size);
+	snd_pcm_sframes_t n = _snd_pcm_writei(file->gen.slave, buffer, size);
 	if (n > 0) {
 		snd_pcm_areas_from_buf(pcm, areas, (void*) buffer);
+		__snd_pcm_lock(pcm);
 		snd_pcm_file_add_frames(pcm, areas, 0, n);
+		__snd_pcm_unlock(pcm);
 	}
 	return n;
 }
 
+/* locking */
 static snd_pcm_sframes_t snd_pcm_file_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
 {
 	snd_pcm_file_t *file = pcm->private_data;
 	snd_pcm_channel_area_t areas[pcm->channels];
-	snd_pcm_sframes_t n = snd_pcm_writen(file->gen.slave, bufs, size);
+	snd_pcm_sframes_t n = _snd_pcm_writen(file->gen.slave, bufs, size);
 	if (n > 0) {
 		snd_pcm_areas_from_bufs(pcm, areas, bufs);
+		__snd_pcm_lock(pcm);
 		snd_pcm_file_add_frames(pcm, areas, 0, n);
+		__snd_pcm_unlock(pcm);
 	}
 	return n;
 }
 
+/* locking */
 static snd_pcm_sframes_t snd_pcm_file_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size)
 {
 	snd_pcm_file_t *file = pcm->private_data;
 	snd_pcm_sframes_t n;
 
-	n = snd_pcm_readi(file->gen.slave, buffer, size);
+	n = _snd_pcm_readi(file->gen.slave, buffer, size);
 	if (n <= 0)
 		return n;
 	if (file->ifd >= 0) {
+		__snd_pcm_lock(pcm);
 		n = read(file->ifd, buffer, n * pcm->frame_bits / 8);
+		__snd_pcm_unlock(pcm);
 		if (n < 0)
 			return n;
 		return n * 8 / pcm->frame_bits;
@@ -548,6 +560,7 @@ static snd_pcm_sframes_t snd_pcm_file_readi(snd_pcm_t *pcm, void *buffer, snd_pc
 	return n;
 }
 
+/* locking */
 static snd_pcm_sframes_t snd_pcm_file_readn(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
 {
 	snd_pcm_file_t *file = pcm->private_data;
@@ -558,7 +571,7 @@ static snd_pcm_sframes_t snd_pcm_file_readn(snd_pcm_t *pcm, void **bufs, snd_pcm
 		return 0;	/* TODO: Noninterleaved read */
 	}
 
-	n = snd_pcm_readn(file->gen.slave, bufs, size);
+	n = _snd_pcm_readn(file->gen.slave, bufs, size);
 	return n;
 }
 
src/pcm/pcm_generic.c +5 -5
index 4dbef08cc2c7..12102c40a372 100644
@@ -235,25 +235,25 @@ int snd_pcm_generic_unlink(snd_pcm_t *pcm)
 snd_pcm_sframes_t snd_pcm_generic_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size)
 {
 	snd_pcm_generic_t *generic = pcm->private_data;
-	return snd_pcm_writei(generic->slave, buffer, size);
+	return _snd_pcm_writei(generic->slave, buffer, size);
 }
 
 snd_pcm_sframes_t snd_pcm_generic_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
 {
 	snd_pcm_generic_t *generic = pcm->private_data;
-	return snd_pcm_writen(generic->slave, bufs, size);
+	return _snd_pcm_writen(generic->slave, bufs, size);
 }
 
 snd_pcm_sframes_t snd_pcm_generic_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size)
 {
 	snd_pcm_generic_t *generic = pcm->private_data;
-	return snd_pcm_readi(generic->slave, buffer, size);
+	return _snd_pcm_readi(generic->slave, buffer, size);
 }
 
 snd_pcm_sframes_t snd_pcm_generic_readn(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size)
 {
 	snd_pcm_generic_t *generic = pcm->private_data;
-	return snd_pcm_readn(generic->slave, bufs, size);
+	return _snd_pcm_readn(generic->slave, bufs, size);
 }
 
 snd_pcm_sframes_t snd_pcm_generic_mmap_commit(snd_pcm_t *pcm, 
@@ -287,7 +287,7 @@ int snd_pcm_generic_real_htimestamp(snd_pcm_t *pcm, snd_pcm_uframes_t *avail,
 	int ok = 0;
 
 	while (1) {
-		avail1 = snd_pcm_avail_update(pcm);
+		avail1 = __snd_pcm_avail_update(pcm);
 		if (avail1 < 0)
 			return avail1;
 		if (ok && (snd_pcm_uframes_t)avail1 == *avail)
src/pcm/pcm_hw.c +3 -0
index 4f4b84b2d2bc..d4045f8efc4f 100644
@@ -1505,6 +1505,9 @@ int snd_pcm_hw_open_fd(snd_pcm_t **pcmp, const char *name,
 	pcm->poll_fd = fd;
 	pcm->poll_events = info.stream == SND_PCM_STREAM_PLAYBACK ? POLLOUT : POLLIN;
 	pcm->tstamp_type = tstamp_type;
+#ifdef THREAD_SAFE_API
+	pcm->thread_safe = 1;
+#endif
 
 	ret = snd_pcm_hw_mmap_status(pcm);
 	if (ret < 0) {
src/pcm/pcm_ioplug.c +52 -18

157 lines not on this page · raw message

src/pcm/pcm_local.h +100 -29

201 lines not on this page · raw message

src/pcm/pcm_mmap.c +14 -4
index 5c4fbe1705eb..1948289cdc44 100644
@@ -82,12 +82,12 @@ static snd_pcm_sframes_t snd_pcm_mmap_write_areas(snd_pcm_t *pcm,
 		snd_pcm_uframes_t frames = size;
 		snd_pcm_sframes_t result;
 
-		snd_pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
+		__snd_pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
 		snd_pcm_areas_copy(pcm_areas, pcm_offset,
 				   areas, offset, 
 				   pcm->channels, 
 				   frames, pcm->format);
-		result = snd_pcm_mmap_commit(pcm, pcm_offset, frames);
+		result = __snd_pcm_mmap_commit(pcm, pcm_offset, frames);
 		if (result < 0)
 			return xfer > 0 ? (snd_pcm_sframes_t)xfer : result;
 		offset += result;
@@ -114,12 +114,12 @@ static snd_pcm_sframes_t snd_pcm_mmap_read_areas(snd_pcm_t *pcm,
 		snd_pcm_uframes_t frames = size;
 		snd_pcm_sframes_t result;
 		
-		snd_pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
+		__snd_pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
 		snd_pcm_areas_copy(areas, offset,
 				   pcm_areas, pcm_offset,
 				   pcm->channels, 
 				   frames, pcm->format);
-		result = snd_pcm_mmap_commit(pcm, pcm_offset, frames);
+		result = __snd_pcm_mmap_commit(pcm, pcm_offset, frames);
 		if (result < 0)
 			return xfer > 0 ? (snd_pcm_sframes_t)xfer : result;
 		offset += result;
@@ -513,6 +513,7 @@ int snd_pcm_munmap(snd_pcm_t *pcm)
 	return 0;
 }
 
+/* called in pcm lock */
 snd_pcm_sframes_t snd_pcm_write_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t offset,
 				     snd_pcm_uframes_t size)
 {
@@ -530,7 +531,9 @@ snd_pcm_sframes_t snd_pcm_write_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t offset,
 		{
 			const snd_pcm_channel_area_t *a = snd_pcm_mmap_areas(pcm);
 			const char *buf = snd_pcm_channel_area_addr(a, offset);
+			snd_pcm_unlock(pcm); /* to avoid deadlock */
 			err = _snd_pcm_writei(pcm, buf, frames);
+			snd_pcm_lock(pcm);
 			if (err >= 0)
 				frames = err;
 			break;
@@ -545,7 +548,9 @@ snd_pcm_sframes_t snd_pcm_write_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t offset,
 				const snd_pcm_channel_area_t *a = &areas[c];
 				bufs[c] = snd_pcm_channel_area_addr(a, offset);
 			}
+			snd_pcm_unlock(pcm); /* to avoid deadlock */
 			err = _snd_pcm_writen(pcm, bufs, frames);
+			snd_pcm_lock(pcm);
 			if (err >= 0)
 				frames = err;
 			break;
@@ -564,6 +569,7 @@ snd_pcm_sframes_t snd_pcm_write_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t offset,
 	return err;
 }
 
+/* called in pcm lock */
 snd_pcm_sframes_t snd_pcm_read_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t offset,
 				    snd_pcm_uframes_t size)
 {
@@ -581,7 +587,9 @@ snd_pcm_sframes_t snd_pcm_read_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t offset,
 		{
 			const snd_pcm_channel_area_t *a = snd_pcm_mmap_areas(pcm);
 			char *buf = snd_pcm_channel_area_addr(a, offset);
+			snd_pcm_unlock(pcm); /* to avoid deadlock */
 			err = _snd_pcm_readi(pcm, buf, frames);
+			snd_pcm_lock(pcm);
 			if (err >= 0)
 				frames = err;
 			break;
@@ -596,7 +604,9 @@ snd_pcm_sframes_t snd_pcm_read_mmap(snd_pcm_t *pcm, snd_pcm_uframes_t offset,
 				const snd_pcm_channel_area_t *a = &areas[c];
 				bufs[c] = snd_pcm_channel_area_addr(a, offset);
 			}
+			snd_pcm_unlock(pcm); /* to avoid deadlock */
 			err = _snd_pcm_readn(pcm->fast_op_arg, bufs, frames);
+			snd_pcm_lock(pcm);
 			if (err >= 0)
 				frames = err;
 			break;
src/pcm/pcm_rate.c +6 -1
index 41bddac1a83f..06854fdee7f9 100644
@@ -1004,6 +1004,7 @@ static int snd_pcm_rate_poll_revents(snd_pcm_t *pcm, struct pollfd *pfds, unsign
 	return snd_pcm_poll_descriptors_revents(rate->gen.slave, pfds, nfds, revents);
 }
 
+/* locking */
 static int snd_pcm_rate_drain(snd_pcm_t *pcm)
 {
 	snd_pcm_rate_t *rate = pcm->private_data;
@@ -1013,6 +1014,7 @@ static int snd_pcm_rate_drain(snd_pcm_t *pcm)
 		snd_pcm_uframes_t size, ofs, saved_avail_min;
 		snd_pcm_sw_params_t sw_params;
 
+		__snd_pcm_lock(pcm);
 		/* temporarily set avail_min to one */
 		sw_params = rate->sw_params;
 		saved_avail_min = sw_params.avail_min;
@@ -1023,8 +1025,10 @@ static int snd_pcm_rate_drain(snd_pcm_t *pcm)
 		ofs = rate->last_commit_ptr % pcm->buffer_size;
 		while (size > 0) {
 			snd_pcm_uframes_t psize, spsize;
+			int err;
 
-			if (snd_pcm_wait(rate->gen.slave, -1) < 0)
+			err = __snd_pcm_wait_in_lock(rate->gen.slave, -1);
+			if (err < 0)
 				break;
 			if (size > pcm->period_size) {
 				psize = pcm->period_size;
@@ -1042,6 +1046,7 @@ static int snd_pcm_rate_drain(snd_pcm_t *pcm)
 		}
 		sw_params.avail_min = saved_avail_min;
 		snd_pcm_sw_params(rate->gen.slave, &sw_params);
+		__snd_pcm_unlock(pcm);
 	}
 	return snd_pcm_drain(rate->gen.slave);
 }
src/pcm/pcm_route.c +1 -1
index 361160302be8..508d5b0fc3d2 100644
@@ -877,7 +877,7 @@ static int route_chmap_init(snd_pcm_t *pcm)
 	snd_pcm_route_t *route = pcm->private_data;
 	if (!route->chmap)
 		return 0;
-	if (snd_pcm_state(pcm) != SND_PCM_STATE_PREPARED)
+	if (__snd_pcm_state(pcm) != SND_PCM_STATE_PREPARED)
 		return 0;
 
 	/* Check if we really need to set the chmap or not.
Signature · 1 line
2.9.0
#2 lore raw

PATCH v2 2/3 pcm: Remove home brew atomic operations

In reply to Takashi Iwai

We've had a few home brew atomic operations in a couple of places in the PCM code. This was for supporting the concurrent accesses, but in practice, it couldn't cover the race properly by itself alone.

Since we have a wider concurrency protection via mutex now, we can get rid of these atomic codes, which worsens the portability significantly.

---
 include/Makefile.am  |   2 +-
 include/iatomic.h    | 170 ---------------------------------------------------
 src/pcm/Makefile.am  |   2 +-
 src/pcm/atomic.c     |  43 -------------
 src/pcm/pcm_plugin.c |  72 +++++-----------------
 src/pcm/pcm_plugin.h |   2 -
 src/pcm/pcm_rate.c   |  34 ++---------
 7 files changed, 20 insertions(+), 305 deletions(-)
 delete mode 100644 include/iatomic.h
 delete mode 100644 src/pcm/atomic.c
include/Makefile.am +1 -1
index 31a3f748dc46..67f32e36c911 100644
@@ -5,7 +5,7 @@ alsaincludedir = ${includedir}/alsa
 
 alsainclude_HEADERS = asoundlib.h asoundef.h \
 		      version.h global.h input.h output.h error.h \
-		      conf.h control.h iatomic.h
+		      conf.h control.h
 
 if BUILD_CTL_PLUGIN_EXT
 alsainclude_HEADERS += control_external.h
include/iatomic.h deleted +0 -170

170 lines not on this page · raw message

src/pcm/Makefile.am +1 -1
index 81598f634bc3..8edbd0b5c719 100644
@@ -3,7 +3,7 @@ DIST_SUBDIRS = scopes
 
 EXTRA_LTLIBRARIES = libpcm.la
 
-libpcm_la_SOURCES = atomic.c mask.c interval.c \
+libpcm_la_SOURCES = mask.c interval.c \
 		    pcm.c pcm_params.c pcm_simple.c \
 		    pcm_hw.c pcm_misc.c pcm_mmap.c pcm_symbols.c
 
src/pcm/atomic.c deleted +0 -43

43 lines not on this page · raw message

src/pcm/pcm_plugin.c +14 -58

236 lines not on this page · raw message

src/pcm/pcm_plugin.h +0 -2
index b0a3e1869ea1..217f0757ea59 100644
@@ -19,7 +19,6 @@
  *
  */
   
-#include "iatomic.h"
 #include "pcm_generic.h"
 
 typedef snd_pcm_uframes_t (*snd_pcm_slave_xfer_areas_func_t)
@@ -46,7 +45,6 @@ typedef struct {
 	snd_pcm_slave_xfer_areas_undo_func_t undo_write;
 	int (*init)(snd_pcm_t *pcm);
 	snd_pcm_uframes_t appl_ptr, hw_ptr;
-	snd_atomic_write_t watom;
 } snd_pcm_plugin_t;	
 
 /* make local functions really local */
src/pcm/pcm_rate.c +4 -30
index 06854fdee7f9..6184defbdde3 100644
@@ -32,7 +32,6 @@
 #include "pcm_local.h"
 #include "pcm_plugin.h"
 #include "pcm_rate.h"
-#include "iatomic.h"
 
 #include "plugin_ops.h"
 
@@ -51,7 +50,6 @@ typedef struct _snd_pcm_rate snd_pcm_rate_t;
 
 struct _snd_pcm_rate {
 	snd_pcm_generic_t gen;
-	snd_atomic_write_t watom;
 	snd_pcm_uframes_t appl_ptr, hw_ptr;
 	snd_pcm_uframes_t last_commit_ptr;
 	snd_pcm_uframes_t orig_avail_min;
@@ -584,9 +582,7 @@ static int snd_pcm_rate_hwsync(snd_pcm_t *pcm)
 	int err = snd_pcm_hwsync(rate->gen.slave);
 	if (err < 0)
 		return err;
-	snd_atomic_write_begin(&rate->watom);
 	snd_pcm_rate_sync_hwptr(pcm);
-	snd_atomic_write_end(&rate->watom);
 	return 0;
 }
 
@@ -602,15 +598,11 @@ static int snd_pcm_rate_prepare(snd_pcm_t *pcm)
 	snd_pcm_rate_t *rate = pcm->private_data;
 	int err;
 
-	snd_atomic_write_begin(&rate->watom);
 	err = snd_pcm_prepare(rate->gen.slave);
-	if (err < 0) {
-		snd_atomic_write_end(&rate->watom);
+	if (err < 0)
 		return err;
-	}
 	*pcm->hw.ptr = 0;
 	*pcm->appl.ptr = 0;
-	snd_atomic_write_end(&rate->watom);
 	err = snd_pcm_rate_init(pcm);
 	if (err < 0)
 		return err;
@@ -621,15 +613,11 @@ static int snd_pcm_rate_reset(snd_pcm_t *pcm)
 {
 	snd_pcm_rate_t *rate = pcm->private_data;
 	int err;
-	snd_atomic_write_begin(&rate->watom);
 	err = snd_pcm_reset(rate->gen.slave);
-	if (err < 0) {
-		snd_atomic_write_end(&rate->watom);
+	if (err < 0)
 		return err;
-	}
 	*pcm->hw.ptr = 0;
 	*pcm->appl.ptr = 0;
-	snd_atomic_write_end(&rate->watom);
 	err = snd_pcm_rate_init(pcm);
 	if (err < 0)
 		return err;
@@ -923,9 +911,7 @@ static snd_pcm_sframes_t snd_pcm_rate_mmap_commit(snd_pcm_t *pcm,
 		if (err < 0)
 			return err;
 	}
-	snd_atomic_write_begin(&rate->watom);
 	snd_pcm_mmap_appl_forward(pcm, size);
-	snd_atomic_write_end(&rate->watom);
 	return size;
 }
 
@@ -938,9 +924,7 @@ static snd_pcm_sframes_t snd_pcm_rate_avail_update(snd_pcm_t *pcm)
 	slave_size = snd_pcm_avail_update(slave);
 	if (pcm->stream == SND_PCM_STREAM_CAPTURE)
 		goto _capture;
-	snd_atomic_write_begin(&rate->watom);
 	snd_pcm_rate_sync_hwptr(pcm);
-	snd_atomic_write_end(&rate->watom);
 	snd_pcm_rate_sync_playback_area(pcm, rate->appl_ptr);
 	return snd_pcm_mmap_avail(pcm);
  _capture: {
@@ -1090,15 +1074,10 @@ static int snd_pcm_rate_status(snd_pcm_t *pcm, snd_pcm_status_t * status)
 {
 	snd_pcm_rate_t *rate = pcm->private_data;
 	snd_pcm_sframes_t err;
-	snd_atomic_read_t ratom;
-	snd_atomic_read_init(&ratom, &rate->watom);
- _again:
-	snd_atomic_read_begin(&ratom);
+
 	err = snd_pcm_status(rate->gen.slave, status);
-	if (err < 0) {
-		snd_atomic_read_ok(&ratom);
+	if (err < 0)
 		return err;
-	}
 	if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
 		if (rate->start_pending)
 			status->state = SND_PCM_STATE_RUNNING;
@@ -1116,10 +1095,6 @@ static int snd_pcm_rate_status(snd_pcm_t *pcm, snd_pcm_status_t * status)
 		status->avail = snd_pcm_mmap_capture_avail(pcm);
 		status->avail_max = rate->ops.output_frames(rate->obj, status->avail_max);
 	}
-	if (!snd_atomic_read_ok(&ratom)) {
-		snd_atomic_read_wait(&ratom);
-		goto _again;
-	}
 	return 0;
 }
 
@@ -1309,7 +1284,6 @@ int snd_pcm_rate_open(snd_pcm_t **pcmp, const char *name,
 	rate->gen.close_slave = close_slave;
 	rate->srate = srate;
 	rate->sformat = sformat;
-	snd_atomic_write_init(&rate->watom);
 
 	err = snd_pcm_new(&pcm, SND_PCM_TYPE_RATE, name, slave->stream, slave->mode);
 	if (err < 0) {
Signature · 1 line
2.9.0
#3 lore raw

PATCH v2 3/3 pcm: Add LIBASOUND_THREAD_SAFE env variable check

In reply to Takashi Iwai

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.

---
 src/pcm/pcm.c       | 16 ++++++++++++++++
 src/pcm/pcm_local.h |  6 ++++--
 2 files changed, 20 insertions(+), 2 deletions(-)
src/pcm/pcm.c +16 -0
index 1567a46146ad..1a9e9574ded8 100644
@@ -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;
src/pcm/pcm_local.h +4 -2
index 5c7eeb4ee07d..bb7964d7833e 100644
@@ -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)
 {
Signature · 1 line
2.9.0
#4 lore raw
In reply to Takashi Iwai
Quoted 22 lines · Takashi Iwai

Hi,

here is a v2 patchset for supporting multi thread safety to alsa-lib PCM API. Now it's out from RFC and should be reviewed as an official patchset.

Basically ALSA PCM functions are thread-unsafe, and applications are supposed to do the proper protection against racy accesses. The reality is, however, that application developers don't care such, as alsa-lib works in most cases. Of course, users still get occasionally mysterious crashes.

As a workaround, this patchset adds the pthread mutex protection to most of exported PCM functions. This is slightly an overkill, but the biggest merit is that it's easy to implement; I just wrapped the functions and replaced the internal ones with unlocked versions.

To be noted, there is an optimization for the direct hw PCM access. Performance-sensitive applications like JACK should work like before without any overhead.

Quoted 6 lines

Another bonus by this addition is that we can finally get rid of home brew (and deadly smelling) atomic macros from the tree, since it's now more widely protected.

I lightly tested on my local machines (dmix, PA, jack and plugins) and all seem working well, so far.

As there's been no objection, I pushed this patchset now. Let me know if this gives any regression.

thanks,

Takashi