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.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
This commit is contained in:
Takashi Iwai 2016-06-30 15:32:40 +02:00
parent 16eb412043
commit 54931e5a54
16 changed files with 607 additions and 148 deletions

View file

@ -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)