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.8 by pcg, Fri Jan 16 22:11:09 2004 UTC

18*/ 18*/
19 19
20#include "../config.h" 20#include "../config.h"
21 21
22#include <cstdio> 22#include <cstdio>
23#include <cstdlib>
24#include <cerrno>
23 25
24#include <sys/select.h> 26#include <sys/select.h>
25#include <sys/time.h> 27#include <sys/time.h>
26 28
27#include "iom.h" 29#include "iom.h"
28 30
31// TSTAMP_MAX must still fit into a positive struct timeval
32#define TSTAMP_MAX (double)(1UL<<31)
33
29tstamp NOW; 34tstamp NOW;
30bool iom_valid; 35static bool iom_valid;
31io_manager iom; 36io_manager iom;
32 37
33template<class watcher> 38template<class watcher>
34void io_manager::reg (watcher *w, simplevec<watcher *> &queue) 39void io_manager::reg (watcher *w, simplevec<watcher *> &queue)
35{ 40{
36 if (find (queue.begin (), queue.end (), w) == queue.end ()) 41 if (!iom_valid)
42 abort ();
43
44 if (!w->active)
45 {
37 queue.push_back (w); 46 queue.push_back (w);
47 w->active = queue.size ();
48 }
38} 49}
39 50
40template<class watcher> 51template<class watcher>
41void io_manager::unreg (watcher *w, simplevec<watcher *> &queue) 52void io_manager::unreg (watcher *w, simplevec<watcher *> &queue)
42{ 53{
43 queue.erase (find (queue.begin (), queue.end (), w)); 54 if (!iom_valid)
55 return;
56
57 if (w->active)
58 {
59 queue [w->active - 1] = 0;
60 w->active = 0;
61 }
44} 62}
63
64#if IOM_TIME
65void time_watcher::trigger ()
66{
67 call (*this);
68
69 iom.reg (this);
70}
71
72void io_manager::reg (time_watcher *w) { reg (w, tw); }
73void io_manager::unreg (time_watcher *w) { unreg (w, tw); }
74#endif
45 75
46#if IOM_IO 76#if IOM_IO
47io_watcher::~io_watcher ()
48{
49 if (iom_valid)
50 iom.unreg (this);
51}
52
53void io_manager::reg (io_watcher *w) 77void io_manager::reg (io_watcher *w) { reg (w, iow); }
54{
55 reg (w, iow);
56}
57
58void io_manager::unreg (io_watcher *w) 78void io_manager::unreg (io_watcher *w) { unreg (w, iow); }
59{
60 unreg (w, iow);
61}
62
63#endif
64
65#if IOM_TIME
66void time_watcher::trigger ()
67{
68 call (*this);
69
70 iom.reg (this);
71}
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 79#endif
91 80
92#if IOM_CHECK 81#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) 82void io_manager::reg (check_watcher *w) { reg (w, cw); }
100{
101 reg (w, cw);
102}
103
104void io_manager::unreg (check_watcher *w) 83void io_manager::unreg (check_watcher *w) { unreg (w, cw); }
105{
106 unreg (w, cw);
107}
108#endif 84#endif
109 85
110#if IOM_IDLE 86#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) 87void io_manager::reg (idle_watcher *w) { reg (w, iw); }
118{
119 reg (w, iw);
120}
121
122void io_manager::unreg (idle_watcher *w) 88void io_manager::unreg (idle_watcher *w) { unreg (w, iw); }
123{
124 unreg (w, iw);
125}
126#endif 89#endif
127 90
128#if IOM_TIME 91#if IOM_TIME
129inline void set_now (void) 92inline void set_now (void)
130{ 93{
156 } 119 }
157 else 120 else
158#endif 121#endif
159 { 122 {
160#if IOM_TIME 123#if IOM_TIME
161 time_watcher *w; 124 time_watcher *next;
162 125
163 for (;tw.size ();) 126 for (;;)
164 { 127 {
128 next = tw[0]; // the first time-watcher must exist at ALL times
129
130 for (int i = tw.size (); i--; )
131 if (!tw[i])
132 tw.erase_unordered (i);
133 else if (tw[i]->at < next->at)
165 w = tw[0]; 134 next = tw[i];
166 135
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) 136 if (next->at > NOW)
172 { 137 {
138 if (next != tw[0])
139 {
173 double diff = w->at - NOW; 140 double diff = next->at - NOW;
174 tval.tv_sec = (int)diff; 141 tval.tv_sec = (int)diff;
175 tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); 142 tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000);
176 to = &tval; 143 to = &tval;
144 }
177 break; 145 break;
178 } 146 }
179 else if (w->at >= 0) 147 else if (next->at >= 0)
180 w->call (*w);
181 else 148 {
182 unreg (w); 149 unreg (next);
150 next->call (*next);
151 }
183 } 152 }
184#endif 153#endif
185 } 154 }
186 155
187#if IOM_CHECK 156#if IOM_CHECK
188 for (int i = 0; i < cw.size (); ++i) 157 for (int i = cw.size (); i--; )
158 if (!cw[i])
159 cw.erase_unordered (i);
160 else
189 cw[i]->call (*cw[i]); 161 cw[i]->call (*cw[i]);
190#endif 162#endif
191 163
192#if IOM_IO 164#if IOM_IO
193 fd_set rfd, wfd; 165 fd_set rfd, wfd, efd;
194 166
195 FD_ZERO (&rfd); 167 FD_ZERO (&rfd);
196 FD_ZERO (&wfd); 168 FD_ZERO (&wfd);
197 169
198 int fds = 0; 170 int fds = 0;
199 171
200 for (io_watcher **w = iow.begin (); w < iow.end (); ++w) 172 for (io_watcher **i = iow.end (); i-- > iow.begin (); )
173 if (*i)
201 { 174 {
202 if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd); 175 if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd);
203 if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); 176 if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd);
204 177
205 if ((*w)->fd >= fds) fds = (*w)->fd + 1; 178 if ((*i)->fd >= fds) fds = (*i)->fd + 1;
206 } 179 }
207 180
208 if (!to && !fds) 181 if (!to && !fds) //TODO: also check idle_watchers and check_watchers
209 break; // no events 182 break; // no events
210 183
211 fds = select (fds, &rfd, &wfd, 0, to); 184 fds = select (fds, &rfd, &wfd, &efd, to);
212# if IOM_TIME 185# if IOM_TIME
213 set_now (); 186 set_now ();
214# endif 187# endif
215 188
216 if (fds > 0) 189 if (fds > 0)
217 for (int i = 0; i < iow.size (); ++i) 190 for (int i = iow.size (); i--; )
191 if (!iow[i])
192 iow.erase_unordered (i);
193 else
218 { 194 {
219 io_watcher *w = iow[i];
220
221 short revents = w->events; 195 short revents = iow[i]->events;
222 196
223 if (!FD_ISSET (w->fd, &rfd)) revents &= ~EVENT_READ; 197 if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ;
224 if (!FD_ISSET (w->fd, &wfd)) revents &= ~EVENT_WRITE; 198 if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE;
225 199
226 if (revents) 200 if (revents)
227 w->call (*w, revents); 201 iow[i]->call (*iow[i], revents);
228 } 202 }
203 else if (fds < 0 && errno != EINTR)
204 {
205 perror ("Error while waiting for I/O or time event");
206 abort ();
207 }
229#if IOM_IDLE 208#if IOM_IDLE
230 else if (iw.size ()) 209 else
231 for (int i = 0; i < iw.size (); ++i) 210 for (int i = iw.size (); i--; )
211 if (!iw[i])
212 iw.erase_unordered (i);
213 else
232 iw[i]->call (*iw[i]); 214 iw[i]->call (*iw[i]);
233#endif 215#endif
234 216
235#elif IOM_TIME 217#elif IOM_TIME
236 if (!to) 218 if (!to)
237 break; 219 break;
242 break; 224 break;
243#endif 225#endif
244 } 226 }
245} 227}
246 228
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 { }
243} tw0;
244
247io_manager::io_manager () 245io_manager::io_manager ()
248{ 246{
247 iom_valid = true;
248
249#if IOM_TIME 249#if IOM_TIME
250 set_now (); 250 set_now ();
251#endif
252 251
253 iom_valid = true; 252 tw0.start (TSTAMP_MAX);
253#endif
254} 254}
255 255
256io_manager::~io_manager () 256io_manager::~io_manager ()
257{ 257{
258 iom_valid = false; 258 iom_valid = false;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines