1 | /* |
1 | /* |
2 | iom.C -- I/O multiplexor |
2 | iom.C -- generic I/O multiplexer |
|
|
3 | Copyright (C) 2003-2006 Marc Lehmann <gvpe@schmorp.de> |
3 | |
4 | |
|
|
5 | This file is part of GVPE. |
|
|
6 | |
4 | This program is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
8 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
9 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
10 | (at your option) any later version. |
8 | |
11 | |
9 | This program is distributed in the hope that it will be useful, |
12 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
15 | GNU General Public License for more details. |
13 | |
16 | |
14 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software |
18 | along with gvpe; if not, write to the Free Software |
16 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
20 | */ |
18 | |
21 | |
19 | #include "config.h" |
22 | #include "iom.h" |
20 | |
23 | |
|
|
24 | #include <cstdio> |
|
|
25 | #include <cstdlib> |
|
|
26 | #include <cerrno> |
|
|
27 | #include <cassert> |
|
|
28 | |
|
|
29 | #include <sys/types.h> |
21 | #include <sys/time.h> |
30 | #include <sys/time.h> |
22 | |
31 | |
23 | #include <algorithm> |
32 | #if 1 // older unices need these includes for select (2) |
|
|
33 | # include <unistd.h> |
|
|
34 | # include <time.h> |
|
|
35 | #endif |
|
|
36 | |
|
|
37 | #if IOM_CHILD |
|
|
38 | # include <sys/wait.h> |
|
|
39 | #endif |
|
|
40 | |
|
|
41 | #if IOM_SIG |
24 | #include <functional> |
42 | # include <csignal> |
|
|
43 | # include <fcntl.h> |
|
|
44 | #endif |
25 | |
45 | |
26 | #include "slog.h" |
46 | // if the BSDs would at least be marginally POSIX-compatible.. *sigh* |
27 | #include "iom.h" |
47 | // until that happens, sys/select.h must come last |
|
|
48 | #include <sys/select.h> |
28 | |
49 | |
29 | inline bool earliest_first (const time_watcher *a, const time_watcher *b) |
50 | #define TIMEVAL timeval |
|
|
51 | #define TV_FRAC tv_usec |
|
|
52 | #define TV_MULT 1000000L |
|
|
53 | |
|
|
54 | #if IOM_IO |
|
|
55 | static io_manager_vec<io_watcher> iow; |
|
|
56 | #endif |
|
|
57 | #if IOM_CHECK |
|
|
58 | static io_manager_vec<check_watcher> cw; |
|
|
59 | #endif |
|
|
60 | #if IOM_TIME |
|
|
61 | static io_manager_vec<time_watcher> tw; |
|
|
62 | #endif |
|
|
63 | #if IOM_IDLE |
|
|
64 | static io_manager_vec<idle_watcher> iw; |
|
|
65 | #endif |
|
|
66 | #if IOM_SIG |
|
|
67 | static int sigpipe[2]; // signal signalling pipe |
|
|
68 | static sigset_t sigs; |
|
|
69 | struct sig_vec : io_manager_vec<sig_watcher> { |
|
|
70 | int pending; |
|
|
71 | sig_vec () |
|
|
72 | : pending (false) |
|
|
73 | { } |
|
|
74 | }; |
|
|
75 | static vector<sig_vec *> sw; |
|
|
76 | #endif |
|
|
77 | #if IOM_CHILD |
|
|
78 | static io_manager_vec<child_watcher> pw; |
|
|
79 | #endif |
|
|
80 | |
|
|
81 | // this is a dummy time watcher to ensure that the first |
|
|
82 | // time watcher is _always_ valid, this gets rid of a lot |
|
|
83 | // of null-pointer-checks |
|
|
84 | // (must come _before_ iom is being defined) |
|
|
85 | static struct tw0 : time_watcher |
30 | { |
86 | { |
31 | return a->at > b->at; |
87 | void cb (time_watcher &w) |
32 | } |
88 | { |
|
|
89 | // should never get called |
|
|
90 | // reached end-of-time, or tstamp has a bogus definition, |
|
|
91 | // or compiler initialisation order broken, or something else :) |
|
|
92 | abort (); |
|
|
93 | } |
|
|
94 | |
|
|
95 | tw0 () |
|
|
96 | : time_watcher (this, &tw0::cb) |
|
|
97 | { } |
|
|
98 | } tw0; |
33 | |
99 | |
34 | tstamp NOW; |
100 | tstamp NOW; |
35 | |
101 | |
36 | io_manager iom; |
102 | #if IOM_CHILD |
37 | |
103 | // sig_watcher for child signal(s) |
38 | void time_watcher::set (tstamp when) |
104 | static struct sw0 : sig_watcher |
39 | { |
105 | { |
40 | at = when; |
106 | void cb (sig_watcher &w) |
|
|
107 | { |
|
|
108 | // SIGCHLD, call corresponding watchera |
|
|
109 | pid_t pid; |
|
|
110 | int status; |
41 | |
111 | |
42 | if (registered) |
112 | while ((pid = waitpid (-1, &status, WNOHANG)) > 0) |
43 | iom.reschedule_time_watchers (); |
113 | for (int i = pw.size (); i--; ) |
44 | else |
114 | { |
45 | iom.reg (this); |
115 | child_watcher *w = pw[i]; |
46 | } |
|
|
47 | |
116 | |
|
|
117 | if (!w) |
|
|
118 | pw.erase_unordered (i); |
|
|
119 | else if (w->pid == pid) |
|
|
120 | { |
|
|
121 | io_manager::unreg (*w); |
|
|
122 | w->call (*w, status); |
|
|
123 | } |
|
|
124 | } |
|
|
125 | } |
|
|
126 | |
|
|
127 | sw0 () |
|
|
128 | : sig_watcher (this, &sw0::cb) |
|
|
129 | { } |
|
|
130 | } sw0; |
|
|
131 | #endif |
|
|
132 | |
|
|
133 | #if IOM_TIME |
|
|
134 | tstamp io_manager::now () |
|
|
135 | { |
|
|
136 | struct timeval tv; |
|
|
137 | |
|
|
138 | gettimeofday (&tv, 0); |
|
|
139 | return (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.; |
|
|
140 | } |
|
|
141 | |
|
|
142 | void io_manager::set_now () |
|
|
143 | { |
|
|
144 | NOW = now (); |
|
|
145 | } |
|
|
146 | #endif |
|
|
147 | |
|
|
148 | static bool iom_valid; |
|
|
149 | |
|
|
150 | // used for initialisation only |
|
|
151 | static struct init { |
|
|
152 | init () |
|
|
153 | { |
|
|
154 | #ifdef IOM_PREINIT |
|
|
155 | { IOM_PREINIT } |
|
|
156 | #endif |
|
|
157 | iom_valid = true; |
|
|
158 | |
|
|
159 | #if IOM_SIG |
|
|
160 | sigemptyset (&sigs); |
|
|
161 | |
|
|
162 | if (pipe (sigpipe)) |
|
|
163 | { |
|
|
164 | perror ("io_manager: unable to create signal pipe, aborting."); |
|
|
165 | abort (); |
|
|
166 | } |
|
|
167 | |
|
|
168 | fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC); |
|
|
169 | fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC); |
|
|
170 | #endif |
|
|
171 | |
|
|
172 | #if IOM_CHILD |
|
|
173 | sw0.start (SIGCHLD); |
|
|
174 | #endif |
|
|
175 | |
|
|
176 | #if IOM_TIME |
|
|
177 | io_manager::set_now (); |
|
|
178 | |
|
|
179 | tw0.start (TSTAMP_MAX); |
|
|
180 | #endif |
|
|
181 | |
|
|
182 | #ifdef IOM_POSTINIT |
|
|
183 | { IOM_POSTINIT } |
|
|
184 | #endif |
|
|
185 | } |
|
|
186 | |
|
|
187 | ~init () |
|
|
188 | { |
|
|
189 | iom_valid = false; |
|
|
190 | } |
|
|
191 | |
|
|
192 | static void required (); |
|
|
193 | } init; |
|
|
194 | |
|
|
195 | void |
|
|
196 | init::required () |
|
|
197 | { |
|
|
198 | if (!iom_valid) |
|
|
199 | { |
|
|
200 | write (2, "io_manager: early registration attempt, aborting.\n", |
|
|
201 | sizeof ("io_manager: early registration attempt, aborting.\n") - 1); |
|
|
202 | abort (); |
|
|
203 | } |
|
|
204 | } |
|
|
205 | |
|
|
206 | template<class watcher> |
|
|
207 | void io_manager::reg (watcher &w, io_manager_vec<watcher> &queue) |
|
|
208 | { |
|
|
209 | init::required (); |
|
|
210 | |
|
|
211 | if (!w.active) |
|
|
212 | { |
|
|
213 | queue.push_back (&w); |
|
|
214 | w.active = queue.size (); |
|
|
215 | } |
|
|
216 | } |
|
|
217 | |
|
|
218 | template<class watcher> |
|
|
219 | void io_manager::unreg (watcher &w, io_manager_vec<watcher> &queue) |
|
|
220 | { |
|
|
221 | if (!iom_valid) |
|
|
222 | return; |
|
|
223 | |
|
|
224 | if (w.active) |
|
|
225 | { |
|
|
226 | queue [w.active - 1] = 0; |
|
|
227 | w.active = 0; |
|
|
228 | } |
|
|
229 | } |
|
|
230 | |
|
|
231 | #if IOM_TIME |
48 | void time_watcher::trigger () |
232 | void time_watcher::trigger () |
49 | { |
233 | { |
50 | call (*this); |
234 | call (*this); |
51 | |
235 | io_manager::reg (*this); |
52 | if (registered) |
|
|
53 | iom.reschedule_time_watchers (); |
|
|
54 | else |
|
|
55 | iom.reg (this); |
|
|
56 | } |
236 | } |
57 | |
237 | |
58 | void time_watcher::start () |
238 | void io_manager::reg (time_watcher &w) { io_manager::reg (w, tw); } |
59 | { |
239 | void io_manager::unreg (time_watcher &w) { io_manager::unreg (w, tw); } |
60 | if (!registered) |
240 | #endif |
61 | iom.reg (this); |
|
|
62 | } |
|
|
63 | |
241 | |
|
|
242 | #if IOM_IO |
|
|
243 | void io_manager::reg (io_watcher &w) { io_manager::reg (w, iow); } |
|
|
244 | void io_manager::unreg (io_watcher &w) { io_manager::unreg (w, iow); } |
|
|
245 | #endif |
|
|
246 | |
|
|
247 | #if IOM_CHECK |
|
|
248 | void io_manager::reg (check_watcher &w) { io_manager::reg (w, cw); } |
|
|
249 | void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); } |
|
|
250 | #endif |
|
|
251 | |
|
|
252 | #if IOM_IDLE |
|
|
253 | void io_manager::reg (idle_watcher &w) { io_manager::reg (w, iw); } |
|
|
254 | void io_manager::unreg (idle_watcher &w) { io_manager::unreg (w, iw); } |
|
|
255 | #endif |
|
|
256 | |
|
|
257 | #if IOM_SIG |
|
|
258 | static void |
|
|
259 | sighandler (int signum) |
|
|
260 | { |
|
|
261 | sw [signum - 1]->pending = true; |
|
|
262 | |
|
|
263 | // we use a pipe for signal notifications, as most current |
|
|
264 | // OSes (Linux...) do not implement pselect correctly. ugh. |
|
|
265 | char ch = signum; // actual content not used |
|
|
266 | write (sigpipe[1], &ch, 1); |
|
|
267 | } |
|
|
268 | |
64 | void io_manager::reg (io_watcher *w) |
269 | void io_manager::reg (sig_watcher &w) |
65 | { |
270 | { |
66 | pollfd pfd; |
271 | init::required (); |
67 | |
272 | |
68 | pfd.fd = w->fd; |
273 | assert (0 < w.signum); |
69 | pfd.events = w->events; |
|
|
70 | |
274 | |
71 | pfs.push_back (pfd); |
275 | sw.reserve (w.signum); |
|
|
276 | |
|
|
277 | while (sw.size () < w.signum) // pathetic |
72 | iow.push_back (w); |
278 | sw.push_back (0); |
73 | } |
|
|
74 | |
279 | |
75 | void io_manager::unreg (io_watcher *w) |
280 | sig_vec *&sv = sw[w.signum - 1]; |
76 | { |
|
|
77 | unsigned int sz = iow.size (); |
|
|
78 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
|
|
79 | |
281 | |
80 | if (i != sz) |
282 | if (!sv) |
81 | { |
283 | { |
82 | if (sz == 1) |
284 | sv = new sig_vec; |
|
|
285 | |
|
|
286 | sigaddset (&sigs, w.signum); |
|
|
287 | sigprocmask (SIG_BLOCK, &sigs, NULL); |
|
|
288 | |
|
|
289 | struct sigaction sa; |
|
|
290 | sa.sa_handler = sighandler; |
|
|
291 | sigfillset (&sa.sa_mask); |
|
|
292 | sa.sa_flags = SA_RESTART; |
|
|
293 | |
|
|
294 | if (sigaction (w.signum, &sa, 0)) |
83 | { |
295 | { |
|
|
296 | perror ("io_manager: error while installing signal handler, ignoring."); |
84 | pfs.clear (); |
297 | abort (); |
85 | iow.clear (); |
|
|
86 | } |
298 | } |
87 | else if (i == sz - 1) |
299 | |
|
|
300 | } |
|
|
301 | |
|
|
302 | io_manager::reg (w, *sv); |
|
|
303 | } |
|
|
304 | |
|
|
305 | void io_manager::unreg (sig_watcher &w) |
|
|
306 | { |
|
|
307 | if (!w.active || !iom_valid) |
|
|
308 | return; |
|
|
309 | |
|
|
310 | assert (0 < w.signum && w.signum <= sw.size ()); |
|
|
311 | |
|
|
312 | io_manager::unreg (w, *sw[w.signum - 1]); |
|
|
313 | } |
|
|
314 | |
|
|
315 | void sig_watcher::start (int signum) |
|
|
316 | { |
|
|
317 | stop (); |
|
|
318 | this->signum = signum; |
|
|
319 | io_manager::reg (*this); |
|
|
320 | } |
|
|
321 | #endif |
|
|
322 | |
|
|
323 | #if IOM_CHILD |
|
|
324 | void io_manager::reg (child_watcher &w) { io_manager::reg (w, pw); } |
|
|
325 | void io_manager::unreg (child_watcher &w) { io_manager::unreg (w, pw); } |
|
|
326 | #endif |
|
|
327 | |
|
|
328 | void io_manager::loop () |
|
|
329 | { |
|
|
330 | init::required (); |
|
|
331 | |
|
|
332 | #if IOM_TIME |
|
|
333 | set_now (); |
|
|
334 | #endif |
|
|
335 | |
|
|
336 | for (;;) |
|
|
337 | { |
|
|
338 | |
|
|
339 | #if IOM_TIME |
|
|
340 | // call pending time watchers |
|
|
341 | { |
|
|
342 | bool activity; |
|
|
343 | |
|
|
344 | do |
|
|
345 | { |
|
|
346 | activity = false; |
|
|
347 | |
|
|
348 | for (int i = tw.size (); i--; ) |
|
|
349 | if (!tw[i]) |
|
|
350 | tw.erase_unordered (i); |
|
|
351 | else if (tw[i]->at <= NOW) |
|
|
352 | { |
|
|
353 | time_watcher &w = *tw[i]; |
|
|
354 | |
|
|
355 | unreg (w); |
|
|
356 | w.call (w); |
|
|
357 | |
|
|
358 | activity = true; |
|
|
359 | } |
|
|
360 | } |
|
|
361 | while (activity); |
|
|
362 | } |
|
|
363 | #endif |
|
|
364 | |
|
|
365 | #if IOM_CHECK |
|
|
366 | // call all check watchers |
|
|
367 | for (int i = cw.size (); i--; ) |
|
|
368 | if (!cw[i]) |
|
|
369 | cw.erase_unordered (i); |
|
|
370 | else |
|
|
371 | cw[i]->call (*cw[i]); |
|
|
372 | #endif |
|
|
373 | |
|
|
374 | struct TIMEVAL *to = 0; |
|
|
375 | struct TIMEVAL tval; |
|
|
376 | |
|
|
377 | #if IOM_IDLE |
|
|
378 | if (iw.size ()) |
88 | { |
379 | { |
89 | iow.pop_back (); |
380 | tval.tv_sec = 0; |
90 | pfs.pop_back (); |
381 | tval.TV_FRAC = 0; |
|
|
382 | to = &tval; |
91 | } |
383 | } |
92 | else |
384 | else |
|
|
385 | #endif |
93 | { |
386 | { |
94 | iow[i] = iow[sz - 1]; iow.pop_back (); |
387 | #if IOM_TIME |
95 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
388 | // find earliest active watcher |
|
|
389 | time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times |
|
|
390 | |
|
|
391 | for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
|
|
392 | if (*i && (*i)->at < next->at) |
|
|
393 | next = *i; |
|
|
394 | |
|
|
395 | if (next->at > NOW && next != tw[0]) |
|
|
396 | { |
|
|
397 | double diff = next->at - NOW; |
|
|
398 | tval.tv_sec = (int)diff; |
|
|
399 | tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT); |
|
|
400 | to = &tval; |
|
|
401 | } |
96 | } |
402 | } |
|
|
403 | #endif |
|
|
404 | |
|
|
405 | #if IOM_IO || IOM_SIG |
|
|
406 | fd_set rfd, wfd; |
|
|
407 | |
|
|
408 | FD_ZERO (&rfd); |
|
|
409 | FD_ZERO (&wfd); |
|
|
410 | |
|
|
411 | int fds = 0; |
|
|
412 | |
|
|
413 | # if IOM_IO |
|
|
414 | for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); ) |
|
|
415 | if (*i) |
|
|
416 | { |
|
|
417 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
|
|
418 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
|
|
419 | |
|
|
420 | if ((*i)->fd >= fds) fds = (*i)->fd + 1; |
|
|
421 | } |
|
|
422 | # endif |
|
|
423 | |
|
|
424 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers? |
|
|
425 | break; // no events |
|
|
426 | |
|
|
427 | # if IOM_SIG |
|
|
428 | FD_SET (sigpipe[0], &rfd); |
|
|
429 | if (sigpipe[0] >= fds) fds = sigpipe[0] + 1; |
|
|
430 | # endif |
|
|
431 | |
|
|
432 | # if IOM_SIG |
|
|
433 | // there is no race, as we use a pipe for signals, so select |
|
|
434 | // will return if a signal is caught. |
|
|
435 | sigprocmask (SIG_UNBLOCK, &sigs, NULL); |
|
|
436 | # endif |
|
|
437 | fds = select (fds, &rfd, &wfd, NULL, to); |
|
|
438 | # if IOM_SIG |
|
|
439 | sigprocmask (SIG_BLOCK, &sigs, NULL); |
|
|
440 | # endif |
|
|
441 | |
|
|
442 | # if IOM_TIME |
|
|
443 | { |
|
|
444 | // update time, try to compensate for gross non-monotonic time changes |
|
|
445 | tstamp diff = NOW; |
|
|
446 | set_now (); |
|
|
447 | diff = NOW - diff; |
|
|
448 | |
|
|
449 | if (diff < 0) |
|
|
450 | for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
|
|
451 | if (*i) |
|
|
452 | (*i)->at += diff; |
|
|
453 | } |
|
|
454 | # endif |
|
|
455 | |
|
|
456 | if (fds > 0) |
|
|
457 | { |
|
|
458 | # if IOM_SIG |
|
|
459 | if (FD_ISSET (sigpipe[0], &rfd)) |
|
|
460 | { |
|
|
461 | char ch; |
|
|
462 | |
|
|
463 | while (read (sigpipe[0], &ch, 1) > 0) |
|
|
464 | ; |
|
|
465 | |
|
|
466 | for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); ) |
|
|
467 | if (*svp && (*svp)->pending) |
|
|
468 | { |
|
|
469 | sig_vec &sv = **svp; |
|
|
470 | for (int i = sv.size (); i--; ) |
|
|
471 | if (!sv[i]) |
|
|
472 | sv.erase_unordered (i); |
|
|
473 | else |
|
|
474 | sv[i]->call (*sv[i]); |
|
|
475 | |
|
|
476 | sv.pending = false; |
|
|
477 | } |
|
|
478 | } |
|
|
479 | # endif |
|
|
480 | |
|
|
481 | # if IOM_IO |
|
|
482 | for (int i = iow.size (); i--; ) |
|
|
483 | if (!iow[i]) |
|
|
484 | iow.erase_unordered (i); |
|
|
485 | else |
|
|
486 | { |
|
|
487 | io_watcher &w = *iow[i]; |
|
|
488 | short revents = w.events; |
|
|
489 | |
|
|
490 | if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ; |
|
|
491 | if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE; |
|
|
492 | |
|
|
493 | if (revents) |
|
|
494 | w.call (w, revents); |
|
|
495 | } |
|
|
496 | #endif |
|
|
497 | } |
|
|
498 | else if (fds < 0 && errno != EINTR) |
|
|
499 | { |
|
|
500 | perror ("io_manager: fatal error while waiting for I/O or time event, aborting."); |
|
|
501 | abort (); |
|
|
502 | } |
|
|
503 | #if IOM_IDLE |
|
|
504 | else |
|
|
505 | for (int i = iw.size (); i--; ) |
|
|
506 | if (!iw[i]) |
|
|
507 | iw.erase_unordered (i); |
|
|
508 | else |
|
|
509 | iw[i]->call (*iw[i]); |
|
|
510 | #endif |
|
|
511 | |
|
|
512 | #elif IOM_TIME |
|
|
513 | if (!to) |
|
|
514 | break; |
|
|
515 | |
|
|
516 | select (0, 0, 0, 0, &to); |
|
|
517 | set_now (); |
|
|
518 | #else |
|
|
519 | break; |
|
|
520 | #endif |
97 | } |
521 | } |
98 | } |
522 | } |
99 | |
523 | |
100 | void io_manager::reschedule_time_watchers () |
|
|
101 | { |
|
|
102 | make_heap (tw.begin (), tw.end (), earliest_first); |
|
|
103 | } |
|
|
104 | |
|
|
105 | void io_manager::reg (time_watcher *w) |
|
|
106 | { |
|
|
107 | if (w->registered) |
|
|
108 | slog (L_CRIT, "FATAL: io_manager::reg(time_watcher) called on already-registered watcher"); |
|
|
109 | |
|
|
110 | w->registered = true; |
|
|
111 | |
|
|
112 | tw.push_back (w); |
|
|
113 | push_heap (tw.begin (), tw.end (), earliest_first); |
|
|
114 | } |
|
|
115 | |
|
|
116 | void io_manager::unreg (time_watcher *w) |
|
|
117 | { |
|
|
118 | if (w->registered) |
|
|
119 | { |
|
|
120 | unsigned int sz = tw.size (); |
|
|
121 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
|
|
122 | |
|
|
123 | if (i != sz) |
|
|
124 | { |
|
|
125 | if (i != sz - 1) |
|
|
126 | tw[i] = tw[sz - 1]; |
|
|
127 | |
|
|
128 | tw.pop_back (); |
|
|
129 | reschedule_time_watchers (); |
|
|
130 | } |
|
|
131 | |
|
|
132 | w->registered = false; |
|
|
133 | } |
|
|
134 | } |
|
|
135 | |
|
|
136 | inline void set_now (void) |
|
|
137 | { |
|
|
138 | struct timeval tv; |
|
|
139 | |
|
|
140 | gettimeofday (&tv, 0); |
|
|
141 | |
|
|
142 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
|
|
143 | } |
|
|
144 | |
|
|
145 | void io_manager::loop () |
|
|
146 | { |
|
|
147 | set_now (); |
|
|
148 | |
|
|
149 | for (;;) |
|
|
150 | { |
|
|
151 | while (tw[0]->at <= NOW) |
|
|
152 | { |
|
|
153 | // remove the first watcher |
|
|
154 | time_watcher *w = tw[0]; |
|
|
155 | |
|
|
156 | pop_heap (tw.begin (), tw.end (), earliest_first); |
|
|
157 | tw.pop_back (); |
|
|
158 | |
|
|
159 | w->registered = false; |
|
|
160 | |
|
|
161 | // call it |
|
|
162 | w->call (*w); |
|
|
163 | |
|
|
164 | // re-add it if necessary |
|
|
165 | if (w->at >= 0 && !w->registered) |
|
|
166 | reg (w); |
|
|
167 | } |
|
|
168 | |
|
|
169 | int timeout = (int) ((tw[0]->at - NOW) * 1000); |
|
|
170 | |
|
|
171 | int fds = poll (&pfs[0], pfs.size (), timeout); |
|
|
172 | |
|
|
173 | set_now (); |
|
|
174 | |
|
|
175 | vector<io_watcher *>::iterator w; |
|
|
176 | vector<pollfd>::iterator p; |
|
|
177 | |
|
|
178 | for (w = iow.begin (), p = pfs.begin (); |
|
|
179 | fds > 0 && w < iow.end (); |
|
|
180 | ++w, ++p) |
|
|
181 | if (p->revents) |
|
|
182 | { |
|
|
183 | --fds; |
|
|
184 | (*w)->call (**w, p->revents); |
|
|
185 | } |
|
|
186 | } |
|
|
187 | } |
|
|
188 | |
|
|
189 | void io_manager::idle_cb (time_watcher &w) |
|
|
190 | { |
|
|
191 | w.at = NOW + 86400; // wake up every day, for no good reason |
|
|
192 | } |
|
|
193 | |
|
|
194 | io_manager::io_manager () |
|
|
195 | { |
|
|
196 | set_now (); |
|
|
197 | idle = new time_watcher (this, &io_manager::idle_cb); |
|
|
198 | idle->start (0); |
|
|
199 | } |
|
|
200 | |
|
|
201 | io_manager::~io_manager () |
|
|
202 | { |
|
|
203 | // |
|
|
204 | } |
|
|
205 | |
|
|