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