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