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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines