ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/iom.C
(Generate patch)

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines