OSDN Git Service

pcm: Add thread-safety to PCM API
authorTakashi Iwai <tiwai@suse.de>
Thu, 30 Jun 2016 13:32:40 +0000 (15:32 +0200)
committerTakashi Iwai <tiwai@suse.de>
Mon, 11 Jul 2016 13:25:30 +0000 (15:25 +0200)
commit54931e5a5455ac681a32a673d4b360d43f34b6b5
treea49dc592f1e3ce16239e4aefa22cdd015cac1fa1
parent16eb412043fd0979c51a511a084a4058d44207a1
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>
16 files changed:
INSTALL
configure.ac
include/pcm_ioplug.h
src/pcm/pcm.c
src/pcm/pcm_direct.c
src/pcm/pcm_dmix.c
src/pcm/pcm_dshare.c
src/pcm/pcm_dsnoop.c
src/pcm/pcm_file.c
src/pcm/pcm_generic.c
src/pcm/pcm_hw.c
src/pcm/pcm_ioplug.c
src/pcm/pcm_local.h
src/pcm/pcm_mmap.c
src/pcm/pcm_rate.c
src/pcm/pcm_route.c