]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/io.c
moved invite/ban lists to channel structure
[ngircd-alex.git] / src / ngircd / io.c
index 39b994b22fb67b19d41f52339e9d3f8f300ead94..e331464647d5c6b9e8141febb591fa426de8311d 100644 (file)
@@ -12,7 +12,7 @@
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: io.c,v 1.17 2006/09/16 14:49:26 fw Exp $";
+static char UNUSED id[] = "$Id: io.c,v 1.21 2006/09/19 18:21:30 fw Exp $";
 
 #include <assert.h>
 #include <stdlib.h>
@@ -43,7 +43,15 @@ typedef struct {
 # ifdef HAVE_KQUEUE
 #define IO_USE_KQUEUE   1
 # else
+#  ifdef HAVE_SYS_DEVPOLL_H
+#define IO_USE_DEVPOLL  1
+#   else
+#    ifdef HAVE_POLL
+#define IO_USE_POLL     1
+#        else
 #define IO_USE_SELECT   1
+#      endif /* HAVE_POLL */
+#    endif /* HAVE_SYS_DEVPOLL_H */
 # endif /* HAVE_KQUEUE */
 #endif /* HAVE_EPOLL_CREATE */
 
@@ -67,6 +75,22 @@ static int io_dispatch_kqueue(struct timeval *tv);
 static bool io_event_change_kqueue(int, short, const int action);
 #endif
 
+#ifdef IO_USE_POLL
+#include <poll.h>
+
+static array pollfds;
+static int poll_maxfd;
+
+static bool io_event_change_poll(int fd, short what);
+#endif
+
+#ifdef IO_USE_DEVPOLL
+#include <sys/devpoll.h>
+static int io_masterfd;
+
+static bool io_event_change_devpoll(int fd, short what);
+#endif
+
 #ifdef IO_USE_SELECT
 #include "defines.h"   /* for conn.h */
 #include "conn.h"      /* for CONN_IDX (needed by resolve.h) */
@@ -98,8 +122,43 @@ io_event_get(int fd)
 }
 
 
+#ifdef IO_USE_DEVPOLL
+static void
+io_library_init_devpoll(unsigned int eventsize)
+{
+       io_masterfd = open("/dev/poll", O_RDWR);
+       if (io_masterfd >= 0)
+               library_initialized = true;
+       Log(LOG_INFO, "IO subsystem: /dev/poll (initial maxfd %u, masterfd %d).",
+               eventsize, io_masterfd);
+}
+#endif
+
+
+#ifdef IO_USE_POLL
+static void
+io_library_init_poll(unsigned int eventsize)
+{
+       struct pollfd *p;
+       array_init(&pollfds);
+       poll_maxfd = 0;
+       Log(LOG_INFO, "IO subsystem: poll (initial maxfd %u).",
+           eventsize);
+       p = array_alloc(&pollfds, sizeof(struct pollfd), eventsize);
+       if (p) {
+               unsigned i;
+               p = array_start(&pollfds);
+               for (i = 0; i < eventsize; i++)
+                       p[i].fd = -1;
+
+               library_initialized = true;
+       }
+}
+#endif
+
+
 #ifdef IO_USE_SELECT
-static bool
+static void
 io_library_init_select(unsigned int eventsize)
 {
        Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
@@ -116,16 +175,14 @@ io_library_init_select(unsigned int eventsize)
        }
 #endif /* FD_SETSIZE */
        library_initialized = true;
-       return true;
 }
 #endif /* SELECT */
 
 
 #ifdef IO_USE_EPOLL
-static bool
+static void
 io_library_init_epoll(unsigned int eventsize)
 {
-       bool ret;
        int ecreate_hint = (int)eventsize;
        if (ecreate_hint <= 0)
                ecreate_hint = 128;
@@ -133,27 +190,23 @@ io_library_init_epoll(unsigned int eventsize)
        Log(LOG_INFO,
            "IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
            ecreate_hint, eventsize, io_masterfd);
-       ret = io_masterfd >= 0;
-       if (ret) library_initialized = true;
-
-       return ret;
+       if (io_masterfd >= 0)
+               library_initialized = true;
 }
 #endif
 
 
 #ifdef IO_USE_KQUEUE
