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