… | |
… | |
37 | |
37 | |
38 | void time_watcher::set (tstamp when) |
38 | void time_watcher::set (tstamp when) |
39 | { |
39 | { |
40 | iom.unreg (this); |
40 | iom.unreg (this); |
41 | at = when; |
41 | at = when; |
|
|
42 | iom.reg (this); |
|
|
43 | } |
|
|
44 | |
|
|
45 | void time_watcher::trigger () |
|
|
46 | { |
|
|
47 | iom.unreg (this); |
|
|
48 | call (at); |
|
|
49 | iom.reg (this); |
|
|
50 | } |
|
|
51 | |
|
|
52 | void time_watcher::start () |
|
|
53 | { |
|
|
54 | iom.unreg (this); |
42 | iom.reg (this); |
55 | iom.reg (this); |
43 | } |
56 | } |
44 | |
57 | |
45 | void io_manager::reg (int fd, short events, io_watcher *w) |
58 | void io_manager::reg (int fd, short events, io_watcher *w) |
46 | { |
59 | { |
… | |
… | |
89 | unsigned int sz = tw.size (); |
102 | unsigned int sz = tw.size (); |
90 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
103 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
91 | |
104 | |
92 | if (i != sz) |
105 | if (i != sz) |
93 | { |
106 | { |
94 | if (sz == 1) |
|
|
95 | tw.clear (); |
|
|
96 | else |
|
|
97 | { |
|
|
98 | if (i != sz - 1) |
107 | if (i != sz - 1) |
99 | tw[i] = tw[sz - 1]; |
108 | tw[i] = tw[sz - 1]; |
100 | |
109 | |
101 | tw.pop_back (); |
110 | tw.pop_back (); |
102 | make_heap (tw.begin (), tw.end (), lowest_first); |
111 | make_heap (tw.begin (), tw.end (), lowest_first); |
103 | } |
|
|
104 | } |
112 | } |
105 | } |
113 | } |
106 | |
114 | |
107 | inline void set_now (void) |
115 | inline void set_now (void) |
108 | { |
116 | { |
… | |
… | |
115 | |
123 | |
116 | void io_manager::loop () |
124 | void io_manager::loop () |
117 | { |
125 | { |
118 | set_now (); |
126 | set_now (); |
119 | |
127 | |
120 | while (!(iow.empty () && tw.empty ())) |
128 | for (;;) |
121 | { |
129 | { |
122 | int timeout = tw.empty () |
130 | while (tw[0]->at <= NOW) |
123 | ? 3600 * 1000 // wake up at least every hour |
131 | { |
124 | : (int) ((tw[0]->at - NOW) * 1000); |
132 | pop_heap (tw.begin (), tw.end (), lowest_first); |
|
|
133 | time_watcher *w = *(tw.end () - 1); |
125 | |
134 | |
126 | printf ("s%d t%d #%d\n", pfs.size (), timeout, tw.size ()); |
135 | if (w->at >= 0) |
127 | |
|
|
128 | if (timeout >= 0) |
|
|
129 | { |
|
|
130 | int fds = poll (&pfs[0], pfs.size (), timeout); |
|
|
131 | |
|
|
132 | set_now (); |
|
|
133 | |
|
|
134 | for (unsigned int i = iow.size (); fds && i--; ) |
|
|
135 | if (pfs[i].revents) |
|
|
136 | { |
136 | { |
137 | --fds; |
137 | w->call (w->at); |
138 | iow[i]->call (pfs[i].revents); |
138 | push_heap (tw.begin (), tw.end (), lowest_first); |
139 | } |
139 | } |
|
|
140 | else |
|
|
141 | tw.pop_back (); |
140 | } |
142 | } |
141 | |
143 | |
142 | while (!tw.empty () && tw[0]->at <= NOW) |
144 | int timeout = (int) ((tw[0]->at - NOW) * 1000); |
|
|
145 | |
|
|
146 | int fds = poll (&pfs[0], pfs.size (), timeout); |
|
|
147 | |
|
|
148 | set_now (); |
|
|
149 | |
|
|
150 | for (unsigned int i = iow.size (); fds > 0 && i--; ) |
|
|
151 | if (pfs[i].revents) |
143 | { |
152 | { |
144 | pop_heap (tw.begin (), tw.end (), lowest_first); |
153 | --fds; |
145 | (*(tw.end () - 1))->trigger (); |
154 | iow[i]->call (pfs[i].revents); |
146 | push_heap (tw.begin (), tw.end (), lowest_first); |
|
|
147 | } |
155 | } |
148 | } |
156 | } |
|
|
157 | } |
|
|
158 | |
|
|
159 | void io_manager::idle_cb (tstamp &ts) |
|
|
160 | { |
|
|
161 | ts = NOW + 86400; // wake up every day, for no good reason |
149 | } |
162 | } |
150 | |
163 | |
151 | io_manager::io_manager () |
164 | io_manager::io_manager () |
152 | { |
165 | { |
153 | set_now (); |
166 | set_now (); |
|
|
167 | idle = new time_watcher (this, &io_manager::idle_cb); |
|
|
168 | idle->start (0); |
154 | } |
169 | } |
155 | |
170 | |
156 | io_manager::~io_manager () |
171 | io_manager::~io_manager () |
157 | { |
172 | { |
158 | // |
173 | // |