-static bool
+static void
 io_library_init_kqueue(unsigned int eventsize)
 {
-       bool ret;
        io_masterfd = kqueue();
 
        Log(LOG_INFO,
            "IO subsystem: kqueue (initial maxfd %u, masterfd %d)",
            eventsize, io_masterfd);
-       ret = io_masterfd >= 0;
-       if (ret) library_initialized = true;
-       return ret;
+       if (io_masterfd >= 0)
+               library_initialized = true;
 }
 #endif
 
@@ -175,14 +228,21 @@ io_library_init(unsigned int eventsize)
        if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
                eventsize = 0;
 #ifdef IO_USE_EPOLL
-       return io_library_init_epoll(eventsize);
+       io_library_init_epoll(eventsize);
 #endif
 #ifdef IO_USE_KQUEUE
-       return io_library_init_kqueue(eventsize);
+       io_library_init_kqueue(eventsize);
+#endif
+#ifdef IO_USE_DEVPOLL
+       io_library_init_devpoll(eventsize);
+#endif
+#ifdef IO_USE_POLL
+       io_library_init_poll(eventsize);
 #endif
 #ifdef IO_USE_SELECT
-       return io_library_init_select(eventsize);
+       io_library_init_select(eventsize);
 #endif
+       return library_initialized;
 }
 
 
@@ -243,6 +303,12 @@ io_event_create(int fd, short what, void (*cbfunc) (int, short))
 
        i->callback = cbfunc;
        i->what = 0;
+#ifdef IO_USE_DEVPOLL
+       ret = io_event_change_devpoll(fd, what);
+#endif
+#ifdef IO_USE_POLL
+       ret = io_event_change_poll(fd, what);
+#endif
 #ifdef IO_USE_EPOLL
        ret = io_event_change_epoll(fd, what, EPOLL_CTL_ADD);
 #endif
@@ -257,6 +323,49 @@ io_event_create(int fd, short what, void (*cbfunc) (int, short))
 }
 
 
+#ifdef IO_USE_DEVPOLL
+static bool
+io_event_change_devpoll(int fd, short what)
+{
+       struct pollfd p;
+
+       p.events = 0;
+
+       if (what & IO_WANTREAD)
+               p.events = POLLIN | POLLPRI;
+       if (what & IO_WANTWRITE)
+               p.events |= POLLOUT;
+
+       p.fd = fd;
+       return write(io_masterfd, &p, sizeof p) == (ssize_t)sizeof p;
+}
+#endif
+
+
+
+#ifdef IO_USE_POLL
+static bool
+io_event_change_poll(int fd, short what)
+{
+       struct pollfd *p;
+       short events = 0;
+
+       if (what & IO_WANTREAD)
+               events = POLLIN | POLLPRI;
+       if (what & IO_WANTWRITE)
+               events |= POLLOUT;
+
+       p = array_alloc(&pollfds, sizeof *p, fd);
+       if (p) {
+               p->events = events;
+               p->fd = fd;
+               if (fd > poll_maxfd)
+                       poll_maxfd = fd;
+       }
+       return p != NULL;
+}
+#endif
+
 #ifdef IO_USE_EPOLL
 static bool
 io_event_change_epoll(int fd, short what, const int action)
@@ -347,6 +456,12 @@ io_event_add(int fd, short what)
 #ifdef IO_USE_KQUEUE
        return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
 #endif
+#ifdef IO_USE_DEVPOLL
+       return io_event_change_devpoll(fd, i->what);
+#endif
+#ifdef IO_USE_POLL
+       return io_event_change_poll(fd, i->what);
+#endif
 #ifdef IO_USE_SELECT
        if (fd > select_maxfd)
                select_maxfd = fd;
@@ -377,6 +492,44 @@ io_setnonblock(int fd)
 }
 
 
+#ifdef IO_USE_DEVPOLL
+static void
+io_close_devpoll(int fd)
+{
+       struct pollfd p;
+       p.events = POLLREMOVE;
+       p.fd = fd;
+       write(io_masterfd, &p, sizeof p);
+}
+#else
+static inline void io_close_devpoll(int UNUSED x) { /* NOTHING */ }
+#endif
+
+
+
+#ifdef IO_USE_POLL
+static void
+io_close_poll(int fd)
+{
+       struct pollfd *p;
+       p = array_get(&pollfds, sizeof *p, fd);
+       if (!p) return;
+
+       p->fd = -1;
+       if (fd == poll_maxfd) {
+               while (poll_maxfd > 0) {
+                       --poll_maxfd;
+                       p = array_get(&pollfds, sizeof *p, poll_maxfd);
+                       if (p && p->fd >= 0)
+                               break;
+               }
+       }
+}
+#else
+static inline void io_close_poll(int UNUSED x) { /* NOTHING */ }
+#endif
+
+
 #ifdef IO_USE_SELECT
 static void
 io_close_select(int fd)
