general: recoded snd_dlobj_ functions

- changed logic to get/put blocks
- added mutex locking of the symbol list
- added reference counting (do not free used dl handles)

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2010-08-23 17:05:36 +02:00
parent be06ab3ee7
commit 91c9c8f1b8
7 changed files with 153 additions and 123 deletions

View file

@ -2068,7 +2068,6 @@ static int snd_pcm_open_conf(snd_pcm_t **pcmp, const char *name,
#ifndef PIC
extern void *snd_pcm_open_symbols(void);
#endif
void *h = NULL;
if (snd_config_get_type(pcm_conf) != SND_CONFIG_TYPE_COMPOUND) {
char *val;
id = NULL;
@ -2157,39 +2156,18 @@ static int snd_pcm_open_conf(snd_pcm_t **pcmp, const char *name,
#ifndef PIC
snd_pcm_open_symbols(); /* this call is for static linking only */
#endif
open_func = snd_dlobj_cache_lookup(open_name);
open_func = snd_dlobj_cache_get(lib, open_name,
SND_DLSYM_VERSION(SND_PCM_DLSYM_VERSION), 1);
if (open_func) {
err = 0;
goto _err;
}
h = snd_dlopen(lib, RTLD_NOW);
if (h)
open_func = snd_dlsym(h, open_name, SND_DLSYM_VERSION(SND_PCM_DLSYM_VERSION));
err = 0;
if (!h) {
SNDERR("Cannot open shared library %s",
lib ? lib : "[builtin]");
err = -ENOENT;
} else if (!open_func) {
SNDERR("symbol %s is not defined inside %s", open_name,
lib ? lib : "[builtin]");
snd_dlclose(h);
err = -ENXIO;
}
_err:
if (err >= 0) {
err = open_func(pcmp, name, pcm_root, pcm_conf, stream, mode);
if (err >= 0) {
if (h /*&& (mode & SND_PCM_KEEP_ALIVE)*/) {
snd_dlobj_cache_add(open_name, h, open_func);
h = NULL;
}
(*pcmp)->dl_handle = h;
(*pcmp)->open_func = open_func;
err = 0;
} else {
if (h)
snd_dlclose(h);
snd_dlobj_cache_put(open_func);
}
} else {
err = -ENXIO;
}
if (err >= 0) {
err = snd_config_search(pcm_root, "defaults.pcm.compat", &tmp);
@ -2209,6 +2187,7 @@ static int snd_pcm_open_conf(snd_pcm_t **pcmp, const char *name,
snd_config_get_integer(tmp, &(*pcmp)->minperiodtime);
err = 0;
}
_err:
if (type_conf)
snd_config_delete(type_conf);
free(buf);
@ -2304,8 +2283,7 @@ int snd_pcm_free(snd_pcm_t *pcm)
free(pcm->name);
free(pcm->hw.link_dst);
free(pcm->appl.link_dst);
if (pcm->dl_handle)
snd_dlclose(pcm->dl_handle);
snd_dlobj_cache_put(pcm->open_func);
free(pcm);
return 0;
}

View file

@ -174,7 +174,7 @@ typedef struct {
} snd_pcm_fast_ops_t;
struct _snd_pcm {
void *dl_handle;
void *open_func;
char *name;
snd_pcm_type_t type;
snd_pcm_stream_t stream;

View file

@ -61,6 +61,7 @@ struct _snd_pcm_rate {
snd_pcm_channel_area_t *pareas; /* areas for splitted period (rate pcm) */
snd_pcm_channel_area_t *sareas; /* areas for splitted period (slave pcm) */
snd_pcm_rate_info_t info;
void *open_func;
void *obj;
snd_pcm_rate_ops_t ops;
unsigned int get_idx;
@ -1204,6 +1205,8 @@ static int snd_pcm_rate_close(snd_pcm_t *pcm)
if (rate->ops.close)
rate->ops.close(rate->obj);
if (rate->open_func)
snd_dlobj_cache_put(rate->open_func);
return snd_pcm_generic_close(pcm);
}
@ -1272,33 +1275,23 @@ static const char *const default_rate_plugins[] = {
"speexrate", "linear", NULL
};
static int rate_open_func(snd_pcm_rate_t *rate, const char *type)
static int rate_open_func(snd_pcm_rate_t *rate, const char *type, int verbose)
{
char open_name[64];
char open_name[64], lib_name[128], *lib = NULL;
snd_pcm_rate_open_func_t open_func;
int err;
snprintf(open_name, sizeof(open_name), "_snd_pcm_rate_%s_open", type);
open_func = snd_dlobj_cache_lookup(open_name);
if (!open_func) {
void *h;
char lib_name[128], *lib = NULL;
if (!is_builtin_plugin(type)) {
snprintf(lib_name, sizeof(lib_name),
if (!is_builtin_plugin(type)) {
snprintf(lib_name, sizeof(lib_name),
"%s/libasound_module_rate_%s.so", ALSA_PLUGIN_DIR, type);
lib = lib_name;
}
h = snd_dlopen(lib, RTLD_NOW);
if (!h)
return -ENOENT;
open_func = snd_dlsym(h, open_name, NULL);
if (!open_func) {
snd_dlclose(h);
return -ENOENT;
}
snd_dlobj_cache_add(open_name, h, open_func);
lib = lib_name;
}
open_func = snd_dlobj_cache_get(lib, open_name, NULL, verbose);
if (!open_func)
return -ENOENT;
rate->open_func = open_func;
rate->rate_min = SND_PCM_PLUGIN_RATE_MIN;
rate->rate_max = SND_PCM_PLUGIN_RATE_MAX;
rate->plugin_version = SND_PCM_RATE_PLUGIN_VERSION;
@ -1315,8 +1308,13 @@ static int rate_open_func(snd_pcm_rate_t *rate, const char *type)
/* try to open with the old protocol version */
rate->plugin_version = SND_PCM_RATE_PLUGIN_VERSION_OLD;
return open_func(SND_PCM_RATE_PLUGIN_VERSION_OLD,
&rate->obj, &rate->ops);
err = open_func(SND_PCM_RATE_PLUGIN_VERSION_OLD,
&rate->obj, &rate->ops);
if (err) {
snd_dlobj_cache_put(open_func);
rate->open_func = NULL;
}
return err;
}
#endif
@ -1373,21 +1371,21 @@ int snd_pcm_rate_open(snd_pcm_t **pcmp, const char *name,
if (!converter) {
const char *const *types;
for (types = default_rate_plugins; *types; types++) {
err = rate_open_func(rate, *types);
err = rate_open_func(rate, *types, 0);
if (!err) {
type = *types;
break;
}
}
} else if (!snd_config_get_string(converter, &type))
err = rate_open_func(rate, type);
err = rate_open_func(rate, type, 1);
else if (snd_config_get_type(converter) == SND_CONFIG_TYPE_COMPOUND) {
snd_config_iterator_t i, next;
snd_config_for_each(i, next, converter) {
snd_config_t *n = snd_config_iterator_entry(i);
if (snd_config_get_string(n, &type) < 0)
break;
err = rate_open_func(rate, type);
err = rate_open_func(rate, type, 0);
if (!err)
break;
}