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

Comparing rxvt-unicode/src/iom.C (file contents):
Revision 1.24 by root, Sat Dec 11 17:18:29 2004 UTC vs.
Revision 1.37 by root, Mon Feb 12 17:34:58 2007 UTC

1/* 1/*
2 iom.C -- generic I/O multiplexer 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;
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
97 132
98#if IOM_TIME 133#if IOM_TIME
99tstamp io_manager::now () 134tstamp io_manager::now ()
100{ 135{
101 struct timeval tv; 136 struct timeval tv;
114 149
115// used for initialisation only 150// used for initialisation only
116static struct init { 151static struct init {
117 init () 152 init ()
118 { 153 {
154#ifdef IOM_PREINIT
155 { IOM_PREINIT }
156#endif
157 iom_valid = true;
158
119#if IOM_SIG 159#if IOM_SIG
120 sigemptyset (&sigs); 160 sigemptyset (&sigs);
121 161
122 if (pipe (sigpipe)) 162 if (pipe (sigpipe))
123 { 163 {
124 perror ("io_manager: unable to create signal pipe, aborting."); 164 perror ("io_manager: unable to create signal pipe, aborting.");
125 abort (); 165 abort ();
126 } 166 }
127 167
128 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); 168 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC);
129 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); 169 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC);
130#endif 170#endif
131 171
132 iom_valid = true; 172#if IOM_CHILD
173 sw0.start (SIGCHLD);
174#endif
133 175
134#if IOM_TIME 176#if IOM_TIME
135 io_manager::set_now (); 177 io_manager::set_now ();
136 178
137 tw0.start (TSTAMP_MAX); 179 tw0.start (TSTAMP_MAX);
138#endif 180#endif
181
182#ifdef IOM_POSTINIT
183 { IOM_POSTINIT }
184#endif
185 }
186
187 ~init ()
188 {
189 iom_valid = false;
139 } 190 }
140 191
141 static void required (); 192 static void required ();
142} init; 193} init;
143 194
215 write (sigpipe[1], &ch, 1); 266 write (sigpipe[1], &ch, 1);
216} 267}
217 268
218void io_manager::reg (sig_watcher &w) 269void io_manager::reg (sig_watcher &w)
219{ 270{
271 init::required ();
272
220 assert (0 < w.signum); 273 assert (0 < w.signum);
221 274
222 sw.reserve (w.signum); 275 sw.reserve (w.signum);
223 276
224 while (sw.size () < w.signum) // pathetic 277 while (sw.size () < w.signum) // pathetic
249 io_manager::reg (w, *sv); 302 io_manager::reg (w, *sv);
250} 303}
251 304
252void io_manager::unreg (sig_watcher &w) 305void io_manager::unreg (sig_watcher &w)
253{ 306{
254 if (!w.active) 307 if (!w.active || !iom_valid)
255 return; 308 return;
256 309
257 assert (0 < w.signum && w.signum <= sw.size ()); 310 assert (0 < w.signum && w.signum <= sw.size ());
258 311
259 io_manager::unreg (w, *sw[w.signum - 1]); 312 io_manager::unreg (w, *sw[w.signum - 1]);
263{ 316{
264 stop (); 317 stop ();
265 this->signum = signum; 318 this->signum = signum;
266 io_manager::reg (*this); 319 io_manager::reg (*this);
267} 320}
321#endif
322
323#if IOM_CHILD
324void io_manager::reg (child_watcher &w) { io_manager::reg (w, pw); }
325void io_manager::unreg (child_watcher &w) { io_manager::unreg (w, pw); }
268#endif 326#endif
269 327
270void io_manager::loop () 328void io_manager::loop ()
271{ 329{
272 init::required (); 330 init::required ();
380# if IOM_SIG 438# if IOM_SIG
381 sigprocmask (SIG_BLOCK, &sigs, NULL); 439 sigprocmask (SIG_BLOCK, &sigs, NULL);
382# endif 440# endif
383 441
384# if IOM_TIME 442# if IOM_TIME
443 {
444 // update time, try to compensate for gross non-monotonic time changes
445 tstamp diff = NOW;
385 set_now (); 446 set_now ();
447 diff = NOW - diff;
448
449 if (diff < 0)
450 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
451 if (*i)
452 (*i)->at += diff;
453 }
386# endif 454# endif
387 455
388 if (fds > 0) 456 if (fds > 0)
389 { 457 {
390# if IOM_SIG 458# if IOM_SIG
414 for (int i = iow.size (); i--; ) 482 for (int i = iow.size (); i--; )
415 if (!iow[i]) 483 if (!iow[i])
416 iow.erase_unordered (i); 484 iow.erase_unordered (i);
417 else 485 else
418 { 486 {
487 io_watcher &w = *iow[i];
419 short revents = iow[i]->events; 488 short revents = w.events;
420 489
421 if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ; 490 if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ;
422 if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE; 491 if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE;
423 492
424 if (revents) 493 if (revents)
425 iow[i]->call (*iow[i], revents); 494 w.call (w, revents);
426 } 495 }
427#endif 496#endif
428 } 497 }
429 else if (fds < 0 && errno != EINTR) 498 else if (fds < 0 && errno != EINTR)
430 { 499 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines