]> code.delx.au - pulseaudio/blob - polyp/glib12-mainloop.c
improve sync clock change
[pulseaudio] / polyp / glib12-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 <assert.h>
27
28 #include "glib-mainloop.h"
29 #include "idxset.h"
30 #include "xmalloc.h"
31
32 /* A mainloop implementation based on GLIB 1.2 */
33
34 struct pa_io_event {
35 struct pa_glib_mainloop *mainloop;
36 int dead;
37 GIOChannel *io_channel;
38 guint source;
39 GIOCondition io_condition;
40 int fd;
41 void (*callback) (struct pa_mainloop_api*m, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata);
42 void *userdata;
43 void (*destroy_callback) (struct pa_mainloop_api *m, struct pa_io_event*e, void *userdata);
44 struct pa_io_event *next, *prev;
45 };
46
47 struct pa_time_event {
48 struct pa_glib_mainloop *mainloop;
49 int dead;
50 guint source;
51 struct timeval timeval;
52 void (*callback) (struct pa_mainloop_api*m, struct pa_time_event *e, const struct timeval *tv, void *userdata);
53 void *userdata;
54 void (*destroy_callback) (struct pa_mainloop_api *m, struct pa_time_event*e, void *userdata);
55 struct pa_time_event *next, *prev;
56 };
57
58 struct pa_defer_event {
59 struct pa_glib_mainloop *mainloop;
60 int dead;
61 guint source;
62 void (*callback) (struct pa_mainloop_api*m, struct pa_defer_event *e, void *userdata);
63 void *userdata;
64 void (*destroy_callback) (struct pa_mainloop_api *m, struct pa_defer_event*e, void *userdata);
65 struct pa_defer_event *next, *prev;
66 };
67
68 struct pa_glib_mainloop {
69 struct pa_mainloop_api api;
70 guint cleanup_source;
71 struct pa_io_event *io_events, *dead_io_events;
72 struct pa_time_event *time_events, *dead_time_events;
73 struct pa_defer_event *defer_events, *dead_defer_events;
74 };
75
76 static void schedule_free_dead_events(struct pa_glib_mainloop *g);
77
78 static void glib_io_enable(struct pa_io_event*e, enum pa_io_event_flags f);
79
80 static struct pa_io_event* glib_io_new(struct pa_mainloop_api*m, int fd, enum pa_io_event_flags f, void (*callback) (struct pa_mainloop_api*m, struct pa_io_event*e, int fd, enum pa_io_event_flags f, void *userdata), void *userdata) {
81 struct pa_io_event *e;
82 struct pa_glib_mainloop *g;
83
84 assert(m && m->userdata && fd >= 0 && callback);
85 g = m->userdata;
86
87 e = pa_xmalloc(sizeof(struct pa_io_event));
88 e->mainloop = m->userdata;
89 e->dead = 0;
90 e->fd = fd;
91 e->callback = callback;
92 e->userdata = userdata;
93 e->destroy_callback = NULL;
94
95 e->io_channel = g_io_channel_unix_new(e->fd);
96 assert(e->io_channel);
97 e->source = (guint) -1;
98 e->io_condition = 0;
99
100 glib_io_enable(e, f);
101
102 e->next = g->io_events;
103 if (e->next) e->next->prev = e;
104 g->io_events = e;
105 e->prev = NULL;
106
107 return e;
108 }
109
110 static gboolean io_cb(GIOChannel *source, GIOCondition condition, gpointer data) {
111 struct pa_io_event *e = data;
112 enum pa_io_event_flags f;
113 assert(source && e && e->io_channel == source);
114
115 f = (condition & G_IO_IN ? PA_IO_EVENT_INPUT : 0) |
116 (condition & G_IO_OUT ? PA_IO_EVENT_OUTPUT : 0) |
117 (condition & G_IO_ERR ? PA_IO_EVENT_ERROR : 0) |
118 (condition & G_IO_HUP ? PA_IO_EVENT_HANGUP : 0);
119
120 e->callback(&e->mainloop->api, e, e->fd, f, e->userdata);
121 return TRUE;
122 }
123
124 static void glib_io_enable(struct pa_io_event*e, enum pa_io_event_flags f) {
125 GIOCondition c;
126 assert(e && !e->dead);
127
128 c = (f & PA_IO_EVENT_INPUT ? G_IO_IN : 0) | (f & PA_IO_EVENT_OUTPUT ? G_IO_OUT : 0);
129
130 if (c == e->io_condition)
131 return;
132
133 if (e->source != (guint) -1)
134 g_source_remove(e->source);
135
136 e->source = g_io_add_watch_full(e->io_channel, G_PRIORITY_DEFAULT, c | G_IO_ERR | G_IO_HUP, io_cb, e, NULL);
137 assert(e->source != (guint) -1);
138 e->io_condition = c;
139 }
140
141 static void glib_io_free(struct pa_io_event*e) {
142 assert(e && !e->dead);
143
144 if (e->source != (guint) -1) {
145 g_source_remove(e->source);
146 e->source = (guint) -1;
147 }
148
149 if (e->prev)
150 e->prev->next = e->next;
151 else
152 e->mainloop->io_events = e->next;
153
154 if (e->next)
155 e->next->prev = e->prev;
156
157 if ((e->next = e->mainloop->dead_io_events))
158 e->next->prev = e;
159
160 e->mainloop->dead_io_events = e;
161 e->prev = NULL;
162
163 e->dead = 1;
164 schedule_free_dead_events(e->mainloop);
165 }
166
167 static void glib_io_set_destroy(struct pa_io_event*e, void (*callback)(struct pa_mainloop_api*m, struct pa_io_event *e, void *userdata)) {
168 assert(e);
169 e->destroy_callback = callback;
170 }
171
172 /* Time sources */
173
174 static void glib_time_restart(struct pa_time_event*e, const struct timeval *tv);
175
176 static struct pa_time_event* glib_time_new(struct pa_mainloop_api*m, const struct timeval *tv, void (*callback) (struct pa_mainloop_api*m, struct pa_time_event*e, const struct timeval *tv, void *userdata), void *userdata) {
177 struct pa_glib_mainloop *g;
178 struct pa_time_event *e;
179
180 assert(m && m->userdata && tv && callback);
181 g = m->userdata;
182
183 e = pa_xmalloc(sizeof(struct pa_time_event));
184 e->mainloop = g;
185 e->dead = 0;
186 e->callback = callback;
187 e->userdata = userdata;
188 e->destroy_callback = NULL;
189 e->source = (guint) -1;
190
191 glib_time_restart(e, tv);
192
193 e->next = g->time_events;
194 if (e->next) e->next->prev = e;
195 g->time_events = e;
196 e->prev = NULL;
197
198 return e;
199 }
200
201 static guint msec_diff(const struct timeval *a, const struct timeval *b) {
202 guint r;
203 assert(a && b);
204
205 if (a->tv_sec < b->tv_sec)
206 return 0;
207
208 if (a->tv_sec == b->tv_sec && a->tv_sec <= b->tv_sec)
209 return 0;
210
211 r = (a->tv_sec-b->tv_sec)*1000;
212
213 if (a->tv_usec >= b->tv_usec)
214 r += (a->tv_usec - b->tv_usec) / 1000;
215 else
216 r -= (b->tv_usec - a->tv_usec) / 1000;
217
218 return r;
219 }
220
221 static gboolean time_cb(gpointer data) {
222 struct pa_time_event* e = data;
223 assert(e && e->mainloop && e->source != (guint) -1);
224
225 g_source_remove(e->source);
226 e->source = (guint) -1;
227
228 e->callback(&e->mainloop->api, e, &e->timeval, e->userdata);
229 return FALSE;
230 }
231
232 static void glib_time_restart(struct pa_time_event*e, const struct timeval *tv) {
233 struct timeval now;
234 assert(e && e->mainloop && !e->dead);
235
236 gettimeofday(&now, NULL);
237 if (e->source != (guint) -1)
238 g_source_remove(e->source);
239
240 if (tv) {
241 e->timeval = *tv;
242 e->source = g_timeout_add_full(G_PRIORITY_DEFAULT, msec_diff(tv, &now), time_cb, e, NULL);
243 assert(e->source != (guint) -1);
244 } else
245 e->source = (guint) -1;
246 }
247
248 static void glib_time_free(struct pa_time_event *e) {
249 assert(e && e->mainloop && !e->dead);
250
251 if (e->source != (guint) -1) {
252 g_source_remove(e->source);
253 e->source = (guint) -1;
254 }
255
256 if (e->prev)
257 e->prev->next = e->next;
258 else
259 e->mainloop->time_events = e->next;
260
261 if (e->next)
262 e->next->prev = e->prev;
263
264 if ((e->next = e->mainloop->dead_time_events))
265 e->next->prev = e;
266
267 e->mainloop->dead_time_events = e;
268 e->prev = NULL;
269
270 e->dead = 1;
271 schedule_free_dead_events(e->mainloop);
272 }
273
274 static void glib_time_set_destroy(struct pa_time_event *e, void (*callback)(struct pa_mainloop_api*m, struct pa_time_event*e, void *userdata)) {
275 assert(e);
276 e->destroy_callback = callback;
277 }
278
279 /* Deferred sources */
280
281 static void glib_defer_enable(struct pa_defer_event *e, int b);
282
283 static struct pa_defer_event* glib_defer_new(struct pa_mainloop_api*m, void (*callback) (struct pa_mainloop_api*m, struct pa_defer_event *e, void *userdata), void *userdata) {
284 struct pa_defer_event *e;
285 struct pa_glib_mainloop *g;
286
287 assert(m && m->userdata && callback);
288 g = m->userdata;
289
290 e = pa_xmalloc(sizeof(struct pa_defer_event));
291 e->mainloop = g;
292 e->dead = 0;
293 e->callback = callback;
294 e->userdata = userdata;
295 e->destroy_callback = NULL;
296 e->source = (guint) -1;
297
298 glib_defer_enable(e, 1);
299
300 e->next = g->defer_events;
301 if (e->next) e->next->prev = e;
302 g->defer_events = e;
303 e->prev = NULL;
304 return e;
305 }
306
307 static gboolean idle_cb(gpointer data) {
308 struct pa_defer_event* e = data;
309 assert(e && e->mainloop && e->source != (guint) -1);
310
311 e->callback(&e->mainloop->api, e, e->userdata);
312 return TRUE;
313 }
314
315 static void glib_defer_enable(struct pa_defer_event *e, int b) {
316 assert(e && e->mainloop);
317
318 if (e->source != (guint) -1 && !b) {
319 g_source_remove(e->source);
320 e->source = (guint) -1;
321 } else if (e->source == (guint) -1 && b) {
322 e->source = g_idle_add_full(G_PRIORITY_HIGH, idle_cb, e, NULL);
323 assert(e->source != (guint) -1);
324 }
325 }
326
327 static void glib_defer_free(struct pa_defer_event *e) {
328 assert(e && e->mainloop && !e->dead);
329
330 if (e->source != (guint) -1) {
331 g_source_remove(e->source);
332 e->source = (guint) -1;
333 }
334
335 if (e->prev)
336 e->prev->next = e->next;
337 else
338 e->mainloop->defer_events = e->next;
339
340 if (e->next)
341 e->next->prev = e->prev;
342
343 if ((e->next = e->mainloop->dead_defer_events))
344 e->next->prev = e;
345
346 e->mainloop->dead_defer_events = e;
347 e->prev = NULL;
348
349 e->dead = 1;
350 schedule_free_dead_events(e->mainloop);
351 }
352
353 static void glib_defer_set_destroy(struct pa_defer_event *e, void (*callback)(struct pa_mainloop_api *m, struct pa_defer_event *e, void *userdata)) {
354 assert(e);
355 e->destroy_callback = callback;
356 }
357
358 /* quit() */
359
360 static void glib_quit(struct pa_mainloop_api*a, int retval) {
361 struct pa_glib_mainloop *g;
362 assert(a && a->userdata);
363 g = a->userdata;
364
365 /* NOOP */
366 }
367
368 static const struct pa_mainloop_api vtable = {
369 .userdata = NULL,
370
371 .io_new = glib_io_new,
372 .io_enable = glib_io_enable,
373 .io_free = glib_io_free,
374 .io_set_destroy= glib_io_set_destroy,
375
376 .time_new = glib_time_new,
377 .time_restart = glib_time_restart,
378 .time_free = glib_time_free,
379 .time_set_destroy = glib_time_set_destroy,
380
381 .defer_new = glib_defer_new,
382 .defer_enable = glib_defer_enable,
383 .defer_free = glib_defer_free,
384 .defer_set_destroy = glib_defer_set_destroy,
385
386 .quit = glib_quit,
387 };
388
389 struct pa_glib_mainloop *pa_glib_mainloop_new(void) {
390 struct pa_glib_mainloop *g;
391
392 g = pa_xmalloc(sizeof(struct pa_glib_mainloop));
393
394 g->api = vtable;
395 g->api.userdata = g;
396
397 g->io_events = g->dead_io_events = NULL;
398 g->time_events = g->dead_time_events = NULL;
399 g->defer_events = g->dead_defer_events = NULL;
400
401 g->cleanup_source = (guint) -1;
402 return g;
403 }
404
405 static void free_io_events(struct pa_io_event *e) {
406 while (e) {
407 struct pa_io_event *r = e;
408 e = r->next;
409
410 if (r->source != (guint) -1)
411 g_source_remove(r->source);
412
413 if (r->io_channel)
414 g_io_channel_unref(r->io_channel);
415
416 if (r->destroy_callback)
417 r->destroy_callback(&r->mainloop->api, r, r->userdata);
418
419 pa_xfree(r);
420 }
421 }
422
423 static void free_time_events(struct pa_time_event *e) {
424 while (e) {
425 struct pa_time_event *r = e;
426 e = r->next;
427
428 if (r->source != (guint) -1)
429 g_source_remove(r->source);
430
431 if (r->destroy_callback)
432 r->destroy_callback(&r->mainloop->api, r, r->userdata);
433
434 pa_xfree(r);
435 }
436 }
437
438 static void free_defer_events(struct pa_defer_event *e) {
439 while (e) {
440 struct pa_defer_event *r = e;
441 e = r->next;
442
443 if (r->source != (guint) -1)
444 g_source_remove(r->source);
445
446 if (r->destroy_callback)
447 r->destroy_callback(&r->mainloop->api, r, r->userdata);
448
449 pa_xfree(r);
450 }
451 }
452
453 void pa_glib_mainloop_free(struct pa_glib_mainloop* g) {
454 assert(g);
455
456 free_io_events(g->io_events);
457 free_io_events(g->dead_io_events);
458 free_defer_events(g->defer_events);
459 free_defer_events(g->dead_defer_events);
460 free_time_events(g->time_events);
461 free_time_events(g->dead_time_events);
462
463 if (g->cleanup_source != (guint) -1)
464 g_source_remove(g->cleanup_source);
465
466 pa_xfree(g);
467 }
468
469 struct pa_mainloop_api* pa_glib_mainloop_get_api(struct pa_glib_mainloop *g) {
470 assert(g);
471 return &g->api;
472 }
473
474 static gboolean free_dead_events(gpointer p) {
475 struct pa_glib_mainloop *g = p;
476 assert(g);
477
478 free_io_events(g->dead_io_events);
479 free_defer_events(g->dead_defer_events);
480 free_time_events(g->dead_time_events);
481
482 g->dead_io_events = NULL;
483 g->dead_defer_events = NULL;
484 g->dead_time_events = NULL;
485
486 g_source_remove(g->cleanup_source);
487 g->cleanup_source = (guint) -1;
488
489 return FALSE;
490 }
491
492 static void schedule_free_dead_events(struct pa_glib_mainloop *g) {
493 assert(g);
494
495 if (g->cleanup_source != (guint) -1)
496 return;
497
498 g->cleanup_source = g_idle_add_full(G_PRIORITY_HIGH, free_dead_events, g, NULL);
499 }