From 5d047523c87ba11aad8c384f7ffde25b4dd746ed Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 12 Aug 2005 19:09:50 +0000 Subject: implement new main loop abstraction layer git-svn-id: file:///home/lennart/svn/public/avahi/trunk@305 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-common/Makefile.am | 33 ++++- avahi-common/simple-watch.c | 324 ++++++++++++++++++++++++++++++++++++++++++++ avahi-common/simple-watch.h | 44 ++++++ avahi-common/timeval-test.c | 41 ++++++ avahi-common/timeval.c | 101 ++++++++++++++ avahi-common/timeval.h | 43 ++++++ avahi-common/watch-test.c | 81 +++++++++++ avahi-common/watch.h | 57 ++++++++ 8 files changed, 717 insertions(+), 7 deletions(-) create mode 100644 avahi-common/simple-watch.c create mode 100644 avahi-common/simple-watch.h create mode 100644 avahi-common/timeval-test.c create mode 100644 avahi-common/timeval.c create mode 100644 avahi-common/timeval.h create mode 100644 avahi-common/watch-test.c create mode 100644 avahi-common/watch.h (limited to 'avahi-common') diff --git a/avahi-common/Makefile.am b/avahi-common/Makefile.am index d91343b..c78e054 100644 --- a/avahi-common/Makefile.am +++ b/avahi-common/Makefile.am @@ -19,10 +19,6 @@ AM_CFLAGS=-I$(top_srcdir) -# GLIB 2.0 -AM_CFLAGS+=$(GLIB20_CFLAGS) -AM_LDADD=$(GLIB20_LIBS) - # This cool debug trap works on i386/gcc only AM_CFLAGS+='-DDEBUG_TRAP=__asm__("int $$3")' @@ -36,6 +32,9 @@ avahi_commoninclude_HEADERS = \ cdecl.h \ defs.h \ malloc.h + watch.h \ + timeval.h \ + simple-watch.h noinst_HEADERS = llist.h @@ -43,11 +42,12 @@ if ENABLE_DBUS noinst_HEADERS += dbus.h endif - noinst_PROGRAMS = \ strlst-test \ domain-test \ - alternative-test + alternative-test \ + timeval-test \ + watch-test lib_LTLIBRARIES = \ libavahi-common.la @@ -58,7 +58,10 @@ libavahi_common_la_SOURCES = \ alternative.c alternative.h \ error.c error.h \ strlst.c strlst.h \ - domain.c domain.h + domain.c domain.h \ + timeval.c timeval.h \ + simple-watch.c simple-watch.h \ + watch.h libavahi_common_la_CFLAGS = $(AM_CFLAGS) libavahi_common_la_LIBADD = $(AM_LDADD) @@ -84,3 +87,19 @@ domain_test_SOURCES = \ domain_test_CFLAGS = $(AM_CFLAGS) domain_test_LDADD = $(AM_LDADD) +watch_test_SOURCES = \ + timeval.c timeval.h \ + simple-watch.c simple-watch.h \ + watch.h \ + malloc.c malloc.h \ + watch-test.c +watch_test_CFLAGS = $(AM_CFLAGS) +watch_test_LDADD = $(AM_LDADD) + +timeval_test_SOURCES = \ + timeval.c timeval.h \ + timeval-test.c +timeval_test_CFLAGS = $(AM_CFLAGS) +timeval_test_LDADD = $(AM_LDADD) + + diff --git a/avahi-common/simple-watch.c b/avahi-common/simple-watch.c new file mode 100644 index 0000000..acc0923 --- /dev/null +++ b/avahi-common/simple-watch.c @@ -0,0 +1,324 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi 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. + + avahi 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 avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +#include +#include + +#include "simple-watch.h" + +struct AvahiWatch { + AvahiSimplePoll *simple_poll; + int dead; + int idx; + struct pollfd pollfd; + AvahiWatchCallback callback; + void *userdata; + + AVAHI_LLIST_FIELDS(AvahiWatch, watches); +}; + +struct AvahiSimplePoll { + AvahiPoll api; + + struct pollfd* pollfds; + int n_pollfds, max_pollfds, rebuild_pollfds; + + struct timeval wakeup; + int use_wakeup; + + int req_cleanup; + + int quit; + + int n_watches; + AVAHI_LLIST_HEAD(AvahiWatch, watches); +}; + +static AvahiWatch* watch_new(AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) { + AvahiWatch *w; + AvahiSimplePoll *s; + + assert(api); + assert(fd >= 0); + assert(callback); + + s = api->userdata; + assert(s); + + if (!(w = avahi_new(AvahiWatch, 1))) + return NULL; + + w->simple_poll = s; + w->pollfd.fd = fd; + w->pollfd.events = event; + w->callback = callback; + w->userdata = userdata; + w->dead = 0; + + if (s->n_pollfds < s->max_pollfds) { + /* If there's space for this pollfd, go on and allocate it */ + w->idx = s->n_pollfds++; + s->pollfds[w->idx] = w->pollfd; + + } else { + /* Unfortunately there's no place for this pollfd, so request a rebuild of the array */ + w->idx = -1; + s->rebuild_pollfds = 1; + } + + AVAHI_LLIST_PREPEND(AvahiWatch, watches, s->watches, w); + s->n_watches++; + + return w; +} + +static void watch_update(AvahiWatch *w, AvahiWatchEvent events) { + assert(w); + assert(!w->dead); + + w->pollfd.events = events; + + if (w->idx != -1) { + assert(w->simple_poll); + w->simple_poll->pollfds[w->idx] = w->pollfd; + } else + w->simple_poll->rebuild_pollfds = 1; +} + +static void remove_pollfd(AvahiWatch *w) { + assert(w); + + if (w->idx == -1) + return; + + if (w->idx == w->simple_poll->n_pollfds-1) { + + /* This pollfd is at the end of the array, so we can easily cut it */ + + assert(w->simple_poll->n_pollfds > 0); + w->simple_poll->n_pollfds -= 1; + } else + + /* Unfortunately this pollfd is in the middle of the array, so request a rebuild of it */ + w->simple_poll->rebuild_pollfds = 1; +} + +static void watch_free(AvahiWatch *w) { + assert(w); + assert(!w->dead); + + remove_pollfd(w); + + w->dead = 1; + w->simple_poll->n_watches --; + w->simple_poll->req_cleanup = 1; +} + +static void set_wakeup_time(AvahiPoll *api, const struct timeval *tv) { + AvahiSimplePoll *s; + + assert(api); + s = api->userdata; + + if (tv) { + s->wakeup = *tv; + s->use_wakeup = 1; + } else + s->use_wakeup = 0; +} + +static void destroy_watch(AvahiWatch *w) { + assert(w); + + remove_pollfd(w); + AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->simple_poll->watches, w); + + if (!w->dead) + w->simple_poll->n_watches --; + + avahi_free(w); +} + +static void cleanup(AvahiSimplePoll *s, int all) { + AvahiWatch *w, *next; + assert(s); + + for (w = s->watches; w; w = next) { + next = w->watches_next; + + if (all || w->dead) + destroy_watch(w); + } + + s->req_cleanup = 0; +} + +AvahiSimplePoll *avahi_simple_poll_new(void) { + AvahiSimplePoll *s; + + if (!(s = avahi_new(AvahiSimplePoll, 1))) + return NULL; + + s->api.userdata = s; + s->api.watch_new = watch_new; + s->api.watch_free = watch_free; + s->api.watch_update = watch_update; + s->api.set_wakeup_time = set_wakeup_time; + s->pollfds = NULL; + s->max_pollfds = s->n_pollfds = 0; + s->use_wakeup = 0; + s->rebuild_pollfds = 0; + s->quit = 0; + s->n_watches = 0; + s->req_cleanup = 0; + + AVAHI_LLIST_HEAD_INIT(AvahiWatch, s->watches); + + return s; +} + +void avahi_simple_poll_free(AvahiSimplePoll *s) { + assert(s); + + cleanup(s, 1); + + assert(s->n_watches == 0); + + avahi_free(s->pollfds); + avahi_free(s); +} + +static int rebuild(AvahiSimplePoll *s) { + AvahiWatch *w; + int idx; + + assert(s); + + if (s->n_watches > s->max_pollfds) { + struct pollfd *n; + + s->max_pollfds = s->n_watches + 10; + + if (!(n = avahi_realloc(s->pollfds, sizeof(struct pollfd) * s->max_pollfds))) + return -1; + + s->pollfds = n; + } + + for (idx = 0, w = s->watches; w; w = w->watches_next) { + + if(w->dead) + continue; + + assert(w->idx < s->max_pollfds); + s->pollfds[w->idx = idx++] = w->pollfd; + } + + s->n_pollfds = idx; + + s->rebuild_pollfds = 0; + + return 0; +} + +int avahi_simple_poll_iterate(AvahiSimplePoll *s, int block) { + int timeout, r, ret = 0; + assert(s); + + if (s->quit) + return 1; + + if (s->req_cleanup) + cleanup(s, 0); + + if (s->rebuild_pollfds) + if (rebuild(s) < 0) + return -1; + + if (block) { + if (s->use_wakeup) { + struct timeval now; + AvahiUsec usec; + + gettimeofday(&now, NULL); + + usec = avahi_timeval_diff(&s->wakeup, &now); + + timeout = usec <= 0 ? 0 : (int) (usec / 1000); + } else + timeout = -1; + } else + timeout = 0; + + if ((r = poll(s->pollfds, s->n_pollfds, timeout)) < 0) + return -1; + + else if (r > 0) { + AvahiWatch *w; + + for (w = s->watches; w; w = w->watches_next) { + + if (w->dead) + continue; + + assert(w->idx >= 0); + assert(w->idx < s->n_pollfds); + + if (s->pollfds[w->idx].revents > 0) + w->callback(w, w->pollfd.fd, s->pollfds[w->idx].revents, w->userdata); + + if (s->quit) { + ret = 1; + goto finish; + } + } + } + + ret = 0; + +finish: + + if (s->req_cleanup) + cleanup(s, 0); + + return ret; +} + +void avahi_simple_poll_quit(AvahiSimplePoll *w) { + assert(w); + + w->quit = 1; +} + +AvahiPoll* avahi_simple_poll_get(AvahiSimplePoll *s) { + assert(s); + + return &s->api; +} diff --git a/avahi-common/simple-watch.h b/avahi-common/simple-watch.h new file mode 100644 index 0000000..600e5ce --- /dev/null +++ b/avahi-common/simple-watch.h @@ -0,0 +1,44 @@ +#ifndef foosimplewatchhfoo +#define foosimplewatchhfoo + +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi 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. + + avahi 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 avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#include + +#include "watch.h" + +AVAHI_C_DECL_BEGIN + +typedef struct AvahiSimplePoll AvahiSimplePoll; + +AvahiSimplePoll *avahi_simple_poll_new(void); +void avahi_simple_poll_free(AvahiSimplePoll *s); + +AvahiPoll* avahi_simple_poll_get(AvahiSimplePoll *s); + +int avahi_simple_poll_iterate(AvahiSimplePoll *s, int block); + +void avahi_simple_poll_quit(AvahiSimplePoll *s); + +AVAHI_C_DECL_END + +#endif diff --git a/avahi-common/timeval-test.c b/avahi-common/timeval-test.c new file mode 100644 index 0000000..7dedf41 --- /dev/null +++ b/avahi-common/timeval-test.c @@ -0,0 +1,41 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi 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. + + avahi 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 avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include "timeval.h" + +int main(int argc, char *argv[]) { + + struct timeval a = { 5, 5 }, b; + + b = a; + + printf("%li.%li\n", a.tv_sec, a.tv_usec); + avahi_timeval_add(&a, -50); + + printf("%li.%li\n", a.tv_sec, a.tv_usec); + + printf("%lli\n", avahi_timeval_diff(&a, &b)); +} diff --git a/avahi-common/timeval.c b/avahi-common/timeval.c new file mode 100644 index 0000000..e5732cd --- /dev/null +++ b/avahi-common/timeval.c @@ -0,0 +1,101 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi 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. + + avahi 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 avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +#include "timeval.h" + +int avahi_timeval_compare(const struct timeval *a, const struct timeval *b) { + assert(a); + assert(b); + + if (a->tv_sec < b->tv_sec) + return -1; + + if (a->tv_sec > b->tv_sec) + return 1; + + if (a->tv_usec < b->tv_usec) + return -1; + + if (a->tv_usec > b->tv_usec) + return 1; + + return 0; +} + +AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b) { + assert(a); + assert(b); + + if (avahi_timeval_compare(a, b) < 0) + return - avahi_timeval_diff(b, a); + + return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec; +} + +struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec) { + AvahiUsec u; + assert(a); + + u = usec + a->tv_usec; + + if (u < 0) { + a->tv_usec = (long) (1000000 + (u % 1000000)); + a->tv_sec += (long) (-1 + (u / 1000000)); + } else { + a->tv_usec = (long) (u % 1000000); + a->tv_sec += (long) (u / 1000000); + } + + return a; +} + +AvahiUsec avahi_age(const struct timeval *a) { + struct timeval now; + + assert(a); + + gettimeofday(&now, NULL); + + return avahi_timeval_diff(&now, a); +} + + +struct timeval *avahi_elapse_time(struct timeval *tv, unsigned msec, unsigned jitter) { + assert(tv); + + gettimeofday(tv, NULL); + + if (msec) + avahi_timeval_add(tv, (AvahiUsec) msec*1000); + + if (jitter) + avahi_timeval_add(tv, (AvahiUsec) (jitter*1000.0*rand()/(RAND_MAX+1.0))); + + return tv; +} + diff --git a/avahi-common/timeval.h b/avahi-common/timeval.h new file mode 100644 index 0000000..c046a19 --- /dev/null +++ b/avahi-common/timeval.h @@ -0,0 +1,43 @@ +#ifndef footimevalhfoo +#define footimevalhfoo + +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi 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. + + avahi 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 avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#include +#include + +#include + +AVAHI_C_DECL_BEGIN + +typedef int64_t AvahiUsec; + +int avahi_timeval_compare(const struct timeval *a, const struct timeval *b); +AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b); +struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec); + +AvahiUsec avahi_age(const struct timeval *a); +struct timeval *avahi_elapse_time(struct timeval *tv, unsigned msec, unsigned jitter); + +AVAHI_C_DECL_END + +#endif diff --git a/avahi-common/watch-test.c b/avahi-common/watch-test.c new file mode 100644 index 0000000..f87a303 --- /dev/null +++ b/avahi-common/watch-test.c @@ -0,0 +1,81 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi 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. + + avahi 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 avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include + +#include "watch.h" +#include "simple-watch.h" +#include "timeval.h" + +static AvahiPoll *api = NULL; + +static void callback(AvahiWatch *w, int fd, AvahiWatchEvent event, void *userdata) { + + if (event & AVAHI_WATCH_IN) { + ssize_t r; + char c; + + if ((r = read(fd, &c, 1)) <= 0) { + fprintf(stderr, "read() failed: %s\n", r < 0 ? strerror(errno) : "EOF"); + api->watch_free(w); + return; + } + + printf("Read: %c\n", c >= 32 && c < 127 ? c : '.'); + } +} + +int main(int argc, char *argv[]) { + int i = 0; + AvahiSimplePoll *s; + + s = avahi_simple_poll_new(); + assert(s); + + api = avahi_simple_poll_get(s); + + api->watch_new(api, 0, AVAHI_WATCH_IN, callback, NULL); + + for (;;) { + struct timeval tv; + printf("Iteration %i\n", i++); + + if (i > 100) + avahi_simple_poll_quit(s); + + avahi_elapse_time(&tv, 1000, 0); + + api->set_wakeup_time(api, &tv); + + if (avahi_simple_poll_iterate(s, 1) != 0) + break; + } + + return 0; +} diff --git a/avahi-common/watch.h b/avahi-common/watch.h new file mode 100644 index 0000000..b26c2ba --- /dev/null +++ b/avahi-common/watch.h @@ -0,0 +1,57 @@ +#ifndef foowatchhfoo +#define foowatchhfoo + +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi 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. + + avahi 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 avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#include +#include + +#include "timeval.h" + +AVAHI_C_DECL_BEGIN + +typedef struct AvahiWatch AvahiWatch; +typedef struct AvahiPoll AvahiPoll; + +typedef enum { + AVAHI_WATCH_IN = POLLIN, + AVAHI_WATCH_OUT = POLLOUT, + AVAHI_WATCH_ERR = POLLERR, + AVAHI_WATCH_HUP = POLLHUP +} AvahiWatchEvent; + +typedef void (*AvahiWatchCallback)(AvahiWatch *w, int fd, AvahiWatchEvent event, void *userdata); + +struct AvahiPoll { + void* userdata; + + AvahiWatch* (*watch_new)(AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata); + void (*watch_update)(AvahiWatch *w, AvahiWatchEvent event); + void (*watch_free)(AvahiWatch *w); + + void (*set_wakeup_time)(AvahiPoll *api, const struct timeval *tv); +}; + +AVAHI_C_DECL_END + +#endif + -- cgit