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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines