2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <pulse/timeval.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/core-subscribe.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/macro.h>
41 #include <pulsecore/ltdl-helper.h>
45 #define PA_SYMBOL_INIT "pa__init"
46 #define PA_SYMBOL_DONE "pa__done"
47 #define PA_SYMBOL_LOAD_ONCE "pa__load_once"
49 #define UNLOAD_POLL_TIME 2
51 static void timeout_callback(pa_mainloop_api
*m
, pa_time_event
*e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
52 pa_core
*c
= PA_CORE(userdata
);
55 pa_core_assert_ref(c
);
56 pa_assert(c
->mainloop
== m
);
57 pa_assert(c
->module_auto_unload_event
== e
);
59 pa_module_unload_unused(c
);
61 pa_gettimeofday(&ntv
);
62 pa_timeval_add(&ntv
, UNLOAD_POLL_TIME
*1000000);
63 m
->time_restart(e
, &ntv
);
66 pa_module
* pa_module_load(pa_core
*c
, const char *name
, const char *argument
) {
68 pa_bool_t (*load_once
)(void);
73 if (c
->disallow_module_loading
)
76 m
= pa_xnew(pa_module
, 1);
77 m
->name
= pa_xstrdup(name
);
78 m
->argument
= pa_xstrdup(argument
);
80 if (!(m
->dl
= lt_dlopenext(name
))) {
81 pa_log("Failed to open module \"%s\": %s", name
, lt_dlerror());
85 if ((load_once
= (pa_bool_t (*)(void)) pa_load_sym(m
->dl
, name
, PA_SYMBOL_LOAD_ONCE
))) {
87 if (load_once() && c
->modules
) {
90 /* OK, the module only wants to be loaded once, let's make sure it is */
92 for (i
= pa_idxset_first(c
->modules
, &idx
); i
; i
= pa_idxset_next(c
->modules
, &idx
)) {
93 if (strcmp(name
, i
->name
) == 0) {
94 pa_log("Module \"%s\" should be loaded once at most. Refusing to load.", name
);
101 if (!(m
->init
= (int (*)(pa_module
*_m
)) pa_load_sym(m
->dl
, name
, PA_SYMBOL_INIT
))) {
102 pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT
"\" not found.", name
);
106 m
->done
= (void (*)(pa_module
*_m
)) pa_load_sym(m
->dl
, name
, PA_SYMBOL_DONE
);
110 m
->auto_unload
= FALSE
;
111 m
->unload_requested
= FALSE
;
113 if (m
->init(m
) < 0) {
114 pa_log_error("Failed to load module \"%s\" (argument: \"%s\"): initialization failed.", name
, argument
? argument
: "");
119 c
->modules
= pa_idxset_new(NULL
, NULL
);
121 if (m
->auto_unload
&& !c
->module_auto_unload_event
) {
123 pa_gettimeofday(&ntv
);
124 pa_timeval_add(&ntv
, UNLOAD_POLL_TIME
*1000000);
125 c
->module_auto_unload_event
= c
->mainloop
->time_new(c
->mainloop
, &ntv
, timeout_callback
, c
);
128 pa_assert_se(pa_idxset_put(c
->modules
, m
, &m
->index
) >= 0);
129 pa_assert(m
->index
!= PA_IDXSET_INVALID
);
131 pa_log_info("Loaded \"%s\" (index: #%u; argument: \"%s\").", m
->name
, m
->index
, m
->argument
? m
->argument
: "");
133 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_NEW
, m
->index
);
140 pa_xfree(m
->argument
);
152 static void pa_module_free(pa_module
*m
) {
156 if (m
->core
->disallow_module_loading
)
159 pa_log_info("Unloading \"%s\" (index: #%u).", m
->name
, m
->index
);
166 pa_log_info("Unloaded \"%s\" (index: #%u).", m
->name
, m
->index
);
168 pa_subscription_post(m
->core
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_REMOVE
, m
->index
);
171 pa_xfree(m
->argument
);
175 void pa_module_unload(pa_core
*c
, pa_module
*m
) {
179 pa_assert(c
->modules
);
180 if (!(m
= pa_idxset_remove_by_data(c
->modules
, m
, NULL
)))
186 void pa_module_unload_by_index(pa_core
*c
, uint32_t idx
) {
189 pa_assert(idx
!= PA_IDXSET_INVALID
);
191 if (!(m
= pa_idxset_remove_by_index(c
->modules
, idx
)))
197 static void free_callback(void *p
, PA_GCC_UNUSED
void *userdata
) {
203 void pa_module_unload_all(pa_core
*c
) {
211 while ((m
= pa_idxset_first(c
->modules
, NULL
)))
212 pa_module_unload(c
, m
);
214 pa_idxset_free(c
->modules
, free_callback
, NULL
);
217 if (c
->module_auto_unload_event
) {
218 c
->mainloop
->time_free(c
->module_auto_unload_event
);
219 c
->module_auto_unload_event
= NULL
;
222 if (c
->module_defer_unload_event
) {
223 c
->mainloop
->defer_free(c
->module_defer_unload_event
);
224 c
->module_defer_unload_event
= NULL
;
228 static int unused_callback(void *p
, PA_GCC_UNUSED
uint32_t idx
, int *del
, void *userdata
) {
230 time_t *now
= userdata
;
236 if (m
->n_used
== 0 && m
->auto_unload
&& m
->last_used_time
+m
->core
->module_idle_time
<= *now
) {
244 void pa_module_unload_unused(pa_core
*c
) {
252 pa_idxset_foreach(c
->modules
, unused_callback
, &now
);
255 static int unload_callback(void *p
, PA_GCC_UNUSED
uint32_t idx
, int *del
, PA_GCC_UNUSED
void *userdata
) {
259 if (m
->unload_requested
) {
267 static void defer_cb(pa_mainloop_api
*api
, pa_defer_event
*e
, void *userdata
) {
268 pa_core
*core
= PA_CORE(userdata
);
270 pa_core_assert_ref(core
);
271 api
->defer_enable(e
, 0);
276 pa_idxset_foreach(core
->modules
, unload_callback
, NULL
);
279 void pa_module_unload_request(pa_module
*m
) {
282 m
->unload_requested
= TRUE
;
284 if (!m
->core
->module_defer_unload_event
)
285 m
->core
->module_defer_unload_event
= m
->core
->mainloop
->defer_new(m
->core
->mainloop
, defer_cb
, m
->core
);
287 m
->core
->mainloop
->defer_enable(m
->core
->module_defer_unload_event
, 1);
290 void pa_module_set_used(pa_module
*m
, int used
) {
293 if (m
->n_used
!= used
)
294 pa_subscription_post(m
->core
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_CHANGE
, m
->index
);
296 if (used
== 0 && m
->n_used
> 0)
297 time(&m
->last_used_time
);
302 pa_modinfo
*pa_module_get_info(pa_module
*m
) {
305 return pa_modinfo_get_by_handle(m
->dl
, m
->name
);