2 iom.C -- generic I/O multiplexor
3 Copyright (C) 2003, 2004 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
24 #include <sys/select.h>
29 // TSTAMP_MAX must still fit into a positive struct timeval
30 #define TSTAMP_MAX (double)(1UL<<31)
33 static bool iom_valid;
36 template<class watcher>
37 void io_manager::reg (watcher *w, io_manager_vec<watcher> &queue)
45 w->active = queue.size ();
49 template<class watcher>
50 void io_manager::unreg (watcher *w, io_manager_vec<watcher> &queue)
57 queue [w->active - 1] = 0;
63 void time_watcher::trigger ()
70 void io_manager::reg (time_watcher *w) { reg (w, tw); }
71 void io_manager::unreg (time_watcher *w) { unreg (w, tw); }
75 void io_manager::reg (io_watcher *w) { reg (w, iow); }
76 void io_manager::unreg (io_watcher *w) { unreg (w, iow); }
80 void io_manager::reg (check_watcher *w) { reg (w, cw); }
81 void io_manager::unreg (check_watcher *w) { unreg (w, cw); }
85 void io_manager::reg (idle_watcher *w) { reg (w, iw); }
86 void io_manager::unreg (idle_watcher *w) { unreg (w, iw); }
90 inline void set_now (void)
94 gettimeofday (&tv, 0);
96 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000;
100 void io_manager::loop ()
108 struct timeval *to = 0;
126 next = tw[0]; // the first time-watcher must exist at ALL times
128 for (int i = tw.size (); i--; )
130 tw.erase_unordered (i);
131 else if (tw[i]->at < next->at)
138 double diff = next->at - NOW;
139 tval.tv_sec = (int)diff;
140 tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000);
155 for (int i = cw.size (); i--; )
157 cw.erase_unordered (i);
159 cw[i]->call (*cw[i]);
163 fd_set rfd, wfd, efd;
170 for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); )
173 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd);
174 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd);
176 if ((*i)->fd >= fds) fds = (*i)->fd + 1;
179 if (!to && !fds) //TODO: also check idle_watchers and check_watchers
182 fds = select (fds, &rfd, &wfd, &efd, to);
188 for (int i = iow.size (); i--; )
190 iow.erase_unordered (i);
193 short revents = iow[i]->events;
195 if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ;
196 if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE;
199 iow[i]->call (*iow[i], revents);
201 else if (fds < 0 && errno != EINTR)
203 perror ("Error while waiting for I/O or time event");
208 for (int i = iw.size (); i--; )
210 iw.erase_unordered (i);
212 iw[i]->call (*iw[i]);
219 select (0, 0, 0, 0, &to);
227 // this is a dummy time watcher to ensure that the first
228 // time watcher is _always_ valid, this gets rid of a lot
229 // of null-pointer-checks
230 static struct tw0 : time_watcher {
231 void cb (time_watcher &w)
233 // should never get called
234 // reached end-of-time, or tstamp has a bogus definition :)
239 : time_watcher (this, &tw0::cb)
243 io_manager::io_manager ()
250 tw0.start (TSTAMP_MAX);
254 io_manager::~io_manager ()