… | |
… | |
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) |
… | |
… | |
45 | { |
50 | { |
46 | void cb (time_watcher &w) |
51 | void cb (time_watcher &w) |
47 | { |
52 | { |
48 | // should never get called |
53 | // should never get called |
49 | // reached end-of-time, or tstamp has a bogus definition, |
54 | // reached end-of-time, or tstamp has a bogus definition, |
50 | // or compiler initilization order broken, or somethine else :) |
55 | // or compiler initialisation order broken, or something else :) |
51 | abort (); |
56 | abort (); |
52 | } |
57 | } |
53 | |
58 | |
54 | tw0 () |
59 | tw0 () |
55 | : time_watcher (this, &tw0::cb) |
60 | : time_watcher (this, &tw0::cb) |
56 | { }} |
61 | { } |
57 | tw0; |
62 | } tw0; |
58 | |
63 | |
59 | tstamp NOW; |
64 | tstamp NOW; |
60 | static bool iom_valid; |
65 | static bool iom_valid; |
61 | io_manager iom; |
66 | io_manager iom; |
62 | |
67 | |
… | |
… | |
122 | struct timeval tv; |
127 | struct timeval tv; |
123 | |
128 | |
124 | gettimeofday (&tv, 0); |
129 | gettimeofday (&tv, 0); |
125 | |
130 | |
126 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
131 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
127 | #endif |
|
|
128 | } |
132 | } |
|
|
133 | #endif |
129 | |
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 | |
130 | void io_manager::loop () |
193 | void io_manager::loop () |
131 | { |
194 | { |
132 | #if IOM_TIME |
195 | #if IOM_TIME |
133 | set_now (); |
196 | set_now (); |
134 | #endif |
197 | #endif |
… | |
… | |
199 | to = &tval; |
262 | to = &tval; |
200 | } |
263 | } |
201 | #endif |
264 | #endif |
202 | |
265 | |
203 | #if IOM_IO |
266 | #if IOM_IO |
204 | fd_set rfd, wfd, efd; |
267 | fd_set rfd, wfd; |
205 | |
268 | |
206 | FD_ZERO (&rfd); |
269 | FD_ZERO (&rfd); |
207 | FD_ZERO (&wfd); |
270 | FD_ZERO (&wfd); |
208 | |
271 | |
209 | int fds = 0; |
272 | int fds = 0; |
… | |
… | |
218 | } |
281 | } |
219 | |
282 | |
220 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
283 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
221 | break; // no events |
284 | break; // no events |
222 | |
285 | |
223 | fds = select (fds, &rfd, &wfd, &efd, to); |
286 | fds = select (fds, &rfd, &wfd, NULL, to); |
224 | # if IOM_TIME |
287 | # if IOM_TIME |
225 | set_now (); |
288 | set_now (); |
226 | # endif |
289 | # endif |
227 | |
290 | |
228 | if (fds > 0) |
291 | if (fds > 0) |