… | |
… | |
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 initialisation order broken, or something 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 | |
… | |
… | |
65 | if (!iom_valid) |
71 | if (!iom_valid) |
66 | abort (); |
72 | abort (); |
67 | |
73 | |
68 | if (!w->active) |
74 | if (!w->active) |
69 | { |
75 | { |
|
|
76 | #if IOM_CHECK |
|
|
77 | queue.activity = true; |
|
|
78 | #endif |
70 | queue.push_back (w); |
79 | queue.push_back (w); |
71 | w->active = queue.size (); |
80 | w->active = queue.size (); |
72 | } |
81 | } |
73 | } |
82 | } |
74 | |
83 | |
… | |
… | |
96 | void io_manager::reg (time_watcher *w) { reg (w, tw); } |
105 | void io_manager::reg (time_watcher *w) { reg (w, tw); } |
97 | void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
106 | void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
98 | #endif |
107 | #endif |
99 | |
108 | |
100 | #if IOM_IO |
109 | #if IOM_IO |
101 | void io_manager::reg (io_watcher *w) { reg (w, iow); } |
110 | void io_manager::reg (io_watcher *w) { reg (w, iow); } |
102 | void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
111 | void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
103 | #endif |
112 | #endif |
104 | |
113 | |
105 | #if IOM_CHECK |
114 | #if IOM_CHECK |
106 | void io_manager::reg (check_watcher *w) { reg (w, cw); } |
115 | void io_manager::reg (check_watcher *w) { reg (w, cw); } |
… | |
… | |
118 | struct timeval tv; |
127 | struct timeval tv; |
119 | |
128 | |
120 | gettimeofday (&tv, 0); |
129 | gettimeofday (&tv, 0); |
121 | |
130 | |
122 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
131 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
123 | #endif |
|
|
124 | } |
132 | } |
|
|
133 | #endif |
125 | |
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 | |
126 | void io_manager::loop () |
193 | void io_manager::loop () |
127 | { |
194 | { |
128 | #if IOM_TIME |
195 | #if IOM_TIME |
129 | set_now (); |
196 | set_now (); |
130 | #endif |
197 | #endif |
… | |
… | |
141 | tval.tv_usec = 0; |
208 | tval.tv_usec = 0; |
142 | to = &tval; |
209 | to = &tval; |
143 | } |
210 | } |
144 | else |
211 | else |
145 | #endif |
212 | #endif |
|
|
213 | |
146 | { |
214 | { |
147 | #if IOM_TIME |
215 | #if IOM_TIME |
148 | time_watcher *next; |
216 | time_watcher *next; |
149 | |
217 | |
150 | for (;;) |
218 | for (;;) |
… | |
… | |
161 | { |
229 | { |
162 | if (next != tw[0]) |
230 | if (next != tw[0]) |
163 | { |
231 | { |
164 | double diff = next->at - NOW; |
232 | double diff = next->at - NOW; |
165 | tval.tv_sec = (int)diff; |
233 | tval.tv_sec = (int)diff; |
166 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
234 | tval.tv_usec = (int) ((diff - tval.tv_sec) * 1000000); |
167 | to = &tval; |
235 | to = &tval; |
168 | } |
236 | } |
169 | break; |
237 | break; |
170 | } |
238 | } |
171 | else |
239 | else |
… | |
… | |
173 | unreg (next); |
241 | unreg (next); |
174 | next->call (*next); |
242 | next->call (*next); |
175 | } |
243 | } |
176 | } |
244 | } |
177 | #endif |
245 | #endif |
|
|
246 | |
178 | } |
247 | } |
179 | |
248 | |
180 | #if IOM_CHECK |
249 | #if IOM_CHECK |
|
|
250 | tw.activity = false; |
|
|
251 | |
181 | for (int i = cw.size (); i--; ) |
252 | for (int i = cw.size (); i--; ) |
182 | if (!cw[i]) |
253 | if (!cw[i]) |
183 | cw.erase_unordered (i); |
254 | cw.erase_unordered (i); |
184 | else |
255 | else |
185 | cw[i]->call (*cw[i]); |
256 | cw[i]->call (*cw[i]); |
|
|
257 | |
|
|
258 | if (tw.activity) |
|
|
259 | { |
|
|
260 | tval.tv_sec = 0; |
|
|
261 | tval.tv_usec = 0; |
|
|
262 | to = &tval; |
|
|
263 | } |
186 | #endif |
264 | #endif |
187 | |
265 | |
188 | #if IOM_IO |
266 | #if IOM_IO |
189 | fd_set rfd, wfd, efd; |
267 | fd_set rfd, wfd; |
190 | |
268 | |
191 | FD_ZERO (&rfd); |
269 | FD_ZERO (&rfd); |
192 | FD_ZERO (&wfd); |
270 | FD_ZERO (&wfd); |
193 | |
271 | |
194 | int fds = 0; |
272 | int fds = 0; |
… | |
… | |
203 | } |
281 | } |
204 | |
282 | |
205 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
283 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
206 | break; // no events |
284 | break; // no events |
207 | |
285 | |
208 | fds = select (fds, &rfd, &wfd, &efd, to); |
286 | fds = select (fds, &rfd, &wfd, NULL, to); |
209 | # if IOM_TIME |
287 | # if IOM_TIME |
210 | set_now (); |
288 | set_now (); |
211 | # endif |
289 | # endif |
212 | |
290 | |
213 | if (fds > 0) |
291 | if (fds > 0) |
… | |
… | |
245 | select (0, 0, 0, 0, &to); |
323 | select (0, 0, 0, 0, &to); |
246 | set_now (); |
324 | set_now (); |
247 | #else |
325 | #else |
248 | break; |
326 | break; |
249 | #endif |
327 | #endif |
|
|
328 | |
250 | } |
329 | } |
251 | } |
330 | } |
252 | |
331 | |
253 | io_manager::io_manager () |
332 | io_manager::io_manager () |
254 | { |
333 | { |