--- gvpe/src/iom.C 2003/03/28 05:40:54 1.7 +++ gvpe/src/iom.C 2004/03/28 01:43:50 1.18 @@ -1,5 +1,6 @@ /* - iom.C -- I/O multiplexor + iom.C -- generic I/O multiplexor + Copyright (C) 2003, 2004 Marc Lehmann This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -16,123 +17,106 @@ Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "config.h" +#include +#include +#include #include -#include -#include +#if 1 // older unices need these includes for select (2) +# include +# include +#endif + +// if the BSDs would at least be marginally POSIX-compatible.. *sigh* +// until that happens, sys/select.h must come last +#include -#include "slog.h" #include "iom.h" -inline bool lowest_first (const time_watcher *a, const time_watcher *b) -{ - return a->at > b->at; -} +// TSTAMP_MAX must still fit into a positive struct timeval +#define TSTAMP_MAX (double)(1UL<<31) -tstamp NOW; - -io_manager iom; - -void time_watcher::set (tstamp when) -{ - at = when; - - if (registered) - iom.reschedule_time_watchers (); - else - iom.reg (this); -} - -void time_watcher::trigger () -{ - call (at); +// this is a dummy time watcher to ensure that the first +// time watcher is _always_ valid, this gets rid of a lot +// of null-pointer-checks +// (must come _before_ iom is being defined) +static struct tw0 : time_watcher + { + void cb (time_watcher &w) + { + // should never get called + // reached end-of-time, or tstamp has a bogus definition, + // or compiler initilization order broken, or somethine else :) + abort (); + } - if (registered) - iom.reschedule_time_watchers (); - else - iom.reg (this); -} + tw0 () + : time_watcher (this, &tw0::cb) + { }} +tw0; -void time_watcher::start () -{ - if (!registered) - iom.reg (this); -} +tstamp NOW; +static bool iom_valid; +io_manager iom; -void io_manager::reg (int fd, short events, io_watcher *w) +template +void io_manager::reg (watcher *w, io_manager_vec &queue) { - pollfd pfd; + if (!iom_valid) + abort (); - pfd.fd = fd; - pfd.events = events; - - pfs.push_back (pfd); - iow.push_back (w); + if (!w->active) + { +#if IOM_CHECK + queue.activity = true; +#endif + queue.push_back (w); + w->active = queue.size (); + } } -void io_manager::unreg (io_watcher *w) +template +void io_manager::unreg (watcher *w, io_manager_vec &queue) { - unsigned int sz = iow.size (); - unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); + if (!iom_valid) + return; - if (i != sz) + if (w->active) { - if (sz == 1) - { - pfs.clear (); - iow.clear (); - } - else if (i == sz - 1) - { - iow.pop_back (); - pfs.pop_back (); - } - else - { - iow[i] = iow[sz - 1]; iow.pop_back (); - pfs[i] = pfs[sz - 1]; pfs.pop_back (); - } + queue [w->active - 1] = 0; + w->active = 0; } } -void io_manager::reschedule_time_watchers () -{ - make_heap (tw.begin (), tw.end (), lowest_first); -} - -void io_manager::reg (time_watcher *w) +#if IOM_TIME +void time_watcher::trigger () { - if (w->registered) - slog (L_CRIT, "FATAL: io_manager::reg(time_watcher) called on already-registered watcher"); + call (*this); - w->registered = true; - - tw.push_back (w); - push_heap (tw.begin (), tw.end (), lowest_first); + iom.reg (this); } -void io_manager::unreg (time_watcher *w) -{ - if (w->registered) - { - unsigned int sz = tw.size (); - unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); +void io_manager::reg (time_watcher *w) { reg (w, tw); } +void io_manager::unreg (time_watcher *w) { unreg (w, tw); } +#endif - if (i != sz) - { - if (i != sz - 1) - tw[i] = tw[sz - 1]; +#if IOM_IO +void io_manager::reg (io_watcher *w) { reg (w, iow); } +void io_manager::unreg (io_watcher *w) { unreg (w, iow); } +#endif - tw.pop_back (); - reschedule_time_watchers (); - } +#if IOM_CHECK +void io_manager::reg (check_watcher *w) { reg (w, cw); } +void io_manager::unreg (check_watcher *w) { unreg (w, cw); } +#endif - w->registered = false; - } -} +#if IOM_IDLE +void io_manager::reg (idle_watcher *w) { reg (w, iw); } +void io_manager::unreg (idle_watcher *w) { unreg (w, iw); } +#endif +#if IOM_TIME inline void set_now (void) { struct timeval tv; @@ -140,61 +124,161 @@ gettimeofday (&tv, 0); NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; +#endif } void io_manager::loop () { +#if IOM_TIME set_now (); +#endif for (;;) { - while (tw[0]->at <= NOW) + struct timeval *to = 0; + struct timeval tval; + +#if IOM_IDLE + if (iw.size ()) { - // remove the first watcher - time_watcher *w = tw[0]; + tval.tv_sec = 0; + tval.tv_usec = 0; + to = &tval; + } + else +#endif + + { +#if IOM_TIME + time_watcher *next; - pop_heap (tw.begin (), tw.end (), lowest_first); - tw.pop_back (); + for (;;) + { + next = tw[0]; // the first time-watcher must exist at ALL times + + for (int i = tw.size (); i--; ) + if (!tw[i]) + tw.erase_unordered (i); + else if (tw[i]->at < next->at) + next = tw[i]; + + if (next->at > NOW) + { + if (next != tw[0]) + { + double diff = next->at - NOW; + tval.tv_sec = (int)diff; + tval.tv_usec = (int) ((diff - tval.tv_sec) * 1000000); + to = &tval; + } + break; + } + else + { + unreg (next); + next->call (*next); + } + } +#endif - w->registered = false; + } + +#if IOM_CHECK + tw.activity = false; - // call it - w->call (w->at); + for (int i = cw.size (); i--; ) + if (!cw[i]) + cw.erase_unordered (i); + else + cw[i]->call (*cw[i]); - // re-add it if necessary - if (w->at >= 0 && !w->registered) - reg (w); + if (tw.activity) + { + tval.tv_sec = 0; + tval.tv_usec = 0; + to = &tval; } +#endif - int timeout = (int) ((tw[0]->at - NOW) * 1000); +#if IOM_IO + fd_set rfd, wfd; - int fds = poll (&pfs[0], pfs.size (), timeout); + FD_ZERO (&rfd); + FD_ZERO (&wfd); - set_now (); + int fds = 0; - for (unsigned int i = iow.size (); fds > 0 && i--; ) - if (pfs[i].revents) + for (io_manager_vec::iterator i = iow.end (); i-- > iow.begin (); ) + if (*i) { - --fds; - iow[i]->call (pfs[i].revents); + if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); + if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); + + if ((*i)->fd >= fds) fds = (*i)->fd + 1; } - } -} -void io_manager::idle_cb (tstamp &ts) -{ - ts = NOW + 86400; // wake up every day, for no good reason + if (!to && !fds) //TODO: also check idle_watchers and check_watchers + break; // no events + + fds = select (fds, &rfd, &wfd, &efd, to); +# if IOM_TIME + set_now (); +# endif + + if (fds > 0) + for (int i = iow.size (); i--; ) + if (!iow[i]) + iow.erase_unordered (i); + else + { + short revents = iow[i]->events; + + if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ; + if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE; + + if (revents) + iow[i]->call (*iow[i], revents); + } + else if (fds < 0 && errno != EINTR) + { + perror ("Error while waiting for I/O or time event"); + abort (); + } +#if IOM_IDLE + else + for (int i = iw.size (); i--; ) + if (!iw[i]) + iw.erase_unordered (i); + else + iw[i]->call (*iw[i]); +#endif + +#elif IOM_TIME + if (!to) + break; + + select (0, 0, 0, 0, &to); + set_now (); +#else + break; +#endif + + } } io_manager::io_manager () { + iom_valid = true; + +#if IOM_TIME set_now (); - idle = new time_watcher (this, &io_manager::idle_cb); - idle->start (0); + + tw0.start (TSTAMP_MAX); +#endif } io_manager::~io_manager () { - // + iom_valid = false; }