@@ -420,6 +573,8 @@ io_close(int fd)
        }
 #endif
 
+       io_close_devpoll(fd);
+       io_close_poll(fd);
        io_close_select(fd);
 
 #ifdef IO_USE_EPOLL
@@ -444,6 +599,12 @@ io_event_del(int fd, short what)
 
        i->what &= ~what;
 
+#ifdef IO_USE_DEVPOLL
+       return io_event_change_devpoll(fd, i->what);
+#endif
+#ifdef IO_USE_POLL
+       return io_event_change_poll(fd, i->what);
+#endif
 #ifdef IO_USE_EPOLL
        return io_event_change_epoll(fd, i->what, EPOLL_CTL_MOD);
 #endif
@@ -500,6 +661,92 @@ io_dispatch_select(struct timeval *tv)
 #endif
 
 
+#ifdef IO_USE_DEVPOLL
+static int
+io_dispatch_devpoll(struct timeval *tv)
+{
+       struct dvpoll dvp;
+       time_t sec = tv->tv_sec * 1000;
+       int i, total, ret, timeout = tv->tv_usec + sec;
+       short what;
+       struct pollfd p[100];
+
+       if (timeout < 0)
+               timeout = 1000;
+
+       total = 0;
+       do {
+               dvp.dp_timeout = timeout;
+               dvp.dp_nfds = 100;
+               dvp.dp_fds = p;
+               ret = ioctl(io_masterfd, DP_POLL, &dvp);
+               total += ret;
+               if (ret <= 0)
+                       return total;
+               for (i=0; i < ret ; i++) {
+                       what = 0;
+                       if (p[i].revents & (POLLIN|POLLPRI))
+                               what = IO_WANTREAD;
+
+                       if (p[i].revents & POLLOUT)
+                               what |= IO_WANTWRITE;
+
+                       if (p[i].revents && !what) {
+                               /* other flag is set, probably POLLERR */
+                               what = IO_ERROR;
+                       }
+                       io_docallback(p[i].fd, what);
+               }
+       } while (ret == 100);
+
+       return total;
+}
+#endif
+
+
+#ifdef IO_USE_POLL
+static int
+io_dispatch_poll(struct timeval *tv)
+{
+       time_t sec = tv->tv_sec * 1000;
+       int i, ret, timeout = tv->tv_usec + sec;
+       int fds_ready;
+       short what;
+       struct pollfd *p = array_start(&pollfds);
+
+       if (timeout < 0)
+               timeout = 1000;
+
+       ret = poll(p, poll_maxfd + 1, timeout);
+       if (ret <= 0)
+               return ret;
+
+       fds_ready = ret;
+       for (i=0; i <= poll_maxfd; i++) {
+               what = 0;
+               if (p[i].revents & (POLLIN|POLLPRI))
+                       what = IO_WANTREAD;
+
+               if (p[i].revents & POLLOUT)
+                       what |= IO_WANTWRITE;
+
+               if (p[i].revents && !what) {
+                       /* other flag is set, probably POLLERR */
+                       what = IO_ERROR;
+               }
+               if (what) {
+                       fds_ready--;
+                       io_docallback(i, what);
+               }
+               if (fds_ready <= 0)
+                       break;
+       }
+
+       return ret;
+}
+#endif
+
+
 #ifdef IO_USE_EPOLL
 static int
 io_dispatch_epoll(struct timeval *tv)
@@ -616,6 +863,12 @@ io_dispatch(struct timeval *tv)
 #ifdef IO_USE_KQUEUE
        return io_dispatch_kqueue(tv);
 #endif
+#ifdef IO_USE_DEVPOLL
+       return io_dispatch_devpoll(tv);
+#endif
+#ifdef IO_USE_POLL
+       return io_dispatch_poll(tv);
+#endif
 #ifdef IO_USE_EPOLL
        return io_dispatch_epoll(tv);
 #endif