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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines