… | |
… | |
21 | #include <cstdlib> |
21 | #include <cstdlib> |
22 | #include <cerrno> |
22 | #include <cerrno> |
23 | |
23 | |
24 | #include <sys/time.h> |
24 | #include <sys/time.h> |
25 | |
25 | |
|
|
26 | #include <assert.h> |
|
|
27 | |
26 | #if 1 // older unices need these includes for select(2) |
28 | #if 1 // older unices need these includes for select (2) |
27 | # include <unistd.h> |
29 | # include <unistd.h> |
28 | # include <sys/types.h> |
30 | # include <sys/types.h> |
29 | #endif |
31 | #endif |
30 | |
32 | |
31 | // if the BSDs would at least be marginally POSIX-compatible.. *sigh* |
33 | // if the BSDs would at least be marginally POSIX-compatible.. *sigh* |
32 | // until that happens, sys/select.h must come last |
34 | // until that happens, sys/select.h must come last |
33 | #include <sys/select.h> |
35 | #include <sys/select.h> |
|
|
36 | |
|
|
37 | // for IOM_SIG |
|
|
38 | #include <signal.h> |
34 | |
39 | |
35 | #include "iom.h" |
40 | #include "iom.h" |
36 | |
41 | |
37 | // TSTAMP_MAX must still fit into a positive struct timeval |
42 | // TSTAMP_MAX must still fit into a positive struct timeval |
38 | #define TSTAMP_MAX (double)(1UL<<31) |
43 | #define TSTAMP_MAX (double)(1UL<<31) |
39 | |
44 | |
40 | // this is a dummy time watcher to ensure that the first |
45 | // this is a dummy time watcher to ensure that the first |
41 | // time watcher is _always_ valid, this gets rid of a lot |
46 | // time watcher is _always_ valid, this gets rid of a lot |
42 | // of null-pointer-checks |
47 | // of null-pointer-checks |
43 | // (must come _before_ iom is being defined) |
48 | // (must come _before_ iom is being defined) |
44 | static struct tw0 : time_watcher { |
49 | static struct tw0 : time_watcher |
45 | void cb (time_watcher &w) |
|
|
46 | { |
50 | { |
|
|
51 | void cb (time_watcher &w) |
|
|
52 | { |
47 | // should never get called |
53 | // should never get called |
48 | // reached end-of-time, or tstamp has a bogus definition, |
54 | // reached end-of-time, or tstamp has a bogus definition, |
49 | // or compiler initilization order broken, or somethine else :) |
55 | // or compiler initilization order broken, or somethine else :) |
50 | abort (); |
56 | abort (); |
51 | } |
57 | } |
52 | |
58 | |
53 | tw0() |
59 | tw0 () |
54 | : time_watcher (this, &tw0::cb) |
60 | : time_watcher (this, &tw0::cb) |
55 | { } |
61 | { } |
56 | } tw0; |
62 | } tw0; |
57 | |
63 | |
58 | tstamp NOW; |
64 | tstamp NOW; |
59 | static bool iom_valid; |
65 | static bool iom_valid; |
60 | io_manager iom; |
66 | io_manager iom; |
61 | |
67 | |
… | |
… | |
99 | void io_manager::reg (time_watcher *w) { reg (w, tw); } |
105 | void io_manager::reg (time_watcher *w) { reg (w, tw); } |
100 | void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
106 | void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
101 | #endif |
107 | #endif |
102 | |
108 | |
103 | #if IOM_IO |
109 | #if IOM_IO |
104 | void io_manager::reg (io_watcher *w) { reg (w, iow); } |
110 | void io_manager::reg (io_watcher *w) { reg (w, iow); } |
105 | void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
111 | void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
106 | #endif |
112 | #endif |
107 | |
113 | |
108 | #if IOM_CHECK |
114 | #if IOM_CHECK |
109 | void io_manager::reg (check_watcher *w) { reg (w, cw); } |
115 | void io_manager::reg (check_watcher *w) { reg (w, cw); } |
… | |
… | |
121 | struct timeval tv; |
127 | struct timeval tv; |
122 | |
128 | |
123 | gettimeofday (&tv, 0); |
129 | gettimeofday (&tv, 0); |
124 | |
130 | |
125 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
131 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
126 | #endif |
|
|
127 | } |
132 | } |
|
|
133 | #endif |
128 | |
134 | |
|
|
135 | #if IOM_SIG |
|
|
136 | // race conditions galore |
|
|
137 | |
|
|
138 | void io_manager::sighandler (int signum) |
|
|
139 | { |
|
|
140 | assert (0 < signum && signum <= iom.sw.size ()); |
|
|
141 | |
|
|
142 | sig_vec &sv = *iom.sw [signum - 1]; |
|
|
143 | |
|
|
144 | for (int i = sv.size (); i--; ) |
|
|
145 | if (!sv[i]) |
|
|
146 | sv.erase_unordered (i); |
|
|
147 | else |
|
|
148 | sv[i]->call (*sv[i]); |
|
|
149 | } |
|
|
150 | |
|
|
151 | void io_manager::reg (sig_watcher *w) |
|
|
152 | { |
|
|
153 | assert (0 < w->signum); |
|
|
154 | |
|
|
155 | sw.reserve (w->signum); |
|
|
156 | |
|
|
157 | sig_vec *&sv = sw [w->signum - 1]; |
|
|
158 | |
|
|
159 | if (!sv) |
|
|
160 | { |
|
|
161 | sv = new sig_vec; |
|
|
162 | |
|
|
163 | struct sigaction sa; |
|
|
164 | sa.sa_handler = io_manager::sighandler; |
|
|
165 | sigfillset (&sa.sa_mask); |
|
|
166 | sa.sa_flags = 0; |
|
|
167 | |
|
|
168 | if (sigaction (w->signum, &sa, 0)) |
|
|
169 | { |
|
|
170 | perror ("Error while installing signal handler"); |
|
|
171 | abort (); |
|
|
172 | } |
|
|
173 | } |
|
|
174 | |
|
|
175 | reg (w, *sv); |
|
|
176 | } |
|
|
177 | |
|
|
178 | void io_manager::unreg (sig_watcher *w) |
|
|
179 | { |
|
|
180 | assert (0 < w->signum && w->signum <= sw.size ()); |
|
|
181 | |
|
|
182 | unreg (w, *sw [w->signum - 1]); |
|
|
183 | } |
|
|
184 | |
|
|
185 | void sig_watcher::start (int signum) |
|
|
186 | { |
|
|
187 | stop (); |
|
|
188 | this->signum = signum; |
|
|
189 | iom.reg (this); |
|
|
190 | } |
|
|
191 | #endif |
|
|
192 | |
129 | void io_manager::loop () |
193 | void io_manager::loop () |
130 | { |
194 | { |
131 | #if IOM_TIME |
195 | #if IOM_TIME |
132 | set_now (); |
196 | set_now (); |
133 | #endif |
197 | #endif |
… | |
… | |
144 | tval.tv_usec = 0; |
208 | tval.tv_usec = 0; |
145 | to = &tval; |
209 | to = &tval; |
146 | } |
210 | } |
147 | else |
211 | else |
148 | #endif |
212 | #endif |
|
|
213 | |
149 | { |
214 | { |
150 | #if IOM_TIME |
215 | #if IOM_TIME |
151 | time_watcher *next; |
216 | time_watcher *next; |
152 | |
217 | |
153 | for (;;) |
218 | for (;;) |
… | |
… | |
164 | { |
229 | { |
165 | if (next != tw[0]) |
230 | if (next != tw[0]) |
166 | { |
231 | { |
167 | double diff = next->at - NOW; |
232 | double diff = next->at - NOW; |
168 | tval.tv_sec = (int)diff; |
233 | tval.tv_sec = (int)diff; |
169 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
234 | tval.tv_usec = (int) ((diff - tval.tv_sec) * 1000000); |
170 | to = &tval; |
235 | to = &tval; |
171 | } |
236 | } |
172 | break; |
237 | break; |
173 | } |
238 | } |
174 | else |
239 | else |
… | |
… | |
176 | unreg (next); |
241 | unreg (next); |
177 | next->call (*next); |
242 | next->call (*next); |
178 | } |
243 | } |
179 | } |
244 | } |
180 | #endif |
245 | #endif |
|
|
246 | |
181 | } |
247 | } |
182 | |
248 | |
183 | #if IOM_CHECK |
249 | #if IOM_CHECK |
184 | tw.activity = false; |
250 | tw.activity = false; |
185 | |
251 | |
… | |
… | |
196 | to = &tval; |
262 | to = &tval; |
197 | } |
263 | } |
198 | #endif |
264 | #endif |
199 | |
265 | |
200 | #if IOM_IO |
266 | #if IOM_IO |
201 | fd_set rfd, wfd, efd; |
267 | fd_set rfd, wfd; |
202 | |
268 | |
203 | FD_ZERO (&rfd); |
269 | FD_ZERO (&rfd); |
204 | FD_ZERO (&wfd); |
270 | FD_ZERO (&wfd); |
205 | |
271 | |
206 | int fds = 0; |
272 | int fds = 0; |
… | |
… | |
215 | } |
281 | } |
216 | |
282 | |
217 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
283 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
218 | break; // no events |
284 | break; // no events |
219 | |
285 | |
220 | fds = select (fds, &rfd, &wfd, &efd, to); |
286 | fds = select (fds, &rfd, &wfd, NULL, to); |
221 | # if IOM_TIME |
287 | # if IOM_TIME |
222 | set_now (); |
288 | set_now (); |
223 | # endif |
289 | # endif |
224 | |
290 | |
225 | if (fds > 0) |
291 | if (fds > 0) |
… | |
… | |
257 | select (0, 0, 0, 0, &to); |
323 | select (0, 0, 0, 0, &to); |
258 | set_now (); |
324 | set_now (); |
259 | #else |
325 | #else |
260 | break; |
326 | break; |
261 | #endif |
327 | #endif |
|
|
328 | |
262 | } |
329 | } |
263 | } |
330 | } |
264 | |
331 | |
265 | io_manager::io_manager () |
332 | io_manager::io_manager () |
266 | { |
333 | { |