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