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.21 by pcg, Thu Sep 2 07:50:43 2004 UTC vs.
Revision 1.35 by pcg, Sat Jan 28 12:02:51 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
128 sw0 ()
129 : sig_watcher (this, &sw0::cb)
130 { }
131} sw0;
132#endif
133
98#if IOM_TIME 134#if IOM_TIME
99inline void set_now (void) 135tstamp io_manager::now ()
100{ 136{
101 struct timeval tv; 137 struct timeval tv;
102 138
103 gettimeofday (&tv, 0); 139 gettimeofday (&tv, 0);
104 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.; 140 return (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.;
141}
142
143void io_manager::set_now ()
144{
145 NOW = now ();
105} 146}
106#endif 147#endif
107 148
108static bool iom_valid; 149static bool iom_valid;
109 150
110// used for initialisation only 151// used for initialisation only
111static struct init { 152static struct init {
112 init () 153 init ()
113 { 154 {
155 iom_valid = true;
156
114#if IOM_SIG 157#if IOM_SIG
115 sigemptyset (&sigs); 158 sigemptyset (&sigs);
116 159
117 if (pipe (sigpipe)) 160 if (pipe (sigpipe))
118 { 161 {
119 perror ("io_manager: unable to create signal pipe, aborting."); 162 perror ("io_manager: unable to create signal pipe, aborting.");
120 abort (); 163 abort ();
121 } 164 }
122 165
123 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); 166 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC);
124 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); 167 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC);
125#endif 168#endif
126 169
127 iom_valid = true; 170#if IOM_CHILD
171 sw0.start (SIGCHLD);
172#endif
128 173
129#if IOM_TIME 174#if IOM_TIME
130 set_now (); 175 io_manager::set_now ();
131 176
132 tw0.start (TSTAMP_MAX); 177 tw0.start (TSTAMP_MAX);
133#endif 178#endif
179 }
180
181 ~init ()
182 {
183 iom_valid = false;
134 } 184 }
135 185
136 static void required (); 186 static void required ();
137} init; 187} init;
138 188
152{ 202{
153 init::required (); 203 init::required ();
154 204
155 if (!w.active) 205 if (!w.active)
156 { 206 {
157#if IOM_CHECK
158 queue.activity = true;
159#endif
160 queue.push_back (&w); 207 queue.push_back (&w);
161 w.active = queue.size (); 208 w.active = queue.size ();
162 } 209 }
163} 210}
164 211
213 write (sigpipe[1], &ch, 1); 260 write (sigpipe[1], &ch, 1);
214} 261}
215 262
216void io_manager::reg (sig_watcher &w) 263void io_manager::reg (sig_watcher &w)
217{ 264{
265 init::required ();
266
218 assert (0 < w.signum); 267 assert (0 < w.signum);
219 268
220 sw.reserve (w.signum); 269 sw.reserve (w.signum);
221 270
222 while (sw.size () < w.signum) // pathetic 271 while (sw.size () < w.signum) // pathetic
247 io_manager::reg (w, *sv); 296 io_manager::reg (w, *sv);
248} 297}
249 298
250void io_manager::unreg (sig_watcher &w) 299void io_manager::unreg (sig_watcher &w)
251{ 300{
252 if (!w.active) 301 if (!w.active || !iom_valid)
253 return; 302 return;
254 303
255 assert (0 < w.signum && w.signum <= sw.size ()); 304 assert (0 < w.signum && w.signum <= sw.size ());
256 305
257 io_manager::unreg (w, *sw[w.signum - 1]); 306 io_manager::unreg (w, *sw[w.signum - 1]);
263 this->signum = signum; 312 this->signum = signum;
264 io_manager::reg (*this); 313 io_manager::reg (*this);
265} 314}
266#endif 315#endif
267 316
317#if IOM_CHILD
318void io_manager::reg (child_watcher &w) { io_manager::reg (w, pw); }
319void io_manager::unreg (child_watcher &w) { io_manager::unreg (w, pw); }
320#endif
321
268void io_manager::loop () 322void io_manager::loop ()
269{ 323{
270 init::required (); 324 init::required ();
271 325
272#if IOM_TIME 326#if IOM_TIME
273 set_now (); 327 set_now ();
274#endif 328#endif
275 329
276 for (;;) 330 for (;;)
277 { 331 {
332
333#if IOM_TIME
334 // call pending time watchers
335 {
336 bool activity;
337
338 do
339 {
340 activity = false;
341
342 for (int i = tw.size (); i--; )
343 if (!tw[i])
344 tw.erase_unordered (i);
345 else if (tw[i]->at <= NOW)
346 {
347 time_watcher &w = *tw[i];
348
349 unreg (w);
350 w.call (w);
351
352 activity = true;
353 }
354 }
355 while (activity);
356 }
357#endif
358
359#if IOM_CHECK
360 // call all check watchers
361 for (int i = cw.size (); i--; )
362 if (!cw[i])
363 cw.erase_unordered (i);
364 else
365 cw[i]->call (*cw[i]);
366#endif
367
278 struct TIMEVAL *to = 0; 368 struct TIMEVAL *to = 0;
279 struct TIMEVAL tval; 369 struct TIMEVAL tval;
280 370
281#if IOM_IDLE 371#if IOM_IDLE
282 if (iw.size ()) 372 if (iw.size ())
287 } 377 }
288 else 378 else
289#endif 379#endif
290 { 380 {
291#if IOM_TIME 381#if IOM_TIME
292 time_watcher *next; 382 // find earliest active watcher
383 time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times
293 384
294 for (;;) 385 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
386 if (*i && (*i)->at < next->at)
387 next = *i;
388
389 if (next->at > NOW && next != tw[0])
295 { 390 {
296 next = tw[0]; // the first time-watcher must exist at ALL times
297
298 for (int i = tw.size (); i--; )
299 if (!tw[i])
300 tw.erase_unordered (i);
301 else if (tw[i]->at < next->at)
302 next = tw[i];
303
304 if (next->at > NOW)
305 {
306 if (next != tw[0])
307 {
308 double diff = next->at - NOW; 391 double diff = next->at - NOW;
309 tval.tv_sec = (int)diff; 392 tval.tv_sec = (int)diff;
310 tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT); 393 tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT);
311 to = &tval; 394 to = &tval;
312 }
313 break;
314 }
315 else
316 {
317 unreg (*next);
318 next->call (*next);
319 }
320 } 395 }
321#endif
322 }
323
324#if IOM_CHECK
325 tw.activity = false;
326
327 for (int i = cw.size (); i--; )
328 if (!cw[i])
329 cw.erase_unordered (i);
330 else
331 cw[i]->call (*cw[i]);
332
333 if (tw.activity)
334 {
335 tval.tv_sec = 0;
336 tval.TV_FRAC = 0;
337 to = &tval;
338 } 396 }
339#endif 397#endif
340 398
341#if IOM_IO || IOM_SIG 399#if IOM_IO || IOM_SIG
342 fd_set rfd, wfd; 400 fd_set rfd, wfd;
345 FD_ZERO (&wfd); 403 FD_ZERO (&wfd);
346 404
347 int fds = 0; 405 int fds = 0;
348 406
349# if IOM_IO 407# if IOM_IO
350 for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); ) 408 for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); )
351 if (*i) 409 if (*i)
352 { 410 {
353 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); 411 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd);
354 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); 412 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd);
355 413
356 if ((*i)->fd >= fds) fds = (*i)->fd + 1; 414 if ((*i)->fd >= fds) fds = (*i)->fd + 1;
357 } 415 }
358# endif 416# endif
359 417
360 if (!to && !fds) //TODO: also check idle_watchers and check_watchers 418 if (!to && !fds) //TODO: also check idle_watchers and check_watchers?
361 break; // no events 419 break; // no events
362 420
363# if IOM_SIG 421# if IOM_SIG
364 FD_SET (sigpipe[0], &rfd); 422 FD_SET (sigpipe[0], &rfd);
365 if (sigpipe[0] >= fds) fds = sigpipe[0] + 1; 423 if (sigpipe[0] >= fds) fds = sigpipe[0] + 1;
374# if IOM_SIG 432# if IOM_SIG
375 sigprocmask (SIG_BLOCK, &sigs, NULL); 433 sigprocmask (SIG_BLOCK, &sigs, NULL);
376# endif 434# endif
377 435
378# if IOM_TIME 436# if IOM_TIME
437 {
438 // update time, try to compensate for gross non-monotonic time changes
439 tstamp diff = NOW;
379 set_now (); 440 set_now ();
441 diff = NOW - diff;
442
443 if (diff < 0)
444 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
445 if (*i)
446 (*i)->at += diff;
447 }
380# endif 448# endif
381 449
382 if (fds > 0) 450 if (fds > 0)
383 { 451 {
384# if IOM_SIG 452# if IOM_SIG
387 char ch; 455 char ch;
388 456
389 while (read (sigpipe[0], &ch, 1) > 0) 457 while (read (sigpipe[0], &ch, 1) > 0)
390 ; 458 ;
391 459
392 for (sig_vec **svp = sw.end (); svp-- > sw.begin (); ) 460 for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); )
393 if (*svp && (*svp)->pending) 461 if (*svp && (*svp)->pending)
394 { 462 {
395 sig_vec &sv = **svp; 463 sig_vec &sv = **svp;
396 for (int i = sv.size (); i--; ) 464 for (int i = sv.size (); i--; )
397 if (!sv[i]) 465 if (!sv[i])
408 for (int i = iow.size (); i--; ) 476 for (int i = iow.size (); i--; )
409 if (!iow[i]) 477 if (!iow[i])
410 iow.erase_unordered (i); 478 iow.erase_unordered (i);
411 else 479 else
412 { 480 {
481 io_watcher &w = *iow[i];
413 short revents = iow[i]->events; 482 short revents = w.events;
414 483
415 if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ; 484 if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ;
416 if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE; 485 if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE;
417 486
418 if (revents) 487 if (revents)
419 iow[i]->call (*iow[i], revents); 488 w.call (w, revents);
420 } 489 }
421#endif 490#endif
422 } 491 }
423 else if (fds < 0 && errno != EINTR) 492 else if (fds < 0 && errno != EINTR)
424 { 493 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines