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.14 by pcg, Sat Jan 17 14:08:57 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*/
21
22#include "iom.h"
19 23
20#include <cstdio> 24#include <cstdio>
21#include <cstdlib> 25#include <cstdlib>
22#include <cerrno> 26#include <cerrno>
27#include <cassert>
23 28
29#include <sys/types.h>
30#include <sys/time.h>
31
32#if 1 // older unices need these includes for select (2)
33# include <unistd.h>
34# include <time.h>
35#endif
36
37#if IOM_CHILD
38# include <sys/wait.h>
39#endif
40
41#if IOM_SIG
42# include <csignal>
43# include <fcntl.h>
44#endif
45
46// if the BSDs would at least be marginally POSIX-compatible.. *sigh*
47// until that happens, sys/select.h must come last
24#include <sys/select.h> 48#include <sys/select.h>
25#include <sys/time.h>
26 49
27#include "iom.h" 50#define TIMEVAL timeval
51#define TV_FRAC tv_usec
52#define TV_MULT 1000000L
28 53
29// TSTAMP_MAX must still fit into a positive struct timeval 54#if IOM_IO
30#define TSTAMP_MAX (double)(1UL<<31) 55static io_manager_vec<io_watcher> iow;
56#endif
57#if IOM_CHECK
58static io_manager_vec<check_watcher> cw;
59#endif
60#if IOM_TIME
61static io_manager_vec<time_watcher> tw;
62#endif
63#if IOM_IDLE
64static io_manager_vec<idle_watcher> iw;
65#endif
66#if IOM_SIG
67static int sigpipe[2]; // signal signalling pipe
68static sigset_t sigs;
69struct sig_vec : io_manager_vec<sig_watcher> {
70 int pending;
71 sig_vec ()
72 : pending (false)
73 { }
74};
75static vector<sig_vec *> sw;
76#endif
77#if IOM_CHILD
78static io_manager_vec<child_watcher> pw;
79#endif
31 80
32// this is a dummy time watcher to ensure that the first 81// this is a dummy time watcher to ensure that the first
33// time watcher is _always_ valid, this gets rid of a lot 82// time watcher is _always_ valid, this gets rid of a lot
34// of null-pointer-checks 83// of null-pointer-checks
35// (must come _before_ iom is being defined) 84// (must come _before_ iom is being defined)
36static struct tw0 : time_watcher { 85static struct tw0 : time_watcher
86{
37 void cb (time_watcher &w) 87 void cb (time_watcher &w)
38 { 88 {
39 // should never get called 89 // should never get called
40 // reached end-of-time, or tstamp has a bogus definition, 90 // reached end-of-time, or tstamp has a bogus definition,
41 // or compiler initilization order broken, or somethine else :) 91 // or compiler initialisation order broken, or something else :)
42 abort (); 92 abort ();
43 } 93 }
44 94
45 tw0() 95 tw0 ()
46 : time_watcher (this, &tw0::cb) 96 : time_watcher (this, &tw0::cb)
47 { } 97 { }
48} tw0; 98} tw0;
49 99
50tstamp 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
132
133#if IOM_TIME
134tstamp io_manager::now ()
135{
136 struct timeval tv;
137
138 gettimeofday (&tv, 0);
139 return (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.;
140}
141
142void io_manager::set_now ()
143{
144 NOW = now ();
145}
146#endif
147
51static bool iom_valid; 148static bool iom_valid;
52io_manager iom; 149
150// used for initialisation only
151static struct init {
152 init ()
153 {
154 iom_valid = true;
155
156#if IOM_SIG
157 sigemptyset (&sigs);
158
159 if (pipe (sigpipe))
160 {
161 perror ("io_manager: unable to create signal pipe, aborting.");
162 abort ();
163 }
164
165 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC);
166 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC);
167#endif
168
169#if IOM_CHILD
170 sw0.start (SIGCHLD);
171#endif
172
173#if IOM_TIME
174 io_manager::set_now ();
175
176 tw0.start (TSTAMP_MAX);
177#endif
178 }
179
180 ~init ()
181 {
182 iom_valid = false;
183 }
184
185 static void required ();
186} init;
187
188void
189init::required ()
190{
191 if (!iom_valid)
192 {
193 write (2, "io_manager: early registration attempt, aborting.\n",
194 sizeof ("io_manager: early registration attempt, aborting.\n") - 1);
195 abort ();
196 }
197}
53 198
54template<class watcher> 199template<class watcher>
55void io_manager::reg (watcher *w, io_manager_vec<watcher> &queue) 200void io_manager::reg (watcher &w, io_manager_vec<watcher> &queue)
56{ 201{
57 if (!iom_valid) 202 init::required ();
58 abort ();
59 203
60 if (!w->active) 204 if (!w.active)
61 { 205 {
62 queue.push_back (w); 206 queue.push_back (&w);
63 w->active = queue.size (); 207 w.active = queue.size ();
64 } 208 }
65} 209}
66 210
67template<class watcher> 211template<class watcher>
68void io_manager::unreg (watcher *w, io_manager_vec<watcher> &queue) 212void io_manager::unreg (watcher &w, io_manager_vec<watcher> &queue)
69{ 213{
70 if (!iom_valid) 214 if (!iom_valid)
71 return; 215 return;
72 216
73 if (w->active) 217 if (w.active)
74 { 218 {
75 queue [w->active - 1] = 0; 219 queue [w.active - 1] = 0;
76 w->active = 0; 220 w.active = 0;
77 } 221 }
78} 222}
79 223
80#if IOM_TIME 224#if IOM_TIME
81void time_watcher::trigger () 225void time_watcher::trigger ()
82{ 226{
83 call (*this); 227 call (*this);
84 228 io_manager::reg (*this);
85 iom.reg (this);
86} 229}
87 230
88void io_manager::reg (time_watcher *w) { reg (w, tw); } 231void io_manager::reg (time_watcher &w) { io_manager::reg (w, tw); }
89void io_manager::unreg (time_watcher *w) { unreg (w, tw); } 232void io_manager::unreg (time_watcher &w) { io_manager::unreg (w, tw); }
90#endif 233#endif
91 234
92#if IOM_IO 235#if IOM_IO
93void io_manager::reg (io_watcher *w) { reg (w, iow); } 236void io_manager::reg (io_watcher &w) { io_manager::reg (w, iow); }
94void io_manager::unreg (io_watcher *w) { unreg (w, iow); } 237void io_manager::unreg (io_watcher &w) { io_manager::unreg (w, iow); }
95#endif 238#endif
96 239
97#if IOM_CHECK 240#if IOM_CHECK
98void io_manager::reg (check_watcher *w) { reg (w, cw); } 241void io_manager::reg (check_watcher &w) { io_manager::reg (w, cw); }
99void io_manager::unreg (check_watcher *w) { unreg (w, cw); } 242void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); }
100#endif 243#endif
101 244
102#if IOM_IDLE 245#if IOM_IDLE
103void io_manager::reg (idle_watcher *w) { reg (w, iw); } 246void io_manager::reg (idle_watcher &w) { io_manager::reg (w, iw); }
104void io_manager::unreg (idle_watcher *w) { unreg (w, iw); } 247void io_manager::unreg (idle_watcher &w) { io_manager::unreg (w, iw); }
105#endif 248#endif
106 249
107#if IOM_TIME 250#if IOM_SIG
108inline void set_now (void) 251static void
252sighandler (int signum)
109{ 253{
110 struct timeval tv; 254 sw [signum - 1]->pending = true;
111 255
112 gettimeofday (&tv, 0); 256 // we use a pipe for signal notifications, as most current
113 257 // OSes (Linux...) do not implement pselect correctly. ugh.
114 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; 258 char ch = signum; // actual content not used
115#endif 259 write (sigpipe[1], &ch, 1);
116} 260}
261
262void io_manager::reg (sig_watcher &w)
263{
264 init::required ();
265
266 assert (0 < w.signum);
267
268 sw.reserve (w.signum);
269
270 while (sw.size () < w.signum) // pathetic
271 sw.push_back (0);
272
273 sig_vec *&sv = sw[w.signum - 1];
274
275 if (!sv)
276 {
277 sv = new sig_vec;
278
279 sigaddset (&sigs, w.signum);
280 sigprocmask (SIG_BLOCK, &sigs, NULL);
281
282 struct sigaction sa;
283 sa.sa_handler = sighandler;
284 sigfillset (&sa.sa_mask);
285 sa.sa_flags = SA_RESTART;
286
287 if (sigaction (w.signum, &sa, 0))
288 {
289 perror ("io_manager: error while installing signal handler, ignoring.");
290 abort ();
291 }
292
293 }
294
295 io_manager::reg (w, *sv);
296}
297
298void io_manager::unreg (sig_watcher &w)
299{
300 if (!w.active || !iom_valid)
301 return;
302
303 assert (0 < w.signum && w.signum <= sw.size ());
304
305 io_manager::unreg (w, *sw[w.signum - 1]);
306}
307
308void sig_watcher::start (int signum)
309{
310 stop ();
311 this->signum = signum;
312 io_manager::reg (*this);
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); }
319#endif
117 320
118void io_manager::loop () 321void io_manager::loop ()
119{ 322{
323 init::required ();
324
120#if IOM_TIME 325#if IOM_TIME
121 set_now (); 326 set_now ();
122#endif 327#endif
123 328
124 for (;;) 329 for (;;)
125 { 330 {
126 struct timeval *to = 0;
127 struct timeval tval;
128 331
129#if IOM_IDLE 332#if IOM_TIME
130 if (iw.size ()) 333 // call pending time watchers
131 { 334 {
132 tval.tv_sec = 0; 335 bool activity;
133 tval.tv_usec = 0; 336
134 to = &tval;
135 } 337 do
136 else
137#endif
138 {
139#if IOM_TIME
140 time_watcher *next;
141
142 for (;;)
143 { 338 {
144 next = tw[0]; // the first time-watcher must exist at ALL times 339 activity = false;
145 340
146 for (int i = tw.size (); i--; ) 341 for (int i = tw.size (); i--; )
147 if (!tw[i]) 342 if (!tw[i])
148 tw.erase_unordered (i); 343 tw.erase_unordered (i);
149 else if (tw[i]->at < next->at) 344 else if (tw[i]->at <= NOW)
150 next = tw[i];
151
152 if (next->at > NOW)
153 { 345 {
154 if (next != tw[0]) 346 time_watcher &w = *tw[i];
155 { 347
156 double diff = next->at - NOW;
157 tval.tv_sec = (int)diff;
158 tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000);
159 to = &tval;
160 }
161 break; 348 unreg (w);
349 w.call (w);
350
351 activity = true;
162 } 352 }
163 else
164 {
165 unreg (next);
166 next->call (*next);
167 }
168 } 353 }
169#endif 354 while (activity);
170 } 355 }
356#endif
171 357
172#if IOM_CHECK 358#if IOM_CHECK
359 // call all check watchers
173 for (int i = cw.size (); i--; ) 360 for (int i = cw.size (); i--; )
174 if (!cw[i]) 361 if (!cw[i])
175 cw.erase_unordered (i); 362 cw.erase_unordered (i);
176 else 363 else
177 cw[i]->call (*cw[i]); 364 cw[i]->call (*cw[i]);
178#endif 365#endif
179 366
367 struct TIMEVAL *to = 0;
368 struct TIMEVAL tval;
369
180#if IOM_IO 370#if IOM_IDLE
371 if (iw.size ())
372 {
373 tval.tv_sec = 0;
374 tval.TV_FRAC = 0;
375 to = &tval;
376 }
377 else
378#endif
379 {
380#if IOM_TIME
381 // find earliest active watcher
382 time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times
383
384 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
385 if (*i && (*i)->at < next->at)
386 next = *i;
387
388 if (next->at > NOW && next != tw[0])
389 {
390 double diff = next->at - NOW;
391 tval.tv_sec = (int)diff;
392 tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT);
393 to = &tval;
394 }
395 }
396#endif
397
398#if IOM_IO || IOM_SIG
181 fd_set rfd, wfd, efd; 399 fd_set rfd, wfd;
182 400
183 FD_ZERO (&rfd); 401 FD_ZERO (&rfd);
184 FD_ZERO (&wfd); 402 FD_ZERO (&wfd);
185 403
186 int fds = 0; 404 int fds = 0;
187 405
406# if IOM_IO
188 for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); ) 407 for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); )
189 if (*i) 408 if (*i)
190 { 409 {
191 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); 410 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd);
192 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); 411 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd);
193 412
194 if ((*i)->fd >= fds) fds = (*i)->fd + 1; 413 if ((*i)->fd >= fds) fds = (*i)->fd + 1;
195 } 414 }
415# endif
196 416
197 if (!to && !fds) //TODO: also check idle_watchers and check_watchers 417 if (!to && !fds) //TODO: also check idle_watchers and check_watchers?
198 break; // no events 418 break; // no events
199 419
420# if IOM_SIG
421 FD_SET (sigpipe[0], &rfd);
422 if (sigpipe[0] >= fds) fds = sigpipe[0] + 1;
423# endif
424
425# if IOM_SIG
426 // there is no race, as we use a pipe for signals, so select
427 // will return if a signal is caught.
428 sigprocmask (SIG_UNBLOCK, &sigs, NULL);
429# endif
200 fds = select (fds, &rfd, &wfd, &efd, to); 430 fds = select (fds, &rfd, &wfd, NULL, to);
431# if IOM_SIG
432 sigprocmask (SIG_BLOCK, &sigs, NULL);
433# endif
434
201# if IOM_TIME 435# if IOM_TIME
436 {
437 // update time, try to compensate for gross non-monotonic time changes
438 tstamp diff = NOW;
202 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 }
203# endif 447# endif
204 448
205 if (fds > 0) 449 if (fds > 0)
206 for (int i = iow.size (); i--; ) 450 {
207 if (!iow[i]) 451# if IOM_SIG
208 iow.erase_unordered (i); 452 if (FD_ISSET (sigpipe[0], &rfd))
209 else
210 { 453 {
211 short revents = iow[i]->events; 454 char ch;
212 455
213 if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ; 456 while (read (sigpipe[0], &ch, 1) > 0)
214 if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE; 457 ;
215 458
216 if (revents) 459 for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); )
217 iow[i]->call (*iow[i], revents); 460 if (*svp && (*svp)->pending)
461 {
462 sig_vec &sv = **svp;
463 for (int i = sv.size (); i--; )
464 if (!sv[i])
465 sv.erase_unordered (i);
466 else
467 sv[i]->call (*sv[i]);
468
469 sv.pending = false;
470 }
218 } 471 }
472# endif
473
474# if IOM_IO
475 for (int i = iow.size (); i--; )
476 if (!iow[i])
477 iow.erase_unordered (i);
478 else
479 {
480 io_watcher &w = *iow[i];
481 short revents = w.events;
482
483 if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ;
484 if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE;
485
486 if (revents)
487 w.call (w, revents);
488 }
489#endif
490 }
219 else if (fds < 0 && errno != EINTR) 491 else if (fds < 0 && errno != EINTR)
220 { 492 {
221 perror ("Error while waiting for I/O or time event"); 493 perror ("io_manager: fatal error while waiting for I/O or time event, aborting.");
222 abort (); 494 abort ();
223 } 495 }
224#if IOM_IDLE 496#if IOM_IDLE
225 else 497 else
226 for (int i = iw.size (); i--; ) 498 for (int i = iw.size (); i--; )
240 break; 512 break;
241#endif 513#endif
242 } 514 }
243} 515}
244 516
245io_manager::io_manager ()
246{
247 iom_valid = true;
248
249#if IOM_TIME
250 set_now ();
251
252 tw0.start (TSTAMP_MAX);
253 printf ("abort, %f but inly on %f\n", NOW, tw0.at);
254#endif
255}
256
257io_manager::~io_manager ()
258{
259 iom_valid = false;
260}
261

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines