… | |
… | |
31 | inline bool lowest_first (const time_watcher *a, const time_watcher *b) |
31 | inline bool lowest_first (const time_watcher *a, const time_watcher *b) |
32 | { |
32 | { |
33 | return a->at > b->at; |
33 | return a->at > b->at; |
34 | } |
34 | } |
35 | |
35 | |
36 | timestamp NOW; |
36 | tstamp NOW; |
37 | |
37 | |
38 | io_manager iom; |
38 | io_manager iom; |
39 | |
39 | |
40 | void time_watcher::set (timestamp when) |
40 | void time_watcher::set (tstamp when) |
41 | { |
41 | { |
42 | iom.unreg (this); |
42 | iom.unreg (this); |
43 | at = when; |
43 | at = when; |
44 | iom.reg (this); |
44 | iom.reg (this); |
45 | } |
45 | } |
… | |
… | |
110 | { |
110 | { |
111 | struct timeval tv; |
111 | struct timeval tv; |
112 | |
112 | |
113 | gettimeofday (&tv, 0); |
113 | gettimeofday (&tv, 0); |
114 | |
114 | |
115 | NOW = (timestamp)tv.tv_sec + (timestamp)tv.tv_usec / 1000000; |
115 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
116 | } |
116 | } |
117 | |
117 | |
118 | void io_manager::loop () |
118 | void io_manager::loop () |
119 | { |
119 | { |
120 | set_now (); |
120 | set_now (); |
121 | |
121 | |
122 | for (;;) |
122 | while (!(iow.empty () && tw.empty ())) |
123 | { |
123 | { |
124 | int timeout = tw.empty () ? -1 : (int) ((tw[0]->at - NOW) * 1000); |
124 | int timeout = tw.empty () |
|
|
125 | ? 3600 * 1000 // wake up at least every hour |
|
|
126 | : (int) ((tw[0]->at - NOW) * 1000); |
125 | |
127 | |
126 | //printf ("s%d t%d #%d <%f<%f<\n", pfs.size (), timeout, tw.size (), tw[0]->at - NOW, tw[1]->at - NOW); |
128 | printf ("s%d t%d #%d\n", pfs.size (), timeout, tw.size ()); |
127 | |
129 | |
128 | if (timeout >= 0) |
130 | if (timeout >= 0) |
129 | { |
131 | { |
130 | int fds = poll (&pfs[0], pfs.size (), timeout); |
132 | int fds = poll (&pfs[0], pfs.size (), timeout); |
131 | |
133 | |
… | |
… | |
140 | } |
142 | } |
141 | |
143 | |
142 | while (!tw.empty () && tw[0]->at <= NOW) |
144 | while (!tw.empty () && tw[0]->at <= NOW) |
143 | { |
145 | { |
144 | pop_heap (tw.begin (), tw.end (), lowest_first); |
146 | pop_heap (tw.begin (), tw.end (), lowest_first); |
145 | time_watcher *w = tw[tw.size () - 1]; |
147 | (*(tw.end () - 1))->trigger (); |
146 | w->call (w->at); |
|
|
147 | push_heap (tw.begin (), tw.end (), lowest_first); |
148 | push_heap (tw.begin (), tw.end (), lowest_first); |
148 | } |
149 | } |
149 | } |
150 | } |
150 | } |
151 | } |
151 | |
152 | |