1 | #include <unistd.h> |
|
|
2 | /* |
1 | /* |
3 | iom.C -- I/O multiplexor |
2 | iom.C -- I/O multiplexor |
4 | |
3 | |
5 | This program is free software; you can redistribute it and/or modify |
4 | 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 |
5 | it under the terms of the GNU General Public License as published by |
… | |
… | |
22 | #include <sys/time.h> |
21 | #include <sys/time.h> |
23 | |
22 | |
24 | #include <algorithm> |
23 | #include <algorithm> |
25 | #include <functional> |
24 | #include <functional> |
26 | |
25 | |
|
|
26 | #include "gettext.h" |
|
|
27 | |
27 | #include "slog.h" |
28 | #include "slog.h" |
28 | |
|
|
29 | #include "iom.h" |
29 | #include "iom.h" |
30 | |
30 | |
|
|
31 | tstamp NOW; |
|
|
32 | bool iom_valid; |
|
|
33 | io_manager iom; |
|
|
34 | |
31 | 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) |
32 | { |
36 | { |
33 | return a->at > b->at; |
37 | return a->at > b->at; |
34 | } |
38 | } |
35 | |
39 | |
36 | tstamp NOW; |
|
|
37 | |
|
|
38 | io_manager iom; |
|
|
39 | |
|
|
40 | void time_watcher::set (tstamp when) |
40 | void time_watcher::set (tstamp when) |
41 | { |
41 | { |
42 | iom.unreg (this); |
|
|
43 | at = when; |
42 | at = when; |
|
|
43 | |
|
|
44 | if (registered) |
|
|
45 | iom.reschedule_time_watchers (); |
|
|
46 | } |
|
|
47 | |
|
|
48 | void time_watcher::trigger () |
|
|
49 | { |
|
|
50 | call (*this); |
|
|
51 | |
|
|
52 | if (registered) |
|
|
53 | iom.reschedule_time_watchers (); |
|
|
54 | else |
44 | iom.reg (this); |
55 | iom.reg (this); |
45 | } |
56 | } |
46 | |
57 | |
47 | void io_manager::reg (int fd, short events, io_watcher *w) |
58 | time_watcher::~time_watcher () |
48 | { |
59 | { |
49 | pollfd pfd; |
60 | if (iom_valid) |
|
|
61 | iom.unreg (this); |
|
|
62 | } |
50 | |
63 | |
51 | pfd.fd = fd; |
64 | void io_watcher::set(int fd_, short events_) |
|
|
65 | { |
|
|
66 | fd = fd_; |
52 | pfd.events = events; |
67 | events = events_; |
53 | |
68 | |
|
|
69 | if (registered) |
|
|
70 | { |
|
|
71 | iom.unreg (this); |
|
|
72 | iom.reg (this); |
|
|
73 | } |
|
|
74 | } |
|
|
75 | |
|
|
76 | io_watcher::~io_watcher () |
|
|
77 | { |
|
|
78 | if (iom_valid) |
|
|
79 | iom.unreg (this); |
|
|
80 | } |
|
|
81 | |
|
|
82 | void io_manager::reg (io_watcher *w) |
|
|
83 | { |
|
|
84 | if (!w->registered) |
|
|
85 | { |
|
|
86 | w->registered = true; |
|
|
87 | |
|
|
88 | pollfd pfd; |
|
|
89 | |
|
|
90 | pfd.fd = w->fd; |
|
|
91 | pfd.events = w->events; |
|
|
92 | |
54 | pfs.push_back (pfd); |
93 | pfs.push_back (pfd); |
55 | iow.push_back (w); |
94 | iow.push_back (w); |
|
|
95 | } |
56 | } |
96 | } |
57 | |
97 | |
58 | void io_manager::unreg (io_watcher *w) |
98 | void io_manager::unreg (io_watcher *w) |
59 | { |
99 | { |
|
|
100 | if (w->registered) |
|
|
101 | { |
|
|
102 | w->registered = false; |
|
|
103 | |
60 | unsigned int sz = iow.size (); |
104 | unsigned int sz = iow.size (); |
61 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
105 | unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
62 | |
106 | |
63 | if (i != sz) |
107 | assert (i != sz); |
64 | { |
108 | |
65 | if (sz == 1) |
109 | if (sz == 1) |
66 | { |
110 | { |
67 | pfs.clear (); |
111 | pfs.clear (); |
68 | iow.clear (); |
112 | iow.clear (); |
69 | } |
113 | } |
… | |
… | |
78 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
122 | pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
79 | } |
123 | } |
80 | } |
124 | } |
81 | } |
125 | } |
82 | |
126 | |
|
|
127 | void io_manager::reschedule_time_watchers () |
|
|
128 | { |
|
|
129 | make_heap (tw.begin (), tw.end (), earliest_first); |
|
|
130 | } |
|
|
131 | |
83 | void io_manager::reg (time_watcher *w) |
132 | void io_manager::reg (time_watcher *w) |
84 | { |
133 | { |
|
|
134 | if (!w->registered) |
|
|
135 | { |
|
|
136 | w->registered = true; |
|
|
137 | |
85 | tw.push_back (w); |
138 | tw.push_back (w); |
86 | push_heap (tw.begin (), tw.end (), lowest_first); |
139 | push_heap (tw.begin (), tw.end (), earliest_first); |
|
|
140 | } |
87 | } |
141 | } |
88 | |
142 | |
89 | void io_manager::unreg (time_watcher *w) |
143 | void io_manager::unreg (time_watcher *w) |
90 | { |
144 | { |
|
|
145 | if (w->registered) |
|
|
146 | { |
|
|
147 | w->registered = false; |
|
|
148 | |
91 | unsigned int sz = tw.size (); |
149 | unsigned int sz = tw.size (); |
92 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
150 | unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
93 | |
151 | |
94 | if (i != sz) |
152 | assert (i != sz); |
95 | { |
|
|
96 | if (sz == 1) |
|
|
97 | tw.clear (); |
|
|
98 | else |
|
|
99 | { |
153 | |
100 | if (i != sz - 1) |
154 | if (i != sz - 1) |
101 | tw[i] = tw[sz - 1]; |
155 | tw[i] = tw[sz - 1]; |
102 | |
156 | |
|
|
157 | tw.pop_back (); |
|
|
158 | reschedule_time_watchers (); |
|
|
159 | } |
|
|
160 | } |
|
|
161 | |
|
|
162 | inline void set_now (void) |
|
|
163 | { |
|
|
164 | struct timeval tv; |
|
|
165 | |
|
|
166 | gettimeofday (&tv, 0); |
|
|
167 | |
|
|
168 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
|
|
169 | } |
|
|
170 | |
|
|
171 | void io_manager::loop () |
|
|
172 | { |
|
|
173 | set_now (); |
|
|
174 | |
|
|
175 | for (;;) |
|
|
176 | { |
|
|
177 | while (tw[0]->at <= NOW) |
|
|
178 | { |
|
|
179 | // remove the first watcher |
|
|
180 | time_watcher *w = tw[0]; |
|
|
181 | |
|
|
182 | pop_heap (tw.begin (), tw.end (), earliest_first); |
103 | tw.pop_back (); |
183 | tw.pop_back (); |
104 | make_heap (tw.begin (), tw.end (), lowest_first); |
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); |
|
|
193 | } |
|
|
194 | |
|
|
195 | int timeout = (int) ((tw[0]->at - NOW) * 1000); |
|
|
196 | |
|
|
197 | int fds = poll (&pfs[0], pfs.size (), timeout); |
|
|
198 | |
|
|
199 | set_now (); |
|
|
200 | |
|
|
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) |
|
|
207 | if (p->revents) |
|
|
208 | { |
|
|
209 | --fds; |
|
|
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); |
105 | } |
218 | } |
106 | } |
219 | } |
107 | } |
220 | } |
108 | |
221 | |
109 | inline void set_now (void) |
222 | void io_manager::idle_cb (time_watcher &w) |
110 | { |
223 | { |
111 | struct timeval tv; |
224 | w.at = NOW + 86400; // wake up every day, for no good reason |
112 | |
|
|
113 | gettimeofday (&tv, 0); |
|
|
114 | |
|
|
115 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
|
|
116 | } |
225 | } |
117 | |
226 | |
118 | void io_manager::loop () |
227 | io_manager::io_manager () |
119 | { |
228 | { |
|
|
229 | iom_valid = true; |
|
|
230 | |
120 | set_now (); |
231 | set_now (); |
121 | |
232 | idle = new time_watcher (this, &io_manager::idle_cb); |
122 | while (!(iow.empty () && tw.empty ())) |
233 | idle->start (0); |
123 | { |
|
|
124 | int timeout = tw.empty () |
|
|
125 | ? 3600 * 1000 // wake up at least every hour |
|
|
126 | : (int) ((tw[0]->at - NOW) * 1000); |
|
|
127 | |
|
|
128 | printf ("s%d t%d #%d\n", pfs.size (), timeout, tw.size ()); |
|
|
129 | |
|
|
130 | if (timeout >= 0) |
|
|
131 | { |
|
|
132 | int fds = poll (&pfs[0], pfs.size (), timeout); |
|
|
133 | |
|
|
134 | set_now (); |
|
|
135 | |
|
|
136 | for (unsigned int i = iow.size (); fds && i--; ) |
|
|
137 | if (pfs[i].revents) |
|
|
138 | { |
|
|
139 | --fds; |
|
|
140 | iow[i]->call (pfs[i].revents); |
|
|
141 | } |
|
|
142 | } |
|
|
143 | |
|
|
144 | while (!tw.empty () && tw[0]->at <= NOW) |
|
|
145 | { |
|
|
146 | pop_heap (tw.begin (), tw.end (), lowest_first); |
|
|
147 | (*(tw.end () - 1))->trigger (); |
|
|
148 | push_heap (tw.begin (), tw.end (), lowest_first); |
|
|
149 | } |
|
|
150 | } |
|
|
151 | } |
|
|
152 | |
|
|
153 | io_manager::io_manager () |
|
|
154 | { |
|
|
155 | set_now (); |
|
|
156 | } |
234 | } |
157 | |
235 | |
158 | io_manager::~io_manager () |
236 | io_manager::~io_manager () |
159 | { |
237 | { |
160 | // |
238 | iom_valid = false; |
161 | } |
239 | } |
162 | |
240 | |