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.12 by pcg, Thu Oct 16 02:41:21 2003 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 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*/
19 21
20#include "config.h" 22#include "iom.h"
23
24#include <cstdio>
25#include <cstdlib>
26#include <cerrno>
21 27
22#include <sys/time.h> 28#include <sys/time.h>
23 29
24#include <algorithm> 30#include <assert.h>
31
32#if 1 // older unices need these includes for select (2)
33# include <unistd.h>
34# include <sys/types.h>
35# include <time.h>
36#endif
37
38// for IOM_SIG
39#if IOM_SIG
25#include <functional> 40# include <csignal>
41# include <fcntl.h>
42#endif
26 43
27#include "gettext.h" 44// if the BSDs would at least be marginally POSIX-compatible.. *sigh*
45// until that happens, sys/select.h must come last
46#include <sys/select.h>
28 47
29#include "slog.h" 48#define TIMEVAL timeval
30#include "iom.h" 49#define TV_FRAC tv_usec
50#define TV_MULT 1000000L
51
52#if IOM_IO
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
75
76// this is a dummy time watcher to ensure that the first
77// time watcher is _always_ valid, this gets rid of a lot
78// of null-pointer-checks
79// (must come _before_ iom is being defined)
80static struct tw0 : time_watcher
81 {
82 void cb (time_watcher &w)
83 {
84 // should never get called
85 // reached end-of-time, or tstamp has a bogus definition,
86 // or compiler initialisation order broken, or something else :)
87 abort ();
88 }
89
90 tw0 ()
91 : time_watcher (this, &tw0::cb)
92 { }
93 } tw0;
31 94
32tstamp NOW; 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
33bool iom_valid; 112static bool iom_valid;
34io_manager iom;
35 113
36inline bool earliest_first (const time_watcher *a, const time_watcher *b) 114// used for initialisation only
37{ 115static struct init {
38 return a->at > b->at; 116 init ()
39} 117 {
118#if IOM_SIG
119 sigemptyset (&sigs);
40 120
41void time_watcher::set (tstamp when) 121 if (pipe (sigpipe))
42{ 122 {
43 at = when; 123 perror ("io_manager: unable to create signal pipe, aborting.");
124 abort ();
125 }
44 126
45 if (registered) 127 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC);
46 iom.reschedule_time_watchers (); 128 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC);
47} 129#endif
48 130
131 iom_valid = true;
132
133#if IOM_TIME
134 io_manager::set_now ();
135
136 tw0.start (TSTAMP_MAX);
137#endif
138 }
139
140 ~init ()
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 ();
156 }
157}
158
159template<class watcher>
160void io_manager::reg (watcher &w, io_manager_vec<watcher> &queue)
161{
162 init::required ();
163
164 if (!w.active)
165 {
166 queue.push_back (&w);
167 w.active = queue.size ();
168 }
169}
170
171template<class watcher>
172void io_manager::unreg (watcher &w, io_manager_vec<watcher> &queue)
173{
174 if (!iom_valid)
175 return;
176
177 if (w.active)
178 {
179 queue [w.active - 1] = 0;
180 w.active = 0;
181 }
182}
183
184#if IOM_TIME
49void time_watcher::trigger () 185void time_watcher::trigger ()
50{ 186{
51 call (*this); 187 call (*this);
52 188 io_manager::reg (*this);
53 if (registered)
54 iom.reschedule_time_watchers ();
55 else
56 iom.reg (this);
57} 189}
58 190
59time_watcher::~time_watcher () 191void io_manager::reg (time_watcher &w) { io_manager::reg (w, tw); }
60{ 192void io_manager::unreg (time_watcher &w) { io_manager::unreg (w, tw); }
61 if (iom_valid) 193#endif
62 iom.unreg (this);
63}
64 194
65void io_watcher::set(int fd_, short events_) 195#if IOM_IO
66{ 196void io_manager::reg (io_watcher &w) { io_manager::reg (w, iow); }
67 fd = fd_; 197void io_manager::unreg (io_watcher &w) { io_manager::unreg (w, iow); }
68 events = events_; 198#endif
69 199
70 if (registered) 200#if IOM_CHECK
71 { 201void io_manager::reg (check_watcher &w) { io_manager::reg (w, cw); }
72 iom.unreg (this); 202void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); }
73 iom.reg (this); 203#endif
74 }
75}
76 204
77io_watcher::~io_watcher () 205#if IOM_IDLE
78{ 206void io_manager::reg (idle_watcher &w) { io_manager::reg (w, iw); }
79 if (iom_valid) 207void io_manager::unreg (idle_watcher &w) { io_manager::unreg (w, iw); }
80 iom.unreg (this); 208#endif
81}
82 209
210#if IOM_SIG
211static void
212sighandler (int signum)
213{
214 sw [signum - 1]->pending = true;
215
216 // we use a pipe for signal notifications, as most current
217 // OSes (Linux...) do not implement pselect correctly. ugh.
218 char ch = signum; // actual content not used
219 write (sigpipe[1], &ch, 1);
220}
221
83void io_manager::reg (io_watcher *w) 222void io_manager::reg (sig_watcher &w)
84{ 223{
85 if (!w->registered) 224 init::required ();
86 {
87 w->registered = true;
88 225
89 pollfd pfd; 226 assert (0 < w.signum);
90 227
91 pfd.fd = w->fd; 228 sw.reserve (w.signum);
92 pfd.events = w->events;
93 229
94 pfs.push_back (pfd); 230 while (sw.size () < w.signum) // pathetic
95 iow.push_back (w); 231 sw.push_back (0);
232
233 sig_vec *&sv = sw[w.signum - 1];
234
235 if (!sv)
96 } 236 {
97} 237 sv = new sig_vec;
98 238
99void io_manager::unreg (io_watcher *w) 239 sigaddset (&sigs, w.signum);
100{ 240 sigprocmask (SIG_BLOCK, &sigs, NULL);
101 if (w->registered)
102 {
103 w->registered = false;
104 241
105 unsigned int sz = iow.size (); 242 struct sigaction sa;
106 unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); 243 sa.sa_handler = sighandler;
244 sigfillset (&sa.sa_mask);
245 sa.sa_flags = SA_RESTART;
107 246
108 assert (i != sz); 247 if (sigaction (w.signum, &sa, 0))
109
110 if (sz == 1)
111 { 248 {
249 perror ("io_manager: error while installing signal handler, ignoring.");
112 pfs.clear (); 250 abort ();
113 iow.clear ();
114 } 251 }
115 else if (i == sz - 1) 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
275
276void io_manager::loop ()
277{
278 init::required ();
279
280#if IOM_TIME
281 set_now ();
282#endif
283
284 for (;;)
285 {
286
287#if IOM_TIME
288 // call pending time watchers
289 {
290 bool activity;
291
292 do
293 {
294 activity = false;
295
296 for (int i = tw.size (); i--; )
297 if (!tw[i])
298 tw.erase_unordered (i);
299 else if (tw[i]->at <= NOW)
300 {
301 time_watcher &w = *tw[i];
302
303 unreg (w);
304 w.call (w);
305
306 activity = true;
307 }
308 }
309 while (activity);
310 }
311#endif
312
313#if IOM_CHECK
314 // call all check watchers
315 for (int i = cw.size (); i--; )
316 if (!cw[i])
317 cw.erase_unordered (i);
318 else
319 cw[i]->call (*cw[i]);
320#endif
321
322 struct TIMEVAL *to = 0;
323 struct TIMEVAL tval;
324
325#if IOM_IDLE
326 if (iw.size ())
116 { 327 {
117 iow.pop_back (); 328 tval.tv_sec = 0;
118 pfs.pop_back (); 329 tval.TV_FRAC = 0;
330 to = &tval;
119 } 331 }
120 else 332 else
333#endif
121 { 334 {
122 iow[i] = iow[sz - 1]; iow.pop_back (); 335#if IOM_TIME
123 pfs[i] = pfs[sz - 1]; pfs.pop_back (); 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 }
124 } 350 }
125 } 351#endif
126}
127 352
128void io_manager::reschedule_time_watchers () 353#if IOM_IO || IOM_SIG
129{ 354 fd_set rfd, wfd;
130 make_heap (tw.begin (), tw.end (), earliest_first);
131}
132 355
133void io_manager::reg (time_watcher *w) 356 FD_ZERO (&rfd);
134{ 357 FD_ZERO (&wfd);
135 if (!w->registered)
136 {
137 w->registered = true;
138 358
139 tw.push_back (w); 359 int fds = 0;
140 push_heap (tw.begin (), tw.end (), earliest_first);
141 }
142}
143 360
144void io_manager::unreg (time_watcher *w) 361# if IOM_IO
145{ 362 for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); )
146 if (w->registered) 363 if (*i)
147 { 364 {
148 w->registered = false; 365 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd);
366 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd);
149 367
150 unsigned int sz = tw.size (); 368 if ((*i)->fd >= fds) fds = (*i)->fd + 1;
151 unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); 369 }
370# endif
152 371
153 assert (i != sz); 372 if (!to && !fds) //TODO: also check idle_watchers and check_watchers?
373 break; // no events
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
385 fds = select (fds, &rfd, &wfd, NULL, to);
386# if IOM_SIG
387 sigprocmask (SIG_BLOCK, &sigs, NULL);
388# endif
389
390# if IOM_TIME
154 391 {
155 if (i != sz - 1) 392 // update time, try to compensate for gross non-monotonic time changes
156 tw[i] = tw[sz - 1]; 393 tstamp diff = NOW;
394 set_now ();
395 diff = NOW - diff;
157 396
158 tw.pop_back (); 397 if (diff < 0)
159 reschedule_time_watchers (); 398 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
399 if (*i)
400 (*i)->at += diff;
160 } 401 }
161} 402# endif
162 403
163inline void set_now (void) 404 if (fds > 0)
164{
165 struct timeval tv;
166
167 gettimeofday (&tv, 0);
168
169 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000;
170}
171
172void io_manager::loop ()
173{
174 set_now ();
175
176 for (;;)
177 {
178 while (tw[0]->at <= NOW)
179 { 405 {
180 // remove the first watcher 406# if IOM_SIG
181 time_watcher *w = tw[0]; 407 if (FD_ISSET (sigpipe[0], &rfd))
408 {
409 char ch;
182 410
183 pop_heap (tw.begin (), tw.end (), earliest_first); 411 while (read (sigpipe[0], &ch, 1) > 0)
184 tw.pop_back (); 412 ;
185 413
186 w->registered = false; 414 for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); )
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]);
187 423
188 // call it 424 sv.pending = false;
189 w->call (*w); 425 }
426 }
427# endif
190 428
191 // re-add it if necessary 429# if IOM_IO
192 if (w->at >= 0 && !w->registered) 430 for (int i = iow.size (); i--; )
193 reg (w); 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
194 } 445 }
446 else if (fds < 0 && errno != EINTR)
447 {
448 perror ("io_manager: fatal error while waiting for I/O or time event, aborting.");
449 abort ();
450 }
451#if IOM_IDLE
452 else
453 for (int i = iw.size (); i--; )
454 if (!iw[i])
455 iw.erase_unordered (i);
456 else
457 iw[i]->call (*iw[i]);
458#endif
195 459
196 int timeout = (int) ((tw[0]->at - NOW) * 1000); 460#elif IOM_TIME
461 if (!to)
462 break;
197 463
198 int fds = poll (&pfs[0], pfs.size (), timeout); 464 select (0, 0, 0, 0, &to);
199
200 set_now (); 465 set_now ();
201 466#else
202 vector<io_watcher *>::iterator w; 467 break;
203 vector<pollfd>::iterator p; 468#endif
204
205 for (w = iow.begin (), p = pfs.begin ();
206 fds > 0 && w < iow.end ();
207 ++w, ++p)
208 if (p->revents)
209 {
210 --fds;
211
212 if (p->revents & POLLNVAL)
213 {
214 slog (L_ERR, _("io_watcher started on illegal file descriptor, disabling."));
215 (*w)->stop ();
216 }
217 else
218 (*w)->call (**w, p->revents);
219 }
220 } 469 }
221} 470}
222 471
223void io_manager::idle_cb (time_watcher &w)
224{
225 w.at = NOW + 86400; // wake up every day, for no good reason
226}
227
228io_manager::io_manager ()
229{
230 iom_valid = true;
231
232 set_now ();
233 idle = new time_watcher (this, &io_manager::idle_cb);
234 idle->start (0);
235}
236
237io_manager::~io_manager ()
238{
239 iom_valid = false;
240}
241

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines