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