]> code.delx.au - pulseaudio/blob - polyp/mainloop.c
move sample cache to namereg
[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 return e;
190 }
191
192 static void mainloop_time_restart(struct pa_time_event *e, const struct timeval *tv) {
193 assert(e);
194
195 if (tv) {
196 e->enabled = 1;
197 e->timeval = *tv;
198 } else
199 e->enabled = 0;
200 }
201
202 static void mainloop_time_free(struct pa_time_event *e) {
203 assert(e);
204 e->dead = e->mainloop->time_events_scan_dead = 1;
205 }
206
207 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)) {
208 assert(e);
209 e->destroy_callback = callback;
210 }
211
212 /* quit() */
213
214 static void mainloop_quit(struct pa_mainloop_api*a, int retval) {
215 struct pa_mainloop *m;
216 assert(a && a->userdata);
217 m = a->userdata;
218 assert(a == &m->api);
219
220 m->quit = 1;
221 m->retval = retval;
222 }
223
224 static const struct pa_mainloop_api vtable = {
225 .userdata = NULL,
226
227 .io_new= mainloop_io_new,
228 .io_enable= mainloop_io_enable,
229 .io_free= mainloop_io_free,
230 .io_set_destroy= mainloop_io_set_destroy,
231
232 .time_new = mainloop_time_new,
233 .time_restart = mainloop_time_restart,
234 .time_free = mainloop_time_free,
235 .time_set_destroy = mainloop_time_set_destroy,
236
237 .defer_new = mainloop_defer_new,
238 .defer_enable = mainloop_defer_enable,
239 .defer_free = mainloop_defer_free,
240 .defer_set_destroy = mainloop_defer_set_destroy,
241
242 .quit = mainloop_quit,
243 };
244
245 struct pa_mainloop *pa_mainloop_new(void) {
246 struct pa_mainloop *m;
247
248 m = pa_xmalloc(sizeof(struct pa_mainloop));
249
250 m->io_events = pa_idxset_new(NULL, NULL);
251 m->defer_events = pa_idxset_new(NULL, NULL);
252 m->time_events = pa_idxset_new(NULL, NULL);
253
254 assert(m->io_events && m->defer_events && m->time_events);
255
256 m->io_events_scan_dead = m->defer_events_scan_dead = m->time_events_scan_dead = 0;
257
258 m->pollfds = NULL;
259 m->max_pollfds = m->n_pollfds = m->rebuild_pollfds = 0;
260
261 m->quit = m->running = m->retval = 0;
262
263 m->api = vtable;
264 m->api.userdata = m;
265
266 return m;
267 }
268
269 static int io_foreach(void *p, uint32_t index, int *del, void*userdata) {
270 struct pa_io_event *e = p;
271 int *all = userdata;
272 assert(e && del && all);
273
274 if (!*all || !e->dead)
275 return 0;
276
277 if (e->destroy_callback)
278 e->destroy_callback(&e->mainloop->api, e, e->userdata);
279 pa_xfree(e);
280 *del = 1;
281 return 0;
282 }
283
284 static int time_foreach(void *p, uint32_t index, int *del, void*userdata) {
285 struct pa_time_event *e = p;
286 int *all = userdata;
287 assert(e && del && all);
288
289 if (!*all || !e->dead)
290 return 0;
291
292 if (e->destroy_callback)
293 e->destroy_callback(&e->mainloop->api, e, e->userdata);
294 pa_xfree(e);
295 *del = 1;
296 return 0;
297 }
298
299 static int defer_foreach(void *p, uint32_t index, int *del, void*userdata) {
300 struct pa_defer_event *e = p;
301 int *all = userdata;
302 assert(e && del && all);
303
304 if (!*all || !e->dead)
305 return 0;
306
307 if (e->destroy_callback)
308 e->destroy_callback(&e->mainloop->api, e, e->userdata);
309 pa_xfree(e);
310 *del = 1;
311 return 0;
312 }
313
314 void pa_mainloop_free(struct pa_mainloop* m) {
315 int all = 1;
316 assert(m);
317
318 pa_idxset_foreach(m->io_events, io_foreach, &all);
319 pa_idxset_foreach(m->time_events, time_foreach, &all);
320 pa_idxset_foreach(m->defer_events, defer_foreach, &all);
321
322 pa_idxset_free(m->io_events, NULL, NULL);
323 pa_idxset_free(m->time_events, NULL, NULL);
324 pa_idxset_free(m->defer_events, NULL, NULL);
325
326 pa_xfree(m->pollfds);
327 pa_xfree(m);
328 }
329
330 static void scan_dead(struct pa_mainloop *m) {
331 int all = 0;
332 assert(m);
333 if (m->io_events_scan_dead)
334 pa_idxset_foreach(m->io_events, io_foreach, &all);
335 if (m->time_events_scan_dead)
336 pa_idxset_foreach(m->time_events, time_foreach, &all);
337 if (m->defer_events_scan_dead)
338 pa_idxset_foreach(m->defer_events, defer_foreach, &all);
339 }
340
341 static void rebuild_pollfds(struct pa_mainloop *m) {
342 struct pa_io_event*e;
343 struct pollfd *p;
344 uint32_t index = PA_IDXSET_INVALID;
345 unsigned l;
346
347 l = pa_idxset_ncontents(m->io_events);
348 if (m->max_pollfds < l) {
349 m->pollfds = pa_xrealloc(m->pollfds, sizeof(struct pollfd)*l);
350 m->max_pollfds = l;
351 }
352
353 m->n_pollfds = 0;
354 p = m->pollfds;
355 for (e = pa_idxset_first(m->io_events, &index); e; e = pa_idxset_next(m->io_events, &index)) {
356 if (e->dead) {
357 e->pollfd = NULL;
358 continue;
359 }
360
361 e->pollfd = p;
362 p->fd = e->fd;
363 p->events =
364 ((e->events & PA_IO_EVENT_INPUT) ? POLLIN : 0) |
365 ((e->events & PA_IO_EVENT_OUTPUT) ? POLLOUT : 0) |
366 POLLHUP |
367 POLLERR;
368 p->revents = 0;
369
370 p++;
371 m->n_pollfds++;
372 }
373 }
374
375 static void dispatch_pollfds(struct pa_mainloop *m) {
376 uint32_t index = PA_IDXSET_INVALID;
377 struct pa_io_event *e;
378
379 for (e = pa_idxset_first(m->io_events, &index); e; e = pa_idxset_next(m->io_events, &index)) {
380 if (e->dead || !e->pollfd || !e->pollfd->revents)
381 continue;
382
383 assert(e->pollfd->fd == e->fd && e->callback);
384 e->callback(&m->api, e, e->fd,
385 (e->pollfd->revents & POLLHUP ? PA_IO_EVENT_HANGUP : 0) |
386 (e->pollfd->revents & POLLIN ? PA_IO_EVENT_INPUT : 0) |
387 (e->pollfd->revents & POLLOUT ? PA_IO_EVENT_OUTPUT : 0) |
388 (e->pollfd->revents & POLLERR ? PA_IO_EVENT_ERROR : 0),
389 e->userdata);
390 e->pollfd->revents = 0;
391 }
392 }
393
394 static void dispatch_defer(struct pa_mainloop *m) {
395 uint32_t index;
396 struct pa_defer_event *e;
397
398 for (e = pa_idxset_first(m->defer_events, &index); e; e = pa_idxset_next(m->defer_events, &index)) {
399 if (e->dead || !e->enabled)
400 continue;
401
402 assert(e->callback);
403 e->callback(&m->api, e, e->userdata);
404 }
405 }
406
407 static int calc_next_timeout(struct pa_mainloop *m) {
408 uint32_t index;
409 struct pa_time_event *e;
410 struct timeval now;
411 int t = -1;
412
413 if (pa_idxset_isempty(m->time_events))
414 return -1;
415
416 gettimeofday(&now, NULL);
417
418 for (e = pa_idxset_first(m->time_events, &index); e; e = pa_idxset_next(m->time_events, &index)) {
419 int tmp;
420
421 if (e->dead || !e->enabled)
422 continue;
423
424 if (e->timeval.tv_sec < now.tv_sec || (e->timeval.tv_sec == now.tv_sec && e->timeval.tv_usec <= now.tv_usec))
425 return 0;
426
427 tmp = (e->timeval.tv_sec - now.tv_sec)*1000;
428
429 if (e->timeval.tv_usec > now.tv_usec)
430 tmp += (e->timeval.tv_usec - now.tv_usec)/1000;
431 else
432 tmp -= (now.tv_usec - e->timeval.tv_usec)/1000;
433
434 if (tmp == 0)
435 return 0;
436 else if (t == -1 || tmp < t)
437 t = tmp;
438 }
439
440 return t;
441 }
442
443 static void dispatch_timeout(struct pa_mainloop *m) {
444 uint32_t index;
445 struct pa_time_event *e;
446 struct timeval now;
447 assert(m);
448
449 if (pa_idxset_isempty(m->time_events))
450 return;
451
452 gettimeofday(&now, NULL);
453 for (e = pa_idxset_first(m->time_events, &index); e; e = pa_idxset_next(m->time_events, &index)) {
454
455 if (e->dead || !e->enabled)
456 continue;
457
458 if (e->timeval.tv_sec < now.tv_sec || (e->timeval.tv_sec == now.tv_sec && e->timeval.tv_usec <= now.tv_usec)) {
459 assert(e->callback);
460
461 e->enabled = 0;
462 e->callback(&m->api, e, &e->timeval, e->userdata);
463 }
464 }
465 }
466
467 int pa_mainloop_iterate(struct pa_mainloop *m, int block, int *retval) {
468 int r;
469 assert(m && !m->running);
470
471 if(m->quit) {
472 if (retval)
473 *retval = m->retval;
474 return 1;
475 }
476
477 m->running = 1;
478
479 scan_dead(m);
480 dispatch_defer(m);
481
482 if (m->rebuild_pollfds) {
483 rebuild_pollfds(m);
484 m->rebuild_pollfds = 0;
485 }
486
487 do {
488 int t = block ? calc_next_timeout(m) : 0;
489 /*fprintf(stderr, "%u\n", t);*/
490 r = poll(m->pollfds, m->n_pollfds, t);
491 } while (r < 0 && errno == EINTR);
492
493 dispatch_timeout(m);
494
495 if (r > 0)
496 dispatch_pollfds(m);
497 else if (r < 0)
498 fprintf(stderr, "select(): %s\n", strerror(errno));
499
500 m->running = 0;
501 return r < 0 ? -1 : 0;
502 }
503
504 int pa_mainloop_run(struct pa_mainloop *m, int *retval) {
505 int r;
506 while ((r = pa_mainloop_iterate(m, 1, retval)) == 0);
507 return r;
508 }
509
510 void pa_mainloop_quit(struct pa_mainloop *m, int r) {
511 assert(m);
512 m->quit = r;
513 }
514
515 struct pa_mainloop_api* pa_mainloop_get_api(struct pa_mainloop*m) {
516 assert(m);
517 return &m->api;
518 }