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.11 by pcg, Sat Apr 5 02:32:40 2003 UTC vs.
Revision 1.30 by pcg, Thu Mar 3 16:54:34 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines