commit 19360c0388b87e3641ef5f33c378e5b3dc03dfb5
Author: Oswald Buddenhagen <o...@kde.org>
Date:   Sun Mar 13 14:29:12 2011 +0100

    add simple mainloop implementation
    
    not used so far

 configure.in |    2 +-
 src/isync.h  |   14 +++++
 src/main.c   |    1 +
 src/util.c   |  152 ++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 168 insertions(+), 1 deletions(-)

diff --git a/configure.in b/configure.in
index 1849994..0bfc364 100644
--- a/configure.in
+++ b/configure.in
@@ -10,7 +10,7 @@ if test "$GCC" = yes; then
     CPPFLAGS="$CPPFLAGS -D_BSD_SOURCE"
 fi
 
-AC_CHECK_HEADERS([sys/filio.h])
+AC_CHECK_HEADERS(sys/filio.h sys/poll.h sys/select.h)
 AC_CHECK_FUNCS(vasprintf)
 
 AC_CHECK_LIB(socket, socket, [SOCK_LIBS="-lsocket"])
diff --git a/src/isync.h b/src/isync.h
index 648d0c3..dd55945 100644
--- a/src/isync.h
+++ b/src/isync.h
@@ -382,6 +382,20 @@ void sort_ints( int *arr, int len );
 void arc4_init( void );
 unsigned char arc4_getbyte( void );
 
+#ifdef HAVE_SYS_POLL_H
+# include <sys/poll.h>
+#else
+# define POLLIN 1
+# define POLLOUT 4
+# define POLLERR 8
+/* POLLHUP? */
+#endif
+
+void add_fd( int fd, void (*cb)( int events, void *aux ), void *aux );
+void conf_fd( int fd, int and_events, int or_events );
+void del_fd( int fd );
+void main_loop( void );
+
 /* sync.c */
 
 extern const char *str_ms[2], *str_hl[2];
diff --git a/src/main.c b/src/main.c
index afe365d..02aef62 100644
--- a/src/main.c
+++ b/src/main.c
@@ -485,6 +485,7 @@ main( int argc, char **argv )
        mvars->argv = argv;
        mvars->cben = 1;
        sync_chans( mvars, E_START );
+       main_loop();
        return mvars->ret;
 }
 
diff --git a/src/util.c b/src/util.c
index 8a718e4..e5b7a2e 100644
--- a/src/util.c
+++ b/src/util.c
@@ -411,3 +411,155 @@ arc4_getbyte( void )
        rs.s[rs.j] = si;
        return rs.s[(si + sj) & 0xff];
 }
+
+
+#ifdef HAVE_SYS_POLL_H
+static struct pollfd *pollfds;
+#else
+# ifdef HAVE_SYS_SELECT_H
+#  include <sys/select.h>
+# endif
+# define pollfds fdparms
+#endif
+static struct {
+       void (*cb)( int what, void *aux );
+       void *aux;
+#ifndef HAVE_SYS_POLL_H
+       int fd, events;
+#endif
+} *fdparms;
+static int npolls, rpolls, deleted;
+
+static int
+find_fd( int fd )
+{
+       int n;
+
+       for (n = 0; n < npolls; n++)
+               if (pollfds[n].fd == fd)
+                       return n;
+       return -1;
+}
+
+void
+add_fd( int fd, void (*cb)( int events, void *aux ), void *aux )
+{
+       int n;
+
+       if (find_fd( fd ) >= 0) {
+               error( "Double-adding FD to event loop.\n" );
+               abort();
+       }
+       n = npolls++;
+       if (rpolls < npolls) {
+               rpolls = npolls;
+#ifdef HAVE_SYS_POLL_H
+               pollfds = nfrealloc(pollfds, npolls * sizeof(*pollfds));
+#endif
+               fdparms = nfrealloc(fdparms, npolls * sizeof(*fdparms));
+       }
+       pollfds[n].fd = fd;
+       pollfds[n].events = 0; /* POLLERR & POLLHUP implicit */
+       fdparms[n].cb = cb;
+       fdparms[n].aux = aux;
+}
+
+void
+conf_fd( int fd, int and_events, int or_events )
+{
+       int n;
+
+       if ((n = find_fd( fd )) < 0) {
+               error( "Configuring unknown FD for event loop.\n" );
+               abort();
+       }
+       pollfds[n].events = (pollfds[n].events & and_events) | or_events;
+}
+
+void
+del_fd( int fd )
+{
+       int n;
+
+       if ((n = find_fd( fd )) < 0) {
+               error( "Deleting unknown FD from event loop.\n" );
+               abort();
+       }
+       npolls--;
+#ifdef HAVE_SYS_POLL_H
+       memcpy(pollfds + n, pollfds + n + 1, (npolls - n) * sizeof(*pollfds));
+#endif
+       memcpy(fdparms + n, fdparms + n + 1, (npolls - n) * sizeof(*fdparms));
+       deleted = 1;
+}
+
+static void
+event_wait( void )
+{
+#ifdef HAVE_SYS_POLL_H
+       int n;
+
+       if (poll( pollfds, npolls, -1 ) < 0) {
+               perror( "poll() failed in event loop" );
+               abort();
+       }
+       for (n = 0; n < npolls; ) {
+               if (pollfds[n].revents) {
+                   fdparms[n].cb( pollfds[n].revents & ~(POLLIN|POLLOUT) ? 
POLLERR : pollfds[n].revents,
+                                  fdparms[n].aux );
+                       if (deleted) {
+                               deleted = 0;
+                               continue;
+                       }
+               }
+               n++;
+       }
+#else
+       fd_set rfds, wfds, efds;
+       int fd, m, n;
+
+       FD_ZERO( &rfds );
+       FD_ZERO( &wfds );
+       FD_ZERO( &efds );
+       m = -1;
+       for (n = 0; n < npolls; n++) {
+               fd = fdparms[n].fd;
+               if (fdparms[n].events & POLLIN)
+                       FD_SET( fd, &rfds );
+               if (fdparms[n].events & POLLOUT)
+                       FD_SET( fd, &wfds );
+               FD_SET( fd, &efds );
+               if (fd > m)
+                       m = fd;
+       }
+       if (select( m + 1, &rfds, &wfds, &efds, 0 ) < 0) {
+               perror( "select() failed in event loop" );
+               abort();
+       }
+       for (n = 0; n < npolls; ) {
+               fd = fdparms[n].fd;
+               m = 0;
+               if (FD_ISSET( fd, &rfds ))
+                       m = POLLIN;
+               if (FD_ISSET( fd, &wfds ))
+                       m |= POLLOUT;
+               if (FD_ISSET( fd, &efds ))
+                       m |= POLLERR;
+               if (m) {
+                       fdparms[n].cb( m, fdparms[n].aux );
+                       if (deleted) {
+                               deleted = 0;
+                               continue;
+                       }
+               }
+               n++;
+       }
+#endif
+}
+
+void
+main_loop( void )
+{
+       while (npolls)
+               event_wait();
+}

------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d
_______________________________________________
isync-devel mailing list
isync-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/isync-devel

Reply via email to