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