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.3 by pcg, Tue Nov 25 17:11:33 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);
43 iom.unreg (this); 76 iom.unreg (this);
44 77
45 at = TSTAMP_CANCEL; 78 at = TSTAMP_CANCEL;
46} 79}
47 80
48io_watcher::~io_watcher ()
49{
50 if (iom_valid)
51 iom.unreg (this);
52}
53
54void io_manager::reg (io_watcher *w)
55{
56 if (find (iow.begin (), iow.end (), w) == iow.end ())
57 iow.push_back (w);
58}
59
60void io_manager::unreg (io_watcher *w)
61{
62 iow.erase (find (iow.begin (), iow.end (), w));
63}
64
65void io_manager::reg (time_watcher *w) 81void io_manager::reg (time_watcher *w)
66{ 82{
67 if (find (tw.begin (), tw.end (), w) == tw.end ()) 83 reg (w, tw);
68 tw.push_back (w);
69} 84}
70 85
71void io_manager::unreg (time_watcher *w) 86void io_manager::unreg (time_watcher *w)
72{ 87{
73 tw.erase (find (tw.begin (), tw.end (), w)); 88 unreg (w, tw);
74} 89}
90#endif
75 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
76inline void set_now (void) 129inline void set_now (void)
77{ 130{
78 struct timeval tv; 131 struct timeval tv;
79 132
80 gettimeofday (&tv, 0); 133 gettimeofday (&tv, 0);
81 134
82 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; 135 NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000;
136#endif
83} 137}
84 138
85void io_manager::loop () 139void io_manager::loop ()
86{ 140{
141#if IOM_TIME
87 set_now (); 142 set_now ();
143#endif
88 144
89 for (;;) 145 for (;;)
90 { 146 {
91 time_watcher *w; 147 struct timeval *to = 0;
148 struct timeval tval;
92 149
93 for (;;) 150#if IOM_IDLE
151 if (iw.size ())
94 { 152 {
95 w = tw[0]; 153 tval.tv_sec = 0;
96 154 tval.tv_usec = 0;
97 for (time_watcher **i = tw.begin (); i != tw.end (); ++i) 155 to = &tval;
98 if ((*i)->at < w->at)
99 w = *i;
100
101 if (w->at > NOW)
102 break;
103
104 // call it
105 w->call (*w);
106
107 if (w->at < 0)
108 unreg (w);
109 } 156 }
157 else
158#endif
159 {
160#if IOM_TIME
161 time_watcher *w;
110 162
111 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 {
112 double diff = w->at - NOW; 173 double diff = w->at - NOW;
113 to.tv_sec = (int)diff; 174 tval.tv_sec = (int)diff;
114 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 }
115 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
116 fd_set rfd, wfd; 193 fd_set rfd, wfd;
117 194
118 FD_ZERO (&rfd); 195 FD_ZERO (&rfd);
119 FD_ZERO (&wfd); 196 FD_ZERO (&wfd);
120 197
123 for (io_watcher **w = iow.begin (); w < iow.end (); ++w) 200 for (io_watcher **w = iow.begin (); w < iow.end (); ++w)
124 { 201 {
125 if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd); 202 if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd);
126 if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); 203 if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd);
127 204
128 if ((*w)->fd > fds) fds = (*w)->fd; 205 if ((*w)->fd >= fds) fds = (*w)->fd + 1;
129 } 206 }
130 207
208 if (!to && !fds)
209 break; // no events
210
131 fds = select (fds + 1, &rfd, &wfd, 0, &to); 211 fds = select (fds, &rfd, &wfd, 0, to);
132 212# if IOM_TIME
133 set_now (); 213 set_now ();
214# endif
134 215
135 if (fds > 0) 216 if (fds > 0)
136 for (io_watcher **w = iow.begin (); w < iow.end (); ++w) 217 for (int i = 0; i < iow.size (); ++i)
137 { 218 {
219 io_watcher *w = iow[i];
220
138 short revents = (*w)->events; 221 short revents = w->events;
139 222
140 if (!FD_ISSET ((*w)->fd, &rfd)) revents &= ~EVENT_READ; 223 if (!FD_ISSET (w->fd, &rfd)) revents &= ~EVENT_READ;
141 if (!FD_ISSET ((*w)->fd, &wfd)) revents &= ~EVENT_WRITE; 224 if (!FD_ISSET (w->fd, &wfd)) revents &= ~EVENT_WRITE;
142 225
143 if (revents) 226 if (revents)
144 (*w)->call (**w, revents); 227 w->call (*w, revents);
145 } 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
146 } 244 }
147} 245}
148 246
149void io_manager::idle_cb (time_watcher &w)
150{
151 w.at = NOW + 1000000000;
152}
153
154io_manager::io_manager () 247io_manager::io_manager ()
155{ 248{
249#if IOM_TIME
156 set_now (); 250 set_now ();
251#endif
157 252
158 iom_valid = true; 253 iom_valid = true;
159
160 idle = new time_watcher (this, &io_manager::idle_cb);
161 idle->start (0);
162} 254}
163 255
164io_manager::~io_manager () 256io_manager::~io_manager ()
165{ 257{
166 iom_valid = false; 258 iom_valid = false;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines