… | |
… | |
28 | |
28 | |
29 | tstamp NOW; |
29 | tstamp NOW; |
30 | bool iom_valid; |
30 | bool iom_valid; |
31 | io_manager iom; |
31 | io_manager iom; |
32 | |
32 | |
33 | void time_watcher::trigger () |
33 | template<class watcher> |
|
|
34 | void io_manager::reg (watcher *w, simplevec<watcher *> &queue) |
34 | { |
35 | { |
35 | call (*this); |
36 | if (find (queue.begin (), queue.end (), w) == queue.end ()) |
36 | |
37 | queue.push_back (w); |
37 | iom.reg (this); |
|
|
38 | } |
38 | } |
39 | |
39 | |
|
|
40 | template<class watcher> |
|
|
41 | void io_manager::unreg (watcher *w, simplevec<watcher *> &queue) |
|
|
42 | { |
|
|
43 | queue.erase (find (queue.begin (), queue.end (), w)); |
|
|
44 | } |
|
|
45 | |
|
|
46 | #if IOM_IO |
40 | time_watcher::~time_watcher () |
47 | io_watcher::~io_watcher () |
41 | { |
48 | { |
42 | if (iom_valid) |
49 | if (iom_valid) |
43 | iom.unreg (this); |
50 | iom.unreg (this); |
44 | |
|
|
45 | at = TSTAMP_CANCEL; |
|
|
46 | } |
51 | } |
47 | |
52 | |
|
|
53 | void io_manager::reg (io_watcher *w) |
|
|
54 | { |
|
|
55 | reg (w, iow); |
|
|
56 | } |
|
|
57 | |
|
|
58 | void io_manager::unreg (io_watcher *w) |
|
|
59 | { |
|
|
60 | unreg (w, iow); |
|
|
61 | } |
|
|
62 | |
|
|
63 | #endif |
|
|
64 | |
|
|
65 | #if IOM_TIME |
|
|
66 | void time_watcher::trigger () |
|
|
67 | { |
|
|
68 | call (*this); |
|
|
69 | |
|
|
70 | iom.reg (this); |
|
|
71 | } |
|
|
72 | |
48 | io_watcher::~io_watcher () |
73 | time_watcher::~time_watcher () |
49 | { |
74 | { |
50 | if (iom_valid) |
75 | if (iom_valid) |
51 | iom.unreg (this); |
76 | iom.unreg (this); |
52 | } |
|
|
53 | |
77 | |
54 | void io_manager::reg (io_watcher *w) |
78 | at = TSTAMP_CANCEL; |
55 | { |
|
|
56 | if (find (iow.begin (), iow.end (), w) == iow.end ()) |
|
|
57 | iow.push_back (w); |
|
|
58 | } |
|
|
59 | |
|
|
60 | void io_manager::unreg (io_watcher *w) |
|
|
61 | { |
|
|
62 | iow.erase (find (iow.begin (), iow.end (), w)); |
|
|
63 | } |
79 | } |
64 | |
80 | |
65 | void io_manager::reg (time_watcher *w) |
81 | void io_manager::reg (time_watcher *w) |
66 | { |
82 | { |
67 | if (find (tw.begin (), tw.end (), w) == tw.end ()) |
83 | reg (w, tw); |
68 | tw.push_back (w); |
|
|
69 | } |
84 | } |
70 | |
85 | |
71 | void io_manager::unreg (time_watcher *w) |
86 | void io_manager::unreg (time_watcher *w) |
72 | { |
87 | { |
73 | tw.erase (find (tw.begin (), tw.end (), w)); |
88 | unreg (w, tw); |
74 | } |
89 | } |
|
|
90 | #endif |
75 | |
91 | |
|
|
92 | #if IOM_CHECK |
|
|
93 | check_watcher::~check_watcher () |
|
|
94 | { |
|
|
95 | if (iom_valid) |
|
|
96 | iom.unreg (this); |
|
|
97 | } |
|
|
98 | |
|
|
99 | void io_manager::reg (check_watcher *w) |
|
|
100 | { |
|
|
101 | reg (w, cw); |
|
|
102 | } |
|
|
103 | |
|
|
104 | void io_manager::unreg (check_watcher *w) |
|
|
105 | { |
|
|
106 | unreg (w, cw); |
|
|
107 | } |
|
|
108 | #endif |
|
|
109 | |
|
|
110 | #if IOM_TIME |
76 | inline void set_now (void) |
111 | inline void set_now (void) |
77 | { |
112 | { |
78 | struct timeval tv; |
113 | struct timeval tv; |
79 | |
114 | |
80 | gettimeofday (&tv, 0); |
115 | gettimeofday (&tv, 0); |
81 | |
116 | |
82 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
117 | NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
|
|
118 | #endif |
83 | } |
119 | } |
84 | |
120 | |
85 | void io_manager::loop () |
121 | void io_manager::loop () |
86 | { |
122 | { |
|
|
123 | #if IOM_TIME |
87 | set_now (); |
124 | set_now (); |
|
|
125 | #endif |
88 | |
126 | |
89 | for (;;) |
127 | for (;;) |
90 | { |
128 | { |
|
|
129 | #if IOM_CHECK |
|
|
130 | for (int i = 0; i < cw.size (); ++i) |
|
|
131 | cw[i]->call (*cw[i]); |
|
|
132 | #endif |
|
|
133 | |
|
|
134 | #if IOM_TIME |
91 | time_watcher *w; |
135 | time_watcher *w; |
92 | |
136 | |
93 | for (;;) |
137 | for (;;) |
94 | { |
138 | { |
95 | w = tw[0]; |
139 | w = tw[0]; |
96 | |
140 | |
97 | for (time_watcher **i = tw.begin (); i != tw.end (); ++i) |
141 | for (time_watcher **i = tw.begin (); i < tw.end (); ++i) |
98 | if ((*i)->at < w->at) |
142 | if ((*i)->at < w->at) |
99 | w = *i; |
143 | w = *i; |
100 | |
144 | |
101 | if (w->at > NOW) |
145 | if (w->at > NOW) |
102 | break; |
146 | break; |
… | |
… | |
106 | |
150 | |
107 | if (w->at < 0) |
151 | if (w->at < 0) |
108 | unreg (w); |
152 | unreg (w); |
109 | } |
153 | } |
110 | |
154 | |
|
|
155 | double diff = w->at - NOW; |
111 | struct timeval to; |
156 | struct timeval to; |
112 | double diff = w->at - NOW; |
|
|
113 | to.tv_sec = (int)diff; |
157 | to.tv_sec = (int)diff; |
114 | to.tv_usec = (int)((diff - to.tv_sec) * 1000000); |
158 | to.tv_usec = (int)((diff - to.tv_sec) * 1000000); |
|
|
159 | #endif |
115 | |
160 | |
|
|
161 | #if IOM_IO |
116 | fd_set rfd, wfd; |
162 | fd_set rfd, wfd; |
117 | |
163 | |
118 | FD_ZERO (&rfd); |
164 | FD_ZERO (&rfd); |
119 | FD_ZERO (&wfd); |
165 | FD_ZERO (&wfd); |
120 | |
166 | |
… | |
… | |
126 | if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); |
172 | if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); |
127 | |
173 | |
128 | if ((*w)->fd > fds) fds = (*w)->fd; |
174 | if ((*w)->fd > fds) fds = (*w)->fd; |
129 | } |
175 | } |
130 | |
176 | |
|
|
177 | # if IOM_TIME |
131 | fds = select (fds + 1, &rfd, &wfd, 0, &to); |
178 | fds = select (fds + 1, &rfd, &wfd, 0, &to); |
132 | |
|
|
133 | set_now (); |
179 | set_now (); |
|
|
180 | # else |
|
|
181 | fds = select (fds + 1, &rfd, &wfd, 0, 0); |
|
|
182 | # endif |
134 | |
183 | |
135 | if (fds > 0) |
184 | if (fds > 0) |
136 | for (io_watcher **w = iow.begin (); w < iow.end (); ++w) |
185 | for (int i = 0; i < iow.size (); ++i) |
137 | { |
186 | { |
|
|
187 | io_watcher *w = iow[i]; |
|
|
188 | |
138 | short revents = (*w)->events; |
189 | short revents = w->events; |
139 | |
190 | |
140 | if (!FD_ISSET ((*w)->fd, &rfd)) revents &= ~EVENT_READ; |
191 | if (!FD_ISSET (w->fd, &rfd)) revents &= ~EVENT_READ; |
141 | if (!FD_ISSET ((*w)->fd, &wfd)) revents &= ~EVENT_WRITE; |
192 | if (!FD_ISSET (w->fd, &wfd)) revents &= ~EVENT_WRITE; |
142 | |
193 | |
143 | if (revents) |
194 | if (revents) |
144 | (*w)->call (**w, revents); |
195 | w->call (*w, revents); |
145 | } |
196 | } |
146 | } |
197 | } |
|
|
198 | #elif IOM_TIME |
|
|
199 | select (0, 0, 0, 0, &to); |
|
|
200 | set_now (); |
|
|
201 | #endif |
147 | } |
202 | } |
148 | |
203 | |
149 | void io_manager::idle_cb (time_watcher &w) |
204 | void io_manager::idle_cb (time_watcher &w) |
150 | { |
205 | { |
151 | w.at = NOW + 1000000000; |
206 | w.at = NOW + 1000000000; |
… | |
… | |
155 | { |
210 | { |
156 | set_now (); |
211 | set_now (); |
157 | |
212 | |
158 | iom_valid = true; |
213 | iom_valid = true; |
159 | |
214 | |
|
|
215 | #if IOM_TIME |
160 | idle = new time_watcher (this, &io_manager::idle_cb); |
216 | idle = new time_watcher (this, &io_manager::idle_cb); |
161 | idle->start (0); |
217 | idle->start (0); |
|
|
218 | #endif |
162 | } |
219 | } |
163 | |
220 | |
164 | io_manager::~io_manager () |
221 | io_manager::~io_manager () |
165 | { |
222 | { |
166 | iom_valid = false; |
223 | iom_valid = false; |