]> code.delx.au - pulseaudio/blob - polyp/module.c
499ea2990a78d0a3f3dce680d2245b02a5011c98
[pulseaudio] / polyp / 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 "module.h"
34 #include "xmalloc.h"
35 #include "subscribe.h"
36 #include "log.h"
37 #include "util.h"
38
39 #define PA_SYMBOL_INIT "pa__init"
40 #define PA_SYMBOL_DONE "pa__done"
41
42 #define UNLOAD_POLL_TIME 2
43
44 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
45 pa_core *c = userdata;
46 struct timeval ntv;
47 assert(c && c->mainloop == m && c->module_auto_unload_event == e);
48
49 pa_module_unload_unused(c);
50
51 pa_gettimeofday(&ntv);
52 ntv.tv_sec += UNLOAD_POLL_TIME;
53 m->time_restart(e, &ntv);
54 }
55
56 pa_module* pa_module_load(pa_core *c, const char *name, const char *argument) {
57 pa_module *m = NULL;
58 int r;
59
60 assert(c && name);
61
62 if (c->disallow_module_loading)
63 goto fail;
64
65 m = pa_xmalloc(sizeof(pa_module));
66
67 m->name = pa_xstrdup(name);
68 m->argument = pa_xstrdup(argument);
69
70 if (!(m->dl = lt_dlopenext(name))) {
71 pa_log(__FILE__": Failed to open module \"%s\": %s\n", name, lt_dlerror());
72 goto fail;
73 }
74
75 if (!(m->init = (int (*)(pa_core *_c, pa_module*_m)) lt_dlsym(m->dl, PA_SYMBOL_INIT))) {
76 pa_log(__FILE__": Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT"\" not found.\n", name);
77 goto fail;
78 }
79
80 if (!(m->done = (void (*)(pa_core *_c, pa_module*_m)) lt_dlsym(m->dl, PA_SYMBOL_DONE))) {
81 pa_log(__FILE__": Failed to load module \"%s\": symbol \""PA_SYMBOL_DONE"\" not found.\n", name);
82 goto fail;
83 }
84
85 m->userdata = NULL;
86 m->core = c;
87 m->n_used = -1;
88 m->auto_unload = 0;
89 m->unload_requested = 0;
90
91 assert(m->init);
92 if (m->init(c, m) < 0) {
93 pa_log_error(__FILE__": Failed to load module \"%s\" (argument: \"%s\"): initialization failed.\n", name, argument ? argument : "");
94 goto fail;
95 }
96
97 if (!c->modules)
98 c->modules = pa_idxset_new(NULL, NULL);
99
100 if (!c->module_auto_unload_event) {
101 struct timeval ntv;
102 pa_gettimeofday(&ntv);
103 ntv.tv_sec += UNLOAD_POLL_TIME;
104 c->module_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
105 }
106 assert(c->module_auto_unload_event);
107
108 assert(c->modules);
109 r = pa_idxset_put(c->modules, m, &m->index);
110 assert(r >= 0 && m->index != PA_IDXSET_INVALID);
111
112 pa_log_info(__FILE__": Loaded \"%s\" (index: #%u; argument: \"%s\").\n", m->name, m->index, m->argument ? m->argument : "");
113
114 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
115
116 return m;
117
118 fail:
119
120 if (m) {
121 pa_xfree(m->argument);
122 pa_xfree(m->name);
123
124 if (m->dl)
125 lt_dlclose(m->dl);
126
127 pa_xfree(m);
128 }
129
130 return NULL;
131 }
132
133 static void pa_module_free(pa_module *m) {
134 assert(m && m->done && m->core);
135
136 if (m->core->disallow_module_loading)
137 return;
138
139 pa_log_info(__FILE__": Unloading \"%s\" (index: #%u).\n", m->name, m->index);
140
141 m->done(m->core, m);
142
143 lt_dlclose(m->dl);
144
145 pa_log_info(__FILE__": Unloaded \"%s\" (index: #%u).\n", m->name, m->index);
146
147 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE, m->index);
148
149 pa_xfree(m->name);
150 pa_xfree(m->argument);
151 pa_xfree(m);
152 }
153
154 void pa_module_unload(pa_core *c, pa_module *m) {
155 assert(c && m);
156
157 assert(c->modules);
158 if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
159 return;
160
161 pa_module_free(m);
162 }
163
164 void pa_module_unload_by_index(pa_core *c, uint32_t idx) {
165 pa_module *m;
166 assert(c && idx != PA_IDXSET_INVALID);
167
168 assert(c->modules);
169 if (!(m = pa_idxset_remove_by_index(c->modules, idx)))
170 return;
171
172 pa_module_free(m);
173 }
174
175 static void free_callback(void *p, PA_GCC_UNUSED void *userdata) {
176 pa_module *m = p;
177 assert(m);
178 pa_module_free(m);
179 }
180
181 void pa_module_unload_all(pa_core *c) {
182 assert(c);
183
184 if (!c->modules)
185 return;
186
187 pa_idxset_free(c->modules, free_callback, NULL);
188 c->modules = NULL;
189
190 if (c->module_auto_unload_event) {
191 c->mainloop->time_free(c->module_auto_unload_event);
192 c->module_auto_unload_event = NULL;
193 }
194
195 if (c->module_defer_unload_event) {
196 c->mainloop->defer_free(c->module_defer_unload_event);
197 c->module_defer_unload_event = NULL;
198 }
199 }
200
201 static int unused_callback(void *p, PA_GCC_UNUSED uint32_t idx, int *del, void *userdata) {
202 pa_module *m = p;
203 time_t *now = userdata;
204 assert(p && del && now);
205
206 if (m->n_used == 0 && m->auto_unload && m->last_used_time+m->core->module_idle_time <= *now) {
207 pa_module_free(m);
208 *del = 1;
209 }
210
211 return 0;
212 }
213
214 void pa_module_unload_unused(pa_core *c) {
215 time_t now;
216 assert(c);
217
218 if (!c->modules)
219 return;
220
221 time(&now);
222 pa_idxset_foreach(c->modules, unused_callback, &now);
223 }
224
225 static int unload_callback(void *p, PA_GCC_UNUSED uint32_t idx, int *del, PA_GCC_UNUSED void *userdata) {
226 pa_module *m = p;
227 assert(m);
228
229 if (m->unload_requested) {
230 pa_module_free(m);
231 *del = 1;
232 }
233
234 return 0;
235 }
236
237 static void defer_cb(pa_mainloop_api*api, pa_defer_event *e, void *userdata) {
238 pa_core *core = userdata;
239 api->defer_enable(e, 0);
240
241 if (!core->modules)
242 return;
243
244 pa_idxset_foreach(core->modules, unload_callback, NULL);
245
246 }
247
248 void pa_module_unload_request(pa_module *m) {
249 assert(m);
250
251 m->unload_requested = 1;
252
253 if (!m->core->module_defer_unload_event)
254 m->core->module_defer_unload_event = m->core->mainloop->defer_new(m->core->mainloop, defer_cb, m->core);
255
256 m->core->mainloop->defer_enable(m->core->module_defer_unload_event, 1);
257 }
258
259 void pa_module_set_used(pa_module*m, int used) {
260 assert(m);
261
262 if (m->n_used != used)
263 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_CHANGE, m->index);
264
265 if (m->n_used != used && used == 0)
266 time(&m->last_used_time);
267
268 m->n_used = used;
269 }
270
271 pa_modinfo *pa_module_get_info(pa_module *m) {
272 assert(m);
273
274 return pa_modinfo_get_by_handle(m->dl);
275 }