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.32 by root, Wed Jan 11 21:24:41 2006 UTC vs.
Revision 1.38 by root, Thu Oct 25 10:44:14 2007 UTC

1/* 1/*
2 iom.C -- generic I/O multiplexer 2 iom.C -- generic I/O multiplexer
3 Copyright (C) 2003, 2004 Marc Lehmann <gvpe@schmorp.de> 3 Copyright (C) 2003-2006 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE. 5 This file is part of GVPE.
6 6
7 GVPE is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify
8 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
22#include "iom.h" 22#include "iom.h"
23 23
24#include <cstdio> 24#include <cstdio>
25#include <cstdlib> 25#include <cstdlib>
26#include <cerrno> 26#include <cerrno>
27#include <cassert>
27 28
29#include <sys/types.h>
28#include <sys/time.h> 30#include <sys/time.h>
29
30#include <assert.h>
31 31
32#if 1 // older unices need these includes for select (2) 32#if 1 // older unices need these includes for select (2)
33# include <unistd.h> 33# include <unistd.h>
34# include <sys/types.h>
35# include <time.h> 34# include <time.h>
36#endif 35#endif
37 36
38// for IOM_SIG 37#if IOM_CHILD
38# include <sys/wait.h>
39#endif
40
39#if IOM_SIG 41#if IOM_SIG
40# include <csignal> 42# include <csignal>
41# include <fcntl.h> 43# include <fcntl.h>
42#endif 44#endif
43 45
47 49
48#define TIMEVAL timeval 50#define TIMEVAL timeval
49#define TV_FRAC tv_usec 51#define TV_FRAC tv_usec
50#define TV_MULT 1000000L 52#define TV_MULT 1000000L
51 53
54#ifndef IOM_LIBEVENT
52#if IOM_IO 55#if IOM_IO
53static io_manager_vec<io_watcher> iow; 56static io_manager_vec<io_watcher> iow;
54#endif 57#endif
58#if IOM_TIME
59static io_manager_vec<time_watcher> tw;
60#endif
61#endif
62
55#if IOM_CHECK 63#if IOM_CHECK
56static io_manager_vec<check_watcher> cw; 64static io_manager_vec<check_watcher> cw;
57#endif 65#endif
58#if IOM_TIME
59static io_manager_vec<time_watcher> tw;
60#endif
61#if IOM_IDLE 66#if IOM_IDLE
62static io_manager_vec<idle_watcher> iw; 67static io_manager_vec<idle_watcher> iw;
63#endif 68#endif
69
64#if IOM_SIG 70#if IOM_SIG
65static int sigpipe[2]; // signal signalling pipe 71static int sigpipe[2]; // signal signalling pipe
66static sigset_t sigs; 72static sigset_t sigs;
67struct sig_vec : io_manager_vec<sig_watcher> { 73struct sig_vec : io_manager_vec<sig_watcher> {
68 int pending; 74 int pending;
71 { } 77 { }
72}; 78};
73static vector<sig_vec *> sw; 79static vector<sig_vec *> sw;
74#endif 80#endif
75 81
82#if IOM_CHILD
83static io_manager_vec<child_watcher> pw;
84#endif
85
76// this is a dummy time watcher to ensure that the first 86// this is a dummy time watcher to ensure that the first
77// time watcher is _always_ valid, this gets rid of a lot 87// time watcher is _always_ valid, this gets rid of a lot
78// of null-pointer-checks 88// of null-pointer-checks
79// (must come _before_ iom is being defined) 89// (must come _before_ iom is being defined)
80static struct tw0 : time_watcher 90static struct tw0 : time_watcher
91{
92 void cb (time_watcher &w)
81 { 93 {
82 void cb (time_watcher &w)
83 {
84 // should never get called 94 // should never get called
85 // reached end-of-time, or tstamp has a bogus definition, 95 // reached end-of-time, or tstamp has a bogus definition,
86 // or compiler initialisation order broken, or something else :) 96 // or compiler initialisation order broken, or something else :)
87 abort (); 97 abort ();
88 } 98 }
89 99
90 tw0 () 100 tw0 ()
91 : time_watcher (this, &tw0::cb) 101 : time_watcher (this, &tw0::cb)
92 { } 102 { }
93 } tw0; 103} tw0;
94 104
95tstamp NOW; 105tstamp NOW;
106
107#if IOM_CHILD
108// sig_watcher for child signal(s)
109static struct sw0 : sig_watcher
110{
111 void cb (sig_watcher &w)
112 {
113 // SIGCHLD, call corresponding watchera
114 pid_t pid;
115 int status;
116
117 while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
118 for (int i = pw.size (); i--; )
119 {
120 child_watcher *w = pw[i];
121
122 if (!w)
123 pw.erase_unordered (i);
124 else if (w->pid == pid)
125 {
126 io_manager::unreg (*w);
127 w->call (*w, status);
128 }
129 }
130 }
131
132 sw0 ()
133 : sig_watcher (this, &sw0::cb)
134 { }
135} sw0;
136#endif
96 137
97#if IOM_TIME 138#if IOM_TIME
98tstamp io_manager::now () 139tstamp io_manager::now ()
99{ 140{
100 struct timeval tv; 141 struct timeval tv;
113 154
114// used for initialisation only 155// used for initialisation only
115static struct init { 156static struct init {
116 init () 157 init ()
117 { 158 {
159#ifdef IOM_PREINIT
160 { IOM_PREINIT }
161#endif
162#ifdef IOM_LIBEVENT
163 event_init ();
164#endif
165 iom_valid = true;
166
118#if IOM_SIG 167#if IOM_SIG
119 sigemptyset (&sigs); 168 sigemptyset (&sigs);
120 169
121 if (pipe (sigpipe)) 170 if (pipe (sigpipe))
122 { 171 {
126 175
127 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC); 176 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); 177 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC);
129#endif 178#endif
130 179
131 iom_valid = true; 180#if IOM_CHILD
181 sw0.start (SIGCHLD);
182#endif
132 183
133#if IOM_TIME 184#if IOM_TIME
134 io_manager::set_now (); 185 io_manager::set_now ();
135 186
136 tw0.start (TSTAMP_MAX); 187 tw0.start (TSTAMP_MAX);
188#endif
189
190#ifdef IOM_POSTINIT
191 { IOM_POSTINIT }
137#endif 192#endif
138 } 193 }
139 194
140 ~init () 195 ~init ()
141 { 196 {
180 w.active = 0; 235 w.active = 0;
181 } 236 }
182} 237}
183 238
184#if IOM_TIME 239#if IOM_TIME
240# ifdef IOM_LIBEVENT
241 void iom_time_c_callback (int fd, short events, void *data)
242 {
243 time_watcher *w = static_cast<time_watcher *>(data);
244 w->call (*w);
245 }
246# else
247 void io_manager::reg (time_watcher &w) { io_manager::reg (w, tw); }
248 void io_manager::unreg (time_watcher &w) { io_manager::unreg (w, tw); }
249# endif
250
185void time_watcher::trigger () 251void time_watcher::trigger ()
186{ 252{
187 call (*this); 253 call (*this);
188 io_manager::reg (*this); 254 start ();
189} 255}
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 256#endif
194 257
195#if IOM_IO 258#if IOM_IO
259# ifdef IOM_LIBEVENT
260 void iom_io_c_callback (int fd, short events, void *data)
261 {
262 io_watcher *w = static_cast<io_watcher *>(data);
263 w->call (*w, events);
264 }
265# else
196void io_manager::reg (io_watcher &w) { io_manager::reg (w, iow); } 266 void io_manager::reg (io_watcher &w) { io_manager::reg (w, iow); }
197void io_manager::unreg (io_watcher &w) { io_manager::unreg (w, iow); } 267 void io_manager::unreg (io_watcher &w) { io_manager::unreg (w, iow); }
268# endif
198#endif 269#endif
199 270
200#if IOM_CHECK 271#if IOM_CHECK
201void io_manager::reg (check_watcher &w) { io_manager::reg (w, cw); } 272void io_manager::reg (check_watcher &w) { io_manager::reg (w, cw); }
202void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); } 273void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); }
271 this->signum = signum; 342 this->signum = signum;
272 io_manager::reg (*this); 343 io_manager::reg (*this);
273} 344}
274#endif 345#endif
275 346
347#if IOM_CHILD
348void io_manager::reg (child_watcher &w) { io_manager::reg (w, pw); }
349void io_manager::unreg (child_watcher &w) { io_manager::unreg (w, pw); }
350#endif
351
276void io_manager::loop () 352void io_manager::loop ()
277{ 353{
278 init::required (); 354 init::required ();
279 355
280#if IOM_TIME 356 #if IOM_TIME
281 set_now (); 357 set_now ();
282#endif 358 #endif
283 359
284 for (;;) 360 for (;;)
285 { 361 {
286 362 #ifndef IOM_LIBEVENT
287#if IOM_TIME 363 #if IOM_TIME
288 // call pending time watchers 364 // call pending time watchers
289 { 365 {
290 bool activity; 366 bool activity;
291 367
292 do 368 do
293 { 369 {
294 activity = false; 370 activity = false;
295 371
296 for (int i = tw.size (); i--; ) 372 for (int i = tw.size (); i--; )
297 if (!tw[i]) 373 if (!tw[i])
298 tw.erase_unordered (i); 374 tw.erase_unordered (i);
299 else if (tw[i]->at <= NOW) 375 else if (tw[i]->at <= NOW)
300 { 376 {
301 time_watcher &w = *tw[i]; 377 time_watcher &w = *tw[i];
378
379 unreg (w);
380 w.call (w);
381
382 activity = true;
302 383 }
303 unreg (w);
304 w.call (w);
305
306 activity = true;
307 }
308 } 384 }
309 while (activity); 385 while (activity);
310 } 386 }
311#endif 387 #endif
388 #endif
312 389
313#if IOM_CHECK 390 #if IOM_CHECK
314 // call all check watchers 391 // call all check watchers
315 for (int i = cw.size (); i--; ) 392 for (int i = cw.size (); i--; )
316 if (!cw[i]) 393 if (!cw[i])
317 cw.erase_unordered (i); 394 cw.erase_unordered (i);
318 else 395 else
319 cw[i]->call (*cw[i]); 396 cw[i]->call (*cw[i]);
320#endif 397 #endif
321 398
322 struct TIMEVAL *to = 0; 399 struct TIMEVAL *to = 0;
323 struct TIMEVAL tval; 400 struct TIMEVAL tval;
324 401
325#if IOM_IDLE 402#if IOM_IDLE
329 tval.TV_FRAC = 0; 406 tval.TV_FRAC = 0;
330 to = &tval; 407 to = &tval;
331 } 408 }
332 else 409 else
333#endif 410#endif
411#ifndef IOM_LIBEVENT
334 { 412 {
335#if IOM_TIME 413 #if IOM_TIME
336 // find earliest active watcher 414 // find earliest active watcher
337 time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times 415 time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times
338 416
339 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) 417 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
340 if (*i && (*i)->at < next->at) 418 if (*i && (*i)->at < next->at)
341 next = *i; 419 next = *i;
342 420
343 if (next->at > NOW && next != tw[0]) 421 if (next->at > NOW && next != tw[0])
344 { 422 {
345 double diff = next->at - NOW; 423 double diff = next->at - NOW;
346 tval.tv_sec = (int)diff; 424 tval.tv_sec = (int)diff;
347 tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT); 425 tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT);
348 to = &tval; 426 to = &tval;
349 } 427 }
428 #endif
350 } 429 }
351#endif
352 430
353#if IOM_IO || IOM_SIG 431#if IOM_IO || IOM_SIG
354 fd_set rfd, wfd; 432 fd_set rfd, wfd;
355 433
356 FD_ZERO (&rfd); 434 FD_ZERO (&rfd);
357 FD_ZERO (&wfd); 435 FD_ZERO (&wfd);
358 436
359 int fds = 0; 437 int fds = 0;
360 438
361# if IOM_IO 439 #if IOM_IO
362 for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); ) 440 for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); )
363 if (*i) 441 if (*i)
364 { 442 {
365 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); 443 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd);
366 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); 444 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd);
367 445
368 if ((*i)->fd >= fds) fds = (*i)->fd + 1; 446 if ((*i)->fd >= fds) fds = (*i)->fd + 1;
369 } 447 }
370# endif 448 #endif
371 449
372 if (!to && !fds) //TODO: also check idle_watchers and check_watchers? 450 if (!to && !fds) //TODO: also check idle_watchers and check_watchers?
373 break; // no events 451 break; // no events
374 452
375# if IOM_SIG 453 #if IOM_SIG
376 FD_SET (sigpipe[0], &rfd); 454 FD_SET (sigpipe[0], &rfd);
377 if (sigpipe[0] >= fds) fds = sigpipe[0] + 1; 455 if (sigpipe[0] >= fds) fds = sigpipe[0] + 1;
378# endif 456 #endif
379 457
380# if IOM_SIG 458 #if IOM_SIG
381 // there is no race, as we use a pipe for signals, so select 459 // there is no race, as we use a pipe for signals, so select
382 // will return if a signal is caught. 460 // will return if a signal is caught.
383 sigprocmask (SIG_UNBLOCK, &sigs, NULL); 461 sigprocmask (SIG_UNBLOCK, &sigs, NULL);
384# endif 462 #endif
385 fds = select (fds, &rfd, &wfd, NULL, to); 463 fds = select (fds, &rfd, &wfd, NULL, to);
386# if IOM_SIG 464 #if IOM_SIG
387 sigprocmask (SIG_BLOCK, &sigs, NULL); 465 sigprocmask (SIG_BLOCK, &sigs, NULL);
466 #endif
467
468#else
469 if (to)
470 event_loop (EVLOOP_NONBLOCK);
471 else
472 event_loop (EVLOOP_ONCE);
388# endif 473#endif
389 474
390# if IOM_TIME 475 #if IOM_TIME
391 { 476 {
392 // update time, try to compensate for gross non-monotonic time changes 477 // update time, try to compensate for gross non-monotonic time changes
393 tstamp diff = NOW; 478 tstamp diff = NOW;
394 set_now (); 479 set_now ();
395 diff = NOW - diff; 480 diff = NOW - diff;
396 481
397 if (diff < 0) 482 if (diff < 0)
398 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) 483 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
399 if (*i) 484 if (*i)
400 (*i)->at += diff; 485 (*i)->at += diff;
401 } 486 }
402# endif 487 #endif
403 488
489#ifndef IOM_LIBEVENT
404 if (fds > 0) 490 if (fds > 0)
405 { 491 {
406# if IOM_SIG 492 #if IOM_SIG
407 if (FD_ISSET (sigpipe[0], &rfd)) 493 if (FD_ISSET (sigpipe[0], &rfd))
408 {
409 char ch;
410
411 while (read (sigpipe[0], &ch, 1) > 0)
412 ;
413
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]);
423
424 sv.pending = false;
425 }
426 }
427# endif
428
429# if IOM_IO
430 for (int i = iow.size (); i--; )
431 if (!iow[i])
432 iow.erase_unordered (i);
433 else
434 { 494 {
435 io_watcher &w = *iow[i]; 495 char ch;
436 short revents = w.events;
437 496
438 if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ; 497 while (read (sigpipe[0], &ch, 1) > 0)
439 if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE; 498 ;
440 499
441 if (revents) 500 for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); )
442 w.call (w, revents); 501 if (*svp && (*svp)->pending)
502 {
503 sig_vec &sv = **svp;
504 for (int i = sv.size (); i--; )
505 if (!sv[i])
506 sv.erase_unordered (i);
507 else
508 sv[i]->call (*sv[i]);
509
510 sv.pending = false;
511 }
443 } 512 }
444#endif 513 #endif
514
515 #if IOM_IO
516 for (int i = iow.size (); i--; )
517 if (!iow[i])
518 iow.erase_unordered (i);
519 else
520 {
521 io_watcher &w = *iow[i];
522 short revents = w.events;
523
524 if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ;
525 if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE;
526
527 if (revents)
528 w.call (w, revents);
529 }
530 #endif
445 } 531 }
446 else if (fds < 0 && errno != EINTR) 532 else if (fds < 0 && errno != EINTR)
447 { 533 {
448 perror ("io_manager: fatal error while waiting for I/O or time event, aborting."); 534 perror ("io_manager: fatal error while waiting for I/O or time event, aborting.");
449 abort (); 535 abort ();
450 } 536 }
537#else
538 if (0)
539 ;
540#endif
451#if IOM_IDLE 541#if IOM_IDLE
452 else 542 else
453 for (int i = iw.size (); i--; ) 543 for (int i = iw.size (); i--; )
454 if (!iw[i]) 544 if (!iw[i])
455 iw.erase_unordered (i); 545 iw.erase_unordered (i);
459 549
460#elif IOM_TIME 550#elif IOM_TIME
461 if (!to) 551 if (!to)
462 break; 552 break;
463 553
464 select (0, 0, 0, 0, &to); 554 select (0, 0, 0, 0, to);
465 set_now (); 555 set_now ();
466#else
467 break; 556 break;
468#endif 557#endif
469 } 558 }
470} 559}
471 560

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines