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.10 by pcg, Sat Jan 17 14:10:40 2004 UTC vs.
Revision 1.17 by root, Tue Aug 10 20:39:19 2004 UTC

19 19
20#include <cstdio> 20#include <cstdio>
21#include <cstdlib> 21#include <cstdlib>
22#include <cerrno> 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
29// TSTAMP_MAX must still fit into a positive struct timeval 42// TSTAMP_MAX must still fit into a positive struct timeval
30#define TSTAMP_MAX (double)(1UL<<31) 43#define TSTAMP_MAX (double)(1UL<<31)
31 44
32// this is a dummy time watcher to ensure that the first 45// this is a dummy time watcher to ensure that the first
33// time watcher is _always_ valid, this gets rid of a lot 46// time watcher is _always_ valid, this gets rid of a lot
34// of null-pointer-checks 47// of null-pointer-checks
35// (must come _before_ iom is being defined) 48// (must come _before_ iom is being defined)
36static struct tw0 : time_watcher { 49static struct tw0 : time_watcher
37 void cb (time_watcher &w)
38 { 50 {
51 void cb (time_watcher &w)
52 {
39 // should never get called 53 // should never get called
40 // reached end-of-time, or tstamp has a bogus definition, 54 // reached end-of-time, or tstamp has a bogus definition,
41 // or compiler initilization order broken, or somethine else :) 55 // or compiler initialisation order broken, or something else :)
42 abort (); 56 abort ();
43 } 57 }
44 58
45 tw0() 59 tw0 ()
46 : time_watcher (this, &tw0::cb) 60 : time_watcher (this, &tw0::cb)
47 { } 61 { }
48} tw0; 62 } tw0;
49 63
50tstamp NOW; 64tstamp NOW;
51static bool iom_valid; 65static bool iom_valid;
52io_manager iom; 66io_manager iom;
53 67
57 if (!iom_valid) 71 if (!iom_valid)
58 abort (); 72 abort ();
59 73
60 if (!w->active) 74 if (!w->active)
61 { 75 {
76#if IOM_CHECK
77 queue.activity = true;
78#endif
62 queue.push_back (w); 79 queue.push_back (w);
63 w->active = queue.size (); 80 w->active = queue.size ();
64 } 81 }
65} 82}
66 83
88void io_manager::reg (time_watcher *w) { reg (w, tw); } 105void io_manager::reg (time_watcher *w) { reg (w, tw); }
89void io_manager::unreg (time_watcher *w) { unreg (w, tw); } 106void io_manager::unreg (time_watcher *w) { unreg (w, tw); }
90#endif 107#endif
91 108
92#if IOM_IO 109#if IOM_IO
93void io_manager::reg (io_watcher *w) { reg (w, iow); } 110void io_manager::reg (io_watcher *w) { reg (w, iow); }
94void io_manager::unreg (io_watcher *w) { unreg (w, iow); } 111void io_manager::unreg (io_watcher *w) { unreg (w, iow); }
95#endif 112#endif
96 113
97#if IOM_CHECK 114#if IOM_CHECK
98void io_manager::reg (check_watcher *w) { reg (w, cw); } 115void io_manager::reg (check_watcher *w) { reg (w, cw); }
110 struct timeval tv; 127 struct timeval tv;
111 128
112 gettimeofday (&tv, 0); 129 gettimeofday (&tv, 0);
113 130
114 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; 131 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000;
115#endif
116} 132}
133#endif
117 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
118void io_manager::loop () 193void io_manager::loop ()
119{ 194{
120#if IOM_TIME 195#if IOM_TIME
121 set_now (); 196 set_now ();
122#endif 197#endif
133 tval.tv_usec = 0; 208 tval.tv_usec = 0;
134 to = &tval; 209 to = &tval;
135 } 210 }
136 else 211 else
137#endif 212#endif
213
138 { 214 {
139#if IOM_TIME 215#if IOM_TIME
140 time_watcher *next; 216 time_watcher *next;
141 217
142 for (;;) 218 for (;;)
153 { 229 {
154 if (next != tw[0]) 230 if (next != tw[0])
155 { 231 {
156 double diff = next->at - NOW; 232 double diff = next->at - NOW;
157 tval.tv_sec = (int)diff; 233 tval.tv_sec = (int)diff;
158 tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); 234 tval.tv_usec = (int) ((diff - tval.tv_sec) * 1000000);
159 to = &tval; 235 to = &tval;
160 } 236 }
161 break; 237 break;
162 } 238 }
163 else 239 else
165 unreg (next); 241 unreg (next);
166 next->call (*next); 242 next->call (*next);
167 } 243 }
168 } 244 }
169#endif 245#endif
246
170 } 247 }
171 248
172#if IOM_CHECK 249#if IOM_CHECK
250 tw.activity = false;
251
173 for (int i = cw.size (); i--; ) 252 for (int i = cw.size (); i--; )
174 if (!cw[i]) 253 if (!cw[i])
175 cw.erase_unordered (i); 254 cw.erase_unordered (i);
176 else 255 else
177 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 }
178#endif 264#endif
179 265
180#if IOM_IO 266#if IOM_IO
181 fd_set rfd, wfd, efd; 267 fd_set rfd, wfd;
182 268
183 FD_ZERO (&rfd); 269 FD_ZERO (&rfd);
184 FD_ZERO (&wfd); 270 FD_ZERO (&wfd);
185 271
186 int fds = 0; 272 int fds = 0;
195 } 281 }
196 282
197 if (!to && !fds) //TODO: also check idle_watchers and check_watchers 283 if (!to && !fds) //TODO: also check idle_watchers and check_watchers
198 break; // no events 284 break; // no events
199 285
200 fds = select (fds, &rfd, &wfd, &efd, to); 286 fds = select (fds, &rfd, &wfd, NULL, to);
201# if IOM_TIME 287# if IOM_TIME
202 set_now (); 288 set_now ();
203# endif 289# endif
204 290
205 if (fds > 0) 291 if (fds > 0)
237 select (0, 0, 0, 0, &to); 323 select (0, 0, 0, 0, &to);
238 set_now (); 324 set_now ();
239#else 325#else
240 break; 326 break;
241#endif 327#endif
328
242 } 329 }
243} 330}
244 331
245io_manager::io_manager () 332io_manager::io_manager ()
246{ 333{
248 335
249#if IOM_TIME 336#if IOM_TIME
250 set_now (); 337 set_now ();
251 338
252 tw0.start (TSTAMP_MAX); 339 tw0.start (TSTAMP_MAX);
253 printf ("abort, %f but inly on %f\n", NOW, tw0.at);
254#endif 340#endif
255} 341}
256 342
257io_manager::~io_manager () 343io_manager::~io_manager ()
258{ 344{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines