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.8 by pcg, Fri Jan 16 22:11:09 2004 UTC vs.
Revision 1.17 by root, Tue Aug 10 20:39:19 2004 UTC

1/* 1/*
2 iom.C -- generic I/O multiplexor 2 iom.C -- generic I/O multiplexor
3 Copyright (C) 2003 Marc Lehmann <pcg@goof.com> 3 Copyright (C) 2003, 2004 Marc Lehmann <pcg@goof.com>
4 4
5 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 6 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 7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 8 (at your option) any later version.
15 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software 16 along with this program; if not, write to the Free Software
17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/ 18*/
19 19
20#include "../config.h"
21
22#include <cstdio> 20#include <cstdio>
23#include <cstdlib> 21#include <cstdlib>
24#include <cerrno> 22#include <cerrno>
25 23
24#include <sys/time.h>
25
26#include <assert.h>
27
28#if 1 // older unices need these includes for select (2)
29# include <unistd.h>
30# include <sys/types.h>
31#endif
32
33// if the BSDs would at least be marginally POSIX-compatible.. *sigh*
34// until that happens, sys/select.h must come last
26#include <sys/select.h> 35#include <sys/select.h>
36
37// for IOM_SIG
27#include <sys/time.h> 38#include <signal.h>
28 39
29#include "iom.h" 40#include "iom.h"
30 41
31// TSTAMP_MAX must still fit into a positive struct timeval 42// TSTAMP_MAX must still fit into a positive struct timeval
32#define TSTAMP_MAX (double)(1UL<<31) 43#define TSTAMP_MAX (double)(1UL<<31)
44
45// this is a dummy time watcher to ensure that the first
46// time watcher is _always_ valid, this gets rid of a lot
47// of null-pointer-checks
48// (must come _before_ iom is being defined)
49static struct tw0 : time_watcher
50 {
51 void cb (time_watcher &w)
52 {
53 // should never get called
54 // reached end-of-time, or tstamp has a bogus definition,
55 // or compiler initialisation order broken, or something else :)
56 abort ();
57 }
58
59 tw0 ()
60 : time_watcher (this, &tw0::cb)
61 { }
62 } tw0;
33 63
34tstamp NOW; 64tstamp NOW;
35static bool iom_valid; 65static bool iom_valid;
36io_manager iom; 66io_manager iom;
37 67
38template<class watcher> 68template<class watcher>
39void io_manager::reg (watcher *w, simplevec<watcher *> &queue) 69void io_manager::reg (watcher *w, io_manager_vec<watcher> &queue)
40{ 70{
41 if (!iom_valid) 71 if (!iom_valid)
42 abort (); 72 abort ();
43 73
44 if (!w->active) 74 if (!w->active)
45 { 75 {
76#if IOM_CHECK
77 queue.activity = true;
78#endif
46 queue.push_back (w); 79 queue.push_back (w);
47 w->active = queue.size (); 80 w->active = queue.size ();
48 } 81 }
49} 82}
50 83
51template<class watcher> 84template<class watcher>
52void io_manager::unreg (watcher *w, simplevec<watcher *> &queue) 85void io_manager::unreg (watcher *w, io_manager_vec<watcher> &queue)
53{ 86{
54 if (!iom_valid) 87 if (!iom_valid)
55 return; 88 return;
56 89
57 if (w->active) 90 if (w->active)
72void io_manager::reg (time_watcher *w) { reg (w, tw); } 105void io_manager::reg (time_watcher *w) { reg (w, tw); }
73void io_manager::unreg (time_watcher *w) { unreg (w, tw); } 106void io_manager::unreg (time_watcher *w) { unreg (w, tw); }
74#endif 107#endif
75 108
76#if IOM_IO 109#if IOM_IO
77void io_manager::reg (io_watcher *w) { reg (w, iow); } 110void io_manager::reg (io_watcher *w) { reg (w, iow); }
78void io_manager::unreg (io_watcher *w) { unreg (w, iow); } 111void io_manager::unreg (io_watcher *w) { unreg (w, iow); }
79#endif 112#endif
80 113
81#if IOM_CHECK 114#if IOM_CHECK
82void io_manager::reg (check_watcher *w) { reg (w, cw); } 115void io_manager::reg (check_watcher *w) { reg (w, cw); }
94 struct timeval tv; 127 struct timeval tv;
95 128
96 gettimeofday (&tv, 0); 129 gettimeofday (&tv, 0);
97 130
98 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; 131 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000;
99#endif
100} 132}
133#endif
101 134
135#if IOM_SIG
136// race conditions galore
137
138void io_manager::sighandler (int signum)
139{
140 assert (0 < signum && signum <= iom.sw.size ());
141
142 sig_vec &sv = *iom.sw [signum - 1];
143
144 for (int i = sv.size (); i--; )
145 if (!sv[i])
146 sv.erase_unordered (i);
147 else
148 sv[i]->call (*sv[i]);
149}
150
151void io_manager::reg (sig_watcher *w)
152{
153 assert (0 < w->signum);
154
155 sw.reserve (w->signum);
156
157 sig_vec *&sv = sw [w->signum - 1];
158
159 if (!sv)
160 {
161 sv = new sig_vec;
162
163 struct sigaction sa;
164 sa.sa_handler = io_manager::sighandler;
165 sigfillset (&sa.sa_mask);
166 sa.sa_flags = 0;
167
168 if (sigaction (w->signum, &sa, 0))
169 {
170 perror ("Error while installing signal handler");
171 abort ();
172 }
173 }
174
175 reg (w, *sv);
176}
177
178void io_manager::unreg (sig_watcher *w)
179{
180 assert (0 < w->signum && w->signum <= sw.size ());
181
182 unreg (w, *sw [w->signum - 1]);
183}
184
185void sig_watcher::start (int signum)
186{
187 stop ();
188 this->signum = signum;
189 iom.reg (this);
190}
191#endif
192
102void io_manager::loop () 193void io_manager::loop ()
103{ 194{
104#if IOM_TIME 195#if IOM_TIME
105 set_now (); 196 set_now ();
106#endif 197#endif
117 tval.tv_usec = 0; 208 tval.tv_usec = 0;
118 to = &tval; 209 to = &tval;
119 } 210 }
120 else 211 else
121#endif 212#endif
213
122 { 214 {
123#if IOM_TIME 215#if IOM_TIME
124 time_watcher *next; 216 time_watcher *next;
125 217
126 for (;;) 218 for (;;)
137 { 229 {
138 if (next != tw[0]) 230 if (next != tw[0])
139 { 231 {
140 double diff = next->at - NOW; 232 double diff = next->at - NOW;
141 tval.tv_sec = (int)diff; 233 tval.tv_sec = (int)diff;
142 tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); 234 tval.tv_usec = (int) ((diff - tval.tv_sec) * 1000000);
143 to = &tval; 235 to = &tval;
144 } 236 }
145 break; 237 break;
146 } 238 }
147 else if (next->at >= 0) 239 else
148 { 240 {
149 unreg (next); 241 unreg (next);
150 next->call (*next); 242 next->call (*next);
151 } 243 }
152 } 244 }
153#endif 245#endif
246
154 } 247 }
155 248
156#if IOM_CHECK 249#if IOM_CHECK
250 tw.activity = false;
251
157 for (int i = cw.size (); i--; ) 252 for (int i = cw.size (); i--; )
158 if (!cw[i]) 253 if (!cw[i])
159 cw.erase_unordered (i); 254 cw.erase_unordered (i);
160 else 255 else
161 cw[i]->call (*cw[i]); 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 }
162#endif 264#endif
163 265
164#if IOM_IO 266#if IOM_IO
165 fd_set rfd, wfd, efd; 267 fd_set rfd, wfd;
166 268
167 FD_ZERO (&rfd); 269 FD_ZERO (&rfd);
168 FD_ZERO (&wfd); 270 FD_ZERO (&wfd);
169 271
170 int fds = 0; 272 int fds = 0;
171 273
172 for (io_watcher **i = iow.end (); i-- > iow.begin (); ) 274 for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); )
173 if (*i) 275 if (*i)
174 { 276 {
175 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); 277 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd);
176 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); 278 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd);
177 279
179 } 281 }
180 282
181 if (!to && !fds) //TODO: also check idle_watchers and check_watchers 283 if (!to && !fds) //TODO: also check idle_watchers and check_watchers
182 break; // no events 284 break; // no events
183 285
184 fds = select (fds, &rfd, &wfd, &efd, to); 286 fds = select (fds, &rfd, &wfd, NULL, to);
185# if IOM_TIME 287# if IOM_TIME
186 set_now (); 288 set_now ();
187# endif 289# endif
188 290
189 if (fds > 0) 291 if (fds > 0)
221 select (0, 0, 0, 0, &to); 323 select (0, 0, 0, 0, &to);
222 set_now (); 324 set_now ();
223#else 325#else
224 break; 326 break;
225#endif 327#endif
226 }
227}
228 328
229// this is a dummy time watcher to ensure that the first
230// time watcher is _always_ valid, this gets rid of a lot
231// of null-pointer-checks
232static struct tw0 : time_watcher {
233 void cb (time_watcher &w)
234 {
235 // should never get called
236 // reached end-of-time, or tstamp has a bogus definition :)
237 abort ();
238 }
239
240 tw0()
241 : time_watcher (this, &tw0::cb)
242 { } 329 }
243} tw0; 330}
244 331
245io_manager::io_manager () 332io_manager::io_manager ()
246{ 333{
247 iom_valid = true; 334 iom_valid = true;
248 335

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines