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.2 by pcg, Tue Nov 25 15:25:17 2003 UTC vs.
Revision 1.7 by pcg, Fri Dec 19 06:17:03 2003 UTC

28 28
29tstamp NOW; 29tstamp NOW;
30bool iom_valid; 30bool iom_valid;
31io_manager iom; 31io_manager iom;
32 32
33template<class watcher>
34void io_manager::reg (watcher *w, simplevec<watcher *> &queue)
35{
36 if (find (queue.begin (), queue.end (), w) == queue.end ())
37 queue.push_back (w);
38}
39
40template<class watcher>
41void io_manager::unreg (watcher *w, simplevec<watcher *> &queue)
42{
43 queue.erase (find (queue.begin (), queue.end (), w));
44}
45
46#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)
54{
55 reg (w, iow);
56}
57
58void io_manager::unreg (io_watcher *w)
59{
60 unreg (w, iow);
61}
62
63#endif
64
65#if IOM_TIME
33void time_watcher::trigger () 66void time_watcher::trigger ()
34{ 67{
35 call (*this); 68 call (*this);
36 69
37 iom.reg (this); 70 iom.reg (this);
39 72
40time_watcher::~time_watcher () 73time_watcher::~time_watcher ()
41{ 74{
42 if (iom_valid) 75 if (iom_valid)
43 iom.unreg (this); 76 iom.unreg (this);
44}
45 77
46io_watcher::~io_watcher () 78 at = TSTAMP_CANCEL;
47{
48 if (iom_valid)
49 iom.unreg (this);
50}
51
52void io_manager::reg (io_watcher *w)
53{
54 if (find (iow.begin (), iow.end (), w) == iow.end ())
55 iow.push_back (w);
56}
57
58void io_manager::unreg (io_watcher *w)
59{
60 iow.erase (find (iow.begin (), iow.end (), w));
61} 79}
62 80
63void io_manager::reg (time_watcher *w) 81void io_manager::reg (time_watcher *w)
64{ 82{
65 if (find (tw.begin (), tw.end (), w) == tw.end ()) 83 reg (w, tw);
66 tw.push_back (w);
67} 84}
68 85
69void io_manager::unreg (time_watcher *w) 86void io_manager::unreg (time_watcher *w)
70{ 87{
71 tw.erase (find (tw.begin (), tw.end (), w)); 88 unreg (w, tw);
72} 89}
90#endif
73 91
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
74inline void set_now (void) 129inline void set_now (void)
75{ 130{
76 struct timeval tv; 131 struct timeval tv;
77 132
78 gettimeofday (&tv, 0); 133 gettimeofday (&tv, 0);
79 134
80 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; 135 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000;
136#endif
81} 137}
82 138
83void io_manager::loop () 139void io_manager::loop ()
84{ 140{
141#if IOM_TIME
85 set_now (); 142 set_now ();
143#endif
86 144
87 for (;;) 145 for (;;)
88 { 146 {
89 time_watcher *w; 147 struct timeval *to = 0;
148 struct timeval tval;
90 149
91 for (;;) 150#if IOM_IDLE
151 if (iw.size ())
92 { 152 {
93 w = tw[0]; 153 tval.tv_sec = 0;
94 154 tval.tv_usec = 0;
95 for (time_watcher **i = tw.begin (); i != tw.end (); ++i) 155 to = &tval;
96 if ((*i)->at < w->at)
97 w = *i;
98
99 if (w->at > NOW)
100 break;
101
102 // call it
103 w->call (*w);
104
105 // re-add it if necessary
106 if (w->at >= 0)
107 reg (w);
108 } 156 }
157 else
158#endif
159 {
160#if IOM_TIME
161 time_watcher *w;
109 162
110 struct timeval to; 163 for (;tw.size ();)
164 {
165 w = tw[0];
166
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)
172 {
111 double diff = w->at - NOW; 173 double diff = w->at - NOW;
112 to.tv_sec = (int)diff; 174 tval.tv_sec = (int)diff;
113 to.tv_usec = (int)((diff - to.tv_sec) * 1000000); 175 tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000);
176 to = &tval;
177 break;
178 }
179 else if (w->at >= 0)
180 w->call (*w);
181 else
182 unreg (w);
183 }
184#endif
185 }
114 186
187#if IOM_CHECK
188 for (int i = 0; i < cw.size (); ++i)
189 cw[i]->call (*cw[i]);
190#endif
191
192#if IOM_IO
115 fd_set rfd, wfd; 193 fd_set rfd, wfd;
116 194
117 FD_ZERO (&rfd); 195 FD_ZERO (&rfd);
118 FD_ZERO (&wfd); 196 FD_ZERO (&wfd);
119 197
122 for (io_watcher **w = iow.begin (); w < iow.end (); ++w) 200 for (io_watcher **w = iow.begin (); w < iow.end (); ++w)
123 { 201 {
124 if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd); 202 if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd);
125 if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); 203 if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd);
126 204
127 if ((*w)->fd > fds) fds = (*w)->fd; 205 if ((*w)->fd >= fds) fds = (*w)->fd + 1;
128 } 206 }
129 207
208 if (!to && !fds)
209 break; // no events
210
130 fds = select (fds + 1, &rfd, &wfd, 0, &to); 211 fds = select (fds, &rfd, &wfd, 0, to);
131 212# if IOM_TIME
132 set_now (); 213 set_now ();
214# endif
133 215
134 if (fds > 0) 216 if (fds > 0)
135 for (io_watcher **w = iow.begin (); w < iow.end (); ++w) 217 for (int i = 0; i < iow.size (); ++i)
136 { 218 {
219 io_watcher *w = iow[i];
220
137 short revents = (*w)->events; 221 short revents = w->events;
138 222
139 if (!FD_ISSET ((*w)->fd, &rfd)) revents &= ~EVENT_READ; 223 if (!FD_ISSET (w->fd, &rfd)) revents &= ~EVENT_READ;
140 if (!FD_ISSET ((*w)->fd, &wfd)) revents &= ~EVENT_WRITE; 224 if (!FD_ISSET (w->fd, &wfd)) revents &= ~EVENT_WRITE;
141 225
142 if (revents) 226 if (revents)
143 (*w)->call (**w, revents); 227 w->call (*w, revents);
144 } 228 }
229#if IOM_IDLE
230 else if (iw.size ())
231 for (int i = 0; i < iw.size (); ++i)
232 iw[i]->call (*iw[i]);
233#endif
234
235#elif IOM_TIME
236 if (!to)
237 break;
238
239 select (0, 0, 0, 0, &to);
240 set_now ();
241#else
242 break;
243#endif
145 } 244 }
146} 245}
147 246
148void io_manager::idle_cb (time_watcher &w)
149{
150 w.at = NOW + 1000000000;
151}
152
153io_manager::io_manager () 247io_manager::io_manager ()
154{ 248{
249#if IOM_TIME
155 set_now (); 250 set_now ();
251#endif
156 252
157 iom_valid = true; 253 iom_valid = true;
158
159 idle = new time_watcher (this, &io_manager::idle_cb);
160 idle->start (0);
161} 254}
162 255
163io_manager::~io_manager () 256io_manager::~io_manager ()
164{ 257{
165 iom_valid = false; 258 iom_valid = false;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines