ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/iom.C
(Generate patch)

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines