]> code.delx.au - pulseaudio/blob - polyp/mainloop.c
Make the whole stuff LGPL only
[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 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 <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 #include "log.h"
41
42 struct pa_io_event {
43 struct pa_mainloop *mainloop;
44 int dead;
45 int fd;
46 enum pa_io_event_flags events;
47 void (*callback) (struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata);
48 struct pollfd *pollfd;
49 void *userdata;
50 void (*destroy_callback) (struct pa_mainloop_api*a, struct pa_io_event *e, void *userdata);
51 };
52
53 struct pa_time_event {
54 struct pa_mainloop *mainloop;
55 int dead;
56 int enabled;
57 struct timeval timeval;
58 void (*callback)(struct pa_mainloop_api*a, struct pa_time_event *e, const struct timeval*tv, void *userdata);
59 void *userdata;
60 void (*destroy_callback) (struct pa_mainloop_api*a, struct pa_time_event *e, void *userdata);
61 };
62
63 struct pa_defer_event {
64 struct pa_mainloop *mainloop;
65 int dead;
66 int enabled;
67 void (*callback)(struct pa_mainloop_api*a, struct pa_defer_event*e, void *userdata);
68 void *userdata;
69 void (*destroy_callback) (struct pa_mainloop_api*a, struct pa_defer_event *e, void *userdata);
70 };
71
72 struct pa_mainloop {
73 struct pa_idxset *io_events, *time_events, *defer_events;
74 int io_events_scan_dead, defer_events_scan_dead, time_events_scan_dead;
75
76 struct pollfd *pollfds;
77 unsigned max_pollfds, n_pollfds;
78 int rebuild_pollfds;
79
80 int quit, running, retval;
81 struct pa_mainloop_api api;
82
83 int deferred_pending;
84 };
85
86 /* IO events */
87 static struct pa_io_event* mainloop_io_new(struct pa_mainloop_api*a, int fd, enum pa_io_event_flags events, void (*callback) (struct pa_mainloop_api*a, struct pa_io_event *e, int fd, enum pa_io_event_flags events, void *userdata), void *userdata) {
88 struct pa_mainloop *m;
89 struct pa_io_event *e;
90
91 assert(a && a->userdata && fd >= 0 && callback);
92 m = a->userdata;
93 assert(a == &m->api);
94
95 e = pa_xmalloc(sizeof(struct pa_io_event));
96 e->mainloop = m;
97 e->dead = 0;
98
99 e->fd = fd;
100 e->events = events;
101 e->callback = callback;
102 e->userdata = userdata;
103 e->destroy_callback = NULL;
104 e->pollfd = NULL;
105
106 pa_idxset_put(m->io_events, e, NULL);
107 m->rebuild_pollfds = 1;
108 return e;
109 }
110
111 static void mainloop_io_enable(struct pa_io_event *e, enum pa_io_event_flags events) {
112 assert(e && e->mainloop);
113
114 e->events = events;
115 if (e->pollfd)
116 e->pollfd->events =
117 (events & PA_IO_EVENT_INPUT ? POLLIN : 0) |
118 (events & PA_IO_EVENT_OUTPUT ? POLLOUT : 0) |
119 POLLHUP |
120 POLLERR;
121 }
122
123 static void mainloop_io_free(struct pa_io_event *e) {
124 assert(e && e->mainloop);
125 e->dead = e->mainloop->io_events_scan_dead = e->mainloop->rebuild_pollfds = 1;
126 }
127
128 static void mainloop_io_set_destroy(struct pa_io_event *e, void (*callback)(struct pa_mainloop_api*a, struct pa_io_event *e, void *userdata)) {
129 assert(e);
130 e->destroy_callback = callback;
131 }
132
133 /* Defer events */
134 struct pa_defer_event* mainloop_defer_new(struct pa_mainloop_api*a, void (*callback) (struct pa_mainloop_api*a, struct pa_defer_event *e, void *userdata), void *userdata) {
135 struct pa_mainloop *m;
136 struct pa_defer_event *e;
137
138 assert(a && a->userdata && callback);
139 m = a->userdata;
140 assert(a == &m->api);
141
142 e = pa_xmalloc(sizeof(struct pa_defer_event));
143 e->mainloop = m;
144 e->dead = 0;
145
146 e->enabled = 1;
147 e->callback = callback;
148 e->userdata = userdata;
149 e->destroy_callback = NULL;
150
151 pa_idxset_put(m->defer_events, e, NULL);
152
153 m->deferred_pending++;
154 return e;
155 }
156
157 static void mainloop_defer_enable(struct pa_defer_event *e, int b) {
158 assert(e);
159
160 if (e->enabled && !b) {
161 assert(e->mainloop->deferred_pending > 0);
162 e->mainloop->deferred_pending--;
163 } else if (!e->enabled && b)
164 e->mainloop->deferred_pending++;
165
166 e->enabled = b;
167 }
168
169 static void mainloop_defer_free(struct pa_defer_event *e) {
170 assert(e);
171 e->dead = e->mainloop->defer_events_scan_dead = 1;
172
173 if (e->enabled) {
174 e->enabled = 0;
175 assert(e->mainloop->deferred_pending > 0);
176 e->mainloop->deferred_pending--;
177 }
178 }
179
180 static void mainloop_defer_set_destroy(struct pa_defer_event *e, void (*callback)(struct pa_mainloop_api*a, struct pa_defer_event *e, void *userdata)) {
181 assert(e);
182 e->destroy_callback = callback;
183 }
184
185 /* Time events */
186 static struct pa_time_event* mainloop_time_new(struct pa_mainloop_api*a, const struct timeval *tv, void (*callback) (struct pa_mainloop_api*a, struct pa_time_event*e, const struct timeval *tv, void *userdata), void *userdata) {
187 struct pa_mainloop *m;
188 struct pa_time_event *e;
189
190 assert(a && a->userdata && callback);
191 m = a->userdata;
192 assert(a == &m->api);
193
194 e = pa_xmalloc(sizeof(struct pa_time_event));
195 e->mainloop = m;
196 e->dead = 0;
197
198 e->enabled = !!tv;
199 if (tv)
200 e->timeval = *tv;
201
202 e->callback = callback;
203 e->userdata = userdata;
204 e->destroy_callback = NULL;
205
206 pa_idxset_put(m->time_events, e, NULL);
207
208 return e;
209 }
210
211 static void mainloop_time_restart(struct pa_time_event *e, const struct timeval *tv) {
212 assert(e);
213
214 if (tv) {
215 e->enabled = 1;
216 e->timeval = *tv;
217 } else
218 e->enabled = 0;
219 }
220
221 static void mainloop_time_free(struct pa_time_event *e) {
222 assert(e);
223
224 e->dead = e->mainloop->time_events_scan_dead = 1;
225 }
226
227 static void mainloop_time_set_destroy(struct pa_time_event *e, void (*callback)(struct pa_mainloop_api*a, struct pa_time_event *e, void *userdata)) {
228 assert(e);
229 e->destroy_callback = callback;
230 }
231
232 /* quit() */
233
234 static void mainloop_quit(struct pa_mainloop_api*a, int retval) {
235 struct pa_mainloop *m;
236 assert(a && a->userdata);
237 m = a->userdata;
238 assert(a == &m->api);
239
240 m->quit = 1;
241 m->retval = retval;
242 }
243
244 static const struct pa_mainloop_api vtable = {
245 .userdata = NULL,
246
247 .io_new= mainloop_io_new,
248 .io_enable= mainloop_io_enable,
249 .io_free= mainloop_io_free,
250 .io_set_destroy= mainloop_io_set_destroy,
251
252 .time_new = mainloop_time_new,
253 .time_restart = mainloop_time_restart,
254 .time_free = mainloop_time_free,
255 .time_set_destroy = mainloop_time_set_destroy,
256
257 .defer_new = mainloop_defer_new,
258 .defer_enable = mainloop_defer_enable,
259 .defer_free = mainloop_defer_free,
260 .defer_set_destroy = mainloop_defer_set_destroy,
261
262 .quit = mainloop_quit,
263 };
264
265 struct pa_mainloop *pa_mainloop_new(void) {
266 struct pa_mainloop *m;
267
268 m = pa_xmalloc(sizeof(struct pa_mainloop));
269
270 m->io_events = pa_idxset_new(NULL, NULL);
271 m->defer_events = pa_idxset_new(NULL, NULL);
272 m->time_events = pa_idxset_new(NULL, NULL);
273
274 assert(m->io_events && m->defer_events && m->time_events);
275
276 m->io_events_scan_dead = m->defer_events_scan_dead = m->time_events_scan_dead = 0;
277
278 m->pollfds = NULL;
279 m->max_pollfds = m->n_pollfds = m->rebuild_pollfds = 0;
280
281 m->quit = m->running = m->retval = 0;
282
283 m->api = vtable;
284 m->api.userdata = m;
285
286 m->deferred_pending = 0;
287
288 return m;
289 }
290
291 static int io_foreach(void *p, uint32_t index, int *del, void*userdata) {
292 struct pa_io_event *e = p;
293 int *all = userdata;
294 assert(e && del && all);
295
296 if (!*all && !e->dead)
297 return 0;
298
299 if (e->destroy_callback)
300 e->destroy_callback(&e->mainloop->api, e, e->userdata);
301 pa_xfree(e);
302 *del = 1;
303 return 0;
304 }
305
306 static int time_foreach(void *p, uint32_t index, int *del, void*userdata) {
307 struct pa_time_event *e = p;
308 int *all = userdata;
309 assert(e && del && all);
310
311 if (!*all && !e->dead)
312 return 0;
313
314 if (e->destroy_callback)
315 e->destroy_callback(&e->mainloop->api, e, e->userdata);
316 pa_xfree(e);
317 *del = 1;
318 return 0;
319 }
320
321 static int defer_foreach(void *p, uint32_t index, int *del, void*userdata) {
322 struct pa_defer_event *e = p;
323 int *all = userdata;
324 assert(e && del && all);
325
326 if (!*all && !e->dead)
327 return 0;
328
329 if (e->destroy_callback)
330 e->destroy_callback(&e->mainloop->api, e, e->userdata);
331 pa_xfree(e);
332 *del = 1;
333 return 0;
334 }
335
336 void pa_mainloop_free(struct pa_mainloop* m) {
337 int all = 1;
338 assert(m);
339
340 pa_idxset_foreach(m->io_events, io_foreach, &all);
341 pa_idxset_foreach(m->time_events, time_foreach, &all);
342 pa_idxset_foreach(m->defer_events, defer_foreach, &all);
343
344 pa_idxset_free(m->io_events, NULL, NULL);
345 pa_idxset_free(m->time_events, NULL, NULL);
346 pa_idxset_free(m->defer_events, NULL, NULL);
347
348 pa_xfree(m->pollfds);
349 pa_xfree(m);
350 }
351
352 static void scan_dead(struct pa_mainloop *m) {
353 int all = 0;
354 assert(m);
355
356 if (m->io_events_scan_dead)
357 pa_idxset_foreach(m->io_events, io_foreach, &all);
358 if (m->time_events_scan_dead)
359 pa_idxset_foreach(m->time_events, time_foreach, &all);
360 if (m->defer_events_scan_dead)
361 pa_idxset_foreach(m->defer_events, defer_foreach, &all);
362
363 m->io_events_scan_dead = m->time_events_scan_dead = m->defer_events_scan_dead = 0;
364 }
365
366 static void rebuild_pollfds(struct pa_mainloop *m) {
367 struct pa_io_event*e;
368 struct pollfd *p;
369 uint32_t index = PA_IDXSET_INVALID;
370 unsigned l;
371
372 l = pa_idxset_ncontents(m->io_events);
373 if (m->max_pollfds < l) {
374 m->pollfds = pa_xrealloc(m->pollfds, sizeof(struct pollfd)*l);
375 m->max_pollfds = l;
376 }
377
378 m->n_pollfds = 0;
379 p = m->pollfds;
380 for (e = pa_idxset_first(m->io_events, &index); e; e = pa_idxset_next(m->io_events, &index)) {
381 if (e->dead) {
382 e->pollfd = NULL;
383 continue;
384 }
385
386 e->pollfd = p;
387 p->fd = e->fd;
388 p->events =
389 ((e->events & PA_IO_EVENT_INPUT) ? POLLIN : 0) |
390 ((e->events & PA_IO_EVENT_OUTPUT) ? POLLOUT : 0) |
391 POLLHUP |
392 POLLERR;
393 p->revents = 0;
394
395 p++;
396 m->n_pollfds++;
397 }
398 }
399
400 static int dispatch_pollfds(struct pa_mainloop *m) {
401 uint32_t index = PA_IDXSET_INVALID;
402 struct pa_io_event *e;
403 int r = 0;
404
405 for (e = pa_idxset_first(m->io_events, &index); e && !m->quit; e = pa_idxset_next(m->io_events, &index)) {
406 if (e->dead || !e->pollfd || !e->pollfd->revents)
407 continue;
408
409 assert(e->pollfd->fd == e->fd && e->callback);
410 e->callback(&m->api, e, e->fd,
411 (e->pollfd->revents & POLLHUP ? PA_IO_EVENT_HANGUP : 0) |
412 (e->pollfd->revents & POLLIN ? PA_IO_EVENT_INPUT : 0) |
413 (e->pollfd->revents & POLLOUT ? PA_IO_EVENT_OUTPUT : 0) |
414 (e->pollfd->revents & POLLERR ? PA_IO_EVENT_ERROR : 0),
415 e->userdata);
416 e->pollfd->revents = 0;
417 r++;
418 }
419
420 return r;
421 }
422
423 static int dispatch_defer(struct pa_mainloop *m) {
424 uint32_t index;
425 struct pa_defer_event *e;
426 int r = 0;
427
428 for (e = pa_idxset_first(m->defer_events, &index); e && !m->quit; e = pa_idxset_next(m->defer_events, &index)) {
429 if (e->dead || !e->enabled)
430 continue;
431
432 assert(e->callback);
433 e->callback(&m->api, e, e->userdata);
434 r++;
435 }
436
437 return r;
438 }
439
440 static int calc_next_timeout(struct pa_mainloop *m) {
441 uint32_t index;
442 struct pa_time_event *e;
443 struct timeval now;
444 int t = -1;
445 int got_time = 0;
446
447 if (pa_idxset_isempty(m->time_events))
448 return -1;
449
450 for (e = pa_idxset_first(m->time_events, &index); e; e = pa_idxset_next(m->time_events, &index)) {
451 int tmp;
452
453 if (e->dead || !e->enabled)
454 continue;
455
456 /* Let's save a system call */
457 if (!got_time) {
458 gettimeofday(&now, NULL);
459 got_time = 1;
460 }
461
462 if (e->timeval.tv_sec < now.tv_sec || (e->timeval.tv_sec == now.tv_sec && e->timeval.tv_usec <= now.tv_usec))
463 return 0;
464
465 tmp = (e->timeval.tv_sec - now.tv_sec)*1000;
466
467 if (e->timeval.tv_usec > now.tv_usec)
468 tmp += (e->timeval.tv_usec - now.tv_usec)/1000;
469 else
470 tmp -= (now.tv_usec - e->timeval.tv_usec)/1000;
471
472 if (tmp == 0)
473 return 0;
474 else if (t == -1 || tmp < t)
475 t = tmp;
476 }
477
478 return t;
479 }
480
481 static int dispatch_timeout(struct pa_mainloop *m) {
482 uint32_t index;
483 struct pa_time_event *e;
484 struct timeval now;
485 int got_time = 0;
486 int r = 0;
487 assert(m);
488
489 if (pa_idxset_isempty(m->time_events))
490 return 0;
491
492 for (e = pa_idxset_first(m->time_events, &index); e && !m->quit; e = pa_idxset_next(m->time_events, &index)) {
493
494 if (e->dead || !e->enabled)
495 continue;
496
497 /* Let's save a system call */
498 if (!got_time) {
499 gettimeofday(&now, NULL);
500 got_time = 1;
501 }
502
503 if (e->timeval.tv_sec < now.tv_sec || (e->timeval.tv_sec == now.tv_sec && e->timeval.tv_usec <= now.tv_usec)) {
504 assert(e->callback);
505
506 e->enabled = 0;
507 e->callback(&m->api, e, &e->timeval, e->userdata);
508
509 r++;
510 }
511 }
512
513 return r;
514 }
515
516 int pa_mainloop_iterate(struct pa_mainloop *m, int block, int *retval) {
517 int r, t, dispatched = 0;
518 assert(m && !m->running);
519
520 m->running = 1;
521
522 if(m->quit)
523 goto quit;
524
525 scan_dead(m);
526 dispatched += dispatch_defer(m);
527
528 if(m->quit)
529 goto quit;
530
531 if (m->rebuild_pollfds) {
532 rebuild_pollfds(m);
533 m->rebuild_pollfds = 0;
534 }
535
536 t = block ? calc_next_timeout(m) : 0;
537 r = poll(m->pollfds, m->n_pollfds, t);
538
539 if (r < 0) {
540 if (errno == EINTR)
541 r = 0;
542 else
543 pa_log(__FILE__": select(): %s\n", strerror(errno));
544 } else {
545 dispatched += dispatch_timeout(m);
546
547 if(m->quit)
548 goto quit;
549
550 if (r > 0) {
551 dispatched += dispatch_pollfds(m);
552
553 if(m->quit)
554 goto quit;
555 }
556 }
557
558 m->running = 0;
559
560 /* pa_log("dispatched: %i\n", dispatched); */
561
562 return r < 0 ? -1 : dispatched;
563
564 quit:
565
566 m->running = 0;
567
568 if (retval)
569 *retval = m->retval;
570
571 return -2;
572 }
573
574 int pa_mainloop_run(struct pa_mainloop *m, int *retval) {
575 int r;
576 while ((r = pa_mainloop_iterate(m, 1, retval)) >= 0);
577
578 if (r == -2)
579 return 1;
580 else if (r < 0)
581 return -1;
582 else
583 return 0;
584 }
585
586 void pa_mainloop_quit(struct pa_mainloop *m, int r) {
587 assert(m);
588 m->quit = r;
589 }
590
591 struct pa_mainloop_api* pa_mainloop_get_api(struct pa_mainloop*m) {
592 assert(m);
593 return &m->api;
594 }
595
596 int pa_mainloop_deferred_pending(struct pa_mainloop *m) {
597 assert(m);
598 return m->deferred_pending > 0;
599 }