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.22 by pcg, Thu Nov 11 17:41:55 2004 UTC vs.
Revision 1.36 by pcg, Thu Dec 14 03:18:53 2006 UTC

1/* 1/*
2 iom.C -- generic I/O multiplexor 2 iom.C -- generic I/O multiplexer
3 Copyright (C) 2003, 2004 Marc Lehmann <pcg@goof.com> 3 Copyright (C) 2003-2006 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE.
6
5 This program is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify
6 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
7 the Free Software Foundation; either version 2 of the License, or 9 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 10 (at your option) any later version.
9 11
10 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,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. 15 GNU General Public License for more details.
14 16
15 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
16 along with this program; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
17 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
18*/ 20*/
19 21
20#include "iom.h" 22#include "iom.h"
21 23
22#include <cstdio> 24#include <cstdio>
23#include <cstdlib> 25#include <cstdlib>
24#include <cerrno> 26#include <cerrno>
27#include <cassert>
25 28
29#include <sys/types.h>
26#include <sys/time.h> 30#include <sys/time.h>
27
28#include <assert.h>
29 31
30#if 1 // older unices need these includes for select (2) 32#if 1 // older unices need these includes for select (2)
31# include <unistd.h> 33# include <unistd.h>
32# include <sys/types.h>
33# include <time.h> 34# include <time.h>
34#endif 35#endif
35 36
36// for IOM_SIG 37#if IOM_CHILD
38# include <sys/wait.h>
39#endif
40
37#if IOM_SIG 41#if IOM_SIG
38# include <signal.h> 42# include <csignal>
39# include <fcntl.h> 43# include <fcntl.h>
40#endif 44#endif
41 45
42// if the BSDs would at least be marginally POSIX-compatible.. *sigh* 46// if the BSDs would at least be marginally POSIX-compatible.. *sigh*
43// until that happens, sys/select.h must come last 47// until that happens, sys/select.h must come last
44#include <sys/select.h> 48#include <sys/select.h>
45
46// TSTAMP_MAX must still fit into a positive struct timeval
47#define TSTAMP_MAX (double)(1UL<<31)
48 49
49#define TIMEVAL timeval 50#define TIMEVAL timeval
50#define TV_FRAC tv_usec 51#define TV_FRAC tv_usec
51#define TV_MULT 1000000L 52#define TV_MULT 1000000L
52 53
71 : pending (false) 72 : pending (false)
72 { } 73 { }
73}; 74};
74static vector<sig_vec *> sw; 75static vector<sig_vec *> sw;
75#endif 76#endif
77#if IOM_CHILD
78static io_manager_vec<child_watcher> pw;
79#endif
76 80
77// this is a dummy time watcher to ensure that the first 81// this is a dummy time watcher to ensure that the first
78// time watcher is _always_ valid, this gets rid of a lot 82// time watcher is _always_ valid, this gets rid of a lot
79// of null-pointer-checks 83// of null-pointer-checks
80// (must come _before_ iom is being defined) 84// (must come _before_ iom is being defined)
81static struct tw0 : time_watcher 85static struct tw0 : time_watcher
86{
87 void cb (time_watcher &w)
82 { 88 {
83 void cb (time_watcher &w)
84 {
85 // should never get called 89 // should never get called
86 // reached end-of-time, or tstamp has a bogus definition, 90 // reached end-of-time, or tstamp has a bogus definition,
87 // or compiler initialisation order broken, or something else :) 91 // or compiler initialisation order broken, or something else :)
88 abort (); 92 abort ();
89 } 93 }
90 94
91 tw0 () 95 tw0 ()
92 : time_watcher (this, &tw0::cb) 96 : time_watcher (this, &tw0::cb)
93 { } 97 { }
94 } tw0; 98} tw0;
95 99
96tstamp NOW; 100tstamp NOW;
97 101
102#if IOM_CHILD
103// sig_watcher for child signal(s)
104static struct sw0 : sig_watcher
105{
106 void cb (sig_watcher &w)
107 {
108 // SIGCHLD, call corresponding watchera
109 pid_t pid;
110 int status;
111
112 while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
113 for (int i = pw.size (); i--; )
114 {
115 child_watcher *w = pw[i];
116
117 if (!w)
118 pw.erase_unordered (i);
119 else if (w->pid == pid)
120 {
121 io_manager::unreg (*w);
122 w->call (*w, status);
123 }
124 }
125 }
126
127 sw0 ()
128 : sig_watcher (this, &sw0::cb)
129 { }
130} sw0;
131#endif
132
98#if IOM_TIME 133#if IOM_TIME
99inline void set_now (void) 134tstamp io_manager::now ()
100{ 135{
101 struct timeval tv; 136 struct timeval tv;
102 137
103 gettimeofday (&tv, 0); 138 gettimeofday (&tv, 0);
104 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.; 139 return (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.;
140}
141
142void io_manager::set_now ()
143{
144 NOW = now ();
105} 145}
106#endif 146#endif
107 147
108static bool iom_valid; 148static bool iom_valid;
109 149
110// used for initialisation only 150// used for initialisation only
111static struct init { 151static struct init {
112 init () 152 init ()
113 { 153 {
154 iom_valid = true;
155
114#if IOM_SIG 156#if IOM_SIG
115 sigemptyset (&sigs); 157 sigemptyset (&sigs);
116 158
117 if (pipe (sigpipe)) 159 if (pipe (sigpipe))
118 { 160 {
119 perror ("io_manager: unable to create signal pipe, aborting."); 161 perror ("io_manager: unable to create signal pipe, aborting.");
120 abort (); 162 abort ();
121 } 163 }
122 164
123 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); 165 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC);
124 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); 166 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC);
125#endif 167#endif
126 168
127 iom_valid = true; 169#if IOM_CHILD
170 sw0.start (SIGCHLD);
171#endif
128 172
129#if IOM_TIME 173#if IOM_TIME
130 set_now (); 174 io_manager::set_now ();
131 175
132 tw0.start (TSTAMP_MAX); 176 tw0.start (TSTAMP_MAX);
133#endif 177#endif
178 }
179
180 ~init ()
181 {
182 iom_valid = false;
134 } 183 }
135 184
136 static void required (); 185 static void required ();
137} init; 186} init;
138 187
210 write (sigpipe[1], &ch, 1); 259 write (sigpipe[1], &ch, 1);
211} 260}
212 261
213void io_manager::reg (sig_watcher &w) 262void io_manager::reg (sig_watcher &w)
214{ 263{
264 init::required ();
265
215 assert (0 < w.signum); 266 assert (0 < w.signum);
216 267
217 sw.reserve (w.signum); 268 sw.reserve (w.signum);
218 269
219 while (sw.size () < w.signum) // pathetic 270 while (sw.size () < w.signum) // pathetic
244 io_manager::reg (w, *sv); 295 io_manager::reg (w, *sv);
245} 296}
246 297
247void io_manager::unreg (sig_watcher &w) 298void io_manager::unreg (sig_watcher &w)
248{ 299{
249 if (!w.active) 300 if (!w.active || !iom_valid)
250 return; 301 return;
251 302
252 assert (0 < w.signum && w.signum <= sw.size ()); 303 assert (0 < w.signum && w.signum <= sw.size ());
253 304
254 io_manager::unreg (w, *sw[w.signum - 1]); 305 io_manager::unreg (w, *sw[w.signum - 1]);
260 this->signum = signum; 311 this->signum = signum;
261 io_manager::reg (*this); 312 io_manager::reg (*this);
262} 313}
263#endif 314#endif
264 315
316#if IOM_CHILD
317void io_manager::reg (child_watcher &w) { io_manager::reg (w, pw); }
318void io_manager::unreg (child_watcher &w) { io_manager::unreg (w, pw); }
319#endif
320
265void io_manager::loop () 321void io_manager::loop ()
266{ 322{
267 init::required (); 323 init::required ();
268 324
269#if IOM_TIME 325#if IOM_TIME
283 activity = false; 339 activity = false;
284 340
285 for (int i = tw.size (); i--; ) 341 for (int i = tw.size (); i--; )
286 if (!tw[i]) 342 if (!tw[i])
287 tw.erase_unordered (i); 343 tw.erase_unordered (i);
288 else if (tw[i]->at <= NOW + IOM_ACCURACY) 344 else if (tw[i]->at <= NOW)
289 { 345 {
290 time_watcher &w = *tw[i]; 346 time_watcher &w = *tw[i];
291 347
292 unreg (w); 348 unreg (w);
293 w.call (w); 349 w.call (w);
375# if IOM_SIG 431# if IOM_SIG
376 sigprocmask (SIG_BLOCK, &sigs, NULL); 432 sigprocmask (SIG_BLOCK, &sigs, NULL);
377# endif 433# endif
378 434
379# if IOM_TIME 435# if IOM_TIME
436 {
437 // update time, try to compensate for gross non-monotonic time changes
438 tstamp diff = NOW;
380 set_now (); 439 set_now ();
440 diff = NOW - diff;
441
442 if (diff < 0)
443 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
444 if (*i)
445 (*i)->at += diff;
446 }
381# endif 447# endif
382 448
383 if (fds > 0) 449 if (fds > 0)
384 { 450 {
385# if IOM_SIG 451# if IOM_SIG
388 char ch; 454 char ch;
389 455
390 while (read (sigpipe[0], &ch, 1) > 0) 456 while (read (sigpipe[0], &ch, 1) > 0)
391 ; 457 ;
392 458
393 for (sig_vec **svp = sw.end (); svp-- > sw.begin (); ) 459 for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); )
394 if (*svp && (*svp)->pending) 460 if (*svp && (*svp)->pending)
395 { 461 {
396 sig_vec &sv = **svp; 462 sig_vec &sv = **svp;
397 for (int i = sv.size (); i--; ) 463 for (int i = sv.size (); i--; )
398 if (!sv[i]) 464 if (!sv[i])
409 for (int i = iow.size (); i--; ) 475 for (int i = iow.size (); i--; )
410 if (!iow[i]) 476 if (!iow[i])
411 iow.erase_unordered (i); 477 iow.erase_unordered (i);
412 else 478 else
413 { 479 {
480 io_watcher &w = *iow[i];
414 short revents = iow[i]->events; 481 short revents = w.events;
415 482
416 if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ; 483 if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ;
417 if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE; 484 if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE;
418 485
419 if (revents) 486 if (revents)
420 iow[i]->call (*iow[i], revents); 487 w.call (w, revents);
421 } 488 }
422#endif 489#endif
423 } 490 }
424 else if (fds < 0 && errno != EINTR) 491 else if (fds < 0 && errno != EINTR)
425 { 492 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines