]> code.delx.au - pulseaudio/blob - src/pulsecore/module.c
modernize idxset a bit, reduce memory consumption, get rid of pa_idxset_foreach()
[pulseaudio] / src / pulsecore / module.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
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.
11
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.
16
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
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <ctype.h>
33
34 #include <pulse/timeval.h>
35 #include <pulse/xmalloc.h>
36
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>
42
43 #include "module.h"
44
45 #define PA_SYMBOL_INIT "pa__init"
46 #define PA_SYMBOL_DONE "pa__done"
47 #define PA_SYMBOL_LOAD_ONCE "pa__load_once"
48
49 #define UNLOAD_POLL_TIME 2
50
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);
53 struct timeval ntv;
54
55 pa_core_assert_ref(c);
56 pa_assert(c->mainloop == m);
57 pa_assert(c->module_auto_unload_event == e);
58
59 pa_module_unload_unused(c);
60
61 pa_gettimeofday(&ntv);
62 pa_timeval_add(&ntv, UNLOAD_POLL_TIME*1000000);
63 m->time_restart(e, &ntv);
64 }
65
66 pa_module* pa_module_load(pa_core *c, const char *name, const char *argument) {
67 pa_module *m = NULL;
68 pa_bool_t (*load_once)(void);
69
70 pa_assert(c);
71 pa_assert(name);
72
73 if (c->disallow_module_loading)
74 goto fail;
75
76 m = pa_xnew(pa_module, 1);
77 m->name = pa_xstrdup(name);
78 m->argument = pa_xstrdup(argument);
79
80 if (!(m->dl = lt_dlopenext(name))) {
81 pa_log("Failed to open module \"%s\": %s", name, lt_dlerror());
82 goto fail;
83 }
84
85 if ((load_once = (pa_bool_t (*)(void)) pa_load_sym(m->dl, name, PA_SYMBOL_LOAD_ONCE))) {
86
87 if (load_once() && c->modules) {
88 pa_module *i;
89 uint32_t idx;
90 /* OK, the module only wants to be loaded once, let's make sure it is */
91
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);
95 goto fail;
96 }
97 }
98 }
99 }
100
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);
103 goto fail;
104 }
105
106 m->done = (void (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_DONE);
107 m->userdata = NULL;
108 m->core = c;
109 m->n_used = -1;
110 m->auto_unload = FALSE;
111 m->unload_requested = FALSE;
112
113 if (m->init(m) < 0) {
114 pa_log_error("Failed to load module \"%s\" (argument: \"%s\"): initialization failed.", name, argument ? argument : "");
115 goto fail;
116 }
117
118 if (!c->modules)
119 c->modules = pa_idxset_new(NULL, NULL);
120
121 if (m->auto_unload && !c->module_auto_unload_event) {
122 struct timeval ntv;
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);
126 }
127
128 pa_assert_se(pa_idxset_put(c->modules, m, &m->index) >= 0);
129 pa_assert(m->index != PA_IDXSET_INVALID);
130
131 pa_log_info("Loaded \"%s\" (index: #%u; argument: \"%s\").", m->name, m->index, m->argument ? m->argument : "");
132
133 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
134
135 return m;
136
137 fail:
138
139 if (m) {
140 pa_xfree(m->argument);
141 pa_xfree(m->name);
142
143 if (m->dl)
144 lt_dlclose(m->dl);
145
146 pa_xfree(m);
147 }
148
149 return NULL;
150 }
151
152 static void pa_module_free(pa_module *m) {
153 pa_assert(m);
154 pa_assert(m->core);
155
156 if (m->core->disallow_module_loading)
157 return;
158
159 pa_log_info("Unloading \"%s\" (index: #%u).", m->name, m->index);
160
161 if (m->done)
162 m->done(m);
163
164 lt_dlclose(m->dl);
165
166 pa_log_info("Unloaded \"%s\" (index: #%u).", m->name, m->index);
167
168 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE, m->index);
169
170 pa_xfree(m->name);
171 pa_xfree(m->argument);
172 pa_xfree(m);
173 }
174
175 void pa_module_unload(pa_core *c, pa_module *m) {
176 pa_assert(c);
177 pa_assert(m);
178
179 pa_assert(c->modules);
180 if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
181 return;
182
183 pa_module_free(m);
184 }
185
186 void pa_module_unload_by_index(pa_core *c, uint32_t idx) {
187 pa_module *m;
188 pa_assert(c);
189 pa_assert(idx != PA_IDXSET_INVALID);
190
191 if (!(m = pa_idxset_remove_by_index(c->modules, idx)))
192 return;
193
194 pa_module_free(m);
195 }
196
197 void pa_module_unload_all(pa_core *c) {
198
199 pa_assert(c);
200
201 if (c->modules) {
202 pa_module *m;
203
204 while ((m = pa_idxset_steal_first(c->modules, NULL)))
205 pa_module_free(m);
206
207 pa_idxset_free(c->modules, NULL, NULL);
208 c->modules = NULL;
209 }
210
211 if (c->module_auto_unload_event) {
212 c->mainloop->time_free(c->module_auto_unload_event);
213 c->module_auto_unload_event = NULL;
214 }
215
216 if (c->module_defer_unload_event) {
217 c->mainloop->defer_free(c->module_defer_unload_event);
218 c->module_defer_unload_event = NULL;
219 }
220 }
221
222 void pa_module_unload_unused(pa_core *c) {
223 void *state = NULL;
224 time_t now;
225 pa_module *m;
226
227 pa_assert(c);
228
229 if (!c->modules)
230 return;
231
232 time(&now);
233
234 while ((m = pa_idxset_iterate(c->modules, &state, NULL))) {
235
236 if (m->n_used > 0)
237 continue;
238
239 if (!m->auto_unload)
240 continue;
241
242 if (m->last_used_time + m->core->module_idle_time > now)
243 continue;
244
245 pa_module_unload(c, m);
246 }
247 }
248
249 static void defer_cb(pa_mainloop_api*api, pa_defer_event *e, void *userdata) {
250 void *state = NULL;
251 pa_core *c = PA_CORE(userdata);
252 pa_module *m;
253
254 pa_core_assert_ref(c);
255 api->defer_enable(e, 0);
256
257 if (!c->modules)
258 return;
259
260 while ((m = pa_idxset_iterate(c->modules, &state, NULL)))
261 if (m->unload_requested)
262 pa_module_unload(c, m);
263 }
264
265 void pa_module_unload_request(pa_module *m) {
266 pa_assert(m);
267
268 m->unload_requested = TRUE;
269
270 if (!m->core->module_defer_unload_event)
271 m->core->module_defer_unload_event = m->core->mainloop->defer_new(m->core->mainloop, defer_cb, m->core);
272
273 m->core->mainloop->defer_enable(m->core->module_defer_unload_event, 1);
274 }
275
276 void pa_module_set_used(pa_module*m, int used) {
277 pa_assert(m);
278
279 if (m->n_used != used)
280 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_CHANGE, m->index);
281
282 if (used == 0 && m->n_used > 0)
283 time(&m->last_used_time);
284
285 m->n_used = used;
286 }
287
288 pa_modinfo *pa_module_get_info(pa_module *m) {
289 pa_assert(m);
290
291 return pa_modinfo_get_by_handle(m->dl, m->name);
292 }