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