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.30 by pcg, Thu Mar 3 16:54:34 2005 UTC vs.
Revision 1.36 by pcg, Thu Dec 14 03:18:53 2006 UTC

1/* 1/*
2 iom.C -- generic I/O multiplexer 2 iom.C -- generic I/O multiplexer
3 Copyright (C) 2003, 2004 Marc Lehmann <gvpe@schmorp.de> 3 Copyright (C) 2003-2006 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE. 5 This file is part of GVPE.
6 6
7 GVPE is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify
8 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
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 15 GNU General Public License for more details.
16 16
17 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
18 along with gvpe; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
19 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
20*/ 20*/
21 21
22#include "iom.h" 22#include "iom.h"
23 23
24#include <cstdio> 24#include <cstdio>
25#include <cstdlib> 25#include <cstdlib>
26#include <cerrno> 26#include <cerrno>
27#include <cassert>
27 28
29#include <sys/types.h>
28#include <sys/time.h> 30#include <sys/time.h>
29
30#include <assert.h>
31 31
32#if 1 // older unices need these includes for select (2) 32#if 1 // older unices need these includes for select (2)
33# include <unistd.h> 33# include <unistd.h>
34# include <sys/types.h>
35# include <time.h> 34# include <time.h>
36#endif 35#endif
37 36
38// for IOM_SIG 37#if IOM_CHILD
38# include <sys/wait.h>
39#endif
40
39#if IOM_SIG 41#if IOM_SIG
40# include <csignal> 42# include <csignal>
41# include <fcntl.h> 43# include <fcntl.h>
42#endif 44#endif
43 45
44// if the BSDs would at least be marginally POSIX-compatible.. *sigh* 46// if the BSDs would at least be marginally POSIX-compatible.. *sigh*
45// until that happens, sys/select.h must come last 47// until that happens, sys/select.h must come last
46#include <sys/select.h> 48#include <sys/select.h>
47
48// TSTAMP_MAX must still fit into a positive struct timeval
49#define TSTAMP_MAX (double)(1UL<<31)
50 49
51#define TIMEVAL timeval 50#define TIMEVAL timeval
52#define TV_FRAC tv_usec 51#define TV_FRAC tv_usec
53#define TV_MULT 1000000L 52#define TV_MULT 1000000L
54 53
73 : pending (false) 72 : pending (false)
74 { } 73 { }
75}; 74};
76static vector<sig_vec *> sw; 75static vector<sig_vec *> sw;
77#endif 76#endif
77#if IOM_CHILD
78static io_manager_vec<child_watcher> pw;
79#endif
78 80
79// this is a dummy time watcher to ensure that the first 81// this is a dummy time watcher to ensure that the first
80// time watcher is _always_ valid, this gets rid of a lot 82// time watcher is _always_ valid, this gets rid of a lot
81// of null-pointer-checks 83// of null-pointer-checks
82// (must come _before_ iom is being defined) 84// (must come _before_ iom is being defined)
83static struct tw0 : time_watcher 85static struct tw0 : time_watcher
86{
87 void cb (time_watcher &w)
84 { 88 {
85 void cb (time_watcher &w)
86 {
87 // should never get called 89 // should never get called
88 // reached end-of-time, or tstamp has a bogus definition, 90 // reached end-of-time, or tstamp has a bogus definition,
89 // or compiler initialisation order broken, or something else :) 91 // or compiler initialisation order broken, or something else :)
90 abort (); 92 abort ();
91 } 93 }
92 94
93 tw0 () 95 tw0 ()
94 : time_watcher (this, &tw0::cb) 96 : time_watcher (this, &tw0::cb)
95 { } 97 { }
96 } tw0; 98} tw0;
97 99
98tstamp 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
99 132
100#if IOM_TIME 133#if IOM_TIME
101tstamp io_manager::now () 134tstamp io_manager::now ()
102{ 135{
103 struct timeval tv; 136 struct timeval tv;
116 149
117// used for initialisation only 150// used for initialisation only
118static struct init { 151static struct init {
119 init () 152 init ()
120 { 153 {
154 iom_valid = true;
155
121#if IOM_SIG 156#if IOM_SIG
122 sigemptyset (&sigs); 157 sigemptyset (&sigs);
123 158
124 if (pipe (sigpipe)) 159 if (pipe (sigpipe))
125 { 160 {
129 164
130 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC); 165 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC);
131 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC); 166 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC);
132#endif 167#endif
133 168
134 iom_valid = true; 169#if IOM_CHILD
170 sw0.start (SIGCHLD);
171#endif
135 172
136#if IOM_TIME 173#if IOM_TIME
137 io_manager::set_now (); 174 io_manager::set_now ();
138 175
139 tw0.start (TSTAMP_MAX); 176 tw0.start (TSTAMP_MAX);
140#endif 177#endif
178 }
179
180 ~init ()
181 {
182 iom_valid = false;
141 } 183 }
142 184
143 static void required (); 185 static void required ();
144} init; 186} init;
145 187
217 write (sigpipe[1], &ch, 1); 259 write (sigpipe[1], &ch, 1);
218} 260}
219 261
220void io_manager::reg (sig_watcher &w) 262void io_manager::reg (sig_watcher &w)
221{ 263{
264 init::required ();
265
222 assert (0 < w.signum); 266 assert (0 < w.signum);
223 267
224 sw.reserve (w.signum); 268 sw.reserve (w.signum);
225 269
226 while (sw.size () < w.signum) // pathetic 270 while (sw.size () < w.signum) // pathetic
251 io_manager::reg (w, *sv); 295 io_manager::reg (w, *sv);
252} 296}
253 297
254void io_manager::unreg (sig_watcher &w) 298void io_manager::unreg (sig_watcher &w)
255{ 299{
256 if (!w.active) 300 if (!w.active || !iom_valid)
257 return; 301 return;
258 302
259 assert (0 < w.signum && w.signum <= sw.size ()); 303 assert (0 < w.signum && w.signum <= sw.size ());
260 304
261 io_manager::unreg (w, *sw[w.signum - 1]); 305 io_manager::unreg (w, *sw[w.signum - 1]);
265{ 309{
266 stop (); 310 stop ();
267 this->signum = signum; 311 this->signum = signum;
268 io_manager::reg (*this); 312 io_manager::reg (*this);
269} 313}
314#endif
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); }
270#endif 319#endif
271 320
272void io_manager::loop () 321void io_manager::loop ()
273{ 322{
274 init::required (); 323 init::required ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines