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.7 by pcg, Fri Dec 19 06:17:03 2003 UTC vs.
Revision 1.16 by pcg, Fri Apr 2 14:30:06 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>
21#include <cstdlib>
22#include <cerrno>
23 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
24#include <sys/select.h> 35#include <sys/select.h>
36
37// for IOM_SIG
25#include <sys/time.h> 38#include <signal.h>
26 39
27#include "iom.h" 40#include "iom.h"
28 41
42// TSTAMP_MAX must still fit into a positive struct timeval
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 initilization order broken, or somethine else :)
56 abort ();
57 }
58
59 tw0 ()
60 : time_watcher (this, &tw0::cb)
61 { }
62 } tw0;
63
29tstamp NOW; 64tstamp NOW;
30bool iom_valid; 65static bool iom_valid;
31io_manager iom; 66io_manager iom;
32 67
33template<class watcher> 68template<class watcher>
34void io_manager::reg (watcher *w, simplevec<watcher *> &queue) 69void io_manager::reg (watcher *w, io_manager_vec<watcher> &queue)
35{ 70{
36 if (find (queue.begin (), queue.end (), w) == queue.end ()) 71 if (!iom_valid)
72 abort ();
73
74 if (!w->active)
75 {
76#if IOM_CHECK
77 queue.activity = true;
78#endif
37 queue.push_back (w); 79 queue.push_back (w);
80 w->active = queue.size ();
81 }
38} 82}
39 83
40template<class watcher> 84template<class watcher>
41void io_manager::unreg (watcher *w, simplevec<watcher *> &queue) 85void io_manager::unreg (watcher *w, io_manager_vec<watcher> &queue)
42{ 86{
43 queue.erase (find (queue.begin (), queue.end (), w)); 87 if (!iom_valid)
88 return;
89
90 if (w->active)
91 {
92 queue [w->active - 1] = 0;
93 w->active = 0;
94 }
44} 95}
96
97#if IOM_TIME
98void time_watcher::trigger ()
99{
100 call (*this);
101
102 iom.reg (this);
103}
104
105void io_manager::reg (time_watcher *w) { reg (w, tw); }
106void io_manager::unreg (time_watcher *w) { unreg (w, tw); }
107#endif
45 108
46#if IOM_IO 109#if IOM_IO
47io_watcher::~io_watcher () 110void io_manager::reg (io_watcher *w) { reg (w, iow); }
48{ 111void io_manager::unreg (io_watcher *w) { unreg (w, iow); }
49 if (iom_valid) 112#endif
50 iom.unreg (this);
51}
52 113
114#if IOM_CHECK
115void io_manager::reg (check_watcher *w) { reg (w, cw); }
116void io_manager::unreg (check_watcher *w) { unreg (w, cw); }
117#endif
118
119#if IOM_IDLE
120void io_manager::reg (idle_watcher *w) { reg (w, iw); }
121void io_manager::unreg (idle_watcher *w) { 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
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
53void io_manager::reg (io_watcher *w) 151void io_manager::reg (sig_watcher *w)
54{ 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
55 reg (w, iow); 175 reg (w, *sv);
56} 176}
57 177
58void io_manager::unreg (io_watcher *w) 178void io_manager::unreg (sig_watcher *w)
59{ 179{
60 unreg (w, iow); 180 assert (0 < w->signum && w->signum <= sw.size ());
181
182 unreg (w, *sw [w->signum - 1]);
61} 183}
62 184
63#endif 185void sig_watcher::start (int signum)
64
65#if IOM_TIME
66void time_watcher::trigger ()
67{ 186{
68 call (*this); 187 stop ();
69 188 this->signum = signum;
70 iom.reg (this); 189 iom.reg (this);
71} 190}
72
73time_watcher::~time_watcher ()
74{
75 if (iom_valid)
76 iom.unreg (this);
77
78 at = TSTAMP_CANCEL;
79}
80
81void io_manager::reg (time_watcher *w)
82{
83 reg (w, tw);
84}
85
86void io_manager::unreg (time_watcher *w)
87{
88 unreg (w, tw);
89}
90#endif 191#endif
91 192
92#if IOM_CHECK
93check_watcher::~check_watcher ()
94{
95 if (iom_valid)
96 iom.unreg (this);
97}
98
99void io_manager::reg (check_watcher *w)
100{
101 reg (w, cw);
102}
103
104void io_manager::unreg (check_watcher *w)
105{
106 unreg (w, cw);
107}
108#endif
109
110#if IOM_IDLE
111idle_watcher::~idle_watcher ()
112{
113 if (iom_valid)
114 iom.unreg (this);
115}
116
117void io_manager::reg (idle_watcher *w)
118{
119 reg (w, iw);
120}
121
122void io_manager::unreg (idle_watcher *w)
123{
124 unreg (w, iw);
125}
126#endif
127
128#if IOM_TIME
129inline void set_now (void)
130{
131 struct timeval tv;
132
133 gettimeofday (&tv, 0);
134
135 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000;
136#endif
137}
138
139void io_manager::loop () 193void io_manager::loop ()
140{ 194{
141#if IOM_TIME 195#if IOM_TIME
142 set_now (); 196 set_now ();
143#endif 197#endif
154 tval.tv_usec = 0; 208 tval.tv_usec = 0;
155 to = &tval; 209 to = &tval;
156 } 210 }
157 else 211 else
158#endif 212#endif
213
159 { 214 {
160#if IOM_TIME 215#if IOM_TIME
161 time_watcher *w; 216 time_watcher *next;
162 217
163 for (;tw.size ();) 218 for (;;)
164 { 219 {
220 next = tw[0]; // the first time-watcher must exist at ALL times
221
222 for (int i = tw.size (); i--; )
223 if (!tw[i])
224 tw.erase_unordered (i);
225 else if (tw[i]->at < next->at)
165 w = tw[0]; 226 next = tw[i];
166 227
167 for (time_watcher **i = tw.begin (); i < tw.end (); ++i)
168 if ((*i)->at < w->at)
169 w = *i;
170
171 if (w->at > NOW) 228 if (next->at > NOW)
172 { 229 {
230 if (next != tw[0])
231 {
173 double diff = w->at - NOW; 232 double diff = next->at - NOW;
174 tval.tv_sec = (int)diff; 233 tval.tv_sec = (int)diff;
175 tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); 234 tval.tv_usec = (int) ((diff - tval.tv_sec) * 1000000);
176 to = &tval; 235 to = &tval;
236 }
177 break; 237 break;
178 } 238 }
179 else if (w->at >= 0)
180 w->call (*w);
181 else 239 else
240 {
182 unreg (w); 241 unreg (next);
242 next->call (*next);
243 }
183 } 244 }
184#endif 245#endif
246
185 } 247 }
186 248
187#if IOM_CHECK 249#if IOM_CHECK
250 tw.activity = false;
251
188 for (int i = 0; i < cw.size (); ++i) 252 for (int i = cw.size (); i--; )
253 if (!cw[i])
254 cw.erase_unordered (i);
255 else
189 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 }
190#endif 264#endif
191 265
192#if IOM_IO 266#if IOM_IO
193 fd_set rfd, wfd; 267 fd_set rfd, wfd;
194 268
195 FD_ZERO (&rfd); 269 FD_ZERO (&rfd);
196 FD_ZERO (&wfd); 270 FD_ZERO (&wfd);
197 271
198 int fds = 0; 272 int fds = 0;
199 273
200 for (io_watcher **w = iow.begin (); w < iow.end (); ++w) 274 for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); )
275 if (*i)
201 { 276 {
202 if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd); 277 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd);
203 if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); 278 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd);
204 279
205 if ((*w)->fd >= fds) fds = (*w)->fd + 1; 280 if ((*i)->fd >= fds) fds = (*i)->fd + 1;
206 } 281 }
207 282
208 if (!to && !fds) 283 if (!to && !fds) //TODO: also check idle_watchers and check_watchers
209 break; // no events 284 break; // no events
210 285
211 fds = select (fds, &rfd, &wfd, 0, to); 286 fds = select (fds, &rfd, &wfd, NULL, to);
212# if IOM_TIME 287# if IOM_TIME
213 set_now (); 288 set_now ();
214# endif 289# endif
215 290
216 if (fds > 0) 291 if (fds > 0)
217 for (int i = 0; i < iow.size (); ++i) 292 for (int i = iow.size (); i--; )
293 if (!iow[i])
294 iow.erase_unordered (i);
295 else
218 { 296 {
219 io_watcher *w = iow[i];
220
221 short revents = w->events; 297 short revents = iow[i]->events;
222 298
223 if (!FD_ISSET (w->fd, &rfd)) revents &= ~EVENT_READ; 299 if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ;
224 if (!FD_ISSET (w->fd, &wfd)) revents &= ~EVENT_WRITE; 300 if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE;
225 301
226 if (revents) 302 if (revents)
227 w->call (*w, revents); 303 iow[i]->call (*iow[i], revents);
228 } 304 }
305 else if (fds < 0 && errno != EINTR)
306 {
307 perror ("Error while waiting for I/O or time event");
308 abort ();
309 }
229#if IOM_IDLE 310#if IOM_IDLE
230 else if (iw.size ()) 311 else
231 for (int i = 0; i < iw.size (); ++i) 312 for (int i = iw.size (); i--; )
313 if (!iw[i])
314 iw.erase_unordered (i);
315 else
232 iw[i]->call (*iw[i]); 316 iw[i]->call (*iw[i]);
233#endif 317#endif
234 318
235#elif IOM_TIME 319#elif IOM_TIME
236 if (!to) 320 if (!to)
237 break; 321 break;
239 select (0, 0, 0, 0, &to); 323 select (0, 0, 0, 0, &to);
240 set_now (); 324 set_now ();
241#else 325#else
242 break; 326 break;
243#endif 327#endif
328
244 } 329 }
245} 330}
246 331
247io_manager::io_manager () 332io_manager::io_manager ()
248{ 333{
334 iom_valid = true;
335
249#if IOM_TIME 336#if IOM_TIME
250 set_now (); 337 set_now ();
251#endif
252 338
253 iom_valid = true; 339 tw0.start (TSTAMP_MAX);
340#endif
254} 341}
255 342
256io_manager::~io_manager () 343io_manager::~io_manager ()
257{ 344{
258 iom_valid = false; 345 iom_valid = false;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines