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