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