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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines