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, 2004 Marc Lehmann <pcg@goof.com> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | 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 |
7 | the Free Software Foundation; either version 2 of the License, or |
… | |
… | |
33 | # include <time.h> |
33 | # include <time.h> |
34 | #endif |
34 | #endif |
35 | |
35 | |
36 | // for IOM_SIG |
36 | // for IOM_SIG |
37 | #if IOM_SIG |
37 | #if IOM_SIG |
38 | # include <signal.h> |
38 | # include <csignal> |
39 | # include <fcntl.h> |
39 | # include <fcntl.h> |
40 | #endif |
40 | #endif |
41 | |
41 | |
42 | // if the BSDs would at least be marginally POSIX-compatible.. *sigh* |
42 | // if the BSDs would at least be marginally POSIX-compatible.. *sigh* |
43 | // until that happens, sys/select.h must come last |
43 | // until that happens, sys/select.h must come last |
… | |
… | |
94 | } tw0; |
94 | } tw0; |
95 | |
95 | |
96 | tstamp NOW; |
96 | tstamp NOW; |
97 | |
97 | |
98 | #if IOM_TIME |
98 | #if IOM_TIME |
99 | inline void set_now (void) |
99 | tstamp io_manager::now () |
100 | { |
100 | { |
101 | struct timeval tv; |
101 | struct timeval tv; |
102 | |
102 | |
103 | gettimeofday (&tv, 0); |
103 | gettimeofday (&tv, 0); |
104 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.; |
104 | return (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000.; |
|
|
105 | } |
|
|
106 | |
|
|
107 | void io_manager::set_now () |
|
|
108 | { |
|
|
109 | NOW = now (); |
105 | } |
110 | } |
106 | #endif |
111 | #endif |
107 | |
112 | |
108 | static bool iom_valid; |
113 | static bool iom_valid; |
109 | |
114 | |
… | |
… | |
118 | { |
123 | { |
119 | perror ("io_manager: unable to create signal pipe, aborting."); |
124 | perror ("io_manager: unable to create signal pipe, aborting."); |
120 | abort (); |
125 | abort (); |
121 | } |
126 | } |
122 | |
127 | |
123 | fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); |
128 | fcntl (sigpipe[0], F_SETFL, O_NONBLOCK); fcntl (sigpipe[0], F_SETFD, FD_CLOEXEC); |
124 | fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); |
129 | fcntl (sigpipe[1], F_SETFL, O_NONBLOCK); fcntl (sigpipe[1], F_SETFD, FD_CLOEXEC); |
125 | #endif |
130 | #endif |
126 | |
131 | |
127 | iom_valid = true; |
132 | iom_valid = true; |
128 | |
133 | |
129 | #if IOM_TIME |
134 | #if IOM_TIME |
130 | set_now (); |
135 | io_manager::set_now (); |
131 | |
136 | |
132 | tw0.start (TSTAMP_MAX); |
137 | tw0.start (TSTAMP_MAX); |
133 | #endif |
138 | #endif |
134 | } |
139 | } |
135 | |
140 | |
… | |
… | |
152 | { |
157 | { |
153 | init::required (); |
158 | init::required (); |
154 | |
159 | |
155 | if (!w.active) |
160 | if (!w.active) |
156 | { |
161 | { |
157 | #if IOM_CHECK |
|
|
158 | queue.activity = true; |
|
|
159 | #endif |
|
|
160 | queue.push_back (&w); |
162 | queue.push_back (&w); |
161 | w.active = queue.size (); |
163 | w.active = queue.size (); |
162 | } |
164 | } |
163 | } |
165 | } |
164 | |
166 | |
… | |
… | |
273 | set_now (); |
275 | set_now (); |
274 | #endif |
276 | #endif |
275 | |
277 | |
276 | for (;;) |
278 | for (;;) |
277 | { |
279 | { |
|
|
280 | |
|
|
281 | #if IOM_TIME |
|
|
282 | // call pending time watchers |
|
|
283 | { |
|
|
284 | bool activity; |
|
|
285 | |
|
|
286 | do |
|
|
287 | { |
|
|
288 | activity = false; |
|
|
289 | |
|
|
290 | for (int i = tw.size (); i--; ) |
|
|
291 | if (!tw[i]) |
|
|
292 | tw.erase_unordered (i); |
|
|
293 | else if (tw[i]->at <= NOW) |
|
|
294 | { |
|
|
295 | time_watcher &w = *tw[i]; |
|
|
296 | |
|
|
297 | unreg (w); |
|
|
298 | w.call (w); |
|
|
299 | |
|
|
300 | activity = true; |
|
|
301 | } |
|
|
302 | } |
|
|
303 | while (activity); |
|
|
304 | } |
|
|
305 | #endif |
|
|
306 | |
|
|
307 | #if IOM_CHECK |
|
|
308 | // call all check watchers |
|
|
309 | for (int i = cw.size (); i--; ) |
|
|
310 | if (!cw[i]) |
|
|
311 | cw.erase_unordered (i); |
|
|
312 | else |
|
|
313 | cw[i]->call (*cw[i]); |
|
|
314 | #endif |
|
|
315 | |
278 | struct TIMEVAL *to = 0; |
316 | struct TIMEVAL *to = 0; |
279 | struct TIMEVAL tval; |
317 | struct TIMEVAL tval; |
280 | |
318 | |
281 | #if IOM_IDLE |
319 | #if IOM_IDLE |
282 | if (iw.size ()) |
320 | if (iw.size ()) |
… | |
… | |
287 | } |
325 | } |
288 | else |
326 | else |
289 | #endif |
327 | #endif |
290 | { |
328 | { |
291 | #if IOM_TIME |
329 | #if IOM_TIME |
292 | time_watcher *next; |
330 | // find earliest active watcher |
|
|
331 | time_watcher *next = tw[0]; // the first time-watcher must exist at ALL times |
293 | |
332 | |
294 | for (;;) |
333 | for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
|
|
334 | if (*i && (*i)->at < next->at) |
|
|
335 | next = *i; |
|
|
336 | |
|
|
337 | if (next->at > NOW && next != tw[0]) |
295 | { |
338 | { |
296 | next = tw[0]; // the first time-watcher must exist at ALL times |
|
|
297 | |
|
|
298 | for (int i = tw.size (); i--; ) |
|
|
299 | if (!tw[i]) |
|
|
300 | tw.erase_unordered (i); |
|
|
301 | else if (tw[i]->at < next->at) |
|
|
302 | next = tw[i]; |
|
|
303 | |
|
|
304 | if (next->at > NOW) |
|
|
305 | { |
|
|
306 | if (next != tw[0]) |
|
|
307 | { |
|
|
308 | double diff = next->at - NOW; |
339 | double diff = next->at - NOW; |
309 | tval.tv_sec = (int)diff; |
340 | tval.tv_sec = (int)diff; |
310 | tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT); |
341 | tval.TV_FRAC = (int) ((diff - tval.tv_sec) * TV_MULT); |
311 | to = &tval; |
342 | to = &tval; |
312 | } |
|
|
313 | break; |
|
|
314 | } |
|
|
315 | else |
|
|
316 | { |
|
|
317 | unreg (*next); |
|
|
318 | next->call (*next); |
|
|
319 | } |
|
|
320 | } |
343 | } |
321 | #endif |
|
|
322 | } |
|
|
323 | |
|
|
324 | #if IOM_CHECK |
|
|
325 | tw.activity = false; |
|
|
326 | |
|
|
327 | for (int i = cw.size (); i--; ) |
|
|
328 | if (!cw[i]) |
|
|
329 | cw.erase_unordered (i); |
|
|
330 | else |
|
|
331 | cw[i]->call (*cw[i]); |
|
|
332 | |
|
|
333 | if (tw.activity) |
|
|
334 | { |
|
|
335 | tval.tv_sec = 0; |
|
|
336 | tval.TV_FRAC = 0; |
|
|
337 | to = &tval; |
|
|
338 | } |
344 | } |
339 | #endif |
345 | #endif |
340 | |
346 | |
341 | #if IOM_IO || IOM_SIG |
347 | #if IOM_IO || IOM_SIG |
342 | fd_set rfd, wfd; |
348 | fd_set rfd, wfd; |
… | |
… | |
345 | FD_ZERO (&wfd); |
351 | FD_ZERO (&wfd); |
346 | |
352 | |
347 | int fds = 0; |
353 | int fds = 0; |
348 | |
354 | |
349 | # if IOM_IO |
355 | # if IOM_IO |
350 | for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); ) |
356 | for (io_manager_vec<io_watcher>::const_iterator i = iow.end (); i-- > iow.begin (); ) |
351 | if (*i) |
357 | if (*i) |
352 | { |
358 | { |
353 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
359 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
354 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
360 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
355 | |
361 | |
356 | if ((*i)->fd >= fds) fds = (*i)->fd + 1; |
362 | if ((*i)->fd >= fds) fds = (*i)->fd + 1; |
357 | } |
363 | } |
358 | # endif |
364 | # endif |
359 | |
365 | |
360 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
366 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers? |
361 | break; // no events |
367 | break; // no events |
362 | |
368 | |
363 | # if IOM_SIG |
369 | # if IOM_SIG |
364 | FD_SET (sigpipe[0], &rfd); |
370 | FD_SET (sigpipe[0], &rfd); |
365 | if (sigpipe[0] >= fds) fds = sigpipe[0] + 1; |
371 | if (sigpipe[0] >= fds) fds = sigpipe[0] + 1; |
… | |
… | |
374 | # if IOM_SIG |
380 | # if IOM_SIG |
375 | sigprocmask (SIG_BLOCK, &sigs, NULL); |
381 | sigprocmask (SIG_BLOCK, &sigs, NULL); |
376 | # endif |
382 | # endif |
377 | |
383 | |
378 | # if IOM_TIME |
384 | # if IOM_TIME |
|
|
385 | { |
|
|
386 | // update time, try to compensate for gross non-monotonic time changes |
|
|
387 | tstamp diff = NOW; |
379 | set_now (); |
388 | set_now (); |
|
|
389 | diff = NOW - diff; |
|
|
390 | |
|
|
391 | if (diff < 0) |
|
|
392 | for (io_manager_vec<time_watcher>::const_iterator i = tw.end (); i-- > tw.begin (); ) |
|
|
393 | if (*i) |
|
|
394 | (*i)->at += diff; |
|
|
395 | } |
380 | # endif |
396 | # endif |
381 | |
397 | |
382 | if (fds > 0) |
398 | if (fds > 0) |
383 | { |
399 | { |
384 | # if IOM_SIG |
400 | # if IOM_SIG |
… | |
… | |
387 | char ch; |
403 | char ch; |
388 | |
404 | |
389 | while (read (sigpipe[0], &ch, 1) > 0) |
405 | while (read (sigpipe[0], &ch, 1) > 0) |
390 | ; |
406 | ; |
391 | |
407 | |
392 | for (sig_vec **svp = sw.end (); svp-- > sw.begin (); ) |
408 | for (vector<sig_vec *>::iterator svp = sw.end (); svp-- > sw.begin (); ) |
393 | if (*svp && (*svp)->pending) |
409 | if (*svp && (*svp)->pending) |
394 | { |
410 | { |
395 | sig_vec &sv = **svp; |
411 | sig_vec &sv = **svp; |
396 | for (int i = sv.size (); i--; ) |
412 | for (int i = sv.size (); i--; ) |
397 | if (!sv[i]) |
413 | if (!sv[i]) |
… | |
… | |
408 | for (int i = iow.size (); i--; ) |
424 | for (int i = iow.size (); i--; ) |
409 | if (!iow[i]) |
425 | if (!iow[i]) |
410 | iow.erase_unordered (i); |
426 | iow.erase_unordered (i); |
411 | else |
427 | else |
412 | { |
428 | { |
|
|
429 | io_watcher &w = *iow[i]; |
413 | short revents = iow[i]->events; |
430 | short revents = w.events; |
414 | |
431 | |
415 | if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ; |
432 | if (!FD_ISSET (w.fd, &rfd)) revents &= ~EVENT_READ; |
416 | if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE; |
433 | if (!FD_ISSET (w.fd, &wfd)) revents &= ~EVENT_WRITE; |
417 | |
434 | |
418 | if (revents) |
435 | if (revents) |
419 | iow[i]->call (*iow[i], revents); |
436 | w.call (w, revents); |
420 | } |
437 | } |
421 | #endif |
438 | #endif |
422 | } |
439 | } |
423 | else if (fds < 0 && errno != EINTR) |
440 | else if (fds < 0 && errno != EINTR) |
424 | { |
441 | { |