1 |
/* |
2 |
iom.C -- I/O multiplexor |
3 |
|
4 |
This program is free software; you can redistribute it and/or modify |
5 |
it under the terms of the GNU General Public License as published by |
6 |
the Free Software Foundation; either version 2 of the License, or |
7 |
(at your option) any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU General Public License |
15 |
along with this program; if not, write to the Free Software |
16 |
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 |
*/ |
18 |
|
19 |
#include "config.h" |
20 |
|
21 |
#include <sys/time.h> |
22 |
|
23 |
#include <algorithm> |
24 |
#include <functional> |
25 |
|
26 |
#include "gettext.h" |
27 |
|
28 |
#include "slog.h" |
29 |
#include "iom.h" |
30 |
|
31 |
tstamp NOW; |
32 |
bool iom_valid; |
33 |
io_manager iom; |
34 |
|
35 |
inline bool earliest_first (const time_watcher *a, const time_watcher *b) |
36 |
{ |
37 |
return a->at > b->at; |
38 |
} |
39 |
|
40 |
void time_watcher::set (tstamp when) |
41 |
{ |
42 |
at = when; |
43 |
|
44 |
if (registered) |
45 |
iom.reschedule_time_watchers (); |
46 |
} |
47 |
|
48 |
void time_watcher::trigger () |
49 |
{ |
50 |
call (*this); |
51 |
|
52 |
if (registered) |
53 |
iom.reschedule_time_watchers (); |
54 |
else |
55 |
iom.reg (this); |
56 |
} |
57 |
|
58 |
time_watcher::~time_watcher () |
59 |
{ |
60 |
if (iom_valid) |
61 |
iom.unreg (this); |
62 |
} |
63 |
|
64 |
void io_watcher::set(int fd_, short events_) |
65 |
{ |
66 |
fd = fd_; |
67 |
events = events_; |
68 |
|
69 |
if (registered) |
70 |
{ |
71 |
iom.unreg (this); |
72 |
iom.reg (this); |
73 |
} |
74 |
} |
75 |
|
76 |
io_watcher::~io_watcher () |
77 |
{ |
78 |
if (iom_valid) |
79 |
iom.unreg (this); |
80 |
} |
81 |
|
82 |
void io_manager::reg (io_watcher *w) |
83 |
{ |
84 |
if (!w->registered) |
85 |
{ |
86 |
w->registered = true; |
87 |
|
88 |
pollfd pfd; |
89 |
|
90 |
pfd.fd = w->fd; |
91 |
pfd.events = w->events; |
92 |
|
93 |
pfs.push_back (pfd); |
94 |
iow.push_back (w); |
95 |
} |
96 |
} |
97 |
|
98 |
void io_manager::unreg (io_watcher *w) |
99 |
{ |
100 |
if (w->registered) |
101 |
{ |
102 |
w->registered = false; |
103 |
|
104 |
unsigned int sz = iow.size (); |
105 |
unsigned int i = find (iow.begin (), iow.end (), w) - iow.begin (); |
106 |
|
107 |
assert (i != sz); |
108 |
|
109 |
if (sz == 1) |
110 |
{ |
111 |
pfs.clear (); |
112 |
iow.clear (); |
113 |
} |
114 |
else if (i == sz - 1) |
115 |
{ |
116 |
iow.pop_back (); |
117 |
pfs.pop_back (); |
118 |
} |
119 |
else |
120 |
{ |
121 |
iow[i] = iow[sz - 1]; iow.pop_back (); |
122 |
pfs[i] = pfs[sz - 1]; pfs.pop_back (); |
123 |
} |
124 |
} |
125 |
} |
126 |
|
127 |
void io_manager::reschedule_time_watchers () |
128 |
{ |
129 |
make_heap (tw.begin (), tw.end (), earliest_first); |
130 |
} |
131 |
|
132 |
void io_manager::reg (time_watcher *w) |
133 |
{ |
134 |
if (!w->registered) |
135 |
{ |
136 |
w->registered = true; |
137 |
|
138 |
tw.push_back (w); |
139 |
push_heap (tw.begin (), tw.end (), earliest_first); |
140 |
} |
141 |
} |
142 |
|
143 |
void io_manager::unreg (time_watcher *w) |
144 |
{ |
145 |
if (w->registered) |
146 |
{ |
147 |
w->registered = false; |
148 |
|
149 |
unsigned int sz = tw.size (); |
150 |
unsigned int i = find (tw.begin (), tw.end (), w) - tw.begin (); |
151 |
|
152 |
assert (i != sz); |
153 |
|
154 |
if (i != sz - 1) |
155 |
tw[i] = tw[sz - 1]; |
156 |
|
157 |
tw.pop_back (); |
158 |
reschedule_time_watchers (); |
159 |
} |
160 |
} |
161 |
|
162 |
inline void set_now (void) |
163 |
{ |
164 |
struct timeval tv; |
165 |
|
166 |
gettimeofday (&tv, 0); |
167 |
|
168 |
NOW = (tstamp)tv.tv_sec + (tstamp)tv.tv_usec / 1000000; |
169 |
} |
170 |
|
171 |
void io_manager::loop () |
172 |
{ |
173 |
set_now (); |
174 |
|
175 |
for (;;) |
176 |
{ |
177 |
while (tw[0]->at <= NOW) |
178 |
{ |
179 |
// remove the first watcher |
180 |
time_watcher *w = tw[0]; |
181 |
|
182 |
pop_heap (tw.begin (), tw.end (), earliest_first); |
183 |
tw.pop_back (); |
184 |
|
185 |
w->registered = false; |
186 |
|
187 |
// call it |
188 |
w->call (*w); |
189 |
|
190 |
// re-add it if necessary |
191 |
if (w->at >= 0 && !w->registered) |
192 |
reg (w); |
193 |
} |
194 |
|
195 |
int timeout = (int) ((tw[0]->at - NOW) * 1000); |
196 |
|
197 |
int fds = poll (&pfs[0], pfs.size (), timeout); |
198 |
|
199 |
set_now (); |
200 |
|
201 |
vector<io_watcher *>::iterator w; |
202 |
vector<pollfd>::iterator p; |
203 |
|
204 |
for (w = iow.begin (), p = pfs.begin (); |
205 |
fds > 0 && w < iow.end (); |
206 |
++w, ++p) |
207 |
if (p->revents) |
208 |
{ |
209 |
--fds; |
210 |
|
211 |
if (p->revents & POLLNVAL) |
212 |
{ |
213 |
slog (L_ERR, _("io_watcher started on illegal file descriptor, disabling.")); |
214 |
(*w)->stop (); |
215 |
} |
216 |
else |
217 |
(*w)->call (**w, p->revents); |
218 |
} |
219 |
} |
220 |
} |
221 |
|
222 |
void io_manager::idle_cb (time_watcher &w) |
223 |
{ |
224 |
w.at = NOW + 86400; // wake up every day, for no good reason |
225 |
} |
226 |
|
227 |
io_manager::io_manager () |
228 |
{ |
229 |
iom_valid = true; |
230 |
|
231 |
set_now (); |
232 |
idle = new time_watcher (this, &io_manager::idle_cb); |
233 |
idle->start (0); |
234 |
} |
235 |
|
236 |
io_manager::~io_manager () |
237 |
{ |
238 |
iom_valid = false; |
239 |
} |
240 |
|