ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/iom.C
(Generate patch)

Comparing gvpe/src/iom.C (file contents):
Revision 1.10 by pcg, Fri Apr 4 05:26:45 2003 UTC vs.
Revision 1.32 by pcg, Tue Apr 26 00:55:56 2005 UTC

1/* 1/*
2 iom.C -- I/O multiplexor 2 iom.C -- generic I/O multiplexer
3 Copyright (C) 2003, 2004 Marc Lehmann <gvpe@schmorp.de>
3 4
5 This file is part of GVPE.
6
4 This program is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 8 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or 9 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version. 10 (at your option) any later version.
8 11
9 This program is distributed in the hope that it will be useful, 12 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details. 15 GNU General Public License for more details.
13 16
14 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
16 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*/ 20*/
18 21
19#include "config.h" 22#include "iom.h"
23
24#include <cstdio>
25#include <cstdlib>
26#include <cerrno>
20 27
21#include <sys/time.h> 28#include <sys/time.h>
22 29
23#include <algorithm> 30#include <assert.h>
31
32#if 1 // older unices need these includes for select (2)
33# include <unistd.h>
34# include <sys/types.h>
35# include <time.h>
36#endif
37
38// for IOM_SIG
39#if IOM_SIG
24#include <functional> 40# include <csignal>
41# include <fcntl.h>
42#endif
25 43
26#include "slog.h" 44// if the BSDs would at least be marginally POSIX-compatible.. *sigh*
27#include "iom.h" 45// until that happens, sys/select.h must come last
46#include <sys/select.h>
47
48#define TIMEVAL timeval
49#define TV_FRAC tv_usec
50#define TV_MULT 1000000L
51
52#if IOM_IO
53static io_manager_vec<io_watcher> iow;
54#endif
55#if IOM_CHECK
56static io_manager_vec<check_watcher> cw;
57#endif
58#if IOM_TIME
59static io_manager_vec<time_watcher> tw;
60#endif
61#if IOM_IDLE
62static io_manager_vec<idle_watcher> iw;
63#endif
64#if IOM_SIG
65static int sigpipe[2]; // signal signalling pipe
66static sigset_t sigs;
67struct sig_vec : io_manager_vec<sig_watcher> {
68 int pending;
69 sig_vec ()
70 : pending (false)
71 { }
72};
73static vector<sig_vec *> sw;
74#endif
75
76// this is a dummy time watcher to ensure that the first
77// time watcher is _always_ valid, this gets rid of a lot
78// of null-pointer-checks
79// (must come _before_ iom is being defined)
80static struct tw0 : time_watcher
81 {
82 void cb (time_watcher &w)
83 {
84 // should never get called
85 // reached end-of-time, or tstamp has a bogus definition,
86 // or compiler initialisation order broken, or something else :)
87 abort ();
88 }
89
90 tw0 ()
91 : time_watcher (this, &tw0::cb)
92 { }
93 } tw0;
28 94
29tstamp NOW; 95tstamp NOW;
96
97#if IOM_TIME
98tstamp io_manager::now ()
99{
100 struct timeval tv;
101
102 gettimeofday (&tv, 0);
103 return (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.;
104}
105
106void io_manager::set_now ()
107{
108 NOW = now ();
109}
110#endif
111
30bool iom_valid; 112static bool iom_valid;
31io_manager iom;
32 113
33inline bool earliest_first (const time_watcher *a, const time_watcher *b) 114// used for initialisation only
34{ 115static struct init {
35 return a->at > b->at; 116 init ()
36} 117 {
118#if IOM_SIG
119 sigemptyset (&sigs);
37 120
38void time_watcher::set (tstamp when) 121 if (pipe (sigpipe))
39{ 122 {
40 at = when; 123 perror ("io_manager: unable to create signal pipe, aborting.");
124 abort ();
125 }
41 126
42 if (registered) 127 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC);
43 iom.reschedule_time_watchers (); 128 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC);
44 else 129#endif
45 iom.reg (this);
46}
47 130
131 iom_valid = true;
132
133#if IOM_TIME
134 io_manager::set_now ();
135
136 tw0.start (TSTAMP_MAX);
137#endif
138 }
139
140 static void required ();
141} init;
142
143void
144init::required ()
145{
146 if (!iom_valid)
147 {
148 write (2, "io_manager: early registration attempt, aborting.\n",
149 sizeof ("io_manager: early registration attempt, aborting.\n") - 1);
150 abort ();
151 }
152}
153
154template<class watcher>
155void io_manager::reg (watcher &w, io_manager_vec<watcher> &queue)
156{
157 init::required ();
158
159 if (!w.active)
160 {
161 queue.push_back (&w);
162 w.active = queue.size ();
163 }
164}
165
166template<class watcher>
167void io_manager::unreg (watcher &w, io_manager_vec<watcher> &queue)
168{
169 if (!iom_valid)
170 return;
171
172 if (w.active)
173 {
174 queue [w.active - 1] = 0;
175 w.active = 0;
176 }
177}
178
179#if IOM_TIME
48void time_watcher::trigger () 180void time_watcher::trigger ()
49{ 181{
50 call (*this); 182 call (*this);
51 183 io_manager::reg (*this);
52 if (registered)
53 iom.reschedule_time_watchers ();
54 else
55 iom.reg (this);
56} 184}
57 185
58void time_watcher::start () 186void io_manager::reg (time_watcher &w) { io_manager::reg (w, tw); }
59{ 187void io_manager::unreg (time_watcher &w) { io_manager::unreg (w, tw); }
60 if (!registered) 188#endif
61 iom.reg (this);
62}
63 189
64time_watcher::~time_watcher () 190#if IOM_IO
65{ 191void io_manager::reg (io_watcher &w) { io_manager::reg (w, iow); }
66 if (iom_valid) 192void io_manager::unreg (io_watcher &w) { io_manager::unreg (w, iow); }
67 iom.unreg (this); 193#endif
68}
69 194
70io_watcher::~io_watcher () 195#if IOM_CHECK
71{ 196void io_manager::reg (check_watcher &w) { io_manager::reg (w, cw); }
72 if (iom_valid) 197void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); }
73 iom.unreg (this); 198#endif
74}
75 199
200#if IOM_IDLE
201void io_manager::reg (idle_watcher &w) { io_manager::reg (w, iw); }
202void io_manager::unreg (idle_watcher &w) { io_manager::unreg (w, iw); }
203#endif
204
205#if IOM_SIG
206static void
207sighandler (int signum)
208{
209 sw [signum - 1]->pending = true;
210
211 // we use a pipe for signal notifications, as most current
212 // OSes (Linux...) do not implement pselect correctly. ugh.
213 char ch = signum; // actual content not used
214 write (sigpipe[1], &ch, 1);
215}
216
76void io_manager::reg (io_watcher *w) 217void io_manager::reg (sig_watcher &w)
77{ 218{
78 pollfd pfd; 219 assert (0 < w.signum);
79 220
80 pfs.push_back (pfd); 221 sw.reserve (w.signum);
222
223 while (sw.size () < w.signum) // pathetic
81 iow.push_back (w); 224 sw.push_back (0);
82 225
83 w->p = &(*(pfs.end () - 1)); 226 sig_vec *&sv = sw[w.signum - 1];
84}
85 227
86void io_manager::unreg (io_watcher *w) 228 if (!sv)
87{
88 if (w->p)
89 { 229 {
90 unsigned int sz = iow.size (); 230 sv = new sig_vec;
91 unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin ();
92 231
93 assert (i != sz); 232 sigaddset (&sigs, w.signum);
233 sigprocmask (SIG_BLOCK, &sigs, NULL);
94 234
95 if (sz == 1) 235 struct sigaction sa;
236 sa.sa_handler = sighandler;
237 sigfillset (&sa.sa_mask);
238 sa.sa_flags = SA_RESTART;
239
240 if (sigaction (w.signum, &sa, 0))
96 { 241 {
242 perror ("io_manager: error while installing signal handler, ignoring.");
97 pfs.clear (); 243 abort ();
98 iow.clear ();
99 } 244 }
100 else if (i == sz - 1) 245
246 }
247
248 io_manager::reg (w, *sv);
249}
250
251void io_manager::unreg (sig_watcher &w)
252{
253 if (!w.active)
254 return;
255
256 assert (0 < w.signum && w.signum <= sw.size ());
257
258 io_manager::unreg (w, *sw[w.signum - 1]);
259}
260
261void sig_watcher::start (int signum)
262{
263 stop ();
264 this->signum = signum;
265 io_manager::reg (*this);
266}
267#endif
268
269void io_manager::loop ()
270{
271 init::required ();
272
273#if IOM_TIME
274 set_now ();
275#endif
276
277 for (;;)
278 {
279
280#if IOM_TIME
281 // call pending time watchers
282 {
283 bool activity;
284
285 do
286 {
287 activity = false;
288
289 for (int i = tw.size (); i--; )
290 if (!tw[i])
291 tw.erase_unordered (i);
292 else if (tw[i]->at <= NOW)
293 {
294 time_watcher &w = *tw[i];
295
296 unreg (w);
297 w.call (w);
298
299 activity = true;
300 }
301 }
302 while (activity);
303 }
304#endif
305
306#if IOM_CHECK
307 // call all check watchers
308 for (int i = cw.size (); i--; )
309 if (!cw[i])
310 cw.erase_unordered (i);
311 else
312 cw[i]->call (*cw[i]);
313#endif
314
315 struct TIMEVAL *to = 0;
316 struct TIMEVAL tval;
317
318#if IOM_IDLE
319 if (iw.size ())
101 { 320 {
102 iow.pop_back (); 321 tval.tv_sec = 0;
103 pfs.pop_back (); 322 tval.TV_FRAC = 0;
323 to = &tval;
104 } 324 }
105 else 325 else
326#endif
106 { 327 {
107 iow[i] = iow[sz - 1]; iow.pop_back (); 328#if IOM_TIME
108 pfs[i] = pfs[sz - 1]; pfs.pop_back (); 329 // find earliest active watcher
330 time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times
331
332 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
333 if (*i && (*i)->at < next->at)
334 next = *i;
335
336 if (next->at > NOW && next != tw[0])
337 {
338 double diff = next->at - NOW;
339 tval.tv_sec = (int)diff;
340 tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT);
341 to = &tval;
342 }
109 } 343 }
344#endif
110 345
111 w->p = 0; 346#if IOM_IO || IOM_SIG
112 } 347 fd_set rfd, wfd;
113}
114 348
115void io_manager::reschedule_time_watchers () 349 FD_ZERO (&rfd);
116{ 350 FD_ZERO (&wfd);
117 make_heap (tw.begin (), tw.end (), earliest_first);
118}
119 351
120void io_manager::reg (time_watcher *w) 352 int fds = 0;
121{
122 w->registered = true;
123 353
124 tw.push_back (w); 354# if IOM_IO
125 push_heap (tw.begin (), tw.end (), earliest_first); 355 for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); )
126} 356 if (*i)
357 {
358 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd);
359 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd);
127 360
128void io_manager::unreg (time_watcher *w) 361 if ((*i)->fd >= fds) fds = (*i)->fd + 1;
129{ 362 }
130 if (w->registered) 363# endif
131 {
132 unsigned int sz = tw.size ();
133 unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin ();
134 364
135 assert (i != sz); 365 if (!to && !fds) //TODO: also check idle_watchers and check_watchers?
366 break; // no events
367
368# if IOM_SIG
369 FD_SET (sigpipe[0], &rfd);
370 if (sigpipe[0] >= fds) fds = sigpipe[0] + 1;
371# endif
372
373# if IOM_SIG
374 // there is no race, as we use a pipe for signals, so select
375 // will return if a signal is caught.
376 sigprocmask (SIG_UNBLOCK, &sigs, NULL);
377# endif
378 fds = select (fds, &rfd, &wfd, NULL, to);
379# if IOM_SIG
380 sigprocmask (SIG_BLOCK, &sigs, NULL);
381# endif
382
383# if IOM_TIME
136 384 {
137 if (i != sz - 1) 385 // update time, try to compensate for gross non-monotonic time changes
138 tw[i] = tw[sz - 1]; 386 tstamp diff = NOW;
387 set_now ();
388 diff = NOW - diff;
139 389
140 tw.pop_back (); 390 if (diff < 0)
141 reschedule_time_watchers (); 391 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
142 392 if (*i)
143 w->registered = false; 393 (*i)->at += diff;
144 } 394 }
145} 395# endif
146 396
147inline void set_now (void) 397 if (fds > 0)
148{
149 struct timeval tv;
150
151 gettimeofday (&tv, 0);
152
153 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000;
154}
155
156void io_manager::loop ()
157{
158 set_now ();
159
160 for (;;)
161 {
162 while (tw[0]->at <= NOW)
163 { 398 {
164 // remove the first watcher 399# if IOM_SIG
165 time_watcher *w = tw[0]; 400 if (FD_ISSET (sigpipe[0], &rfd))
401 {
402 char ch;
166 403
167 pop_heap (tw.begin (), tw.end (), earliest_first); 404 while (read (sigpipe[0], &ch, 1) > 0)
168 tw.pop_back (); 405 ;
169 406
170 w->registered = false; 407 for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); )
408 if (*svp && (*svp)->pending)
409 {
410 sig_vec &sv = **svp;
411 for (int i = sv.size (); i--; )
412 if (!sv[i])
413 sv.erase_unordered (i);
414 else
415 sv[i]->call (*sv[i]);
171 416
172 // call it 417 sv.pending = false;
173 w->call (*w); 418 }
419 }
420# endif
174 421
175 // re-add it if necessary 422# if IOM_IO
176 if (w->at >= 0 && !w->registered) 423 for (int i = iow.size (); i--; )
177 reg (w); 424 if (!iow[i])
425 iow.erase_unordered (i);
426 else
427 {
428 io_watcher &w = *iow[i];
429 short revents = w.events;
430
431 if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ;
432 if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE;
433
434 if (revents)
435 w.call (w, revents);
436 }
437#endif
178 } 438 }
439 else if (fds < 0 && errno != EINTR)
440 {
441 perror ("io_manager: fatal error while waiting for I/O or time event, aborting.");
442 abort ();
443 }
444#if IOM_IDLE
445 else
446 for (int i = iw.size (); i--; )
447 if (!iw[i])
448 iw.erase_unordered (i);
449 else
450 iw[i]->call (*iw[i]);
451#endif
179 452
180 int timeout = (int) ((tw[0]->at - NOW) * 1000); 453#elif IOM_TIME
454 if (!to)
455 break;
181 456
182 int fds = poll (&pfs[0], pfs.size (), timeout); 457 select (0, 0, 0, 0, &to);
183
184 set_now (); 458 set_now ();
185 459#else
186 vector<io_watcher *>::iterator w; 460 break;
187 vector<pollfd>::iterator p; 461#endif
188
189 for (w = iow.begin (), p = pfs.begin ();
190 fds > 0 && w < iow.end ();
191 ++w, ++p)
192 if (p->revents)
193 {
194 --fds;
195 (*w)->call (**w, p->revents);
196 }
197 } 462 }
198} 463}
199 464
200void io_manager::idle_cb (time_watcher &w)
201{
202 w.at = NOW + 86400; // wake up every day, for no good reason
203}
204
205io_manager::io_manager ()
206{
207 iom_valid = true;
208
209 set_now ();
210 idle = new time_watcher (this, &io_manager::idle_cb);
211 idle->start (0);
212}
213
214io_manager::~io_manager ()
215{
216 iom_valid = false;
217}
218

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines