… | |
… | |
21 | #include <sys/time.h> |
21 | #include <sys/time.h> |
22 | |
22 | |
23 | #include <algorithm> |
23 | #include <algorithm> |
24 | #include <functional> |
24 | #include <functional> |
25 | |
25 | |
|
|
26 | #include "gettext.h" |
|
|
27 | |
26 | #include "slog.h" |
28 | #include "slog.h" |
27 | #include "iom.h" |
29 | #include "iom.h" |
28 | |
30 | |
|
|
31 | tstamp NOW; |
|
|
32 | bool iom_valid; |
|
|
33 | io_manager iom; |
|
|
34 | |
29 | inline bool lowest_first (const time_watcher *a, const time_watcher *b) |
35 | inline bool earliest_first (const time_watcher *a, const time_watcher *b) |
30 | { |
36 | { |
31 | return a->at > b->at; |
37 | return a->at > b->at; |
32 | } |
38 | } |
33 | |
39 | |
34 | tstamp NOW; |
|
|
35 | |
|
|
36 | io_manager iom; |
|
|
37 | |
|
|
38 | void time_watcher::set (tstamp when) |
40 | void time_watcher::set (tstamp when) |
39 | { |
41 | { |
40 | iom.unreg (this); |
|
|
41 | at = when; |
42 | at = when; |
42 | iom.reg (this); |
43 | |
|
|
44 | if (registered) |
|
|
45 | iom.reschedule_time_watchers (); |
43 | } |
46 | } |
44 | |
47 | |
45 | void time_watcher::trigger () |
48 | void time_watcher::trigger () |
46 | { |
49 | { |
|
|
50 | call (*this); |
|
|
51 | |
|
|
52 | if (registered) |
|
|
53 | iom.reschedule_time_watchers (); |
|
|
54 | else |
|
|
55 | iom.reg (this); |
|
|
56 | } |
|
|
57 | |
|
|
58 | time_watcher::~time_watcher () |
|
|
59 | { |
|
|
60 | if (iom_valid) |
47 | iom.unreg (this); |
61 | iom.unreg (this); |
48 | call (at); |
62 | } |
|
|
63 | |
|
|
64 | void io_watcher::set(int fd_, short events_) |
|
|
65 | { |
|
|
66 | fd = fd_; |
|
|
67 | events = events_; |
|
|
68 | |
|
|
69 | if (registered) |
|
|
70 | { |
|
|
71 | iom.unreg (this); |
49 | iom.reg (this); |
72 | iom.reg (this); |
|
|
73 | } |
50 | } |
74 | } |
51 | |
75 | |
52 | void time_watcher::start () |
76 | io_watcher::~io_watcher () |
53 | { |
77 | { |
|
|
78 | if (iom_valid) |
54 | iom.unreg (this); |
79 | iom.unreg (this); |
55 | iom.reg (this); |
|
|
56 | } |
80 | } |
57 | |
81 | |
58 | void io_manager::reg (int fd, short events, io_watcher *w) |
82 | void io_manager::reg (io_watcher *w) |
59 | { |
83 | { |
|
|
84 | if (!w->registered) |
|
|
85 | { |
|
|
86 | w->registered = true; |
|
|
87 | |
60 | pollfd pfd; |
88 | pollfd pfd; |
61 | |
89 | |
62 | pfd.fd = fd; |
90 | pfd.fd = w->fd; |
63 | pfd.events = events; |
91 | pfd.events = w->events; |
64 | |
92 | |
65 | pfs.push_back (pfd); |
93 | pfs.push_back (pfd); |
66 | iow.push_back (w); |
94 | iow.push_back (w); |
|
|
95 | } |
67 | } |
96 | } |
68 | |
97 | |
69 | void io_manager::unreg (const io_watcher *w) |
98 | void io_manager::unreg (io_watcher *w) |
70 | { |
99 | { |
|
|
100 | if (w->registered) |
|
|
101 | { |
|
|
102 | w->registered = false; |
|
|
103 | |
71 | unsigned int sz = iow.size (); |
104 | unsigned int sz = iow.size (); |
72 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
105 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
73 | |
106 | |
74 | if (i != sz) |
107 | assert (i != sz); |
75 | { |
108 | |
76 | if (sz == 1) |
109 | if (sz == 1) |
77 | { |
110 | { |
78 | pfs.clear (); |
111 | pfs.clear (); |
79 | iow.clear (); |
112 | iow.clear (); |
80 | } |
113 | } |
… | |
… | |
89 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
122 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
90 | } |
123 | } |
91 | } |
124 | } |
92 | } |
125 | } |
93 | |
126 | |
|
|
127 | void io_manager::reschedule_time_watchers () |
|
|
128 | { |
|
|
129 | make_heap (tw.begin (), tw.end (), earliest_first); |
|
|
130 | } |
|
|
131 | |
94 | void io_manager::reg (time_watcher *w) |
132 | void io_manager::reg (time_watcher *w) |
95 | { |
133 | { |
|
|
134 | if (!w->registered) |
|
|
135 | { |
|
|
136 | w->registered = true; |
|
|
137 | |
96 | tw.push_back (w); |
138 | tw.push_back (w); |
97 | push_heap (tw.begin (), tw.end (), lowest_first); |
139 | push_heap (tw.begin (), tw.end (), earliest_first); |
|
|
140 | } |
98 | } |
141 | } |
99 | |
142 | |
100 | void io_manager::unreg (const time_watcher *w) |
143 | void io_manager::unreg (time_watcher *w) |
101 | { |
144 | { |
|
|
145 | if (w->registered) |
|
|
146 | { |
|
|
147 | w->registered = false; |
|
|
148 | |
102 | unsigned int sz = tw.size (); |
149 | unsigned int sz = tw.size (); |
103 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
150 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
104 | |
151 | |
105 | if (i != sz) |
152 | assert (i != sz); |
106 | { |
153 | |
107 | if (i != sz - 1) |
154 | if (i != sz - 1) |
108 | tw[i] = tw[sz - 1]; |
155 | tw[i] = tw[sz - 1]; |
109 | |
156 | |
110 | tw.pop_back (); |
157 | tw.pop_back (); |
111 | make_heap (tw.begin (), tw.end (), lowest_first); |
158 | reschedule_time_watchers (); |
112 | } |
159 | } |
113 | } |
160 | } |
114 | |
161 | |
115 | inline void set_now (void) |
162 | inline void set_now (void) |
116 | { |
163 | { |
… | |
… | |
127 | |
174 | |
128 | for (;;) |
175 | for (;;) |
129 | { |
176 | { |
130 | while (tw[0]->at <= NOW) |
177 | while (tw[0]->at <= NOW) |
131 | { |
178 | { |
|
|
179 | // remove the first watcher |
|
|
180 | time_watcher *w = tw[0]; |
|
|
181 | |
132 | pop_heap (tw.begin (), tw.end (), lowest_first); |
182 | pop_heap (tw.begin (), tw.end (), earliest_first); |
133 | time_watcher *w = *(tw.end () - 1); |
|
|
134 | |
|
|
135 | if (w->at >= 0) |
|
|
136 | { |
|
|
137 | w->call (w->at); |
|
|
138 | push_heap (tw.begin (), tw.end (), lowest_first); |
|
|
139 | } |
|
|
140 | else |
|
|
141 | tw.pop_back (); |
183 | tw.pop_back (); |
|
|
184 | |
|
|
185 | w->registered = false; |
|
|
186 | |
|
|
187 | // call it |
|
|
188 | w->call (*w); |
|
|
189 | |
|
|
190 | // re-add it if necessary |
|
|
191 | if (w->at >= 0 && !w->registered) |
|
|
192 | reg (w); |
142 | } |
193 | } |
143 | |
194 | |
144 | int timeout = (int) ((tw[0]->at - NOW) * 1000); |
195 | int timeout = (int) ((tw[0]->at - NOW) * 1000); |
145 | |
196 | |
146 | int fds = poll (&pfs[0], pfs.size (), timeout); |
197 | int fds = poll (&pfs[0], pfs.size (), timeout); |
147 | |
198 | |
148 | set_now (); |
199 | set_now (); |
149 | |
200 | |
150 | for (unsigned int i = iow.size (); fds > 0 && i--; ) |
201 | vector<io_watcher *>::iterator w; |
|
|
202 | vector<pollfd>::iterator p; |
|
|
203 | |
|
|
204 | for (w = iow.begin (), p = pfs.begin (); |
|
|
205 | fds > 0 && w < iow.end (); |
|
|
206 | ++w, ++p) |
151 | if (pfs[i].revents) |
207 | if (p->revents) |
152 | { |
208 | { |
153 | --fds; |
209 | --fds; |
154 | iow[i]->call (pfs[i].revents); |
210 | |
|
|
211 | if (p->revents & POLLNVAL) |
|
|
212 | { |
|
|
213 | slog (L_ERR, _("io_watcher started on illegal file descriptor, disabling.")); |
|
|
214 | (*w)->stop (); |
|
|
215 | } |
|
|
216 | else |
|
|
217 | (*w)->call (**w, p->revents); |
155 | } |
218 | } |
156 | } |
219 | } |
157 | } |
220 | } |
158 | |
221 | |
159 | void io_manager::idle_cb (tstamp &ts) |
222 | void io_manager::idle_cb (time_watcher &w) |
160 | { |
223 | { |
161 | ts = NOW + 86400; // wake up every day, for no good reason |
224 | w.at = NOW + 86400; // wake up every day, for no good reason |
162 | } |
225 | } |
163 | |
226 | |
164 | io_manager::io_manager () |
227 | io_manager::io_manager () |
165 | { |
228 | { |
|
|
229 | iom_valid = true; |
|
|
230 | |
166 | set_now (); |
231 | set_now (); |
167 | idle = new time_watcher (this, &io_manager::idle_cb); |
232 | idle = new time_watcher (this, &io_manager::idle_cb); |
168 | idle->start (0); |
233 | idle->start (0); |
169 | } |
234 | } |
170 | |
235 | |
171 | io_manager::~io_manager () |
236 | io_manager::~io_manager () |
172 | { |
237 | { |
173 | // |
238 | iom_valid = false; |
174 | } |
239 | } |
175 | |
240 | |