ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/iom.C
Revision: 1.36
Committed: Thu Dec 14 03:18:34 2006 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.35: +0 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*
2 root 1.23 iom.C -- generic I/O multiplexer
3 root 1.34 Copyright (C) 2003-2006 Marc Lehmann <gvpe@schmorp.de>
4 pcg 1.1
5 root 1.30 This file is part of GVPE.
6    
7     GVPE is free software; you can redistribute it and/or modify
8 pcg 1.1 it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11    
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16    
17     You should have received a copy of the GNU General Public License
18 root 1.30 along with gvpe; if not, write to the Free Software
19 root 1.29 Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 pcg 1.1 */
21    
22 root 1.19 #include "iom.h"
23    
24 pcg 1.1 #include <cstdio>
25 pcg 1.8 #include <cstdlib>
26     #include <cerrno>
27 root 1.33 #include <cassert>
28 pcg 1.1
29 root 1.33 #include <sys/types.h>
30 pcg 1.11 #include <sys/time.h>
31    
32 pcg 1.14 #if 1 // older unices need these includes for select (2)
33 pcg 1.11 # include <unistd.h>
34 root 1.19 # include <time.h>
35     #endif
36    
37 root 1.33 #if IOM_CHILD
38     # include <sys/wait.h>
39     #endif
40    
41 root 1.19 #if IOM_SIG
42 root 1.27 # include <csignal>
43 root 1.19 # include <fcntl.h>
44 pcg 1.11 #endif
45    
46     // if the BSDs would at least be marginally POSIX-compatible.. *sigh*
47     // until that happens, sys/select.h must come last
48 pcg 1.1 #include <sys/select.h>
49    
50 root 1.19 #define TIMEVAL timeval
51     #define TV_FRAC tv_usec
52     #define TV_MULT 1000000L
53 pcg 1.1
54 root 1.19 #if IOM_IO
55     static io_manager_vec<io_watcher> iow;
56     #endif
57     #if IOM_CHECK
58     static io_manager_vec<check_watcher> cw;
59     #endif
60     #if IOM_TIME
61     static io_manager_vec<time_watcher> tw;
62     #endif
63     #if IOM_IDLE
64     static io_manager_vec<idle_watcher> iw;
65     #endif
66     #if IOM_SIG
67     static int sigpipe[2]; // signal signalling pipe
68     static sigset_t sigs;
69     struct sig_vec : io_manager_vec<sig_watcher> {
70     int pending;
71     sig_vec ()
72     : pending (false)
73     { }
74     };
75     static vector<sig_vec *> sw;
76     #endif
77 root 1.33 #if IOM_CHILD
78     static io_manager_vec<child_watcher> pw;
79     #endif
80 pcg 1.8
81 pcg 1.10 // this is a dummy time watcher to ensure that the first
82     // time watcher is _always_ valid, this gets rid of a lot
83     // of null-pointer-checks
84     // (must come _before_ iom is being defined)
85 pcg 1.13 static struct tw0 : time_watcher
86 root 1.33 {
87     void cb (time_watcher &w)
88 pcg 1.10 {
89 root 1.33 // 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 pcg 1.10
95 root 1.33 tw0 ()
96     : time_watcher (this, &tw0::cb)
97     { }
98     } tw0;
99 pcg 1.10
100 pcg 1.1 tstamp NOW;
101 root 1.19
102 root 1.33 #if IOM_CHILD
103     // sig_watcher for child signal(s)
104     static struct sw0 : sig_watcher
105     {
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 root 1.35 io_manager::unreg (*w);
122 root 1.33 w->call (*w, status);
123     }
124     }
125     }
126    
127     sw0 ()
128     : sig_watcher (this, &sw0::cb)
129     { }
130     } sw0;
131     #endif
132    
133 root 1.19 #if IOM_TIME
134 root 1.24 tstamp io_manager::now ()
135 root 1.19 {
136     struct timeval tv;
137    
138     gettimeofday (&tv, 0);
139 root 1.24 return (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.;
140     }
141    
142     void io_manager::set_now ()
143     {
144     NOW = now ();
145 root 1.19 }
146     #endif
147    
148 pcg 1.8 static bool iom_valid;
149 root 1.19
150     // used for initialisation only
151     static struct init {
152     init ()
153     {
154 root 1.33 iom_valid = true;
155    
156 root 1.19 #if IOM_SIG
157     sigemptyset (&sigs);
158    
159     if (pipe (sigpipe))
160     {
161     perror ("io_manager: unable to create signal pipe, aborting.");
162     abort ();
163     }
164    
165 root 1.25 fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC);
166     fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC);
167 root 1.19 #endif
168    
169 root 1.33 #if IOM_CHILD
170     sw0.start (SIGCHLD);
171     #endif
172 root 1.19
173     #if IOM_TIME
174 root 1.24 io_manager::set_now ();
175 root 1.19
176     tw0.start (TSTAMP_MAX);
177     #endif
178     }
179    
180 root 1.31 ~init ()
181     {
182     iom_valid = false;
183     }
184    
185 root 1.19 static void required ();
186     } init;
187    
188     void
189     init::required ()
190     {
191     if (!iom_valid)
192     {
193     write (2, "io_manager: early registration attempt, aborting.\n",
194     sizeof ("io_manager: early registration attempt, aborting.\n") - 1);
195     abort ();
196     }
197     }
198 pcg 1.1
199 pcg 1.4 template<class watcher>
200 root 1.19 void io_manager::reg (watcher &w, io_manager_vec<watcher> &queue)
201 pcg 1.4 {
202 root 1.19 init::required ();
203 pcg 1.8
204 root 1.19 if (!w.active)
205 pcg 1.8 {
206 root 1.19 queue.push_back (&w);
207     w.active = queue.size ();
208 pcg 1.8 }
209 pcg 1.4 }
210    
211     template<class watcher>
212 root 1.19 void io_manager::unreg (watcher &w, io_manager_vec<watcher> &queue)
213 pcg 1.4 {
214 pcg 1.8 if (!iom_valid)
215     return;
216 pcg 1.4
217 root 1.19 if (w.active)
218 pcg 1.8 {
219 root 1.19 queue [w.active - 1] = 0;
220     w.active = 0;
221 pcg 1.8 }
222 pcg 1.4 }
223    
224     #if IOM_TIME
225 pcg 1.1 void time_watcher::trigger ()
226     {
227     call (*this);
228 root 1.19 io_manager::reg (*this);
229 pcg 1.1 }
230    
231 root 1.19 void io_manager::reg (time_watcher &w) { io_manager::reg (w, tw); }
232     void io_manager::unreg (time_watcher &w) { io_manager::unreg (w, tw); }
233 pcg 1.8 #endif
234 pcg 1.3
235 pcg 1.8 #if IOM_IO
236 root 1.19 void io_manager::reg (io_watcher &w) { io_manager::reg (w, iow); }
237     void io_manager::unreg (io_watcher &w) { io_manager::unreg (w, iow); }
238 pcg 1.4 #endif
239 pcg 1.1
240 pcg 1.4 #if IOM_CHECK
241 root 1.19 void io_manager::reg (check_watcher &w) { io_manager::reg (w, cw); }
242     void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); }
243 pcg 1.4 #endif
244 pcg 1.1
245 pcg 1.7 #if IOM_IDLE
246 root 1.19 void io_manager::reg (idle_watcher &w) { io_manager::reg (w, iw); }
247     void io_manager::unreg (idle_watcher &w) { io_manager::unreg (w, iw); }
248 pcg 1.7 #endif
249    
250 root 1.19 #if IOM_SIG
251     static void
252     sighandler (int signum)
253 pcg 1.1 {
254 root 1.19 sw [signum - 1]->pending = true;
255 pcg 1.1
256 root 1.19 // we use a pipe for signal notifications, as most current
257     // OSes (Linux...) do not implement pselect correctly. ugh.
258     char ch = signum; // actual content not used
259     write (sigpipe[1], &ch, 1);
260 pcg 1.16 }
261    
262 root 1.19 void io_manager::reg (sig_watcher &w)
263 pcg 1.16 {
264 root 1.32 init::required ();
265    
266 root 1.19 assert (0 < w.signum);
267 pcg 1.16
268 root 1.19 sw.reserve (w.signum);
269 pcg 1.16
270 root 1.19 while (sw.size () < w.signum) // pathetic
271     sw.push_back (0);
272 pcg 1.16
273 root 1.19 sig_vec *&sv = sw[w.signum - 1];
274 pcg 1.16
275     if (!sv)
276     {
277     sv = new sig_vec;
278    
279 root 1.19 sigaddset (&sigs, w.signum);
280     sigprocmask (SIG_BLOCK, &sigs, NULL);
281    
282 pcg 1.16 struct sigaction sa;
283 root 1.19 sa.sa_handler = sighandler;
284 pcg 1.16 sigfillset (&sa.sa_mask);
285 root 1.19 sa.sa_flags = SA_RESTART;
286 pcg 1.16
287 root 1.19 if (sigaction (w.signum, &sa, 0))
288 pcg 1.16 {
289 root 1.19 perror ("io_manager: error while installing signal handler, ignoring.");
290 pcg 1.16 abort ();
291     }
292 root 1.19
293 pcg 1.16 }
294    
295 root 1.19 io_manager::reg (w, *sv);
296 pcg 1.16 }
297    
298 root 1.19 void io_manager::unreg (sig_watcher &w)
299 pcg 1.16 {
300 root 1.32 if (!w.active || !iom_valid)
301 root 1.19 return;
302    
303     assert (0 < w.signum && w.signum <= sw.size ());
304 pcg 1.16
305 root 1.19 io_manager::unreg (w, *sw[w.signum - 1]);
306 pcg 1.16 }
307    
308     void sig_watcher::start (int signum)
309     {
310     stop ();
311     this->signum = signum;
312 root 1.19 io_manager::reg (*this);
313 pcg 1.16 }
314     #endif
315 root 1.19
316 root 1.33 #if IOM_CHILD
317     void io_manager::reg (child_watcher &w) { io_manager::reg (w, pw); }
318     void io_manager::unreg (child_watcher &w) { io_manager::unreg (w, pw); }
319     #endif
320    
321 pcg 1.1 void io_manager::loop ()
322     {
323 root 1.19 init::required ();
324    
325 pcg 1.4 #if IOM_TIME
326 pcg 1.1 set_now ();
327 pcg 1.4 #endif
328 pcg 1.1
329     for (;;)
330     {
331    
332 pcg 1.7 #if IOM_TIME
333 root 1.23 // call pending time watchers
334     {
335     bool activity;
336 pcg 1.1
337 root 1.23 do
338     {
339     activity = false;
340 pcg 1.1
341 root 1.23 for (int i = tw.size (); i--; )
342     if (!tw[i])
343     tw.erase_unordered (i);
344     else if (tw[i]->at <= NOW)
345     {
346     time_watcher &w = *tw[i];
347    
348     unreg (w);
349     w.call (w);
350 pcg 1.7
351 root 1.23 activity = true;
352 pcg 1.7 }
353 root 1.23 }
354     while (activity);
355     }
356 pcg 1.7 #endif
357 pcg 1.6
358     #if IOM_CHECK
359 root 1.23 // call all check watchers
360 pcg 1.8 for (int i = cw.size (); i--; )
361     if (!cw[i])
362     cw.erase_unordered (i);
363     else
364     cw[i]->call (*cw[i]);
365 root 1.23 #endif
366 pcg 1.12
367 root 1.23 struct TIMEVAL *to = 0;
368     struct TIMEVAL tval;
369    
370     #if IOM_IDLE
371     if (iw.size ())
372 pcg 1.12 {
373     tval.tv_sec = 0;
374 root 1.19 tval.TV_FRAC = 0;
375 pcg 1.12 to = &tval;
376     }
377 root 1.23 else
378     #endif
379     {
380     #if IOM_TIME
381     // find earliest active watcher
382     time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times
383    
384     for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
385     if (*i && (*i)->at < next->at)
386     next = *i;
387    
388     if (next->at > NOW && next != tw[0])
389     {
390     double diff = next->at - NOW;
391     tval.tv_sec = (int)diff;
392     tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT);
393     to = &tval;
394     }
395     }
396 pcg 1.4 #endif
397 pcg 1.1
398 root 1.19 #if IOM_IO || IOM_SIG
399 pcg 1.15 fd_set rfd, wfd;
400 pcg 1.1
401     FD_ZERO (&rfd);
402     FD_ZERO (&wfd);
403    
404     int fds = 0;
405    
406 root 1.19 # if IOM_IO
407 root 1.23 for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); )
408 pcg 1.8 if (*i)
409     {
410     if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd);
411     if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd);
412 pcg 1.1
413 pcg 1.8 if ((*i)->fd >= fds) fds = (*i)->fd + 1;
414     }
415 root 1.19 # endif
416 pcg 1.1
417 root 1.23 if (!to && !fds) //TODO: also check idle_watchers and check_watchers?
418 pcg 1.5 break; // no events
419    
420 root 1.19 # if IOM_SIG
421     FD_SET (sigpipe[0], &rfd);
422     if (sigpipe[0] >= fds) fds = sigpipe[0] + 1;
423     # endif
424    
425     # if IOM_SIG
426     // there is no race, as we use a pipe for signals, so select
427     // will return if a signal is caught.
428     sigprocmask (SIG_UNBLOCK, &sigs, NULL);
429     # endif
430 pcg 1.15 fds = select (fds, &rfd, &wfd, NULL, to);
431 root 1.19 # if IOM_SIG
432     sigprocmask (SIG_BLOCK, &sigs, NULL);
433     # endif
434    
435 pcg 1.4 # if IOM_TIME
436 root 1.28 {
437     // update time, try to compensate for gross non-monotonic time changes
438     tstamp diff = NOW;
439     set_now ();
440     diff = NOW - diff;
441    
442     if (diff < 0)
443     for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); )
444     if (*i)
445     (*i)->at += diff;
446     }
447 pcg 1.4 # endif
448 pcg 1.1
449     if (fds > 0)
450 root 1.19 {
451     # if IOM_SIG
452     if (FD_ISSET (sigpipe[0], &rfd))
453 pcg 1.8 {
454 root 1.19 char ch;
455    
456     while (read (sigpipe[0], &ch, 1) > 0)
457     ;
458 pcg 1.1
459 root 1.23 for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); )
460 root 1.19 if (*svp && (*svp)->pending)
461     {
462     sig_vec &sv = **svp;
463     for (int i = sv.size (); i--; )
464     if (!sv[i])
465     sv.erase_unordered (i);
466     else
467     sv[i]->call (*sv[i]);
468 pcg 1.4
469 root 1.19 sv.pending = false;
470     }
471 pcg 1.8 }
472 root 1.19 # endif
473    
474     # if IOM_IO
475     for (int i = iow.size (); i--; )
476     if (!iow[i])
477     iow.erase_unordered (i);
478     else
479     {
480 root 1.27 io_watcher &w = *iow[i];
481     short revents = w.events;
482 root 1.19
483 root 1.27 if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ;
484     if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE;
485 root 1.19
486     if (revents)
487 root 1.27 w.call (w, revents);
488 root 1.19 }
489     #endif
490     }
491 pcg 1.8 else if (fds < 0 && errno != EINTR)
492     {
493 root 1.19 perror ("io_manager: fatal error while waiting for I/O or time event, aborting.");
494 pcg 1.8 abort ();
495     }
496 pcg 1.7 #if IOM_IDLE
497 pcg 1.8 else
498     for (int i = iw.size (); i--; )
499     if (!iw[i])
500     iw.erase_unordered (i);
501     else
502     iw[i]->call (*iw[i]);
503 pcg 1.7 #endif
504    
505 pcg 1.4 #elif IOM_TIME
506 pcg 1.5 if (!to)
507     break;
508    
509 pcg 1.4 select (0, 0, 0, 0, &to);
510     set_now ();
511 pcg 1.5 #else
512     break;
513 pcg 1.4 #endif
514 pcg 1.5 }
515 pcg 1.1 }
516