2 iom.C -- generic I/O multiplexor
3 Copyright (C) 2003 Marc Lehmann <pcg@goof.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "../config.h"
24 #include <sys/select.h>
33 template<class watcher>
34 void io_manager::reg (watcher *w, simplevec<watcher *> &queue)
36 if (find (queue.begin (), queue.end (), w) == queue.end ())
40 template<class watcher>
41 void io_manager::unreg (watcher *w, simplevec<watcher *> &queue)
43 queue.erase (find (queue.begin (), queue.end (), w));
47 io_watcher::~io_watcher ()
53 void io_manager::reg (io_watcher *w)
58 void io_manager::unreg (io_watcher *w)
66 void time_watcher::trigger ()
73 time_watcher::~time_watcher ()
81 void io_manager::reg (time_watcher *w)
86 void io_manager::unreg (time_watcher *w)
93 check_watcher::~check_watcher ()
99 void io_manager::reg (check_watcher *w)
104 void io_manager::unreg (check_watcher *w)
111 inline void set_now (void)
115 gettimeofday (&tv, 0);
117 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000;
121 void io_manager::loop ()
130 for (int i = 0; i < cw.size (); ++i)
131 cw[i]->call (*cw[i]);
134 struct timeval *to = 0;
144 for (time_watcher **i = tw.begin (); i < tw.end (); ++i)
145 if ((*i)->at < w->at)
150 double diff = w->at - NOW;
151 tval.tv_sec = (int)diff;
152 tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000);
171 for (io_watcher **w = iow.begin (); w < iow.end (); ++w)
173 if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd);
174 if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd);
176 if ((*w)->fd >= fds) fds = (*w)->fd + 1;
182 fds = select (fds, &rfd, &wfd, 0, to);
188 for (int i = 0; i < iow.size (); ++i)
190 io_watcher *w = iow[i];
192 short revents = w->events;
194 if (!FD_ISSET (w->fd, &rfd)) revents &= ~EVENT_READ;
195 if (!FD_ISSET (w->fd, &wfd)) revents &= ~EVENT_WRITE;
198 w->call (*w, revents);
204 select (0, 0, 0, 0, &to);
212 io_manager::io_manager ()
221 io_manager::~io_manager ()