… | |
… | |
18 | */ |
18 | */ |
19 | |
19 | |
20 | #include "../config.h" |
20 | #include "../config.h" |
21 | |
21 | |
22 | #include <cstdio> |
22 | #include <cstdio> |
|
|
23 | #include <cstdlib> |
|
|
24 | #include <cerrno> |
23 | |
25 | |
24 | #include <sys/select.h> |
26 | #include <sys/select.h> |
25 | #include <sys/time.h> |
27 | #include <sys/time.h> |
26 | |
28 | |
27 | #include "iom.h" |
29 | #include "iom.h" |
28 | |
30 | |
|
|
31 | // TSTAMP_MAX must still fit into a positive struct timeval |
|
|
32 | #define TSTAMP_MAX (double)(1UL<<31) |
|
|
33 | |
29 | tstamp NOW; |
34 | tstamp NOW; |
30 | bool iom_valid; |
35 | static bool iom_valid; |
31 | io_manager iom; |
36 | io_manager iom; |
32 | |
37 | |
|
|
38 | template<class watcher> |
|
|
39 | void io_manager::reg (watcher *w, simplevec<watcher *> &queue) |
|
|
40 | { |
|
|
41 | if (!iom_valid) |
|
|
42 | abort (); |
|
|
43 | |
|
|
44 | if (!w->active) |
|
|
45 | { |
|
|
46 | queue.push_back (w); |
|
|
47 | w->active = queue.size (); |
|
|
48 | } |
|
|
49 | } |
|
|
50 | |
|
|
51 | template<class watcher> |
|
|
52 | void io_manager::unreg (watcher *w, simplevec<watcher *> &queue) |
|
|
53 | { |
|
|
54 | if (!iom_valid) |
|
|
55 | return; |
|
|
56 | |
|
|
57 | if (w->active) |
|
|
58 | { |
|
|
59 | queue [w->active - 1] = 0; |
|
|
60 | w->active = 0; |
|
|
61 | } |
|
|
62 | } |
|
|
63 | |
|
|
64 | #if IOM_TIME |
33 | void time_watcher::trigger () |
65 | void time_watcher::trigger () |
34 | { |
66 | { |
35 | call (*this); |
67 | call (*this); |
36 | |
68 | |
37 | iom.reg (this); |
69 | iom.reg (this); |
38 | } |
70 | } |
39 | |
71 | |
40 | time_watcher::~time_watcher () |
|
|
41 | { |
|
|
42 | if (iom_valid) |
|
|
43 | iom.unreg (this); |
|
|
44 | } |
|
|
45 | |
|
|
46 | io_watcher::~io_watcher () |
|
|
47 | { |
|
|
48 | if (iom_valid) |
|
|
49 | iom.unreg (this); |
|
|
50 | } |
|
|
51 | |
|
|
52 | void io_manager::reg (io_watcher *w) |
|
|
53 | { |
|
|
54 | if (find (iow.begin (), iow.end (), w) == iow.end ()) |
|
|
55 | iow.push_back (w); |
|
|
56 | } |
|
|
57 | |
|
|
58 | void io_manager::unreg (io_watcher *w) |
|
|
59 | { |
|
|
60 | iow.erase (find (iow.begin (), iow.end (), w)); |
|
|
61 | } |
|
|
62 | |
|
|
63 | void io_manager::reg (time_watcher *w) |
72 | void io_manager::reg (time_watcher *w) { reg (w, tw); } |
64 | { |
|
|
65 | if (find (tw.begin (), tw.end (), w) == tw.end ()) |
|
|
66 | tw.push_back (w); |
|
|
67 | } |
|
|
68 | |
|
|
69 | void io_manager::unreg (time_watcher *w) |
73 | void io_manager::unreg (time_watcher *w) { unreg (w, tw); } |
70 | { |
74 | #endif |
71 | tw.erase (find (tw.begin (), tw.end (), w)); |
|
|
72 | } |
|
|
73 | |
75 | |
|
|
76 | #if IOM_IO |
|
|
77 | void io_manager::reg (io_watcher *w) { reg (w, iow); } |
|
|
78 | void io_manager::unreg (io_watcher *w) { unreg (w, iow); } |
|
|
79 | #endif |
|
|
80 | |
|
|
81 | #if IOM_CHECK |
|
|
82 | void io_manager::reg (check_watcher *w) { reg (w, cw); } |
|
|
83 | void io_manager::unreg (check_watcher *w) { unreg (w, cw); } |
|
|
84 | #endif |
|
|
85 | |
|
|
86 | #if IOM_IDLE |
|
|
87 | void io_manager::reg (idle_watcher *w) { reg (w, iw); } |
|
|
88 | void io_manager::unreg (idle_watcher *w) { unreg (w, iw); } |
|
|
89 | #endif |
|
|
90 | |
|
|
91 | #if IOM_TIME |
74 | inline void set_now (void) |
92 | inline void set_now (void) |
75 | { |
93 | { |
76 | struct timeval tv; |
94 | struct timeval tv; |
77 | |
95 | |
78 | gettimeofday (&tv, 0); |
96 | gettimeofday (&tv, 0); |
79 | |
97 | |
80 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
98 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
|
|
99 | #endif |
81 | } |
100 | } |
82 | |
101 | |
83 | void io_manager::loop () |
102 | void io_manager::loop () |
84 | { |
103 | { |
|
|
104 | #if IOM_TIME |
85 | set_now (); |
105 | set_now (); |
|
|
106 | #endif |
86 | |
107 | |
87 | for (;;) |
108 | for (;;) |
88 | { |
109 | { |
89 | time_watcher *w; |
110 | struct timeval *to = 0; |
|
|
111 | struct timeval tval; |
90 | |
112 | |
91 | for (;;) |
113 | #if IOM_IDLE |
|
|
114 | if (iw.size ()) |
92 | { |
115 | { |
93 | w = tw[0]; |
116 | tval.tv_sec = 0; |
94 | |
117 | tval.tv_usec = 0; |
95 | for (time_watcher **i = tw.begin (); i != tw.end (); ++i) |
118 | to = &tval; |
96 | if ((*i)->at < w->at) |
|
|
97 | w = *i; |
|
|
98 | |
|
|
99 | if (w->at > NOW) |
|
|
100 | break; |
|
|
101 | |
|
|
102 | // call it |
|
|
103 | w->call (*w); |
|
|
104 | |
|
|
105 | // re-add it if necessary |
|
|
106 | if (w->at >= 0) |
|
|
107 | reg (w); |
|
|
108 | } |
119 | } |
|
|
120 | else |
|
|
121 | #endif |
|
|
122 | { |
|
|
123 | #if IOM_TIME |
|
|
124 | time_watcher *next; |
109 | |
125 | |
110 | struct timeval to; |
126 | for (;;) |
111 | double diff = w->at - NOW; |
127 | { |
112 | to.tv_sec = (int)diff; |
128 | next = tw[0]; // the first time-watcher must exist at ALL times |
|
|
129 | |
|
|
130 | for (int i = tw.size (); i--; ) |
|
|
131 | if (!tw[i]) |
|
|
132 | tw.erase_unordered (i); |
|
|
133 | else if (tw[i]->at < next->at) |
|
|
134 | next = tw[i]; |
|
|
135 | |
|
|
136 | if (next->at > NOW) |
|
|
137 | { |
|
|
138 | if (next != tw[0]) |
|
|
139 | { |
|
|
140 | double diff = next->at - NOW; |
|
|
141 | tval.tv_sec = (int)diff; |
113 | to.tv_usec = (int)((diff - to.tv_sec) * 1000000); |
142 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
|
|
143 | to = &tval; |
|
|
144 | } |
|
|
145 | break; |
|
|
146 | } |
|
|
147 | else if (next->at >= 0) |
|
|
148 | { |
|
|
149 | unreg (next); |
|
|
150 | next->call (*next); |
|
|
151 | } |
|
|
152 | } |
|
|
153 | #endif |
|
|
154 | } |
114 | |
155 | |
|
|
156 | #if IOM_CHECK |
|
|
157 | for (int i = cw.size (); i--; ) |
|
|
158 | if (!cw[i]) |
|
|
159 | cw.erase_unordered (i); |
|
|
160 | else |
|
|
161 | cw[i]->call (*cw[i]); |
|
|
162 | #endif |
|
|
163 | |
|
|
164 | #if IOM_IO |
115 | fd_set rfd, wfd; |
165 | fd_set rfd, wfd, efd; |
116 | |
166 | |
117 | FD_ZERO (&rfd); |
167 | FD_ZERO (&rfd); |
118 | FD_ZERO (&wfd); |
168 | FD_ZERO (&wfd); |
119 | |
169 | |
120 | int fds = 0; |
170 | int fds = 0; |
121 | |
171 | |
122 | for (io_watcher **w = iow.begin (); w < iow.end (); ++w) |
172 | for (io_watcher **i = iow.end (); i-- > iow.begin (); ) |
|
|
173 | if (*i) |
|
|
174 | { |
|
|
175 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
|
|
176 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
|
|
177 | |
|
|
178 | if ((*i)->fd >= fds) fds = (*i)->fd + 1; |
|
|
179 | } |
|
|
180 | |
|
|
181 | if (!to && !fds) //TODO: also check idle_watchers and check_watchers |
|
|
182 | break; // no events |
|
|
183 | |
|
|
184 | fds = select (fds, &rfd, &wfd, &efd, to); |
|
|
185 | # if IOM_TIME |
|
|
186 | set_now (); |
|
|
187 | # endif |
|
|
188 | |
|
|
189 | if (fds > 0) |
|
|
190 | for (int i = iow.size (); i--; ) |
|
|
191 | if (!iow[i]) |
|
|
192 | iow.erase_unordered (i); |
|
|
193 | else |
|
|
194 | { |
|
|
195 | short revents = iow[i]->events; |
|
|
196 | |
|
|
197 | if (!FD_ISSET (iow[i]->fd, &rfd)) revents &= ~EVENT_READ; |
|
|
198 | if (!FD_ISSET (iow[i]->fd, &wfd)) revents &= ~EVENT_WRITE; |
|
|
199 | |
|
|
200 | if (revents) |
|
|
201 | iow[i]->call (*iow[i], revents); |
|
|
202 | } |
|
|
203 | else if (fds < 0 && errno != EINTR) |
123 | { |
204 | { |
124 | if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd); |
205 | perror ("Error while waiting for I/O or time event"); |
125 | if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); |
206 | abort (); |
126 | |
|
|
127 | if ((*w)->fd > fds) fds = (*w)->fd; |
|
|
128 | } |
207 | } |
|
|
208 | #if IOM_IDLE |
|
|
209 | else |
|
|
210 | for (int i = iw.size (); i--; ) |
|
|
211 | if (!iw[i]) |
|
|
212 | iw.erase_unordered (i); |
|
|
213 | else |
|
|
214 | iw[i]->call (*iw[i]); |
|
|
215 | #endif |
129 | |
216 | |
130 | fds = select (fds + 1, &rfd, &wfd, 0, &to); |
217 | #elif IOM_TIME |
|
|
218 | if (!to) |
|
|
219 | break; |
131 | |
220 | |
|
|
221 | select (0, 0, 0, 0, &to); |
132 | set_now (); |
222 | set_now (); |
133 | |
223 | #else |
134 | if (fds > 0) |
224 | break; |
135 | for (io_watcher **w = iow.begin (); w < iow.end (); ++w) |
225 | #endif |
136 | { |
|
|
137 | short revents = (*w)->events; |
|
|
138 | |
|
|
139 | if (!FD_ISSET ((*w)->fd, &rfd)) revents &= ~EVENT_READ; |
|
|
140 | if (!FD_ISSET ((*w)->fd, &wfd)) revents &= ~EVENT_WRITE; |
|
|
141 | |
|
|
142 | if (revents) |
|
|
143 | (*w)->call (**w, revents); |
|
|
144 | } |
|
|
145 | } |
226 | } |
146 | } |
227 | } |
147 | |
228 | |
148 | void io_manager::idle_cb (time_watcher &w) |
229 | // this is a dummy time watcher to ensure that the first |
149 | { |
230 | // time watcher is _always_ valid, this gets rid of a lot |
150 | w.at = NOW + 1000000000; |
231 | // of null-pointer-checks |
151 | } |
232 | static struct tw0 : time_watcher { |
|
|
233 | void cb (time_watcher &w) |
|
|
234 | { |
|
|
235 | // should never get called |
|
|
236 | // reached end-of-time, or tstamp has a bogus definition :) |
|
|
237 | abort (); |
|
|
238 | } |
|
|
239 | |
|
|
240 | tw0() |
|
|
241 | : time_watcher (this, &tw0::cb) |
|
|
242 | { } |
|
|
243 | } tw0; |
152 | |
244 | |
153 | io_manager::io_manager () |
245 | io_manager::io_manager () |
154 | { |
246 | { |
|
|
247 | iom_valid = true; |
|
|
248 | |
|
|
249 | #if IOM_TIME |
155 | set_now (); |
250 | set_now (); |
156 | |
251 | |
157 | iom_valid = true; |
252 | tw0.start (TSTAMP_MAX); |
158 | |
253 | #endif |
159 | idle = new time_watcher (this, &io_manager::idle_cb); |
|
|
160 | idle->start (0); |
|
|
161 | } |
254 | } |
162 | |
255 | |
163 | io_manager::~io_manager () |
256 | io_manager::~io_manager () |
164 | { |
257 | { |
165 | iom_valid = false; |
258 | iom_valid = false; |