]> code.delx.au - pulseaudio/blob - src/polypcore/module.c
b938750c83186316f0c84c1fb52097ec81ff3899
[pulseaudio] / src / polypcore / module.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #include <polyp/timeval.h>
34 #include <polyp/xmalloc.h>
35
36 #include <polypcore/core-subscribe.h>
37 #include <polypcore/log.h>
38 #include <polypcore/core-util.h>
39
40 #include "module.h"
41
42 #define PA_SYMBOL_INIT "pa__init"
43 #define PA_SYMBOL_DONE "pa__done"
44
45 #define UNLOAD_POLL_TIME 2
46
47 /* lt_dlsym() violates ISO C, so confide the breakage into this function to
48 * avoid warnings. */
49 typedef void (*fnptr)(void);
50 static inline fnptr lt_dlsym_fn(lt_dlhandle handle, const char *symbol) {
51 return (fnptr) (long) lt_dlsym(handle, symbol);
52 }
53
54 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
55 pa_core *c = userdata;
56 struct timeval ntv;
57 assert(c && c->mainloop == m && c->module_auto_unload_event == e);
58
59 pa_module_unload_unused(c);
60
61 pa_gettimeofday(&ntv);
62 ntv.tv_sec += UNLOAD_POLL_TIME;
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 int r;
69
70 assert(c && name);
71
72 if (c->disallow_module_loading)
73 goto fail;
74
75 m = pa_xmalloc(sizeof(pa_module));
76
77 m->name = pa_xstrdup(name);
78 m->argument = pa_xstrdup(argument);
79
80 if (!(m->dl = lt_dlopenext(name))) {
81 pa_log(__FILE__": Failed to open module \"%s\": %s", name, lt_dlerror());
82 goto fail;
83 }
84
85 if (!(m->init = (int (*)(pa_core *_c, pa_module*_m)) lt_dlsym_fn(m->dl, PA_SYMBOL_INIT))) {
86 pa_log(__FILE__": Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT"\" not found.", name);
87 goto fail;
88 }
89
90 if (!(m->done = (void (*)(pa_core *_c, pa_module*_m)) lt_dlsym_fn(m->dl, PA_SYMBOL_DONE))) {
91 pa_log(__FILE__": Failed to load module \"%s\": symbol \""PA_SYMBOL_DONE"\" not found.", name);
92 goto fail;
93 }
94
95 m->userdata = NULL;
96 m->core = c;
97 m->n_used = -1;
98 m->auto_unload = 0;
99 m->unload_requested = 0;
100
101 assert(m->init);
102 if (m->init(c, m) < 0) {
103 pa_log_error(__FILE__": Failed to load module \"%s\" (argument: \"%s\"): initialization failed.", name, argument ? argument : "");
104 goto fail;
105 }
106
107 if (!c->modules)
108 c->modules = pa_idxset_new(NULL, NULL);
109
110 if (!c->module_auto_unload_event) {
111 struct timeval ntv;
112 pa_gettimeofday(&ntv);
113 ntv.tv_sec += UNLOAD_POLL_TIME;
114 c->module_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
115 }
116 assert(c->module_auto_unload_event);
117
118 assert(c->modules);
119 r = pa_idxset_put(c->modules, m, &m->index);
120 assert(r >= 0 && m->index != PA_IDXSET_INVALID);
121
122 pa_log_info(__FILE__": Loaded \"%s\" (index: #%u; argument: \"%s\").", m->name, m->index, m->argument ? m->argument : "");
123
124 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
125
126 return m;
127
128 fail:
129
130 if (m) {
131 pa_xfree(m->argument);
132 pa_xfree(m->name);
133
134 if (m->dl)
135 lt_dlclose(m->dl);
136
137 pa_xfree(m);
138 }
139
140 return NULL;
141 }
142
143 static void pa_module_free(pa_module *m) {
144 assert(m && m->done && m->core);
145
146 if (m->core->disallow_module_loading)
147 return;
148
149 pa_log_info(__FILE__": Unloading \"%s\" (index: #%u).", m->name, m->index);
150
151 m->done(m->core, m);
152
153 lt_dlclose(m->dl);
154
155 pa_log_info(__FILE__": Unloaded \"%s\" (index: #%u).", m->name, m->index);
156
157 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE, m->index);
158
159 pa_xfree(m->name);
160 pa_xfree(m->argument);
161 pa_xfree(m);
162 }
163
164 void pa_module_unload(pa_core *c, pa_module *m) {
165 assert(c && m);
166
167 assert(c->modules);
168 if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
169 return;
170
171 pa_module_free(m);
172 }
173
174 void pa_module_unload_by_index(pa_core *c, uint32_t idx) {
175 pa_module *m;
176 assert(c && idx != PA_IDXSET_INVALID);
177
178 assert(c->modules);
179 if (!(m = pa_idxset_remove_by_index(c->modules, idx)))
180 return;
181
182 pa_module_free(m);
183 }
184
185 static void free_callback(void *p, PA_GCC_UNUSED void *userdata) {
186 pa_module *m = p;
187 assert(m);
188 pa_module_free(m);
189 }
190
191 void pa_module_unload_all(pa_core *c) {
192 assert(c);
193
194 if (!c->modules)
195 return;
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 assert(p && del && now);
215
216 if (m->n_used == 0 && m->auto_unload && m->last_used_time+m->core->module_idle_time <= *now) {
217 pa_module_free(m);
218 *del = 1;
219 }
220
221 return 0;
222 }
223
224 void pa_module_unload_unused(pa_core *c) {
225 time_t now;
226 assert(c);
227
228 if (!c->modules)
229 return;
230
231 time(&now);
232 pa_idxset_foreach(c->modules, unused_callback, &now);
233 }
234
235 static int unload_callback(void *p, PA_GCC_UNUSED uint32_t idx, int *del, PA_GCC_UNUSED void *userdata) {
236 pa_module *m = p;
237 assert(m);
238
239 if (m->unload_requested) {
240 pa_module_free(m);
241 *del = 1;
242 }
243
244 return 0;
245 }
246
247 static void defer_cb(pa_mainloop_api*api, pa_defer_event *e, void *userdata) {
248 pa_core *core = userdata;
249 api->defer_enable(e, 0);
250
251 if (!core->modules)
252 return;
253
254 pa_idxset_foreach(core->modules, unload_callback, NULL);
255
256 }
257
258 void pa_module_unload_request(pa_module *m) {
259 assert(m);
260
261 m->unload_requested = 1;
262
263 if (!m->core->module_defer_unload_event)
264 m->core->module_defer_unload_event = m->core->mainloop->defer_new(m->core->mainloop, defer_cb, m->core);
265
266 m->core->mainloop->defer_enable(m->core->module_defer_unload_event, 1);
267 }
268
269 void pa_module_set_used(pa_module*m, int used) {
270 assert(m);
271
272 if (m->n_used != used)
273 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_CHANGE, m->index);
274
275 if (m->n_used != used && used == 0)
276 time(&m->last_used_time);
277
278 m->n_used = used;
279 }
280
281 pa_modinfo *pa_module_get_info(pa_module *m) {
282 assert(m);
283
284 return pa_modinfo_get_by_handle(m->dl);
285 }