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