4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 #include <pulse/timeval.h>
37 #include <pulse/xmalloc.h>
39 #include <pulsecore/core-subscribe.h>
40 #include <pulsecore/log.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/macro.h>
43 #include <pulsecore/ltdl-helper.h>
47 #define PA_SYMBOL_INIT "pa__init"
48 #define PA_SYMBOL_DONE "pa__done"
49 #define PA_SYMBOL_LOAD_ONCE "pa__load_once"
51 #define UNLOAD_POLL_TIME 2
53 static void timeout_callback(pa_mainloop_api
*m
, pa_time_event
*e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
54 pa_core
*c
= PA_CORE(userdata
);
57 pa_core_assert_ref(c
);
58 pa_assert(c
->mainloop
== m
);
59 pa_assert(c
->module_auto_unload_event
== e
);
61 pa_module_unload_unused(c
);
63 pa_gettimeofday(&ntv
);
64 pa_timeval_add(&ntv
, UNLOAD_POLL_TIME
*1000000);
65 m
->time_restart(e
, &ntv
);
68 pa_module
* pa_module_load(pa_core
*c
, const char *name
, const char *argument
) {
70 pa_bool_t (*load_once
)(void);
75 if (c
->disallow_module_loading
)
78 m
= pa_xnew(pa_module
, 1);
79 m
->name
= pa_xstrdup(name
);
80 m
->argument
= pa_xstrdup(argument
);
82 if (!(m
->dl
= lt_dlopenext(name
))) {
83 pa_log("Failed to open module \"%s\": %s", name
, lt_dlerror());
87 if ((load_once
= (pa_bool_t (*)(void)) pa_load_sym(m
->dl
, name
, PA_SYMBOL_LOAD_ONCE
))) {
89 if (load_once() && c
->modules
) {
92 /* OK, the module only wants to be loaded once, let's make sure it is */
94 for (i
= pa_idxset_first(c
->modules
, &idx
); i
; i
= pa_idxset_next(c
->modules
, &idx
)) {
95 if (strcmp(name
, i
->name
) == 0) {
96 pa_log("Module \"%s\" should be loaded once at most. Refusing to load.", name
);
103 if (!(m
->init
= (int (*)(pa_module
*_m
)) pa_load_sym(m
->dl
, name
, PA_SYMBOL_INIT
))) {
104 pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT
"\" not found.", name
);
108 m
->done
= (void (*)(pa_module
*_m
)) pa_load_sym(m
->dl
, name
, PA_SYMBOL_DONE
);
112 m
->auto_unload
= FALSE
;
113 m
->unload_requested
= FALSE
;
115 if (m
->init(m
) < 0) {
116 pa_log_error("Failed to load module \"%s\" (argument: \"%s\"): initialization failed.", name
, argument
? argument
: "");
121 c
->modules
= pa_idxset_new(NULL
, NULL
);
123 if (!c
->module_auto_unload_event
) {
125 pa_gettimeofday(&ntv
);
126 pa_timeval_add(&ntv
, UNLOAD_POLL_TIME
*1000000);
127 c
->module_auto_unload_event
= c
->mainloop
->time_new(c
->mainloop
, &ntv
, timeout_callback
, c
);
130 pa_assert_se(pa_idxset_put(c
->modules
, m
, &m
->index
) >= 0);
131 pa_assert(m
->index
!= PA_IDXSET_INVALID
);
133 pa_log_info("Loaded \"%s\" (index: #%u; argument: \"%s\").", m
->name
, m
->index
, m
->argument
? m
->argument
: "");
135 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_NEW
, m
->index
);
142 pa_xfree(m
->argument
);
154 static void pa_module_free(pa_module
*m
) {
158 if (m
->core
->disallow_module_loading
)
161 pa_log_info("Unloading \"%s\" (index: #%u).", m
->name
, m
->index
);
168 pa_log_info("Unloaded \"%s\" (index: #%u).", m
->name
, m
->index
);
170 pa_subscription_post(m
->core
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_REMOVE
, m
->index
);
173 pa_xfree(m
->argument
);
177 void pa_module_unload(pa_core
*c
, pa_module
*m
) {
181 pa_assert(c
->modules
);
182 if (!(m
= pa_idxset_remove_by_data(c
->modules
, m
, NULL
)))
188 void pa_module_unload_by_index(pa_core
*c
, uint32_t idx
) {
191 pa_assert(idx
!= PA_IDXSET_INVALID
);
193 if (!(m
= pa_idxset_remove_by_index(c
->modules
, idx
)))
199 static void free_callback(void *p
, PA_GCC_UNUSED
void *userdata
) {
205 void pa_module_unload_all(pa_core
*c
) {
213 while ((m
= pa_idxset_first(c
->modules
, NULL
)))
214 pa_module_unload(c
, m
);
216 pa_idxset_free(c
->modules
, free_callback
, NULL
);
219 if (c
->module_auto_unload_event
) {
220 c
->mainloop
->time_free(c
->module_auto_unload_event
);
221 c
->module_auto_unload_event
= NULL
;
224 if (c
->module_defer_unload_event
) {
225 c
->mainloop
->defer_free(c
->module_defer_unload_event
);
226 c
->module_defer_unload_event
= NULL
;
230 static int unused_callback(void *p
, PA_GCC_UNUSED
uint32_t idx
, int *del
, void *userdata
) {
232 time_t *now
= userdata
;
238 if (m
->n_used
== 0 && m
->auto_unload
&& m
->last_used_time
+m
->core
->module_idle_time
<= *now
) {
246 void pa_module_unload_unused(pa_core
*c
) {
254 pa_idxset_foreach(c
->modules
, unused_callback
, &now
);
257 static int unload_callback(void *p
, PA_GCC_UNUSED
uint32_t idx
, int *del
, PA_GCC_UNUSED
void *userdata
) {
261 if (m
->unload_requested
) {
269 static void defer_cb(pa_mainloop_api
*api
, pa_defer_event
*e
, void *userdata
) {
270 pa_core
*core
= PA_CORE(userdata
);
272 pa_core_assert_ref(core
);
273 api
->defer_enable(e
, 0);
278 pa_idxset_foreach(core
->modules
, unload_callback
, NULL
);
281 void pa_module_unload_request(pa_module
*m
) {
284 m
->unload_requested
= TRUE
;
286 if (!m
->core
->module_defer_unload_event
)
287 m
->core
->module_defer_unload_event
= m
->core
->mainloop
->defer_new(m
->core
->mainloop
, defer_cb
, m
->core
);
289 m
->core
->mainloop
->defer_enable(m
->core
->module_defer_unload_event
, 1);
292 void pa_module_set_used(pa_module
*m
, int used
) {
295 if (m
->n_used
!= used
)
296 pa_subscription_post(m
->core
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_CHANGE
, m
->index
);
298 if (used
== 0 && m
->n_used
> 0)
299 time(&m
->last_used_time
);
304 pa_modinfo
*pa_module_get_info(pa_module
*m
) {
307 return pa_modinfo_get_by_handle(m
->dl
, m
->name
);