4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <polypcore/xmalloc.h>
34 #include <polypcore/core-subscribe.h>
35 #include <polypcore/log.h>
36 #include <polypcore/util.h>
40 #define PA_SYMBOL_INIT "pa__init"
41 #define PA_SYMBOL_DONE "pa__done"
43 #define UNLOAD_POLL_TIME 2
45 static void timeout_callback(pa_mainloop_api
*m
, pa_time_event
*e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
46 pa_core
*c
= userdata
;
48 assert(c
&& c
->mainloop
== m
&& c
->module_auto_unload_event
== e
);
50 pa_module_unload_unused(c
);
52 pa_gettimeofday(&ntv
);
53 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
54 m
->time_restart(e
, &ntv
);
57 pa_module
* pa_module_load(pa_core
*c
, const char *name
, const char *argument
) {
63 if (c
->disallow_module_loading
)
66 m
= pa_xmalloc(sizeof(pa_module
));
68 m
->name
= pa_xstrdup(name
);
69 m
->argument
= pa_xstrdup(argument
);
71 if (!(m
->dl
= lt_dlopenext(name
))) {
72 pa_log(__FILE__
": Failed to open module \"%s\": %s\n", name
, lt_dlerror());
76 if (!(m
->init
= (int (*)(pa_core
*_c
, pa_module
*_m
)) lt_dlsym(m
->dl
, PA_SYMBOL_INIT
))) {
77 pa_log(__FILE__
": Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT
"\" not found.\n", name
);
81 if (!(m
->done
= (void (*)(pa_core
*_c
, pa_module
*_m
)) lt_dlsym(m
->dl
, PA_SYMBOL_DONE
))) {
82 pa_log(__FILE__
": Failed to load module \"%s\": symbol \""PA_SYMBOL_DONE
"\" not found.\n", name
);
90 m
->unload_requested
= 0;
93 if (m
->init(c
, m
) < 0) {
94 pa_log_error(__FILE__
": Failed to load module \"%s\" (argument: \"%s\"): initialization failed.\n", name
, argument
? argument
: "");
99 c
->modules
= pa_idxset_new(NULL
, NULL
);
101 if (!c
->module_auto_unload_event
) {
103 pa_gettimeofday(&ntv
);
104 ntv
.tv_sec
+= UNLOAD_POLL_TIME
;
105 c
->module_auto_unload_event
= c
->mainloop
->time_new(c
->mainloop
, &ntv
, timeout_callback
, c
);
107 assert(c
->module_auto_unload_event
);
110 r
= pa_idxset_put(c
->modules
, m
, &m
->index
);
111 assert(r
>= 0 && m
->index
!= PA_IDXSET_INVALID
);
113 pa_log_info(__FILE__
": Loaded \"%s\" (index: #%u; argument: \"%s\").\n", m
->name
, m
->index
, m
->argument
? m
->argument
: "");
115 pa_subscription_post(c
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_NEW
, m
->index
);
122 pa_xfree(m
->argument
);
134 static void pa_module_free(pa_module
*m
) {
135 assert(m
&& m
->done
&& m
->core
);
137 if (m
->core
->disallow_module_loading
)
140 pa_log_info(__FILE__
": Unloading \"%s\" (index: #%u).\n", m
->name
, m
->index
);
146 pa_log_info(__FILE__
": Unloaded \"%s\" (index: #%u).\n", m
->name
, m
->index
);
148 pa_subscription_post(m
->core
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_REMOVE
, m
->index
);
151 pa_xfree(m
->argument
);
155 void pa_module_unload(pa_core
*c
, pa_module
*m
) {
159 if (!(m
= pa_idxset_remove_by_data(c
->modules
, m
, NULL
)))
165 void pa_module_unload_by_index(pa_core
*c
, uint32_t idx
) {
167 assert(c
&& idx
!= PA_IDXSET_INVALID
);
170 if (!(m
= pa_idxset_remove_by_index(c
->modules
, idx
)))
176 static void free_callback(void *p
, PA_GCC_UNUSED
void *userdata
) {
182 void pa_module_unload_all(pa_core
*c
) {
188 pa_idxset_free(c
->modules
, free_callback
, NULL
);
191 if (c
->module_auto_unload_event
) {
192 c
->mainloop
->time_free(c
->module_auto_unload_event
);
193 c
->module_auto_unload_event
= NULL
;
196 if (c
->module_defer_unload_event
) {
197 c
->mainloop
->defer_free(c
->module_defer_unload_event
);
198 c
->module_defer_unload_event
= NULL
;
202 static int unused_callback(void *p
, PA_GCC_UNUSED
uint32_t idx
, int *del
, void *userdata
) {
204 time_t *now
= userdata
;
205 assert(p
&& del
&& now
);
207 if (m
->n_used
== 0 && m
->auto_unload
&& m
->last_used_time
+m
->core
->module_idle_time
<= *now
) {
215 void pa_module_unload_unused(pa_core
*c
) {
223 pa_idxset_foreach(c
->modules
, unused_callback
, &now
);
226 static int unload_callback(void *p
, PA_GCC_UNUSED
uint32_t idx
, int *del
, PA_GCC_UNUSED
void *userdata
) {
230 if (m
->unload_requested
) {
238 static void defer_cb(pa_mainloop_api
*api
, pa_defer_event
*e
, void *userdata
) {
239 pa_core
*core
= userdata
;
240 api
->defer_enable(e
, 0);
245 pa_idxset_foreach(core
->modules
, unload_callback
, NULL
);
249 void pa_module_unload_request(pa_module
*m
) {
252 m
->unload_requested
= 1;
254 if (!m
->core
->module_defer_unload_event
)
255 m
->core
->module_defer_unload_event
= m
->core
->mainloop
->defer_new(m
->core
->mainloop
, defer_cb
, m
->core
);
257 m
->core
->mainloop
->defer_enable(m
->core
->module_defer_unload_event
, 1);
260 void pa_module_set_used(pa_module
*m
, int used
) {
263 if (m
->n_used
!= used
)
264 pa_subscription_post(m
->core
, PA_SUBSCRIPTION_EVENT_MODULE
|PA_SUBSCRIPTION_EVENT_CHANGE
, m
->index
);
266 if (m
->n_used
!= used
&& used
== 0)
267 time(&m
->last_used_time
);
272 pa_modinfo
*pa_module_get_info(pa_module
*m
) {
275 return pa_modinfo_get_by_handle(m
->dl
);