mirror of
https://github.com/alsa-project/alsa-lib.git
synced 2025-11-02 09:01:48 -05:00
conf: Add thread-safe global tree reference
Most of open functions in alsa-lib have the call pattern: snd_config_update(); return snd_xxx_open(x, snd_config, ...); This means that the toplevel config gets updated, and passed to a local open function. Although snd_config_update() itself has a pthread mutex to be thread safe, the whole procedure above isn't thread safe. Namely, the global snd_config tree may be deleted and recreated at any time while the open function is being processed. This may lead to a data corruption and crash of the program. For avoiding the corruption, this patch introduces a refcount to config tree object. A few new helper functions are introduced as well: - snd_config_update_ref() does update and take the refcount of the toplevel tree. The obtained config tree has to be freed via snd_config_unref() below. - snd_config_ref() and snd_config_unref() manage the refcount of the config object. The latter eventually deletes the object when all references are gone. Along with these additions, the caller of snd_config_update() and snd_config global tree in alsa-lib are replaced with the new helpers. Signed-off-by: Takashi Iwai <tiwai@suse.de>
This commit is contained in:
parent
5fb3fe1724
commit
c9a0d7d601
9 changed files with 125 additions and 14 deletions
|
|
@ -305,12 +305,16 @@ static int snd_rawmidi_open_noupdate(snd_rawmidi_t **inputp, snd_rawmidi_t **out
|
|||
int snd_rawmidi_open(snd_rawmidi_t **inputp, snd_rawmidi_t **outputp,
|
||||
const char *name, int mode)
|
||||
{
|
||||
snd_config_t *top;
|
||||
int err;
|
||||
|
||||
assert((inputp || outputp) && name);
|
||||
err = snd_config_update();
|
||||
err = snd_config_update_ref(&top);
|
||||
if (err < 0)
|
||||
return err;
|
||||
return snd_rawmidi_open_noupdate(inputp, outputp, snd_config, name, mode);
|
||||
err = snd_rawmidi_open_noupdate(inputp, outputp, top, name, mode);
|
||||
snd_config_unref(top);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue