--- rxvt-unicode/src/iom.C 2003/11/25 15:25:17 1.2 +++ rxvt-unicode/src/iom.C 2003/12/19 06:17:03 1.7 @@ -30,6 +30,39 @@ bool iom_valid; io_manager iom; +template +void io_manager::reg (watcher *w, simplevec &queue) +{ + if (find (queue.begin (), queue.end (), w) == queue.end ()) + queue.push_back (w); +} + +template +void io_manager::unreg (watcher *w, simplevec &queue) +{ + queue.erase (find (queue.begin (), queue.end (), w)); +} + +#if IOM_IO +io_watcher::~io_watcher () +{ + if (iom_valid) + iom.unreg (this); +} + +void io_manager::reg (io_watcher *w) +{ + reg (w, iow); +} + +void io_manager::unreg (io_watcher *w) +{ + unreg (w, iow); +} + +#endif + +#if IOM_TIME void time_watcher::trigger () { call (*this); @@ -41,36 +74,58 @@ { if (iom_valid) iom.unreg (this); + + at = TSTAMP_CANCEL; } -io_watcher::~io_watcher () +void io_manager::reg (time_watcher *w) +{ + reg (w, tw); +} + +void io_manager::unreg (time_watcher *w) +{ + unreg (w, tw); +} +#endif + +#if IOM_CHECK +check_watcher::~check_watcher () { if (iom_valid) iom.unreg (this); } -void io_manager::reg (io_watcher *w) +void io_manager::reg (check_watcher *w) { - if (find (iow.begin (), iow.end (), w) == iow.end ()) - iow.push_back (w); + reg (w, cw); } -void io_manager::unreg (io_watcher *w) +void io_manager::unreg (check_watcher *w) { - iow.erase (find (iow.begin (), iow.end (), w)); + unreg (w, cw); } +#endif -void io_manager::reg (time_watcher *w) +#if IOM_IDLE +idle_watcher::~idle_watcher () { - if (find (tw.begin (), tw.end (), w) == tw.end ()) - tw.push_back (w); + if (iom_valid) + iom.unreg (this); } -void io_manager::unreg (time_watcher *w) +void io_manager::reg (idle_watcher *w) +{ + reg (w, iw); +} + +void io_manager::unreg (idle_watcher *w) { - tw.erase (find (tw.begin (), tw.end (), w)); + unreg (w, iw); } +#endif +#if IOM_TIME inline void set_now (void) { struct timeval tv; @@ -78,40 +133,63 @@ 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 (;;) { - time_watcher *w; + struct timeval *to = 0; + struct timeval tval; - for (;;) +#if IOM_IDLE + if (iw.size ()) { - w = tw[0]; - - for (time_watcher **i = tw.begin (); i != tw.end (); ++i) - if ((*i)->at < w->at) - w = *i; - - if (w->at > NOW) - break; - - // call it - w->call (*w); + tval.tv_sec = 0; + tval.tv_usec = 0; + to = &tval; + } + else +#endif + { +#if IOM_TIME + time_watcher *w; - // re-add it if necessary - if (w->at >= 0) - reg (w); + for (;tw.size ();) + { + w = tw[0]; + + for (time_watcher **i = tw.begin (); i < tw.end (); ++i) + if ((*i)->at < w->at) + w = *i; + + if (w->at > NOW) + { + double diff = w->at - NOW; + tval.tv_sec = (int)diff; + tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); + to = &tval; + break; + } + else if (w->at >= 0) + w->call (*w); + else + unreg (w); + } +#endif } - struct timeval to; - double diff = w->at - NOW; - to.tv_sec = (int)diff; - to.tv_usec = (int)((diff - to.tv_sec) * 1000000); +#if IOM_CHECK + for (int i = 0; i < cw.size (); ++i) + cw[i]->call (*cw[i]); +#endif +#if IOM_IO fd_set rfd, wfd; FD_ZERO (&rfd); @@ -124,40 +202,55 @@ if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd); if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); - if ((*w)->fd > fds) fds = (*w)->fd; + if ((*w)->fd >= fds) fds = (*w)->fd + 1; } - fds = select (fds + 1, &rfd, &wfd, 0, &to); + if (!to && !fds) + break; // no events + fds = select (fds, &rfd, &wfd, 0, to); +# if IOM_TIME set_now (); +# endif if (fds > 0) - for (io_watcher **w = iow.begin (); w < iow.end (); ++w) + for (int i = 0; i < iow.size (); ++i) { - short revents = (*w)->events; + io_watcher *w = iow[i]; + + short revents = w->events; - if (!FD_ISSET ((*w)->fd, &rfd)) revents &= ~EVENT_READ; - if (!FD_ISSET ((*w)->fd, &wfd)) revents &= ~EVENT_WRITE; + if (!FD_ISSET (w->fd, &rfd)) revents &= ~EVENT_READ; + if (!FD_ISSET (w->fd, &wfd)) revents &= ~EVENT_WRITE; if (revents) - (*w)->call (**w, revents); + w->call (*w, revents); } - } -} +#if IOM_IDLE + else if (iw.size ()) + for (int i = 0; i < iw.size (); ++i) + iw[i]->call (*iw[i]); +#endif + +#elif IOM_TIME + if (!to) + break; -void io_manager::idle_cb (time_watcher &w) -{ - w.at = NOW + 1000000000; + select (0, 0, 0, 0, &to); + set_now (); +#else + break; +#endif + } } io_manager::io_manager () { +#if IOM_TIME set_now (); +#endif iom_valid = true; - - idle = new time_watcher (this, &io_manager::idle_cb); - idle->start (0); } io_manager::~io_manager ()