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"
50 #define UNLOAD_POLL_TIME 2
52 static void timeout_callback(pa_mainloop_api
*m
, pa_time_event
*e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
53 pa_core
*c
= PA_CORE(userdata
);
56 pa_core_assert_ref(c
);
57 pa_assert(c
->mainloop
== m
);
58 pa_assert(c
->module_auto_unload_event
== e
);
60 pa_module_unload_unused(c
);
62 pa_gettimeofday(&ntv
);
63 pa_timeval_add(&ntv
, UNLOAD_POLL_TIME
*1000000);
64 m
->time_restart(e
, &ntv
);
67 pa_module
* pa_module_load(pa_core
*c
, const char *name
, const char *argument
) {
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 (!(m
->init
= (int (*)(pa_module
*_m
)) pa_load_sym(m
->dl
, name
, PA_SYMBOL_INIT
))) {
86 pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT
"\" not found.", name
);
90 m
->done
= (void (*)(pa_module
*_m
)) pa_load_sym(m
->dl
, name
, PA_SYMBOL_DONE
);
95 m
->unload_requested
= 0;
98 pa_log_error("Failed to load module \"%s\" (argument: \"%s\"): initialization failed.", name
, argument
? argument
: "");
103 c
->modules
= pa_idxset_new(NULL
, NULL
);
105 if (!c
->module_auto_unload_event
) {
107 pa_gettimeofday(&ntv
);
108 pa_timeval_add(&ntv
, UNLOAD_POLL_TIME
*1000000);
109 c
->module_auto_unload_event
= c
->mainloop
->time_new(c
->mainloop
, &ntv
, timeout_callback
, c
);
112 pa_assert_se(pa_idxset_put(c
->modules
, m
, &m
->index
) >= 0);
113 pa_assert(m
->index
!= PA_IDXSET_INVALID
);
115 pa_log_info("Loaded \"%s\" (index: #%u; argument: \"%s\").", m
->name
, m
->index
, m
->argument
? m
->argument
: "");
117 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_NEW
, m
->index
);
124 pa_xfree(m
->argument
);
136 static void pa_module_free(pa_module
*m
) {
140 if (m
->core
->disallow_module_loading
)
143 pa_log_info("Unloading \"%s\" (index: #%u).", m
->name
, m
->index
);
150 pa_log_info("Unloaded \"%s\" (index: #%u).", m
->name
, m
->index
);
152 pa_subscription_post(m
->core
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_REMOVE
, m
->index
);
155 pa_xfree(m
->argument
);
159 void pa_module_unload(pa_core
*c
, pa_module
*m
) {
162 pa_assert(c
->modules
);
163 if (!(m
= pa_idxset_remove_by_data(c
->modules
, m
, NULL
)))
169 void pa_module_unload_by_index(pa_core
*c
, uint32_t idx
) {
172 pa_assert(idx
!= PA_IDXSET_INVALID
);
174 if (!(m
= pa_idxset_remove_by_index(c
->modules
, idx
)))
180 static void free_callback(void *p
, PA_GCC_UNUSED
void *userdata
) {
186 void pa_module_unload_all(pa_core
*c
) {
194 while ((m
= pa_idxset_first(c
->modules
, NULL
)))
195 pa_module_unload(c
, m
);
197 pa_idxset_free(c
->modules
, free_callback
, NULL
);
200 if (c
->module_auto_unload_event
) {
201 c
->mainloop
->time_free(c
->module_auto_unload_event
);
202 c
->module_auto_unload_event
= NULL
;
205 if (c
->module_defer_unload_event
) {
206 c
->mainloop
->defer_free(c
->module_defer_unload_event
);
207 c
->module_defer_unload_event
= NULL
;
211 static int unused_callback(void *p
, PA_GCC_UNUSED
uint32_t idx
, int *del
, void *userdata
) {
213 time_t *now
= userdata
;
219 if (m
->n_used
== 0 && m
->auto_unload
&& m
->last_used_time
+m
->core
->module_idle_time
<= *now
) {
227 void pa_module_unload_unused(pa_core
*c
) {
235 pa_idxset_foreach(c
->modules
, unused_callback
, &now
);
238 static int unload_callback(void *p
, PA_GCC_UNUSED
uint32_t idx
, int *del
, PA_GCC_UNUSED
void *userdata
) {
242 if (m
->unload_requested
) {
250 static void defer_cb(pa_mainloop_api
*api
, pa_defer_event
*e
, void *userdata
) {
251 pa_core
*core
= PA_CORE(userdata
);
253 pa_core_assert_ref(core
);
254 api
->defer_enable(e
, 0);
259 pa_idxset_foreach(core
->modules
, unload_callback
, NULL
);
262 void pa_module_unload_request(pa_module
*m
) {
265 m
->unload_requested
= 1;
267 if (!m
->core
->module_defer_unload_event
)
268 m
->core
->module_defer_unload_event
= m
->core
->mainloop
->defer_new(m
->core
->mainloop
, defer_cb
, m
->core
);
270 m
->core
->mainloop
->defer_enable(m
->core
->module_defer_unload_event
, 1);
273 void pa_module_set_used(pa_module
*m
, int used
) {
276 if (m
->n_used
!= used
)
277 pa_subscription_post(m
->core
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_CHANGE
, m
->index
);
279 if (used
== 0 && m
->n_used
> 0)
280 time(&m
->last_used_time
);
285 pa_modinfo
*pa_module_get_info(pa_module
*m
) {
288 return pa_modinfo_get_by_handle(m
->dl
, m
->name
);