[pulseaudio-commits] r1689 - in /branches/lennart/src/pulsecore: rtpoll.c rtpoll.h
svnmailer-noreply at 0pointer.de
svnmailer-noreply at 0pointer.de
Tue Aug 21 17:24:13 PDT 2007
Author: lennart
Date: Wed Aug 22 02:24:12 2007
New Revision: 1689
URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=3D1689&root=3Dpulseaudio&vi=
ew=3Drev
Log:
add new realtime event loop abstraction which precise time keeping by using=
hrtimers on Linux, if they are available
Added:
branches/lennart/src/pulsecore/rtpoll.c
branches/lennart/src/pulsecore/rtpoll.h
Added: branches/lennart/src/pulsecore/rtpoll.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/pulsecore/=
rtpoll.c?rev=3D1689&root=3Dpulseaudio&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/lennart/src/pulsecore/rtpoll.c (added)
+++ branches/lennart/src/pulsecore/rtpoll.c Wed Aug 22 02:24:12 2007
@@ -1,0 +1,454 @@
+/* $Id$ */
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2006 Lennart Poettering
+ Copyright 2006 Pierre Ossman <ossman at cendio.se> for Cendio AB
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <sys/utsname.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <signal.h>
+#include <string.h>
+#include <errno.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core-error.h>
+#include <pulsecore/rtclock.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/llist.h>
+#include <pulsecore/rtsig.h>
+#include <pulsecore/flist.h>
+
+#include "rtpoll.h"
+
+struct pa_rtpoll {
+
+ struct pollfd *pollfd, *pollfd2;
+ unsigned n_pollfd_alloc, n_pollfd_used;
+
+ pa_usec_t interval;
+
+ int scan_for_dead;
+ int running, installed, rebuild_needed;
+
+#ifdef HAVE_PPOLL
+ int rtsig;
+ sigset_t sigset_unblocked;
+ struct timespec interval_timespec;
+ timer_t timer;
+#ifdef __linux__
+ int dont_use_ppoll;
+#endif =
+#endif
+ =
+ PA_LLIST_HEAD(pa_rtpoll_item, items);
+};
+
+struct pa_rtpoll_item {
+ pa_rtpoll *rtpoll;
+ int dead;
+
+ struct pollfd *pollfd;
+ unsigned n_pollfd;
+
+ int (*before_cb)(pa_rtpoll_item *i);
+ void (*after_cb)(pa_rtpoll_item *i);
+ void *userdata;
+ =
+ PA_LLIST_FIELDS(pa_rtpoll_item);
+};
+
+PA_STATIC_FLIST_DECLARE(items, 0, pa_xfree);
+
+static void signal_handler_noop(int s) { }
+
+pa_rtpoll *pa_rtpoll_new(void) {
+ pa_rtpoll *p;
+
+ p =3D pa_xnew(pa_rtpoll, 1);
+
+#ifdef HAVE_PPOLL
+
+#ifdef __linux__
+ /* ppoll is broken on Linux < 2.6.16 */
+ =
+ p->dont_use_ppoll =3D 0;
+
+ {
+ struct utsname u;
+ unsigned major, minor, micro;
+ =
+ pa_assert_se(uname(&u) =3D=3D 0);
+
+ if (sscanf(u.release, "%u.%u.%u", &major, &minor, µ) !=3D 3 ||
+ (major < 2) ||
+ (major =3D=3D 2 && minor < 6) ||
+ (major =3D=3D 2 && minor =3D=3D 6 && micro < 16))
+
+ p->dont_use_ppoll =3D 1;
+ }
+
+#endif
+
+ p->rtsig =3D -1;
+ sigemptyset(&p->sigset_unblocked);
+ memset(&p->interval_timespec, 0, sizeof(p->interval_timespec));
+ p->timer =3D (timer_t) -1;
+ =
+#endif
+
+ p->n_pollfd_alloc =3D 32;
+ p->pollfd =3D pa_xnew(struct pollfd, p->n_pollfd_alloc);
+ p->pollfd2 =3D pa_xnew(struct pollfd, p->n_pollfd_alloc);
+ p->n_pollfd_used =3D 0;
+
+ p->interval =3D 0;
+
+ p->running =3D 0;
+ p->installed =3D 0;
+ p->scan_for_dead =3D 0;
+ p->rebuild_needed =3D 0;
+ =
+ PA_LLIST_HEAD_INIT(pa_rtpoll_item, p->items);
+
+ return p;
+}
+
+void pa_rtpoll_install(pa_rtpoll *p) {
+ pa_assert(p);
+ pa_assert(!p->installed);
+ =
+ p->installed =3D 1;
+
+#ifdef HAVE_PPOLL
+ if (p->dont_use_ppoll)
+ return;
+
+ if ((p->rtsig =3D pa_rtsig_get_for_thread()) < 0) {
+ pa_log_warn("Failed to reserve POSIX realtime signal.");
+ return;
+ }
+
+ pa_log_debug("Acquired POSIX realtime signal SIGRTMIN+%i", p->rtsig - =
SIGRTMIN);
+
+ {
+ sigset_t ss;
+ struct sigaction sa;
+ =
+ pa_assert_se(sigemptyset(&ss) =3D=3D 0);
+ pa_assert_se(sigaddset(&ss, p->rtsig) =3D=3D 0);
+ pa_assert_se(pthread_sigmask(SIG_BLOCK, &ss, &p->sigset_unblocked)=
=3D=3D 0);
+ pa_assert_se(sigdelset(&p->sigset_unblocked, p->rtsig) =3D=3D 0);
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_handler =3D signal_handler_noop;
+ pa_assert_se(sigemptyset(&sa.sa_mask) =3D=3D 0);
+ =
+ pa_assert_se(sigaction(p->rtsig, &sa, NULL) =3D=3D 0);
+ =
+ /* We never reset the signal handler. Why should we? */
+ }
+ =
+#endif
+}
+
+static void rtpoll_rebuild(pa_rtpoll *p) {
+
+ struct pollfd *e, *t;
+ pa_rtpoll_item *i;
+ int ra =3D 0;
+ =
+ pa_assert(p);
+
+ p->rebuild_needed =3D 0;
+
+ if (p->n_pollfd_used > p->n_pollfd_alloc) {
+ /* Hmm, we have to allocate some more space */
+ p->n_pollfd_alloc =3D p->n_pollfd_used * 2;
+ p->pollfd2 =3D pa_xrealloc(p->pollfd2, p->n_pollfd_alloc * sizeof(=
struct pollfd));
+ ra =3D 1;
+ }
+
+ e =3D p->pollfd2;
+
+ for (i =3D p->items; i; i =3D i->next) {
+
+ if (i->n_pollfd > 0) {
+ size_t l =3D i->n_pollfd * sizeof(struct pollfd);
+ =
+ if (i->pollfd)
+ memcpy(e, i->pollfd, l);
+ else
+ memset(e, 0, l);
+
+ i->pollfd =3D e;
+ } else
+ i->pollfd =3D NULL;
+ =
+ e +=3D i->n_pollfd;
+ }
+
+ pa_assert((unsigned) (e - p->pollfd2) =3D=3D p->n_pollfd_used);
+ t =3D p->pollfd;
+ p->pollfd =3D p->pollfd2;
+ p->pollfd2 =3D t;
+ =
+ if (ra)
+ p->pollfd2 =3D pa_xrealloc(p->pollfd2, p->n_pollfd_alloc * sizeof(=
struct pollfd));
+
+}
+
+static void rtpoll_item_destroy(pa_rtpoll_item *i) {
+ pa_rtpoll *p;
+
+ pa_assert(i);
+
+ p =3D i->rtpoll;
+
+ PA_LLIST_REMOVE(pa_rtpoll_item, p->items, i);
+
+ p->n_pollfd_used -=3D i->n_pollfd;
+ =
+ if (pa_flist_push(PA_STATIC_FLIST_GET(items), i) < 0)
+ pa_xfree(i);
+
+ p->rebuild_needed =3D 1;
+}
+
+void pa_rtpoll_free(pa_rtpoll *p) {
+ pa_assert(p);
+
+ pa_assert(!p->items);
+ pa_xfree(p->pollfd);
+ pa_xfree(p->pollfd2);
+
+#ifdef HAVE_PPOLL
+ if (p->timer !=3D (timer_t) -1) =
+ timer_delete(p->timer);
+#endif
+ =
+ pa_xfree(p);
+}
+
+int pa_rtpoll_run(pa_rtpoll *p) {
+ pa_rtpoll_item *i;
+ int r =3D 0;
+ =
+ pa_assert(p);
+ pa_assert(!p->running);
+ pa_assert(p->installed);
+ =
+ p->running =3D 1;
+
+ for (i =3D p->items; i; i =3D i->next) {
+
+ if (i->dead)
+ continue;
+ =
+ if (!i->before_cb)
+ continue;
+
+ if (i->before_cb(i) < 0) {
+
+ /* Hmm, this one doesn't let us enter the poll, so rewind ever=
ything */
+
+ for (i =3D i->prev; i; i =3D i->prev) {
+
+ if (i->dead)
+ continue;
+ =
+ if (!i->after_cb)
+ continue;
+
+ i->after_cb(i);
+ }
+ =
+ goto finish;
+ }
+ }
+
+ if (p->rebuild_needed)
+ rtpoll_rebuild(p);
+ =
+ /* OK, now let's sleep */
+#ifdef HAVE_PPOLL
+
+#ifdef __linux__
+ if (!p->dont_use_ppoll)
+#endif
+ r =3D ppoll(p->pollfd, p->n_pollfd_used, p->interval > 0 ? &p->in=
terval_timespec : NULL, p->rtsig < 0 ? NULL : &p->sigset_unblocked);
+#ifdef __linux__
+ else
+#endif
+
+#else
+ r =3D poll(p->pollfd, p->n_pollfd_used, p->interval > 0 ? p->inter=
val / 1000 : -1);
+#endif
+
+ if (r < 0 && (errno =3D=3D EAGAIN || errno =3D=3D EINTR))
+ r =3D 0;
+
+ for (i =3D p->items; i; i =3D i->next) {
+
+ if (i->dead)
+ continue;
+
+ if (!i->after_cb)
+ continue;
+
+ i->after_cb(i);
+ }
+
+finish:
+
+ p->running =3D 0;
+ =
+ if (p->scan_for_dead) {
+ pa_rtpoll_item *n;
+
+ p->scan_for_dead =3D 0;
+ =
+ for (i =3D p->items; i; i =3D n) {
+ n =3D i->next;
+
+ if (i->dead)
+ rtpoll_item_destroy(i);
+ }
+ }
+
+ return r;
+}
+
+void pa_rtpoll_set_itimer(pa_rtpoll *p, pa_usec_t usec) {
+ pa_assert(p);
+
+ p->interval =3D usec;
+
+#ifdef HAVE_PPOLL
+ pa_timespec_store(&p->interval_timespec, usec);
+
+#ifdef __linux__
+ if (!p->dont_use_ppoll) {
+#endif
+ =
+ if (p->timer =3D=3D (timer_t) -1) {
+ struct sigevent se;
+
+ memset(&se, 0, sizeof(se));
+ se.sigev_notify =3D SIGEV_SIGNAL;
+ se.sigev_signo =3D p->rtsig;
+
+ if (timer_create(CLOCK_MONOTONIC, &se, &p->timer) < 0)
+ if (timer_create(CLOCK_REALTIME, &se, &p->timer) < 0) {
+ pa_log_warn("Failed to allocate POSIX timer: %s", pa_c=
strerror(errno));
+ p->timer =3D (timer_t) -1;
+ }
+ }
+
+ if (p->timer !=3D (timer_t) -1) {
+ struct itimerspec its;
+
+ memset(&its, 0, sizeof(its));
+ pa_timespec_store(&its.it_value, usec);
+ pa_timespec_store(&its.it_interval, usec);
+
+ assert(timer_settime(p->timer, 0, &its, NULL) =3D=3D 0);
+ }
+
+#ifdef __linux__
+ }
+#endif
+ =
+#endif
+}
+
+pa_rtpoll_item *pa_rtpoll_item_new(pa_rtpoll *p, unsigned n_fds) {
+ pa_rtpoll_item *i;
+ =
+ pa_assert(p);
+ pa_assert(n_fds > 0);
+
+ if (!(i =3D pa_flist_pop(PA_STATIC_FLIST_GET(items))))
+ i =3D pa_xnew(pa_rtpoll_item, 1);
+
+ i->rtpoll =3D p;
+ i->dead =3D 0;
+ i->n_pollfd =3D n_fds;
+ i->pollfd =3D NULL;
+
+ i->userdata =3D NULL;
+ i->before_cb =3D NULL;
+ i->after_cb =3D NULL;
+ =
+ PA_LLIST_PREPEND(pa_rtpoll_item, p->items, i);
+
+ p->rebuild_needed =3D 1;
+ p->n_pollfd_used +=3D n_fds;
+
+ return i;
+}
+
+void pa_rtpoll_item_free(pa_rtpoll_item *i) {
+ pa_assert(i);
+
+ if (i->rtpoll->running) {
+ i->dead =3D 1;
+ i->rtpoll->scan_for_dead =3D 1;
+ return;
+ }
+
+ rtpoll_item_destroy(i);
+}
+
+struct pollfd *pa_rtpoll_item_get_pollfd(pa_rtpoll_item *i, unsigned *n_fd=
s) {
+ pa_assert(i);
+
+ if (i->rtpoll->rebuild_needed)
+ rtpoll_rebuild(i->rtpoll);
+ =
+ if (n_fds)
+ *n_fds =3D i->n_pollfd;
+ =
+ return i->pollfd;
+}
+
+void pa_rtpoll_item_set_before_callback(pa_rtpoll_item *i, int (*before_cb=
)(pa_rtpoll_item *i)) {
+ pa_assert(i);
+
+ i->before_cb =3D before_cb;
+}
+
+void pa_rtpoll_item_set_after_callback(pa_rtpoll_item *i, void (*after_cb)=
(pa_rtpoll_item *i)) {
+ pa_assert(i);
+
+ i->after_cb =3D after_cb;
+}
+
+void pa_rtpoll_item_set_userdata(pa_rtpoll_item *i, void *userdata) {
+ pa_assert(i);
+
+ i->userdata =3D userdata;
+}
Added: branches/lennart/src/pulsecore/rtpoll.h
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/pulsecore/=
rtpoll.h?rev=3D1689&root=3Dpulseaudio&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/lennart/src/pulsecore/rtpoll.h (added)
+++ branches/lennart/src/pulsecore/rtpoll.h Wed Aug 22 02:24:12 2007
@@ -1,0 +1,71 @@
+#ifndef foopulsertpollhfoo
+#define foopulsertpollhfoo
+
+/* $Id$ */
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2004-2006 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <poll.h>
+#include <sys/types.h>
+
+#include <pulse/sample.h>
+
+/* An implementation of a "real-time" poll loop. Basically, this is
+ * yet another wrapper around poll(). However it has certain
+ * advantages over pa_mainloop and suchlike:
+ *
+ * 1) It uses timer_create() and POSIX real time signals to guarantee
+ * optimal high-resolution timing. Starting with Linux 2.6.21 hrtimers
+ * are available, and since right now only nanosleep() and the POSIX
+ * clock and timer interfaces have been ported to hrtimers (and not
+ * ppoll/pselect!) we have to combine ppoll() with timer_create(). The
+ * fact that POSIX timers and POSIX rt signals are used internally is
+ * completely hidden.
+ *
+ * 2) It allows raw access to the pollfd data to users
+ *
+ * 3) It allows arbitrary functions to be run before entering the
+ * actual poll() and after it.
+ *
+ * Only a single interval timer is supported..*/
+
+typedef struct pa_rtpoll pa_rtpoll;
+typedef struct pa_rtpoll_item pa_rtpoll_item;
+
+pa_rtpoll *pa_rtpoll_new(void);
+void pa_rtpoll_free(pa_rtpoll *p);
+
+void pa_rtpoll_install(pa_rtpoll *p);
+
+int pa_rtpoll_run(pa_rtpoll *f);
+void pa_rtpoll_set_itimer(pa_rtpoll *p, pa_usec_t usec);
+
+pa_rtpoll_item *pa_rtpoll_item_new(pa_rtpoll *p, unsigned n_fds);
+void pa_rtpoll_item_free(pa_rtpoll_item *i);
+
+struct pollfd *pa_rtpoll_item_get_pollfd(pa_rtpoll_item *i, unsigned *n_fd=
s);
+
+void pa_rtpoll_item_set_before_callback(pa_rtpoll_item *i, int (*before_cb=
)(pa_rtpoll_item *i));
+void pa_rtpoll_item_set_after_callback(pa_rtpoll_item *i, void (*after_cb)=
(pa_rtpoll_item *i));
+void pa_rtpoll_item_set_userdata(pa_rtpoll_item *i, void *userdata);
+
+#endif
More information about the pulseaudio-commits
mailing list