]> code.delx.au - pulseaudio/blob - polyp/mainloop.c
introduce pa_xmalloc() and friends
[pulseaudio] / polyp / mainloop.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 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 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 <stdio.h>
27 #include <signal.h>
28 #include <unistd.h>
29 #include <sys/poll.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <fcntl.h>
34 #include <errno.h>
35
36 #include "mainloop.h"
37 #include "util.h"
38 #include "idxset.h"
39 #include "xmalloc.h"
40
41 struct mainloop_source_header {
42 struct pa_mainloop *mainloop;
43 int dead;
44 };
45
46 struct mainloop_source_io {
47 struct mainloop_source_header header;
48
49 int fd;
50 enum pa_mainloop_api_io_events events;
51 void (*callback) (struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata);
52 void *userdata;
53
54 struct pollfd *pollfd;
55 };
56
57 struct mainloop_source_fixed_or_idle {
58 struct mainloop_source_header header;
59 int enabled;
60
61 void (*callback)(struct pa_mainloop_api*a, void *id, void *userdata);
62 void *userdata;
63 };
64
65 struct mainloop_source_time {
66 struct mainloop_source_header header;
67 int enabled;
68
69 struct timeval timeval;
70 void (*callback)(struct pa_mainloop_api*a, void *id, const struct timeval*tv, void *userdata);
71 void *userdata;
72 };
73
74 struct pa_mainloop {
75 struct pa_idxset *io_sources, *fixed_sources, *idle_sources, *time_sources;
76 int io_sources_scan_dead, fixed_sources_scan_dead, idle_sources_scan_dead, time_sources_scan_dead;
77
78 struct pollfd *pollfds;
79 unsigned max_pollfds, n_pollfds;
80 int rebuild_pollfds;
81
82 int quit, running, retval;
83 struct pa_mainloop_api api;
84 };
85
86 static void setup_api(struct pa_mainloop *m);
87
88 struct pa_mainloop *pa_mainloop_new(void) {
89 struct pa_mainloop *m;
90
91 m = pa_xmalloc(sizeof(struct pa_mainloop));
92
93 m->io_sources = pa_idxset_new(NULL, NULL);
94 m->fixed_sources = pa_idxset_new(NULL, NULL);
95 m->idle_sources = pa_idxset_new(NULL, NULL);
96 m->time_sources = pa_idxset_new(NULL, NULL);
97
98 assert(m->io_sources && m->fixed_sources && m->idle_sources && m->time_sources);
99
100 m->io_sources_scan_dead = m->fixed_sources_scan_dead = m->idle_sources_scan_dead = m->time_sources_scan_dead = 0;
101
102 m->pollfds = NULL;
103 m->max_pollfds = m->n_pollfds = m->rebuild_pollfds = 0;
104
105 m->quit = m->running = m->retval = 0;
106
107 setup_api(m);
108
109 return m;
110 }
111
112 static int foreach(void *p, uint32_t index, int *del, void*userdata) {
113 struct mainloop_source_header *h = p;
114 int *all = userdata;
115 assert(p && del && all);
116
117 if (*all || h->dead) {
118 pa_xfree(h);
119 *del = 1;
120 }
121
122 return 0;
123 };
124
125 void pa_mainloop_free(struct pa_mainloop* m) {
126 int all = 1;
127 assert(m);
128 pa_idxset_foreach(m->io_sources, foreach, &all);
129 pa_idxset_foreach(m->fixed_sources, foreach, &all);
130 pa_idxset_foreach(m->idle_sources, foreach, &all);
131 pa_idxset_foreach(m->time_sources, foreach, &all);
132
133 pa_idxset_free(m->io_sources, NULL, NULL);
134 pa_idxset_free(m->fixed_sources, NULL, NULL);
135 pa_idxset_free(m->idle_sources, NULL, NULL);
136 pa_idxset_free(m->time_sources, NULL, NULL);
137
138 pa_xfree(m->pollfds);
139 pa_xfree(m);
140 }
141
142 static void scan_dead(struct pa_mainloop *m) {
143 int all = 0;
144 assert(m);
145 if (m->io_sources_scan_dead)
146 pa_idxset_foreach(m->io_sources, foreach, &all);
147 if (m->fixed_sources_scan_dead)
148 pa_idxset_foreach(m->fixed_sources, foreach, &all);
149 if (m->idle_sources_scan_dead)
150 pa_idxset_foreach(m->idle_sources, foreach, &all);
151 if (m->time_sources_scan_dead)
152 pa_idxset_foreach(m->time_sources, foreach, &all);
153 }
154
155 static void rebuild_pollfds(struct pa_mainloop *m) {
156 struct mainloop_source_io*s;
157 struct pollfd *p;
158 uint32_t index = PA_IDXSET_INVALID;
159 unsigned l;
160
161 l = pa_idxset_ncontents(m->io_sources);
162 if (m->max_pollfds < l) {
163 m->pollfds = pa_xrealloc(m->pollfds, sizeof(struct pollfd)*l);
164 m->max_pollfds = l;
165 }
166
167 m->n_pollfds = 0;
168 p = m->pollfds;
169 for (s = pa_idxset_first(m->io_sources, &index); s; s = pa_idxset_next(m->io_sources, &index)) {
170 if (s->header.dead) {
171 s->pollfd = NULL;
172 continue;
173 }
174
175 s->pollfd = p;
176 p->fd = s->fd;
177 p->events = ((s->events & PA_MAINLOOP_API_IO_EVENT_INPUT) ? POLLIN : 0) | ((s->events & PA_MAINLOOP_API_IO_EVENT_OUTPUT) ? POLLOUT : 0);
178 p->revents = 0;
179
180 p++;
181 m->n_pollfds++;
182 }
183 }
184
185 static void dispatch_pollfds(struct pa_mainloop *m) {
186 uint32_t index = PA_IDXSET_INVALID;
187 struct mainloop_source_io *s;
188
189 for (s = pa_idxset_first(m->io_sources, &index); s; s = pa_idxset_next(m->io_sources, &index)) {
190 if (s->header.dead || !s->pollfd || !s->pollfd->revents)
191 continue;
192
193 assert(s->pollfd->fd == s->fd && s->callback);
194 s->callback(&m->api, s, s->fd,
195 ((s->pollfd->revents & POLLHUP) ? PA_MAINLOOP_API_IO_EVENT_HUP : 0) |
196 ((s->pollfd->revents & POLLIN) ? PA_MAINLOOP_API_IO_EVENT_INPUT : 0) |
197 ((s->pollfd->revents & POLLOUT) ? PA_MAINLOOP_API_IO_EVENT_OUTPUT : 0), s->userdata);
198 s->pollfd->revents = 0;
199 }
200 }
201
202 static void run_fixed_or_idle(struct pa_mainloop *m, struct pa_idxset *i) {
203 uint32_t index = PA_IDXSET_INVALID;
204 struct mainloop_source_fixed_or_idle *s;
205
206 for (s = pa_idxset_first(i, &index); s; s = pa_idxset_next(i, &index)) {
207 if (s->header.dead || !s->enabled)
208 continue;
209
210 assert(s->callback);
211 s->callback(&m->api, s, s->userdata);
212 }
213 }
214
215 static int calc_next_timeout(struct pa_mainloop *m) {
216 uint32_t index = PA_IDXSET_INVALID;
217 struct mainloop_source_time *s;
218 struct timeval now;
219 int t = -1;
220
221 if (pa_idxset_isempty(m->time_sources))
222 return -1;
223
224 gettimeofday(&now, NULL);
225
226 for (s = pa_idxset_first(m->time_sources, &index); s; s = pa_idxset_next(m->time_sources, &index)) {
227 int tmp;
228
229 if (s->header.dead || !s->enabled)
230 continue;
231
232 if (s->timeval.tv_sec < now.tv_sec || (s->timeval.tv_sec == now.tv_sec && s->timeval.tv_usec <= now.tv_usec))
233 return 0;
234
235 tmp = (s->timeval.tv_sec - now.tv_sec)*1000;
236
237 if (s->timeval.tv_usec > now.tv_usec)
238 tmp += (s->timeval.tv_usec - now.tv_usec)/1000;
239 else
240 tmp -= (now.tv_usec - s->timeval.tv_usec)/1000;
241
242 if (tmp == 0)
243 return 0;
244 else if (t == -1 || tmp < t)
245 t = tmp;
246 }
247
248 return t;
249 }
250
251 static void dispatch_timeout(struct pa_mainloop *m) {
252 uint32_t index = PA_IDXSET_INVALID;
253 struct mainloop_source_time *s;
254 struct timeval now;
255 assert(m);
256
257 if (pa_idxset_isempty(m->time_sources))
258 return;
259
260 gettimeofday(&now, NULL);
261 for (s = pa_idxset_first(m->time_sources, &index); s; s = pa_idxset_next(m->time_sources, &index)) {
262
263 if (s->header.dead || !s->enabled)
264 continue;
265
266 if (s->timeval.tv_sec < now.tv_sec || (s->timeval.tv_sec == now.tv_sec && s->timeval.tv_usec <= now.tv_usec)) {
267 assert(s->callback);
268
269 s->enabled = 0;
270 s->callback(&m->api, s, &s->timeval, s->userdata);
271 }
272 }
273 }
274
275 static int any_idle_sources(struct pa_mainloop *m) {
276 struct mainloop_source_fixed_or_idle *s;
277 uint32_t index;
278 assert(m);
279
280 for (s = pa_idxset_first(m->idle_sources, &index); s; s = pa_idxset_next(m->idle_sources, &index))
281 if (!s->header.dead && s->enabled)
282 return 1;
283
284 return 0;
285 }
286
287 int pa_mainloop_iterate(struct pa_mainloop *m, int block, int *retval) {
288 int r, idle;
289 assert(m && !m->running);
290
291 if(m->quit) {
292 if (retval)
293 *retval = m->retval;
294 return 1;
295 }
296
297 m->running = 1;
298
299 scan_dead(m);
300 run_fixed_or_idle(m, m->fixed_sources);
301
302 if (m->rebuild_pollfds) {
303 rebuild_pollfds(m);
304 m->rebuild_pollfds = 0;
305 }
306
307 idle = any_idle_sources(m);
308
309 do {
310 int t;
311
312 if (!block || idle)
313 t = 0;
314 else
315 t = calc_next_timeout(m);
316
317 r = poll(m->pollfds, m->n_pollfds, t);
318 } while (r < 0 && errno == EINTR);
319
320 dispatch_timeout(m);
321
322 if (r > 0)
323 dispatch_pollfds(m);
324 else if (r == 0 && idle)
325 run_fixed_or_idle(m, m->idle_sources);
326 else if (r < 0)
327 fprintf(stderr, "select(): %s\n", strerror(errno));
328
329 m->running = 0;
330 return r < 0 ? -1 : 0;
331 }
332
333 int pa_mainloop_run(struct pa_mainloop *m, int *retval) {
334 int r;
335 while ((r = pa_mainloop_iterate(m, 1, retval)) == 0);
336 return r;
337 }
338
339 void pa_mainloop_quit(struct pa_mainloop *m, int r) {
340 assert(m);
341 m->quit = r;
342 }
343
344 /* IO sources */
345 static void* mainloop_source_io(struct pa_mainloop_api*a, int fd, enum pa_mainloop_api_io_events events, void (*callback) (struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata), void *userdata) {
346 struct pa_mainloop *m;
347 struct mainloop_source_io *s;
348 assert(a && a->userdata && fd >= 0 && callback);
349 m = a->userdata;
350 assert(a == &m->api);
351
352 s = pa_xmalloc(sizeof(struct mainloop_source_io));
353 s->header.mainloop = m;
354 s->header.dead = 0;
355
356 s->fd = fd;
357 s->events = events;
358 s->callback = callback;
359 s->userdata = userdata;
360 s->pollfd = NULL;
361
362 pa_idxset_put(m->io_sources, s, NULL);
363 m->rebuild_pollfds = 1;
364 return s;
365 }
366
367 static void mainloop_enable_io(struct pa_mainloop_api*a, void* id, enum pa_mainloop_api_io_events events) {
368 struct pa_mainloop *m;
369 struct mainloop_source_io *s = id;
370 assert(a && a->userdata && s && !s->header.dead);
371 m = a->userdata;
372 assert(a == &m->api && s->header.mainloop == m);
373
374 s->events = events;
375 if (s->pollfd)
376 s->pollfd->events = ((s->events & PA_MAINLOOP_API_IO_EVENT_INPUT) ? POLLIN : 0) | ((s->events & PA_MAINLOOP_API_IO_EVENT_OUTPUT) ? POLLOUT : 0);
377 }
378
379 static void mainloop_cancel_io(struct pa_mainloop_api*a, void* id) {
380 struct pa_mainloop *m;
381 struct mainloop_source_io *s = id;
382 assert(a && a->userdata && s && !s->header.dead);
383 m = a->userdata;
384 assert(a == &m->api && s->header.mainloop == m);
385
386 s->header.dead = 1;
387 m->io_sources_scan_dead = 1;
388 m->rebuild_pollfds = 1;
389 }
390
391 /* Fixed sources */
392 static void* mainloop_source_fixed(struct pa_mainloop_api*a, void (*callback) (struct pa_mainloop_api*a, void *id, void *userdata), void *userdata) {
393 struct pa_mainloop *m;
394 struct mainloop_source_fixed_or_idle *s;
395 assert(a && a->userdata && callback);
396 m = a->userdata;
397 assert(a == &m->api);
398
399 s = pa_xmalloc(sizeof(struct mainloop_source_fixed_or_idle));
400 s->header.mainloop = m;
401 s->header.dead = 0;
402
403 s->enabled = 1;
404 s->callback = callback;
405 s->userdata = userdata;
406
407 pa_idxset_put(m->fixed_sources, s, NULL);
408 return s;
409 }
410
411 static void mainloop_enable_fixed(struct pa_mainloop_api*a, void* id, int b) {
412 struct pa_mainloop *m;
413 struct mainloop_source_fixed_or_idle *s = id;
414 assert(a && a->userdata && s && !s->header.dead);
415 m = a->userdata;
416 assert(a == &m->api);
417
418 s->enabled = b;
419 }
420
421 static void mainloop_cancel_fixed(struct pa_mainloop_api*a, void* id) {
422 struct pa_mainloop *m;
423 struct mainloop_source_fixed_or_idle *s = id;
424 assert(a && a->userdata && s && !s->header.dead);
425 m = a->userdata;
426 assert(a == &m->api);
427
428 s->header.dead = 1;
429 m->fixed_sources_scan_dead = 1;
430 }
431
432 /* Idle sources */
433 static void* mainloop_source_idle(struct pa_mainloop_api*a, void (*callback) (struct pa_mainloop_api*a, void *id, void *userdata), void *userdata) {
434 struct pa_mainloop *m;
435 struct mainloop_source_fixed_or_idle *s;
436 assert(a && a->userdata && callback);
437 m = a->userdata;
438 assert(a == &m->api);
439
440 s = pa_xmalloc(sizeof(struct mainloop_source_fixed_or_idle));
441 s->header.mainloop = m;
442 s->header.dead = 0;
443
444 s->enabled = 1;
445 s->callback = callback;
446 s->userdata = userdata;
447
448 pa_idxset_put(m->idle_sources, s, NULL);
449 return s;
450 }
451
452 static void mainloop_cancel_idle(struct pa_mainloop_api*a, void* id) {
453 struct pa_mainloop *m;
454 struct mainloop_source_fixed_or_idle *s = id;
455 assert(a && a->userdata && s && !s->header.dead);
456 m = a->userdata;
457 assert(a == &m->api);
458
459 s->header.dead = 1;
460 m->idle_sources_scan_dead = 1;
461 }
462
463 /* Time sources */
464 static void* mainloop_source_time(struct pa_mainloop_api*a, const struct timeval *tv, void (*callback) (struct pa_mainloop_api*a, void *id, const struct timeval *tv, void *userdata), void *userdata) {
465 struct pa_mainloop *m;
466 struct mainloop_source_time *s;
467 assert(a && a->userdata && callback);
468 m = a->userdata;
469 assert(a == &m->api);
470
471 s = pa_xmalloc(sizeof(struct mainloop_source_time));
472 s->header.mainloop = m;
473 s->header.dead = 0;
474
475 s->enabled = !!tv;
476 if (tv)
477 s->timeval = *tv;
478
479 s->callback = callback;
480 s->userdata = userdata;
481
482 pa_idxset_put(m->time_sources, s, NULL);
483 return s;
484 }
485
486 static void mainloop_enable_time(struct pa_mainloop_api*a, void *id, const struct timeval *tv) {
487 struct pa_mainloop *m;
488 struct mainloop_source_time *s = id;
489 assert(a && a->userdata && s && !s->header.dead);
490 m = a->userdata;
491 assert(a == &m->api);
492
493 if (tv) {
494 s->enabled = 1;
495 s->timeval = *tv;
496 } else
497 s->enabled = 0;
498 }
499
500 static void mainloop_cancel_time(struct pa_mainloop_api*a, void* id) {
501 struct pa_mainloop *m;
502 struct mainloop_source_time *s = id;
503 assert(a && a->userdata && s && !s->header.dead);
504 m = a->userdata;
505 assert(a == &m->api);
506
507 s->header.dead = 1;
508 m->time_sources_scan_dead = 1;
509
510 }
511
512 static void mainloop_quit(struct pa_mainloop_api*a, int retval) {
513 struct pa_mainloop *m;
514 assert(a && a->userdata);
515 m = a->userdata;
516 assert(a == &m->api);
517
518 m->quit = 1;
519 m->retval = retval;
520 }
521
522 static void setup_api(struct pa_mainloop *m) {
523 assert(m);
524
525 m->api.userdata = m;
526 m->api.source_io = mainloop_source_io;
527 m->api.enable_io = mainloop_enable_io;
528 m->api.cancel_io = mainloop_cancel_io;
529
530 m->api.source_fixed = mainloop_source_fixed;
531 m->api.enable_fixed = mainloop_enable_fixed;
532 m->api.cancel_fixed = mainloop_cancel_fixed;
533
534 m->api.source_idle = mainloop_source_idle;
535 m->api.enable_idle = mainloop_enable_fixed; /* (!) */
536 m->api.cancel_idle = mainloop_cancel_idle;
537
538 m->api.source_time = mainloop_source_time;
539 m->api.enable_time = mainloop_enable_time;
540 m->api.cancel_time = mainloop_cancel_time;
541
542 m->api.quit = mainloop_quit;
543 }
544
545 struct pa_mainloop_api* pa_mainloop_get_api(struct pa_mainloop*m) {
546 assert(m);
547 return &m->api;
548 }
549