]> code.delx.au - pulseaudio/blob - src/pulsecore/rtpoll.h
port module-rtp-send.c to lock-free core
[pulseaudio] / src / pulsecore / rtpoll.h
1 #ifndef foopulsertpollhfoo
2 #define foopulsertpollhfoo
3
4 /* $Id$ */
5
6 /***
7 This file is part of PulseAudio.
8
9 Copyright 2004-2006 Lennart Poettering
10
11 PulseAudio is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as
13 published by the Free Software Foundation; either version 2.1 of the
14 License, or (at your option) any later version.
15
16 PulseAudio is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public
22 License along with PulseAudio; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 USA.
25 ***/
26
27 #include <poll.h>
28 #include <sys/types.h>
29 #include <limits.h>
30
31 #include <pulse/sample.h>
32 #include <pulsecore/asyncmsgq.h>
33 #include <pulsecore/fdsem.h>
34
35 /* An implementation of a "real-time" poll loop. Basically, this is
36 * yet another wrapper around poll(). However it has certain
37 * advantages over pa_mainloop and suchlike:
38 *
39 * 1) It uses timer_create() and POSIX real time signals to guarantee
40 * optimal high-resolution timing. Starting with Linux 2.6.21 hrtimers
41 * are available, and since right now only nanosleep() and the POSIX
42 * clock and timer interfaces have been ported to hrtimers (and not
43 * ppoll/pselect!) we have to combine ppoll() with timer_create(). The
44 * fact that POSIX timers and POSIX rt signals are used internally is
45 * completely hidden.
46 *
47 * 2) It allows raw access to the pollfd data to users
48 *
49 * 3) It allows arbitrary functions to be run before entering the
50 * actual poll() and after it.
51 *
52 * Only a single interval timer is supported..*/
53
54 typedef struct pa_rtpoll pa_rtpoll;
55 typedef struct pa_rtpoll_item pa_rtpoll_item;
56
57 typedef enum pa_rtpoll_priority {
58 PA_RTPOLL_EARLY = -100, /* For veeery important stuff, like handling control messages */
59 PA_RTPOLL_NORMAL = 0, /* For normal stuff */
60 PA_RTPOLL_LATE = +100, /* For housekeeping */
61 PA_RTPOLL_NEVER = INT_MAX, /* For stuff that doesn't register any callbacks, but only fds to listen on */
62 } pa_rtpoll_priority_t;
63
64 pa_rtpoll *pa_rtpoll_new(void);
65 void pa_rtpoll_free(pa_rtpoll *p);
66
67 /* Install the rtpoll in the current thread */
68 void pa_rtpoll_install(pa_rtpoll *p);
69
70 /* Sleep on the rtpoll until the time event, or any of the fd events
71 * is triggered. If "wait" is 0 we don't sleep but only update the
72 * struct pollfd. Returns negative on error, positive if the loop
73 * should continue to run, 0 when the loop should be terminated
74 * cleanly. */
75 int pa_rtpoll_run(pa_rtpoll *f, int wait);
76
77 void pa_rtpoll_set_timer_absolute(pa_rtpoll *p, const struct timespec *ts);
78 void pa_rtpoll_set_timer_periodic(pa_rtpoll *p, pa_usec_t usec);
79 void pa_rtpoll_set_timer_relative(pa_rtpoll *p, pa_usec_t usec);
80 void pa_rtpoll_set_timer_disabled(pa_rtpoll *p);
81
82 /* A new fd wakeup item for pa_rtpoll */
83 pa_rtpoll_item *pa_rtpoll_item_new(pa_rtpoll *p, pa_rtpoll_priority_t prio, unsigned n_fds);
84 void pa_rtpoll_item_free(pa_rtpoll_item *i);
85
86 /* Please note that this pointer might change on every call and when
87 * pa_rtpoll_run() is called. Hence: call this immediately before
88 * using the pointer and don't save the result anywhere */
89 struct pollfd *pa_rtpoll_item_get_pollfd(pa_rtpoll_item *i, unsigned *n_fds);
90
91 /* Set the callback that shall be called when there's time to do some work: If the
92 * callback returns a value > 0, the poll is skipped and the next
93 * iteraton of the loop will start immediately. */
94 void pa_rtpoll_item_set_work_callback(pa_rtpoll_item *i, int (*work_cb)(pa_rtpoll_item *i));
95
96 /* Set the callback that shall be called immediately before entering
97 * the sleeping poll: If the callback returns a value > 0, the poll is
98 * skipped and the next iteraton of the loop will start
99 * immediately.. */
100 void pa_rtpoll_item_set_before_callback(pa_rtpoll_item *i, int (*before_cb)(pa_rtpoll_item *i));
101
102 /* Set the callback that shall be called immediately after having
103 * entered the sleeping poll */
104 void pa_rtpoll_item_set_after_callback(pa_rtpoll_item *i, void (*after_cb)(pa_rtpoll_item *i));
105
106 void pa_rtpoll_item_set_userdata(pa_rtpoll_item *i, void *userdata);
107 void* pa_rtpoll_item_get_userdata(pa_rtpoll_item *i);
108
109 pa_rtpoll_item *pa_rtpoll_item_new_fdsem(pa_rtpoll *p, pa_rtpoll_priority_t prio, pa_fdsem *s);
110 pa_rtpoll_item *pa_rtpoll_item_new_asyncmsgq(pa_rtpoll *p, pa_rtpoll_priority_t prio, pa_asyncmsgq *q);
111
112 /* Requests the loop to exit. Will cause the next iteration of
113 * pa_rtpoll_run() to return 0 */
114 void pa_rtpoll_quit(pa_rtpoll *p);
115
116 #endif