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.37 by root, Mon Feb 12 17:34:58 2007 UTC vs.
Revision 1.39 by root, Thu Oct 25 12:42:00 2007 UTC

49 49
50#define TIMEVAL timeval 50#define TIMEVAL timeval
51#define TV_FRAC tv_usec 51#define TV_FRAC tv_usec
52#define TV_MULT 1000000L 52#define TV_MULT 1000000L
53 53
54#if IOM_IO
55static io_manager_vec<io_watcher> iow;
56#endif
57#if IOM_CHECK 54#if IOM_CHECK
58static io_manager_vec<check_watcher> cw; 55static io_manager_vec<check_watcher> cw;
59#endif 56#endif
60#if IOM_TIME
61static io_manager_vec<time_watcher> tw;
62#endif
63#if IOM_IDLE 57#if IOM_IDLE
64static io_manager_vec<idle_watcher> iw; 58static io_manager_vec<idle_watcher> iw;
65#endif 59#endif
60
66#if IOM_SIG 61#if IOM_SIG
67static int sigpipe[2]; // signal signalling pipe 62static int sigpipe[2]; // signal signalling pipe
68static sigset_t sigs; 63static sigset_t sigs;
69struct sig_vec : io_manager_vec<sig_watcher> { 64struct sig_vec : io_manager_vec<sig_watcher> {
70 int pending; 65 int pending;
72 : pending (false) 67 : pending (false)
73 { } 68 { }
74}; 69};
75static vector<sig_vec *> sw; 70static vector<sig_vec *> sw;
76#endif 71#endif
72
77#if IOM_CHILD 73#if IOM_CHILD
78static io_manager_vec<child_watcher> pw; 74static io_manager_vec<child_watcher> pw;
79#endif 75#endif
80 76
77#ifdef IOM_LIBEVENT
78static bool need_set_now; // need to set_now in callback
79#else
80 #if IOM_IO
81 static io_manager_vec<io_watcher> iow;
82 #endif
83 #if IOM_TIME
84 static io_manager_vec<time_watcher> tw;
85 #endif
86#endif
87
88#if IOM_TIME
89tstamp io_manager::now ()
90{
91 struct timeval tv;
92
93 gettimeofday (&tv, 0);
94 return (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.;
95}
96
97void io_manager::set_now ()
98{
99 NOW = now ();
100 #ifdef IOM_LIBEVENT
101 need_set_now = false;
102 #endif
103}
104#endif
105
106#ifndef IOM_LIBEVENT
81// this is a dummy time watcher to ensure that the first 107// this is a dummy time watcher to ensure that the first
82// time watcher is _always_ valid, this gets rid of a lot 108// time watcher is _always_ valid, this gets rid of a lot
83// of null-pointer-checks 109// of null-pointer-checks
84// (must come _before_ iom is being defined) 110// (must come _before_ iom is being defined)
85static struct tw0 : time_watcher 111static struct tw0 : time_watcher
94 120
95 tw0 () 121 tw0 ()
96 : time_watcher (this, &tw0::cb) 122 : time_watcher (this, &tw0::cb)
97 { } 123 { }
98} tw0; 124} tw0;
125#endif
99 126
100tstamp NOW; 127tstamp NOW;
101 128
102#if IOM_CHILD 129#if IOM_CHILD
103// sig_watcher for child signal(s) 130// sig_watcher for child signal(s)
128 : sig_watcher (this, &sw0::cb) 155 : sig_watcher (this, &sw0::cb)
129 { } 156 { }
130} sw0; 157} sw0;
131#endif 158#endif
132 159
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; 160static bool iom_valid;
149 161
150// used for initialisation only 162// used for initialisation only
151static struct init { 163static struct init {
152 init () 164 init ()
153 { 165 {
154#ifdef IOM_PREINIT 166 #ifdef IOM_PREINIT
155 { IOM_PREINIT } 167 { IOM_PREINIT }
156#endif 168 #endif
169
170 #ifdef IOM_LIBEVENT
171 event_init ();
172 #endif
157 iom_valid = true; 173 iom_valid = true;
158 174
159#if IOM_SIG 175 #if IOM_SIG
160 sigemptyset (&sigs); 176 sigemptyset (&sigs);
161 177
162 if (pipe (sigpipe)) 178 if (pipe (sigpipe))
163 { 179 {
164 perror ("io_manager: unable to create signal pipe, aborting."); 180 perror ("io_manager: unable to create signal pipe, aborting.");
165 abort (); 181 abort ();
166 } 182 }
167 183
168 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC); 184 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); 185 fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC);
170#endif 186 #endif
171 187
172#if IOM_CHILD 188 #if IOM_CHILD
173 sw0.start (SIGCHLD); 189 sw0.start (SIGCHLD);
174#endif 190 #endif
175 191
176#if IOM_TIME 192 #if IOM_TIME
177 io_manager::set_now (); 193 io_manager::set_now ();
178 194
195 #ifndef IOM_LIBEVENT
179 tw0.start (TSTAMP_MAX); 196 tw0.start (TSTAMP_MAX);
197 #endif
180#endif 198 #endif
181 199
182#ifdef IOM_POSTINIT 200 #ifdef IOM_POSTINIT
183 { IOM_POSTINIT } 201 { IOM_POSTINIT }
184#endif 202 #endif
185 } 203 }
186 204
187 ~init () 205 ~init ()
188 { 206 {
189 iom_valid = false; 207 iom_valid = false;
227 w.active = 0; 245 w.active = 0;
228 } 246 }
229} 247}
230 248
231#if IOM_TIME 249#if IOM_TIME
250 #ifdef IOM_LIBEVENT
251 void iom_time_c_callback (int fd, short events, void *data)
252 {
253 if (need_set_now) io_manager::set_now ();
254 time_watcher *w = static_cast<time_watcher *>(data);
255 w->call (*w);
256 }
257
232void time_watcher::trigger () 258 void time_watcher::start ()
233{ 259 {
234 call (*this); 260 stop ();
235 io_manager::reg (*this); 261 evtimer_set (&ev, iom_time_c_callback, (void *)this);
236} 262 struct timeval tv;
237 263 tv.tv_sec = (long)at;
264 tv.tv_usec = (long)((at - (tstamp)tv.tv_sec) * 1000000.);
265 evtimer_add (&ev, &tv);
266 active = 1;
267 }
268 #else
238void io_manager::reg (time_watcher &w) { io_manager::reg (w, tw); } 269 void io_manager::reg (time_watcher &w) { io_manager::reg (w, tw); }
239void io_manager::unreg (time_watcher &w) { io_manager::unreg (w, tw); } 270 void io_manager::unreg (time_watcher &w) { io_manager::unreg (w, tw); }
271 #endif
272
273 void time_watcher::trigger ()
274 {
275 call (*this);
276 start ();
277 }
240#endif 278#endif
241 279
242#if IOM_IO 280#if IOM_IO
281 #ifdef IOM_LIBEVENT
282 void iom_io_c_callback (int fd, short events, void *data)
283 {
284 if (need_set_now) io_manager::set_now ();
285 io_watcher *w = static_cast<io_watcher *>(data);
286 w->call (*w, events);
287 }
288
289 void io_watcher::set (int fd_, short events_)
290 {
291 if (active) event_del (&ev);
292 fd = fd_;
293 events = events_;
294 event_set (&ev, fd_, events_ | EV_PERSIST, iom_io_c_callback, (void *)this);
295 if (active) event_add (&ev, 0);
296 }
297 #else
243void io_manager::reg (io_watcher &w) { io_manager::reg (w, iow); } 298 void io_manager::reg (io_watcher &w) { io_manager::reg (w, iow); }
244void io_manager::unreg (io_watcher &w) { io_manager::unreg (w, iow); } 299 void io_manager::unreg (io_watcher &w) { io_manager::unreg (w, iow); }
300 #endif
245#endif 301#endif
246 302
247#if IOM_CHECK 303#if IOM_CHECK
248void io_manager::reg (check_watcher &w) { io_manager::reg (w, cw); } 304void io_manager::reg (check_watcher &w) { io_manager::reg (w, cw); }
249void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); } 305void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); }
327 383
328void io_manager::loop () 384void io_manager::loop ()
329{ 385{
330 init::required (); 386 init::required ();
331 387
332#if IOM_TIME 388 #if IOM_TIME
333 set_now (); 389 set_now ();
334#endif 390 #endif
335 391
336 for (;;) 392 for (;;)
337 { 393 {
338 394 #ifndef IOM_LIBEVENT
339#if IOM_TIME 395 #if IOM_TIME
340 // call pending time watchers 396 // call pending time watchers
341 { 397 {
342 bool activity; 398 bool activity;
343 399
344 do 400 do
345 { 401 {
346 activity = false; 402 activity = false;
347 403
348 for (int i = tw.size (); i--; ) 404 for (int i = tw.size (); i--; )
349 if (!tw[i]) 405 if (!tw[i])
350 tw.erase_unordered (i); 406 tw.erase_unordered (i);
351 else if (tw[i]->at <= NOW) 407 else if (tw[i]->at <= NOW)
352 { 408 {
353 time_watcher &w = *tw[i]; 409 time_watcher &w = *tw[i];
410
411 unreg (w);
412 w.call (w);
413
414 activity = true;
354 415 }
355 unreg (w);
356 w.call (w);
357
358 activity = true;
359 }
360 } 416 }
361 while (activity); 417 while (activity);
362 } 418 }
363#endif 419 #endif
420 #endif
364 421
365#if IOM_CHECK 422 #if IOM_CHECK
366 // call all check watchers 423 // call all check watchers
367 for (int i = cw.size (); i--; ) 424 for (int i = cw.size (); i--; )
368 if (!cw[i]) 425 if (!cw[i])
369 cw.erase_unordered (i); 426 cw.erase_unordered (i);
370 else 427 else
371 cw[i]->call (*cw[i]); 428 cw[i]->call (*cw[i]);
372#endif 429 #endif
373 430
374 struct TIMEVAL *to = 0; 431 struct TIMEVAL *to = 0;
375 struct TIMEVAL tval; 432 struct TIMEVAL tval;
376 433
377#if IOM_IDLE 434#if IOM_IDLE
382 to = &tval; 439 to = &tval;
383 } 440 }
384 else 441 else
385#endif 442#endif
386 { 443 {
387#if IOM_TIME 444 #ifndef IOM_LIBEVENT
445 #if IOM_TIME
388 // find earliest active watcher 446 // find earliest active watcher
389 time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times 447 time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times
390 448
391 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) 449 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
392 if (*i && (*i)->at < next->at) 450 if (*i && (*i)->at < next->at)
393 next = *i; 451 next = *i;
394 452
395 if (next->at > NOW && next != tw[0]) 453 if (next->at > NOW && next != tw[0])
396 { 454 {
397 double diff = next->at - NOW; 455 double diff = next->at - NOW;
398 tval.tv_sec = (int)diff; 456 tval.tv_sec = (int)diff;
399 tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT); 457 tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT);
400 to = &tval; 458 to = &tval;
401 } 459 }
460 #endif
461 #endif
402 } 462 }
403#endif
404 463
464 #ifndef IOM_LIBEVENT
405#if IOM_IO || IOM_SIG 465 #if IOM_IO || IOM_SIG
406 fd_set rfd, wfd; 466 fd_set rfd, wfd;
407 467
408 FD_ZERO (&rfd); 468 FD_ZERO (&rfd);
409 FD_ZERO (&wfd); 469 FD_ZERO (&wfd);
410 470
411 int fds = 0; 471 int fds = 0;
412 472
413# if IOM_IO 473 #if IOM_IO
414 for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); ) 474 for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); )
415 if (*i) 475 if (*i)
476 {
477 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd);
478 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd);
479
480 if ((*i)->fd >= fds) fds = (*i)->fd + 1;
481 }
482 #endif
483
484 if (!to && !fds) //TODO: also check idle_watchers and check_watchers?
485 break; // no events
486
487 #if IOM_SIG
488 FD_SET (sigpipe[0], &rfd);
489 if (sigpipe[0] >= fds) fds = sigpipe[0] + 1;
490 #endif
491
492 #if IOM_SIG
493 // there is no race, as we use a pipe for signals, so select
494 // will return if a signal is caught.
495 sigprocmask (SIG_UNBLOCK, &sigs, NULL);
496 #endif
497 fds = select (fds, &rfd, &wfd, NULL, to);
498 #if IOM_SIG
499 sigprocmask (SIG_BLOCK, &sigs, NULL);
500 #endif
501 #elif IOM_TIME
502 if (!to)
503 break;
504
505 select (0, 0, 0, 0, to);
506 #endif
507
508 #if IOM_TIME
416 { 509 {
417 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); 510 // update time, try to compensate for gross non-monotonic time changes
418 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); 511 tstamp diff = NOW;
512 set_now ();
513 diff = NOW - diff;
419 514
420 if ((*i)->fd >= fds) fds = (*i)->fd + 1; 515 if (diff < 0)
516 for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
517 if (*i)
518 (*i)->at += diff;
421 } 519 }
422# endif 520 #endif
423 521
424 if (!to && !fds) //TODO: also check idle_watchers and check_watchers?
425 break; // no events
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
437 fds = select (fds, &rfd, &wfd, NULL, to);
438# if IOM_SIG
439 sigprocmask (SIG_BLOCK, &sigs, NULL);
440# endif
441
442# if IOM_TIME
443 {
444 // update time, try to compensate for gross non-monotonic time changes
445 tstamp diff = 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 }
454# endif
455
456 if (fds > 0) 522 if (fds > 0)
457 { 523 {
458# if IOM_SIG 524 #if IOM_SIG
459 if (FD_ISSET (sigpipe[0], &rfd)) 525 if (FD_ISSET (sigpipe[0], &rfd))
460 { 526 {
461 char ch; 527 char ch;
462 528
463 while (read (sigpipe[0], &ch, 1) > 0) 529 while (read (sigpipe[0], &ch, 1) > 0)
464 ; 530 ;
465 531
466 for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); ) 532 for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); )
467 if (*svp && (*svp)->pending) 533 if (*svp && (*svp)->pending)
534 {
535 sig_vec &sv = **svp;
536 for (int i = sv.size (); i--; )
537 if (!sv[i])
538 sv.erase_unordered (i);
539 else
540 sv[i]->call (*sv[i]);
541
542 sv.pending = false;
543 }
544 }
545 #endif
546
547 #if IOM_IO
548 for (int i = iow.size (); i--; )
549 if (!iow[i])
550 iow.erase_unordered (i);
551 else
468 { 552 {
469 sig_vec &sv = **svp; 553 io_watcher &w = *iow[i];
470 for (int i = sv.size (); i--; ) 554 short revents = w.events;
555
556 if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ;
557 if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE;
558
471 if (!sv[i]) 559 if (revents)
472 sv.erase_unordered (i); 560 w.call (w, revents);
473 else
474 sv[i]->call (*sv[i]);
475
476 sv.pending = false;
477 } 561 }
562 #endif
478 } 563 }
479# endif 564 else if (fds < 0 && errno != EINTR)
480 565 {
566 perror ("io_manager: fatal error while waiting for I/O or time event, aborting.");
567 abort ();
568 }
481# if IOM_IO 569#if IOM_IDLE
570 else
482 for (int i = iow.size (); i--; ) 571 for (int i = iw.size (); i--; )
483 if (!iow[i]) 572 if (!iw[i])
484 iow.erase_unordered (i); 573 iw.erase_unordered (i);
485 else 574 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 }
498 else if (fds < 0 && errno != EINTR)
499 {
500 perror ("io_manager: fatal error while waiting for I/O or time event, aborting.");
501 abort ();
502 }
503#if IOM_IDLE
504 else
505 for (int i = iw.size (); i--; )
506 if (!iw[i])
507 iw.erase_unordered (i);
508 else
509 iw[i]->call (*iw[i]); 575 iw[i]->call (*iw[i]);
510#endif 576#endif
511 577
512#elif IOM_TIME 578 #else
579 need_set_now = true;
580
513 if (!to) 581 if (to)
514 break; 582 event_loop (EVLOOP_NONBLOCK);
583 else
584 event_loop (EVLOOP_ONCE);
515 585
516 select (0, 0, 0, 0, &to); 586 if (need_set_now) set_now ();
517 set_now (); 587 #endif
518#else 588 //TODO: IOM_IDLE
519 break;
520#endif
521 } 589 }
522} 590}
523 591

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines