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 | |
22 | #include <cstdio> |
24 | #include <cstdio> |
|
|
25 | #include <cstdlib> |
|
|
26 | #include <cerrno> |
|
|
27 | #include <cassert> |
23 | |
28 | |
|
|
29 | #include <sys/types.h> |
|
|
30 | #include <sys/time.h> |
|
|
31 | |
|
|
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 |
|
|
42 | # include <csignal> |
|
|
43 | # include <fcntl.h> |
|
|
44 | #endif |
|
|
45 | |
|
|
46 | // if the BSDs would at least be marginally POSIX-compatible.. *sigh* |
|
|
47 | // until that happens, sys/select.h must come last |
24 | #include <sys/select.h> |
48 | #include <sys/select.h> |
25 | #include <sys/time.h> |
|
|
26 | |
49 | |
27 | #include "iom.h" |
50 | #define TIMEVAL timeval |
|
|
51 | #define TV_FRAC tv_usec |
|
|
52 | #define TV_MULT 1000000L |
|
|
53 | |
|
|
54 | #if IOM_CHECK |
|
|
55 | static io_manager_vec<check_watcher> cw; |
|
|
56 | #endif |
|
|
57 | #if IOM_IDLE |
|
|
58 | static io_manager_vec<idle_watcher> iw; |
|
|
59 | #endif |
|
|
60 | |
|
|
61 | #if IOM_SIG |
|
|
62 | static int sigpipe[2]; // signal signalling pipe |
|
|
63 | static sigset_t sigs; |
|
|
64 | struct sig_vec : io_manager_vec<sig_watcher> { |
|
|
65 | int pending; |
|
|
66 | sig_vec () |
|
|
67 | : pending (false) |
|
|
68 | { } |
|
|
69 | }; |
|
|
70 | static vector<sig_vec *> sw; |
|
|
71 | #endif |
|
|
72 | |
|
|
73 | #if IOM_CHILD |
|
|
74 | static io_manager_vec<child_watcher> pw; |
|
|
75 | #endif |
|
|
76 | |
|
|
77 | #ifdef IOM_LIBEVENT |
|
|
78 | static bool need_set_now; // need to set_now in callback |
|
|
79 | #else |
|
|
80 | #if IOM_IO |
|
|
81 | static io_manager_vec<io_watcher> iow; |
|
|
82 | #endif |
|
|
83 | #if IOM_TIME |
|
|
84 | static io_manager_vec<time_watcher> tw; |
|
|
85 | #endif |
|
|
86 | #endif |
|
|
87 | |
|
|
88 | #if IOM_TIME |
|
|
89 | tstamp io_manager::now () |
|
|
90 | { |
|
|
91 | struct timeval tv; |
|
|
92 | |
|
|
93 | gettimeofday (&tv, 0); |
|
|
94 | return (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.; |
|
|
95 | } |
|
|
96 | |
|
|
97 | void io_manager::set_now () |
|
|
98 | { |
|
|
99 | NOW = now (); |
|
|
100 | #ifdef IOM_LIBEVENT |
|
|
101 | need_set_now = false; |
|
|
102 | #endif |
|
|
103 | } |
|
|
104 | #endif |
|
|
105 | |
|
|
106 | #ifndef IOM_LIBEVENT |
|
|
107 | // this is a dummy time watcher to ensure that the first |
|
|
108 | // time watcher is _always_ valid, this gets rid of a lot |
|
|
109 | // of null-pointer-checks |
|
|
110 | // (must come _before_ iom is being defined) |
|
|
111 | static struct tw0 : time_watcher |
|
|
112 | { |
|
|
113 | void cb (time_watcher &w) |
|
|
114 | { |
|
|
115 | // should never get called |
|
|
116 | // reached end-of-time, or tstamp has a bogus definition, |
|
|
117 | // or compiler initialisation order broken, or something else :) |
|
|
118 | abort (); |
|
|
119 | } |
|
|
120 | |
|
|
121 | tw0 () |
|
|
122 | : time_watcher (this, &tw0::cb) |
|
|
123 | { } |
|
|
124 | } tw0; |
|
|
125 | #endif |
28 | |
126 | |
29 | tstamp NOW; |
127 | tstamp NOW; |
|
|
128 | |
|
|
129 | #if IOM_CHILD |
|
|
130 | // sig_watcher for child signal(s) |
|
|
131 | static struct sw0 : sig_watcher |
|
|
132 | { |
|
|
133 | void cb (sig_watcher &w) |
|
|
134 | { |
|
|
135 | // SIGCHLD, call corresponding watchera |
|
|
136 | pid_t pid; |
|
|
137 | int status; |
|
|
138 | |
|
|
139 | while ((pid = waitpid (-1, &status, WNOHANG)) > 0) |
|
|
140 | for (int i = pw.size (); i--; ) |
|
|
141 | { |
|
|
142 | child_watcher *w = pw[i]; |
|
|
143 | |
|
|
144 | if (!w) |
|
|
145 | pw.erase_unordered (i); |
|
|
146 | else if (w->pid == pid) |
|
|
147 | { |
|
|
148 | io_manager::unreg (*w); |
|
|
149 | w->call (*w, status); |
|
|
150 | } |
|
|
151 | } |
|
|
152 | } |
|
|
153 | |
|
|
154 | sw0 () |
|
|
155 | : sig_watcher (this, &sw0::cb) |
|
|
156 | { } |
|
|
157 | } sw0; |
|
|
158 | #endif |
|
|
159 | |
30 | bool iom_valid; |
160 | static bool iom_valid; |
31 | io_manager iom; |
|
|
32 | |
161 | |
33 | void time_watcher::trigger () |
162 | // used for initialisation only |
34 | { |
163 | static struct init { |
35 | call (*this); |
164 | init () |
|
|
165 | { |
|
|
166 | #ifdef IOM_PREINIT |
|
|
167 | { IOM_PREINIT } |
|
|
168 | #endif |
36 | |
169 | |
37 | iom.reg (this); |
170 | #ifdef IOM_LIBEVENT |
38 | } |
171 | event_init (); |
|
|
172 | #endif |
|
|
173 | iom_valid = true; |
39 | |
174 | |
40 | time_watcher::~time_watcher () |
175 | #if IOM_SIG |
|
|
176 | sigemptyset (&sigs); |
|
|
177 | |
|
|
178 | if (pipe (sigpipe)) |
|
|
179 | { |
|
|
180 | perror ("io_manager: unable to create signal pipe, aborting."); |
|
|
181 | abort (); |
|
|
182 | } |
|
|
183 | |
|
|
184 | fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC); |
|
|
185 | fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC); |
|
|
186 | #endif |
|
|
187 | |
|
|
188 | #if IOM_CHILD |
|
|
189 | sw0.start (SIGCHLD); |
|
|
190 | #endif |
|
|
191 | |
|
|
192 | #if IOM_TIME |
|
|
193 | io_manager::set_now (); |
|
|
194 | |
|
|
195 | #ifndef IOM_LIBEVENT |
|
|
196 | tw0.start (TSTAMP_MAX); |
|
|
197 | #endif |
|
|
198 | #endif |
|
|
199 | |
|
|
200 | #ifdef IOM_POSTINIT |
|
|
201 | { IOM_POSTINIT } |
|
|
202 | #endif |
|
|
203 | } |
|
|
204 | |
|
|
205 | ~init () |
|
|
206 | { |
|
|
207 | iom_valid = false; |
|
|
208 | } |
|
|
209 | |
|
|
210 | static void required (); |
|
|
211 | } init; |
|
|
212 | |
|
|
213 | void |
|
|
214 | init::required () |
41 | { |
215 | { |
42 | if (iom_valid) |
216 | if (!iom_valid) |
43 | iom.unreg (this); |
217 | { |
44 | |
218 | write (2, "io_manager: early registration attempt, aborting.\n", |
45 | at = TSTAMP_CANCEL; |
219 | sizeof ("io_manager: early registration attempt, aborting.\n") - 1); |
|
|
220 | abort (); |
|
|
221 | } |
46 | } |
222 | } |
47 | |
223 | |
48 | io_watcher::~io_watcher () |
224 | template<class watcher> |
|
|
225 | void io_manager::reg (watcher &w, io_manager_vec<watcher> &queue) |
49 | { |
226 | { |
|
|
227 | init::required (); |
|
|
228 | |
|
|
229 | if (!w.active) |
|
|
230 | { |
|
|
231 | queue.push_back (&w); |
|
|
232 | w.active = queue.size (); |
|
|
233 | } |
|
|
234 | } |
|
|
235 | |
|
|
236 | template<class watcher> |
|
|
237 | void io_manager::unreg (watcher &w, io_manager_vec<watcher> &queue) |
|
|
238 | { |
50 | if (iom_valid) |
239 | if (!iom_valid) |
51 | iom.unreg (this); |
240 | return; |
52 | } |
|
|
53 | |
241 | |
|
|
242 | if (w.active) |
|
|
243 | { |
|
|
244 | queue [w.active - 1] = 0; |
|
|
245 | w.active = 0; |
|
|
246 | } |
|
|
247 | } |
|
|
248 | |
|
|
249 | #if IOM_TIME |
|
|
250 | #ifdef IOM_LIBEVENT |
|
|
251 | void iom_time_c_callback (int fd, short events, void *data) |
|
|
252 | { |
|
|
253 | if (need_set_now) io_manager::set_now (); |
|
|
254 | time_watcher *w = static_cast<time_watcher *>(data); |
|
|
255 | w->call (*w); |
|
|
256 | } |
|
|
257 | |
|
|
258 | void time_watcher::start () |
|
|
259 | { |
|
|
260 | stop (); |
|
|
261 | evtimer_set (&ev, iom_time_c_callback, (void *)this); |
|
|
262 | struct timeval tv; |
|
|
263 | tv.tv_sec = (long)at; |
|
|
264 | tv.tv_usec = (long)((at - (tstamp)tv.tv_sec) * 1000000.); |
|
|
265 | evtimer_add (&ev, &tv); |
|
|
266 | active = 1; |
|
|
267 | } |
|
|
268 | #else |
|
|
269 | void io_manager::reg (time_watcher &w) { io_manager::reg (w, tw); } |
|
|
270 | void io_manager::unreg (time_watcher &w) { io_manager::unreg (w, tw); } |
|
|
271 | #endif |
|
|
272 | |
|
|
273 | void time_watcher::trigger () |
|
|
274 | { |
|
|
275 | call (*this); |
|
|
276 | start (); |
|
|
277 | } |
|
|
278 | #endif |
|
|
279 | |
|
|
280 | #if IOM_IO |
|
|
281 | #ifdef IOM_LIBEVENT |
|
|
282 | void iom_io_c_callback (int fd, short events, void *data) |
|
|
283 | { |
|
|
284 | if (need_set_now) io_manager::set_now (); |
|
|
285 | io_watcher *w = static_cast<io_watcher *>(data); |
|
|
286 | w->call (*w, events); |
|
|
287 | } |
|
|
288 | |
|
|
289 | void io_watcher::set (int fd_, short events_) |
|
|
290 | { |
|
|
291 | if (active) event_del (&ev); |
|
|
292 | fd = fd_; |
|
|
293 | events = events_; |
|
|
294 | event_set (&ev, fd_, events_ | EV_PERSIST, iom_io_c_callback, (void *)this); |
|
|
295 | if (active) event_add (&ev, 0); |
|
|
296 | } |
|
|
297 | #else |
|
|
298 | void io_manager::reg (io_watcher &w) { io_manager::reg (w, iow); } |
|
|
299 | void io_manager::unreg (io_watcher &w) { io_manager::unreg (w, iow); } |
|
|
300 | #endif |
|
|
301 | #endif |
|
|
302 | |
|
|
303 | #if IOM_CHECK |
|
|
304 | void io_manager::reg (check_watcher &w) { io_manager::reg (w, cw); } |
|
|
305 | void io_manager::unreg (check_watcher &w) { io_manager::unreg (w, cw); } |
|
|
306 | #endif |
|
|
307 | |
|
|
308 | #if IOM_IDLE |
|
|
309 | void io_manager::reg (idle_watcher &w) { io_manager::reg (w, iw); } |
|
|
310 | void io_manager::unreg (idle_watcher &w) { io_manager::unreg (w, iw); } |
|
|
311 | #endif |
|
|
312 | |
|
|
313 | #if IOM_SIG |
|
|
314 | static void |
|
|
315 | sighandler (int signum) |
|
|
316 | { |
|
|
317 | sw [signum - 1]->pending = true; |
|
|
318 | |
|
|
319 | // we use a pipe for signal notifications, as most current |
|
|
320 | // OSes (Linux...) do not implement pselect correctly. ugh. |
|
|
321 | char ch = signum; // actual content not used |
|
|
322 | write (sigpipe[1], &ch, 1); |
|
|
323 | } |
|
|
324 | |
54 | void io_manager::reg (io_watcher *w) |
325 | void io_manager::reg (sig_watcher &w) |
55 | { |
326 | { |
56 | if (find (iow.begin (), iow.end (), w) == iow.end ()) |
327 | init::required (); |
|
|
328 | |
|
|
329 | assert (0 < w.signum); |
|
|
330 | |
|
|
331 | sw.reserve (w.signum); |
|
|
332 | |
|
|
333 | while (sw.size () < w.signum) // pathetic |
57 | iow.push_back (w); |
334 | sw.push_back (0); |
58 | } |
|
|
59 | |
335 | |
|
|
336 | sig_vec *&sv = sw[w.signum - 1]; |
|
|
337 | |
|
|
338 | if (!sv) |
|
|
339 | { |
|
|
340 | sv = new sig_vec; |
|
|
341 | |
|
|
342 | sigaddset (&sigs, w.signum); |
|
|
343 | sigprocmask (SIG_BLOCK, &sigs, NULL); |
|
|
344 | |
|
|
345 | struct sigaction sa; |
|
|
346 | sa.sa_handler = sighandler; |
|
|
347 | sigfillset (&sa.sa_mask); |
|
|
348 | sa.sa_flags = SA_RESTART; |
|
|
349 | |
|
|
350 | if (sigaction (w.signum, &sa, 0)) |
|
|
351 | { |
|
|
352 | perror ("io_manager: error while installing signal handler, ignoring."); |
|
|
353 | abort (); |
|
|
354 | } |
|
|
355 | |
|
|
356 | } |
|
|
357 | |
|
|
358 | io_manager::reg (w, *sv); |
|
|
359 | } |
|
|
360 | |
60 | void io_manager::unreg (io_watcher *w) |
361 | void io_manager::unreg (sig_watcher &w) |
61 | { |
362 | { |
62 | iow.erase (find (iow.begin (), iow.end (), w)); |
363 | if (!w.active || !iom_valid) |
63 | } |
364 | return; |
64 | |
365 | |
65 | void io_manager::reg (time_watcher *w) |
366 | assert (0 < w.signum && w.signum <= sw.size ()); |
66 | { |
367 | |
67 | if (find (tw.begin (), tw.end (), w) == tw.end ()) |
368 | io_manager::unreg (w, *sw[w.signum - 1]); |
68 | tw.push_back (w); |
|
|
69 | } |
369 | } |
70 | |
370 | |
71 | void io_manager::unreg (time_watcher *w) |
371 | void sig_watcher::start (int signum) |
72 | { |
372 | { |
73 | tw.erase (find (tw.begin (), tw.end (), w)); |
373 | stop (); |
|
|
374 | this->signum = signum; |
|
|
375 | io_manager::reg (*this); |
74 | } |
376 | } |
|
|
377 | #endif |
75 | |
378 | |
76 | inline void set_now (void) |
379 | #if IOM_CHILD |
77 | { |
380 | void io_manager::reg (child_watcher &w) { io_manager::reg (w, pw); } |
78 | struct timeval tv; |
381 | void io_manager::unreg (child_watcher &w) { io_manager::unreg (w, pw); } |
79 | |
382 | #endif |
80 | gettimeofday (&tv, 0); |
|
|
81 | |
|
|
82 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
|
|
83 | } |
|
|
84 | |
383 | |
85 | void io_manager::loop () |
384 | void io_manager::loop () |
86 | { |
385 | { |
|
|
386 | init::required (); |
|
|
387 | |
|
|
388 | #if IOM_TIME |
87 | set_now (); |
389 | set_now (); |
|
|
390 | #endif |
88 | |
391 | |
89 | for (;;) |
392 | for (;;) |
90 | { |
393 | { |
91 | time_watcher *w; |
394 | #ifndef IOM_LIBEVENT |
92 | |
395 | #if IOM_TIME |
93 | for (;;) |
396 | // call pending time watchers |
94 | { |
397 | { |
95 | w = tw[0]; |
398 | bool activity; |
96 | |
399 | |
97 | for (time_watcher **i = tw.begin (); i != tw.end (); ++i) |
400 | do |
|
|
401 | { |
|
|
402 | activity = false; |
|
|
403 | |
|
|
404 | for (int i = tw.size (); i--; ) |
|
|
405 | if (!tw[i]) |
|
|
406 | tw.erase_unordered (i); |
|
|
407 | else if (tw[i]->at <= NOW) |
|
|
408 | { |
|
|
409 | time_watcher &w = *tw[i]; |
|
|
410 | |
|
|
411 | unreg (w); |
|
|
412 | w.call (w); |
|
|
413 | |
|
|
414 | activity = true; |
|
|
415 | } |
|
|
416 | } |
|
|
417 | while (activity); |
|
|
418 | } |
|
|
419 | #endif |
|
|
420 | #endif |
|
|
421 | |
|
|
422 | #if IOM_CHECK |
|
|
423 | // call all check watchers |
|
|
424 | for (int i = cw.size (); i--; ) |
|
|
425 | if (!cw[i]) |
|
|
426 | cw.erase_unordered (i); |
|
|
427 | else |
|
|
428 | cw[i]->call (*cw[i]); |
|
|
429 | #endif |
|
|
430 | |
|
|
431 | struct TIMEVAL *to = 0; |
|
|
432 | struct TIMEVAL tval; |
|
|
433 | |
|
|
434 | #if IOM_IDLE |
|
|
435 | if (iw.size ()) |
|
|
436 | { |
|
|
437 | tval.tv_sec = 0; |
|
|
438 | tval.TV_FRAC = 0; |
|
|
439 | to = &tval; |
|
|
440 | } |
|
|
441 | else |
|
|
442 | #endif |
|
|
443 | { |
|
|
444 | #ifndef IOM_LIBEVENT |
|
|
445 | #if IOM_TIME |
|
|
446 | // find earliest active watcher |
|
|
447 | time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times |
|
|
448 | |
|
|
449 | for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
98 | if ((*i)->at < w->at) |
450 | if (*i && (*i)->at < next->at) |
99 | w = *i; |
451 | next = *i; |
100 | |
452 | |
101 | if (w->at > NOW) |
453 | if (next->at > NOW && next != tw[0]) |
|
|
454 | { |
|
|
455 | double diff = next->at - NOW; |
|
|
456 | tval.tv_sec = (int)diff; |
|
|
457 | tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT); |
|
|
458 | to = &tval; |
|
|
459 | } |
|
|
460 | #endif |
|
|
461 | #endif |
|
|
462 | } |
|
|
463 | |
|
|
464 | #ifndef IOM_LIBEVENT |
|
|
465 | #if IOM_IO || IOM_SIG |
|
|
466 | fd_set rfd, wfd; |
|
|
467 | |
|
|
468 | FD_ZERO (&rfd); |
|
|
469 | FD_ZERO (&wfd); |
|
|
470 | |
|
|
471 | int fds = 0; |
|
|
472 | |
|
|
473 | #if IOM_IO |
|
|
474 | for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); ) |
|
|
475 | if (*i) |
|
|
476 | { |
|
|
477 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
|
|
478 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
|
|
479 | |
|
|
480 | if ((*i)->fd >= fds) fds = (*i)->fd + 1; |
|
|
481 | } |
|
|
482 | #endif |
|
|
483 | |
|
|
484 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers? |
|
|
485 | break; // no events |
|
|
486 | |
|
|
487 | #if IOM_SIG |
|
|
488 | FD_SET (sigpipe[0], &rfd); |
|
|
489 | if (sigpipe[0] >= fds) fds = sigpipe[0] + 1; |
|
|
490 | #endif |
|
|
491 | |
|
|
492 | #if IOM_SIG |
|
|
493 | // there is no race, as we use a pipe for signals, so select |
|
|
494 | // will return if a signal is caught. |
|
|
495 | sigprocmask (SIG_UNBLOCK, &sigs, NULL); |
|
|
496 | #endif |
|
|
497 | fds = select (fds, &rfd, &wfd, NULL, to); |
|
|
498 | #if IOM_SIG |
|
|
499 | sigprocmask (SIG_BLOCK, &sigs, NULL); |
|
|
500 | #endif |
|
|
501 | #elif IOM_TIME |
|
|
502 | if (!to) |
102 | break; |
503 | break; |
103 | |
504 | |
104 | // call it |
505 | select (0, 0, 0, 0, to); |
105 | w->call (*w); |
506 | #endif |
106 | |
507 | |
107 | if (w->at < 0) |
508 | #if IOM_TIME |
108 | unreg (w); |
|
|
109 | } |
|
|
110 | |
|
|
111 | struct timeval to; |
|
|
112 | double diff = w->at - NOW; |
|
|
113 | to.tv_sec = (int)diff; |
|
|
114 | to.tv_usec = (int)((diff - to.tv_sec) * 1000000); |
|
|
115 | |
|
|
116 | fd_set rfd, wfd; |
|
|
117 | |
|
|
118 | FD_ZERO (&rfd); |
|
|
119 | FD_ZERO (&wfd); |
|
|
120 | |
|
|
121 | int fds = 0; |
|
|
122 | |
|
|
123 | for (io_watcher **w = iow.begin (); w < iow.end (); ++w) |
|
|
124 | { |
|
|
125 | if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd); |
|
|
126 | if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); |
|
|
127 | |
|
|
128 | if ((*w)->fd > fds) fds = (*w)->fd; |
|
|
129 | } |
|
|
130 | |
|
|
131 | fds = select (fds + 1, &rfd, &wfd, 0, &to); |
|
|
132 | |
|
|
133 | set_now (); |
|
|
134 | |
|
|
135 | if (fds > 0) |
|
|
136 | for (io_watcher **w = iow.begin (); w < iow.end (); ++w) |
|
|
137 | { |
509 | { |
138 | short revents = (*w)->events; |
510 | // update time, try to compensate for gross non-monotonic time changes |
|
|
511 | tstamp diff = NOW; |
|
|
512 | set_now (); |
|
|
513 | diff = NOW - diff; |
139 | |
514 | |
140 | if (!FD_ISSET ((*w)->fd, &rfd)) revents &= ~EVENT_READ; |
515 | if (diff < 0) |
141 | if (!FD_ISSET ((*w)->fd, &wfd)) revents &= ~EVENT_WRITE; |
516 | for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
142 | |
517 | if (*i) |
143 | if (revents) |
518 | (*i)->at += diff; |
144 | (*w)->call (**w, revents); |
|
|
145 | } |
519 | } |
146 | } |
520 | #endif |
147 | } |
|
|
148 | |
521 | |
149 | void io_manager::idle_cb (time_watcher &w) |
522 | if (fds > 0) |
150 | { |
523 | { |
151 | w.at = NOW + 1000000000; |
524 | #if IOM_SIG |
152 | } |
525 | if (FD_ISSET (sigpipe[0], &rfd)) |
|
|
526 | { |
|
|
527 | char ch; |
153 | |
528 | |
154 | io_manager::io_manager () |
529 | while (read (sigpipe[0], &ch, 1) > 0) |
155 | { |
530 | ; |
156 | set_now (); |
|
|
157 | |
531 | |
158 | iom_valid = true; |
532 | for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); ) |
|
|
533 | if (*svp && (*svp)->pending) |
|
|
534 | { |
|
|
535 | sig_vec &sv = **svp; |
|
|
536 | for (int i = sv.size (); i--; ) |
|
|
537 | if (!sv[i]) |
|
|
538 | sv.erase_unordered (i); |
|
|
539 | else |
|
|
540 | sv[i]->call (*sv[i]); |
159 | |
541 | |
160 | idle = new time_watcher (this, &io_manager::idle_cb); |
542 | sv.pending = false; |
161 | idle->start (0); |
543 | } |
162 | } |
544 | } |
|
|
545 | #endif |
163 | |
546 | |
164 | io_manager::~io_manager () |
547 | #if IOM_IO |
165 | { |
548 | for (int i = iow.size (); i--; ) |
166 | iom_valid = false; |
549 | if (!iow[i]) |
167 | } |
550 | iow.erase_unordered (i); |
|
|
551 | else |
|
|
552 | { |
|
|
553 | io_watcher &w = *iow[i]; |
|
|
554 | short revents = w.events; |
168 | |
555 | |
|
|
556 | if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ; |
|
|
557 | if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE; |
|
|
558 | |
|
|
559 | if (revents) |
|
|
560 | w.call (w, revents); |
|
|
561 | } |
|
|
562 | #endif |
|
|
563 | } |
|
|
564 | else if (fds < 0 && errno != EINTR) |
|
|
565 | { |
|
|
566 | perror ("io_manager: fatal error while waiting for I/O or time event, aborting."); |
|
|
567 | abort (); |
|
|
568 | } |
|
|
569 | #if IOM_IDLE |
|
|
570 | else |
|
|
571 | for (int i = iw.size (); i--; ) |
|
|
572 | if (!iw[i]) |
|
|
573 | iw.erase_unordered (i); |
|
|
574 | else |
|
|
575 | iw[i]->call (*iw[i]); |
|
|
576 | #endif |
|
|
577 | |
|
|
578 | #else |
|
|
579 | need_set_now = true; |
|
|
580 | |
|
|
581 | if (to) |
|
|
582 | event_loop (EVLOOP_NONBLOCK); |
|
|
583 | else |
|
|
584 | event_loop (EVLOOP_ONCE); |
|
|
585 | |
|
|
586 | if (need_set_now) set_now (); |
|
|
587 | #endif |
|
|
588 | //TODO: IOM_IDLE |
|
|
589 | } |
|
|
590 | } |
|
|
591 | |